UNPKG

1.16 kBPlain TextView Raw
1import Component from './base/component';
2import Children from './children';
3
4class Timeline extends Component {
5 index: number;
6 delay: number;
7
8 constructor(props) {
9 super(props);
10 const { delay, start = 0, children } = props;
11 const count = Children.toArray(children).length;
12 this.state = {
13 delay,
14 count,
15 index: start,
16 };
17 }
18
19 didMount() {
20 const { context } = this;
21 const { root } = context;
22 root.on('animationEnd', this.next);
23 }
24
25 didUnmount() {
26 const { context } = this;
27 const { root } = context;
28 root.off('animationEnd', this.next);
29 }
30
31 next = () => {
32 const { state, props } = this;
33 const { index, count, delay } = state;
34 const { loop } = props;
35
36 const next = loop ? (index + 1) % count : index + 1;
37 if (next < count) {
38 setTimeout(() => {
39 this.setState({
40 index: next,
41 });
42 }, delay || 0);
43 }
44 };
45
46 render() {
47 const { state, props } = this;
48 const { children } = props;
49 const { index } = state;
50 const childrenArray = Children.toArray(children);
51 return childrenArray[index];
52 }
53}
54
55export default Timeline;