1
1
import React , { Component } from "react" ;
2
- import SwipeableViews from "react-swipeable-views" ;
3
- import virtualize from "react-swipeable-views/lib/virtualize" ;
4
- import mod from "react-swipeable-views/lib/utils/mod" ;
5
-
6
- const EnhancedSwipeableViews = virtualize ( SwipeableViews ) ;
7
-
8
- const CONFIG = {
9
- loop : true
10
- }
2
+ import CustomAutoPlay from "./CustomAutoPlay" ;
11
3
12
4
class App extends Component {
13
-
14
- constructor ( props ) {
15
- super ( props ) ;
16
- this . state = { index : 0 } ;
17
- }
18
-
19
- componentWillUnmount ( ) {
20
- clearInterval ( this . timer ) ;
21
- }
22
-
23
- timer = null ;
24
-
25
- slideRenderer = ( params , children ) => {
26
- let childrenLength = children . length ;
27
- const { index, key} = params ;
28
- if ( mod ( index , childrenLength ) < childrenLength ) {
29
- return this . dealChild ( children [ mod ( index , childrenLength ) ] , key )
30
- }
31
- return null
32
- }
33
-
34
- middle = ( params ) => {
5
+ render ( ) {
35
6
let children = [
36
7
< div id = "test" >
37
8
{ 'slide n°0' }
@@ -42,79 +13,16 @@ class App extends Component {
42
13
< div id = "test" >
43
14
{ 'slide n°2' }
44
15
</ div > ,
16
+ < div id = "test" >
17
+ { 'slide n°3' }
18
+ </ div > ,
45
19
] ;
46
20
47
- return this . slideRenderer ( params , children ) ;
48
- }
49
-
50
-
51
- dealChild = ( div , key ) => {
52
- return < div key = { key } >
53
- { div }
54
- </ div >
55
- }
56
-
57
- componentDidMount ( ) {
58
- this . startInterval ( ) ;
59
- }
60
-
61
-
62
- startInterval ( interval = 2000 ) {
63
- if ( CONFIG . loop != true ) {
64
- return
65
- }
66
- clearInterval ( this . timer ) ;
67
- this . timer = setInterval ( this . handleInterval , interval ) ;
68
- }
69
-
70
- handleInterval = ( ) => {
71
- const indexLatest = this . state . index ;
72
- //Todo use props.children.length
73
- let indexNew = mod ( ( indexLatest + 1 ) , 3 ) ;
74
- console . info ( 'setTimer:indexNew' + indexNew )
75
- this . setState ( {
76
- index : indexNew ,
77
- } ) ;
78
- } ;
79
-
80
-
81
- onChangeIndex = ( index , indexLatest ) => {
82
- console . info ( 'onChangeIndexindex:[' + index + "]===indexLatest[" + indexLatest + "]---this.state.index=[" + ( this . state . index ) + "]" )
83
- let stateIndex = this . state . index ;
84
- if ( ( index - indexLatest ) > 0 ) {
85
- stateIndex = ( stateIndex + 1 ) ;
86
- } else if ( ( index - indexLatest ) < 0 ) {
87
- stateIndex = ( stateIndex - 1 ) ;
88
- }
89
- console . info ( 'result===' + stateIndex + "mod=" + ( mod ( stateIndex , 3 ) ) ) ;
90
- this . setState ( {
91
- index : stateIndex
92
- } )
93
- }
94
-
95
- handleSwitching = ( index , type ) => {
96
- if ( this . timer ) {
97
- console . error ( 'clear timer' ) ;
98
- clearInterval ( this . timer ) ;
99
- this . timer = null ;
100
- } else if ( type === 'end' ) {
101
- console . error ( 'start timer' ) ;
102
- this . startInterval ( ) ;
103
- }
104
-
105
- console . log ( 'invoke handleSwitching' ) ;
106
- } ;
107
-
108
-
109
- render ( ) {
110
- let { index} = this . state ;
111
- return (
112
- < EnhancedSwipeableViews
113
- index = { index }
114
- onSwitching = { this . handleSwitching }
115
- onChangeIndex = { this . onChangeIndex }
116
- slideRenderer = { this . middle } />
117
- ) ;
21
+ return < CustomAutoPlay
22
+ children = { children }
23
+ autoplay = { true }
24
+ interval = { 3500 }
25
+ /> ;
118
26
}
119
27
}
120
28
0 commit comments