problems timeline: fix point highlighting animation

This commit is contained in:
Alexander Zobnin
2018-12-18 23:13:50 +03:00
parent 8b894b9385
commit 4b2503d701

View File

@@ -257,28 +257,31 @@ interface TimelinePointsProps {
interface TimelinePointsState { interface TimelinePointsState {
order: number[]; order: number[];
highlighted: number | null;
} }
class TimelinePoints extends PureComponent<TimelinePointsProps, TimelinePointsState> { class TimelinePoints extends PureComponent<TimelinePointsProps, TimelinePointsState> {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { order: [] }; this.state = { order: [], highlighted: null };
} }
bringToFront = index => { bringToFront = (index: number, highlight = false ) => {
const { events } = this.props; const { events } = this.props;
const length = events.length; const length = events.length;
const order = events.map((v, i) => i); const order = events.map((v, i) => i);
order.splice(index, 1); order.splice(index, 1);
order.push(index); order.push(index);
this.setState({ order }); const highlighted = highlight ? index : null;
this.setState({ order, highlighted });
} }
highlightPoint = index => () => { highlightPoint = index => () => {
if (this.props.onPointHighlight) { if (this.props.onPointHighlight) {
this.props.onPointHighlight(this.props.events[index]); this.props.onPointHighlight(this.props.events[index]);
} }
this.bringToFront(index); this.bringToFront(index, true);
// this.setState({ highlighted: this.props.events.length - 1 });
} }
@@ -287,7 +290,7 @@ class TimelinePoints extends PureComponent<TimelinePointsProps, TimelinePointsSt
this.props.onPointUnHighlight(); this.props.onPointUnHighlight();
} }
const order = this.props.events.map((v, i) => i); const order = this.props.events.map((v, i) => i);
this.setState({ order }); this.setState({ order, highlighted: null });
} }
render() { render() {
@@ -299,6 +302,7 @@ class TimelinePoints extends PureComponent<TimelinePointsProps, TimelinePointsSt
const ts = Number(event.clock); const ts = Number(event.clock);
const posLeft = Math.round((ts - timeFrom) / range * width - pointR); const posLeft = Math.round((ts - timeFrom) / range * width - pointR);
const eventColor = event.value === '1' ? problemColor : okColor; const eventColor = event.value === '1' ? problemColor : okColor;
const highlighted = this.state.highlighted === index;
return ( return (
<TimelinePoint <TimelinePoint
@@ -307,6 +311,7 @@ class TimelinePoints extends PureComponent<TimelinePointsProps, TimelinePointsSt
x={posLeft} x={posLeft}
r={pointR} r={pointR}
color={eventColor} color={eventColor}
highlighted={highlighted}
onPointHighlight={this.highlightPoint(index)} onPointHighlight={this.highlightPoint(index)}
onPointUnHighlight={this.unHighlightPoint(index)} onPointUnHighlight={this.unHighlightPoint(index)}
/> />
@@ -323,6 +328,7 @@ interface TimelinePointProps {
x: number; x: number;
r: number; r: number;
color: string; color: string;
highlighted?: boolean;
className?: string; className?: string;
onPointHighlight?: () => void; onPointHighlight?: () => void;
onPointUnHighlight?: () => void; onPointUnHighlight?: () => void;
@@ -338,18 +344,23 @@ class TimelinePoint extends PureComponent<TimelinePointProps, TimelinePointState
this.state = { highlighted: false }; this.state = { highlighted: false };
} }
componentDidUpdate(prevProps: TimelinePointProps) {
// Update component after reordering to make animation working
if (prevProps.highlighted !== this.props.highlighted) {
this.setState({ highlighted: this.props.highlighted });
}
}
handleMouseOver = () => { handleMouseOver = () => {
if (this.props.onPointHighlight) { if (this.props.onPointHighlight) {
this.props.onPointHighlight(); this.props.onPointHighlight();
} }
this.setState({ highlighted: true });
} }
handleMouseLeave = () => { handleMouseLeave = () => {
if (this.props.onPointUnHighlight) { if (this.props.onPointUnHighlight) {
this.props.onPointUnHighlight(); this.props.onPointUnHighlight();
} }
this.setState({ highlighted: false });
} }
render() { render() {