-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathRow.js
263 lines (214 loc) · 6.68 KB
/
Row.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
import React, {Component, cloneElement} from 'react';
import PropTypes from 'prop-types';
import {Animated, PanResponder, StyleSheet} from 'react-native';
import {shallowEqual} from './utils';
export default class Row extends Component {
static propTypes = {
children: PropTypes.node,
animated: PropTypes.bool,
disabled: PropTypes.bool,
horizontal: PropTypes.bool,
style: PropTypes.object,
location: PropTypes.shape({
x: PropTypes.number,
y: PropTypes.number,
}),
manuallyActivateRows: PropTypes.bool,
activationTime: PropTypes.number,
// Will be called on long press.
onActivate: PropTypes.func,
onLayout: PropTypes.func,
onPress: PropTypes.func,
// Will be called, when user (directly) move the view.
onMove: PropTypes.func,
// Will be called, when user release the view.
onRelease: PropTypes.func,
};
static defaultProps = {
location: {x: 0, y: 0},
activationTime: 200,
};
constructor(props) {
super(props);
this._animatedLocation = new Animated.ValueXY(props.location);
this._location = props.location;
this._animatedLocation.addListener(this._onChangeLocation);
}
_panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => !this._isDisabled(),
onMoveShouldSetPanResponder: (e, gestureState) => {
if (this._isDisabled()) return false;
const vy = Math.abs(gestureState.vy)
const vx = Math.abs(gestureState.vx)
return this._active && (this.props.horizontal ? vx > vy : vy > vx);
},
onShouldBlockNativeResponder: () => {
// Returns whether this component should block native components from becoming the JS
// responder. Returns true by default. Is currently only supported on android.
// NOTE: Returning false here allows us to scroll unless it's a long press on a row.
return false;
},
onPanResponderGrant: (e, gestureState) => {
e.persist();
this._target = e.nativeEvent.target;
this._prevGestureState = {
...gestureState,
moveX: gestureState.x0,
moveY: gestureState.y0,
};
if (this.props.manuallyActivateRows) return;
this._longPressTimer = setTimeout(() => {
if (this._active) return;
this._toggleActive(e, gestureState);
}, this.props.activationTime);
},
onPanResponderMove: (e, gestureState) => {
if (
!this._active ||
gestureState.numberActiveTouches > 1 ||
e.nativeEvent.target !== this._target
) {
if (!this._isTouchInsideElement(e)) {
this._cancelLongPress();
}
return;
}
const elementMove = this._mapGestureToMove(this._prevGestureState, gestureState);
this.moveBy(elementMove);
this._prevGestureState = {...gestureState};
if (this.props.onMove) {
this.props.onMove(e, gestureState, this._nextLocation);
}
},
onPanResponderRelease: (e, gestureState) => {
if (this._active) {
this._toggleActive(e, gestureState);
} else {
this._cancelLongPress();
if (this._isTouchInsideElement(e) && this.props.onPress) {
this.props.onPress();
}
}
},
onPanResponderTerminationRequest: () => {
if (this._active) {
// If a view is active do not release responder.
return false;
}
this._cancelLongPress();
return true;
},
onPanResponderTerminate: (e, gestureState) => {
this._cancelLongPress();
// If responder terminated while dragging,
// deactivate the element and move to the initial location.
if (this._active) {
this._toggleActive(e, gestureState);
if (shallowEqual(this.props.location, this._location)) {
this._relocate(this.props.location);
}
}
},
});
componentDidUpdate() {
if (!this._active && !shallowEqual(this._location, this.props.location)) {
const animated = !this._active && this.props.animated;
this._relocate(this.props.location, animated);
}
}
shouldComponentUpdate(nextProps, nextState) {
return this.props.disabled !== nextProps.disabled ||
this.props.children !== nextProps.children ||
!shallowEqual(this.props.style, nextProps.style);
}
moveBy({dx = 0, dy = 0, animated = false}) {
this._nextLocation = {
x: this._location.x + dx,
y: this._location.y + dy,
};
this._relocate(this._nextLocation, animated);
}
render() {
const {children, style, horizontal} = this.props;
const rowStyle = [
style, styles.container, this._animatedLocation.getLayout(),
horizontal ? styles.horizontalContainer : styles.verticalContainer,
];
return (
<Animated.View
{...this._panResponder.panHandlers}
style={rowStyle}
onLayout={this._onLayout}>
{this.props.manuallyActivateRows && children
? cloneElement(children, {
toggleRowActive: this._toggleActive,
})
: children
}
</Animated.View>
);
}
_cancelLongPress() {
clearTimeout(this._longPressTimer);
}
_relocate(nextLocation, animated) {
this._location = nextLocation;
if (animated) {
this._isAnimationRunning = true;
Animated.timing(this._animatedLocation, {
toValue: nextLocation,
duration: 300,
useNativeDriver: false,
}).start(() => {
this._isAnimationRunning = false;
});
} else {
this._animatedLocation.setValue(nextLocation);
}
}
_toggleActive = (e, gestureState) => {
const callback = this._active ? this.props.onRelease : this.props.onActivate;
this._active = !this._active;
if (callback) {
callback(e, gestureState, this._location);
}
};
_mapGestureToMove(prevGestureState, gestureState) {
return this.props.horizontal
? {dx: gestureState.moveX - prevGestureState.moveX}
: {dy: gestureState.moveY - prevGestureState.moveY};
}
_isDisabled() {
return this.props.disabled ||
this._isAnimationRunning;
}
_isTouchInsideElement({nativeEvent}) {
return this._layout &&
nativeEvent.locationX >= 0 &&
nativeEvent.locationX <= this._layout.width &&
nativeEvent.locationY >= 0 &&
nativeEvent.locationY <= this._layout.height;
}
_onChangeLocation = (value) => {
this._location = value;
};
_onLayout = (e) => {
this._layout = e.nativeEvent.layout;
if (this.props.onLayout) {
this.props.onLayout(e);
}
};
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
},
horizontalContainer: {
top: 0,
bottom: 0,
},
verticalContainer: {
left: 0,
right: 0,
},
});