forked from dancormier/react-native-swipeout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
305 lines (274 loc) · 9.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
var React = require('react-native')
var tweenState = require('react-tween-state')
var {PanResponder, TouchableHighlight, StyleSheet, Text, View} = React
var styles = require('./styles.js')
var SwipeoutBtn = React.createClass({
getDefaultProps: function() {
return {
backgroundColor: null,
color: null,
component: null,
underlayColor: null,
height: 0,
key: null,
onPress: null,
text: 'Click me',
type: '',
width: 0,
}
}
, render: function() {
var btn = this.props
var styleSwipeoutBtn = [styles.swipeoutBtn]
// apply "type" styles (delete || primary || secondary)
if (btn.type === 'delete') styleSwipeoutBtn.push(styles.colorDelete)
else if (btn.type === 'primary') styleSwipeoutBtn.push(styles.colorPrimary)
else if (btn.type === 'secondary') styleSwipeoutBtn.push(styles.colorSecondary)
// apply background color
if (btn.backgroundColor) styleSwipeoutBtn.push([{ backgroundColor: btn.backgroundColor }])
styleSwipeoutBtn.push([{
height: btn.height,
width: btn.width,
}])
var styleSwipeoutBtnComponent = []
// set button dimensions
styleSwipeoutBtnComponent.push([{
height: btn.height,
width: btn.width,
}])
var styleSwipeoutBtnText = [styles.swipeoutBtnText]
// apply text color
if (btn.color) styleSwipeoutBtnText.push([{ color: btn.color }])
return (
<TouchableHighlight
onPress={this.props.onPress}
style={styles.swipeoutBtnTouchable}
underlayColor={this.props.underlayColor}
>
<View style={styleSwipeoutBtn}>
{btn.component ?
<View style={styleSwipeoutBtnComponent}>{btn.component}</View>
: <Text style={styleSwipeoutBtnText}>{btn.text}</Text>
}
</View>
</TouchableHighlight>
)
}
})
var Swipeout = React.createClass({
mixins: [tweenState.Mixin]
, getDefaultProps: function() {
return {
rowID: -1,
sectionID: -1,
}
}
, getInitialState: function() {
return {
autoClose: this.props.autoClose || false,
btnWidth: 0,
btnsLeftWidth: 0,
btnsRightWidth: 0,
contentHeight: 0,
contentPos: 0,
contentWidth: 0,
openedRight: false,
swiping: false,
tweenDuration: 160,
timeStart: null,
}
}
, componentWillMount: function() {
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: (event, gestureState) => true,
onMoveShouldSetPanResponder: (event, gestureState) => !(gestureState.dx === 0 || gestureState.dy === 0),
onPanResponderGrant: this._handlePanResponderGrant,
onPanResponderMove: this._handlePanResponderMove,
onPanResponderRelease: this._handlePanResponderEnd,
onPanResponderTerminate: this._handlePanResponderEnd,
onShouldBlockNativeResponder: (event, gestureState) => true,
});
}
, componentWillReceiveProps: function(nextProps) {
if (nextProps.close) this._close()
}
, _handlePanResponderGrant: function(e: Object, gestureState: Object) {
if(this.props.onOpen){
this.props.onOpen(this.props.sectionID, this.props.rowID)
}
this.refs.swipeoutContent.measure((ox, oy, width, height) => {
this.setState({
btnWidth: (width/5),
btnsLeftWidth: this.props.left ? (width/5)*this.props.left.length : 0,
btnsRightWidth: this.props.right ? (width/5)*this.props.right.length : 0,
swiping: true,
timeStart: (new Date()).getTime(),
})
})
}
, _handlePanResponderMove: function(e: Object, gestureState: Object) {
var posX = gestureState.dx
var posY = gestureState.dy
var leftWidth = this.state.btnsLeftWidth
var rightWidth = this.state.btnsRightWidth
if (this.state.openedRight) var posX = gestureState.dx - rightWidth
else if (this.state.openedLeft) var posX = gestureState.dx + leftWidth
// prevent scroll if moveX is true
var moveX = Math.abs(posX) > Math.abs(posY)
if (this.props.scroll) {
if (moveX) this.props.scroll(false)
else this.props.scroll(true)
}
if (this.state.swiping) {
// move content to reveal swipeout
if (posX < 0 && this.props.right) this.setState({ contentPos: Math.min(posX, 0) })
else if (posX > 0 && this.props.left) this.setState({ contentPos: Math.max(posX, 0) })
}
}
, _handlePanResponderEnd: function(e: Object, gestureState: Object) {
var posX = gestureState.dx
var contentPos = this.state.contentPos
var contentWidth = this.state.contentWidth
var btnsLeftWidth = this.state.btnsLeftWidth
var btnsRightWidth = this.state.btnsRightWidth
// minimum threshold to open swipeout
var openX = contentWidth*0.33
// should open swipeout
var openLeft = posX > openX || posX > btnsLeftWidth/2
var openRight = posX < -openX || posX < -btnsRightWidth/2
// account for open swipeouts
if (this.state.openedRight) var openRight = posX-openX < -openX
if (this.state.openedLeft) var openLeft = posX+openX > openX
// reveal swipeout on quick swipe
var timeDiff = (new Date()).getTime() - this.state.timeStart < 200
if (timeDiff) {
var openRight = posX < -openX/10 && !this.state.openedLeft
var openLeft = posX > openX/10 && !this.state.openedRight
}
if (this.state.swiping) {
if (openRight && contentPos < 0 && posX < 0) {
// open swipeout right
this._tweenContent('contentPos', -btnsRightWidth)
this.setState({ contentPos: -btnsRightWidth, openedLeft: false, openedRight: true })
} else if (openLeft && contentPos > 0 && posX > 0) {
// open swipeout left
this._tweenContent('contentPos', btnsLeftWidth)
this.setState({ contentPos: btnsLeftWidth, openedLeft: true, openedRight: false })
}
else {
// close swipeout
this._tweenContent('contentPos', 0)
this.setState({ contentPos: 0, openedLeft: false, openedRight: false })
}
}
// Allow scroll
if (this.props.scroll) this.props.scroll(true)
}
, _tweenContent: function(state, endValue) {
this.tweenState(state, {
easing: tweenState.easingTypes.easeInOutQuad,
duration: endValue === 0 ? this.state.tweenDuration*1.5 : this.state.tweenDuration,
endValue: endValue
})
}
, _rubberBandEasing: function(value, limit) {
if (value < 0 && value < limit) return limit - Math.pow(limit - value, 0.85);
else if (value > 0 && value > limit) return limit + Math.pow(value - limit, 0.85);
return value;
}
// close swipeout on button press
, _autoClose: function(btn) {
var onPress = btn.onPress
if (onPress) onPress()
if (this.state.autoClose) this._close()
}
, _close: function() {
this._tweenContent('contentPos', 0)
this.setState({
openedRight: false,
openedLeft: false,
})
}
, render: function() {
var contentWidth = this.state.contentWidth
var posX = this.getTweeningValue('contentPos')
var styleSwipeout = [styles.swipeout]
if (this.props.backgroundColor) {
styleSwipeout.push([{ backgroundColor: this.props.backgroundColor }])
}
var limit = -this.state.btnsRightWidth
if (posX > 0) var limit = this.state.btnsLeftWidth
var styleLeftPos = {
left: {
left: 0,
overflow: 'hidden',
width: Math.min(limit*(posX/limit), limit),
}
}
var styleRightPos = {
right: {
left: Math.abs(contentWidth + Math.max(limit, posX)),
right: 0,
}
}
var styleContentPos = {
content: {
left: this._rubberBandEasing(posX, limit),
}
}
var styleContent = [styles.swipeoutContent]
styleContent.push(styleContentPos.content)
var styleRight = [styles.swipeoutBtns]
styleRight.push(styleRightPos.right)
var styleLeft = [styles.swipeoutBtns]
styleLeft.push(styleLeftPos.left)
var isRightVisible = posX < 0;
var isLeftVisible = posX > 0;
return (
<View style={styleSwipeout}>
<View
ref="swipeoutContent"
style={styleContent}
onLayout={this._onLayout}
{...this._panResponder.panHandlers}>
{this.props.children}
</View>
{ this._renderButtons(this.props.right, isRightVisible, styleRight) }
{ this._renderButtons(this.props.left, isLeftVisible, styleLeft) }
</View>
)},
_onLayout: function(event) {
var { width, height } = event.nativeEvent.layout;
this.setState({
contentWidth: width,
contentHeight: height
});
},
_renderButtons: function(buttons, isVisible, style) {
if (buttons && isVisible) {
return( <View style={style}>
{ buttons.map(this._renderButton) }
</View>);
} else {
return (
<View/>
);
}
},
_renderButton: function(btn, i) {
return (
<SwipeoutBtn
backgroundColor={btn.backgroundColor}
color={btn.color}
component={btn.component}
height={this.state.contentHeight}
key={i}
onPress={() => this._autoClose(btn)}
text={btn.text}
type={btn.type}
underlayColor={btn.underlayColor}
width={this.state.btnWidth}/>
)
}
})
module.exports = Swipeout