1
1
import React , { PureComponent } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
4
- import './SwipeableListItem.css' ;
4
+ import styles from './SwipeableListItem.css' ;
5
5
6
6
const SwipeActionPropType = PropTypes . shape ( {
7
7
action : PropTypes . func . isRequired ,
8
- background : PropTypes . node . isRequired
8
+ content : PropTypes . node . isRequired
9
9
} ) ;
10
10
11
11
class SwipeableListItem extends PureComponent {
12
12
constructor ( props ) {
13
13
super ( props ) ;
14
14
15
- this . backgroundLeft = null ;
16
- this . backgroundRight = null ;
15
+ this . contentLeft = null ;
16
+ this . contentRight = null ;
17
17
this . listElement = null ;
18
18
this . wrapper = null ;
19
19
@@ -49,12 +49,12 @@ class SwipeableListItem extends PureComponent {
49
49
onDragStart = ( { clientX } ) => {
50
50
this . dragged = true ;
51
51
this . dragStartX = clientX ;
52
- this . listElement . className = ' content' ;
53
- if ( this . backgroundLeft ) {
54
- this . backgroundLeft . className = 'background' ;
52
+ this . listElement . className = styles . content ;
53
+ if ( this . contentLeft ) {
54
+ this . contentLeft . className = styles . contentLeft ;
55
55
}
56
- if ( this . backgroundRight ) {
57
- this . backgroundRight . className = 'background right' ;
56
+ if ( this . contentRight ) {
57
+ this . contentRight . className = styles . contentRight ;
58
58
}
59
59
this . startTime = Date . now ( ) ;
60
60
requestAnimationFrame ( this . updatePosition ) ;
@@ -83,34 +83,34 @@ class SwipeableListItem extends PureComponent {
83
83
}
84
84
85
85
this . left = 0 ;
86
- this . listElement . className = 'content return' ;
86
+ this . listElement . className = styles . contentReturn ;
87
87
this . listElement . style . transform = `translateX(${ this . left } px)` ;
88
88
89
89
// hide backgrounds
90
- if ( this . backgroundLeft ) {
91
- this . backgroundLeft . style . opacity = 0 ;
92
- this . backgroundLeft . className = ` ${ this . backgroundLeft . className } return` ;
90
+ if ( this . contentLeft ) {
91
+ this . contentLeft . style . opacity = 0 ;
92
+ this . contentLeft . className = styles . contentLeftReturn ;
93
93
}
94
94
95
- if ( this . backgroundRight ) {
96
- this . backgroundRight . style . opacity = 0 ;
97
- this . backgroundRight . className = ` ${ this . backgroundRight . className } return` ;
95
+ if ( this . contentRight ) {
96
+ this . contentRight . style . opacity = 0 ;
97
+ this . contentRight . className = styles . contentRightReturn ;
98
98
}
99
99
}
100
100
} ;
101
101
102
102
shouldMoveItem = delta => {
103
103
const {
104
- swipeLeft : { background : backgroundLeft } = { } ,
105
- swipeRight : { background : backgroundRight } = { } ,
104
+ swipeLeft : { content : contentLeft } = { } ,
105
+ swipeRight : { content : contentRight } = { } ,
106
106
blockSwipe
107
107
} = this . props ;
108
108
const swipingLeft = delta < 0 ;
109
109
const swipingRight = delta > 0 ;
110
110
111
111
return (
112
112
! blockSwipe &&
113
- ( ( swipingLeft && backgroundLeft ) || ( swipingRight && backgroundRight ) )
113
+ ( ( swipingLeft && contentLeft ) || ( swipingRight && contentRight ) )
114
114
) ;
115
115
} ;
116
116
@@ -145,32 +145,27 @@ class SwipeableListItem extends PureComponent {
145
145
const elapsed = now - this . startTime ;
146
146
147
147
if ( this . dragged && elapsed > this . fpsInterval ) {
148
- let backgroundToShow =
149
- this . left < 0 ? this . backgroundLeft : this . backgroundRight ;
150
- let backgroundToHide =
151
- this . left < 0 ? this . backgroundRight : this . backgroundLeft ;
148
+ let contentToShow = this . left < 0 ? this . contentLeft : this . contentRight ;
149
+ let contentToHide = this . left < 0 ? this . contentRight : this . contentLeft ;
152
150
153
- if ( ! backgroundToShow ) {
151
+ if ( ! contentToShow ) {
154
152
return ;
155
153
}
156
154
157
155
const opacity = ( Math . abs ( this . left ) / 100 ) . toFixed ( 2 ) ;
158
156
159
157
this . listElement . style . transform = `translateX(${ this . left } px)` ;
160
158
161
- if (
162
- opacity < 1 &&
163
- opacity . toString ( ) !== backgroundToShow . style . opacity
164
- ) {
165
- backgroundToShow . style . opacity = opacity . toString ( ) ;
159
+ if ( opacity < 1 && opacity . toString ( ) !== contentToShow . style . opacity ) {
160
+ contentToShow . style . opacity = opacity . toString ( ) ;
166
161
167
- if ( backgroundToHide ) {
168
- backgroundToHide . style . opacity = '0' ;
162
+ if ( contentToHide ) {
163
+ contentToHide . style . opacity = '0' ;
169
164
}
170
165
}
171
166
172
167
if ( opacity >= 1 ) {
173
- backgroundToShow . style . opacity = '1' ;
168
+ contentToShow . style . opacity = '1' ;
174
169
}
175
170
176
171
this . startTime = Date . now ( ) ;
@@ -193,31 +188,31 @@ class SwipeableListItem extends PureComponent {
193
188
}
194
189
} ;
195
190
196
- bindBackgroundLeft = ref => ( this . backgroundLeft = ref ) ;
197
- bindBackgroundRight = ref => ( this . backgroundRight = ref ) ;
191
+ bindContentLeft = ref => ( this . contentLeft = ref ) ;
192
+ bindContentRight = ref => ( this . contentRight = ref ) ;
198
193
bindListElement = ref => ( this . listElement = ref ) ;
199
194
bindWrapper = ref => ( this . wrapper = ref ) ;
200
195
201
196
render ( ) {
202
197
const { children, swipeLeft, swipeRight } = this . props ;
203
198
204
199
return (
205
- < div className = "swipeable-list-item" ref = { this . bindWrapper } >
200
+ < div className = { styles . swipeableListItem } ref = { this . bindWrapper } >
206
201
{ swipeLeft && (
207
- < div ref = { this . bindBackgroundLeft } className = "background" >
208
- { swipeLeft . background }
202
+ < div ref = { this . bindContentLeft } className = { styles . contentLeft } >
203
+ { swipeLeft . content }
209
204
</ div >
210
205
) }
211
206
{ swipeRight && (
212
- < div ref = { this . bindBackgroundRight } className = "background right" >
213
- { swipeRight . background }
207
+ < div ref = { this . bindContentRight } className = { styles . contentRight } >
208
+ { swipeRight . content }
214
209
</ div >
215
210
) }
216
211
< div
217
212
ref = { this . bindListElement }
218
213
onMouseDown = { this . onDragStartMouse }
219
214
onTouchStart = { this . onDragStartTouch }
220
- className = " content"
215
+ className = { styles . content }
221
216
>
222
217
{ children }
223
218
</ div >
0 commit comments