forked from beefe/react-native-picker-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
330 lines (292 loc) · 7.32 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'use strict';
import React, {Component, PropTypes} from 'react';
import {
StyleSheet,
View,
Text,
Image,
Dimensions,
PixelRatio,
PanResponder
} from 'react-native';
class PickerAndroidItem extends Component{
static propTypes = {
value: PropTypes.any,
label: PropTypes.any
};
constructor(props, context){
super(props, context);
}
render() {
return null;
}
};
export default class PickerAndroid extends Component{
static Item = PickerAndroidItem;
static propTypes = {
//picker's style
pickerStyle: View.propTypes.style,
//picker item's style
itemStyle: Text.propTypes.style,
//picked value changed then call this function
onValueChange: PropTypes.func,
//default to be selected value
selectedValue: PropTypes.any
};
constructor(props, context){
super(props, context);
this.state = this._stateFromProps(this.props);
}
componentWillReceiveProps(nextProps){
this.setState(this._stateFromProps(nextProps));
}
shouldComponentUpdate(nextProps, nextState, context){
return JSON.stringify([{
selectedIndex: nextState.selectedIndex,
items: nextState.items,
pickerStyle: nextState.pickerStyle,
itemStyle: nextState.itemStyle,
onValueChange: nextState.onValueChange
}, context]) !== JSON.stringify([{
selectedIndex: this.state.selectedIndex,
items: this.state.items,
pickerStyle: this.state.pickerStyle,
itemStyle: this.state.itemStyle,
onValueChange: this.state.onValueChange
}, this.context]);
}
_stateFromProps(props){
let selectedIndex = 0;
let items = [];
let pickerStyle = props.pickerStyle;
let itemStyle = props.itemStyle;
let onValueChange = props.onValueChange;
React.Children.forEach(props.children, (child, index) => {
child.props.value === props.selectedValue && ( selectedIndex = index );
items.push({value: child.props.value, label: child.props.label});
});
//fix issue#https://github.com/beefe/react-native-picker/issues/51
this.index = selectedIndex;
return {
selectedIndex,
items,
pickerStyle,
itemStyle,
onValueChange
};
}
_move(dy){
let index = this.index;
this.middleHeight = Math.abs(-index * 40 + dy);
this.up && this.up.setNativeProps({
style: {
marginTop: (3 - index) * 30 + dy * .75,
},
});
this.middle && this.middle.setNativeProps({
style: {
marginTop: -index * 40 + dy,
},
});
this.down && this.down.setNativeProps({
style: {
marginTop: (-index - 1) * 30 + dy * .75,
},
});
}
_moveTo(index){
let _index = this.index;
let diff = _index - index;
let marginValue;
let that = this;
if(diff && !this.isMoving) {
marginValue = diff * 40;
this._move(marginValue);
this.index = index;
this._onValueChange();
}
}
//cascade mode will reset the wheel position
moveTo(index){
this._moveTo(index);
}
moveUp(){
this._moveTo(Math.max(this.state.items.index - 1, 0));
}
moveDown() {
this._moveTo(Math.min(this.index + 1, this.state.items.length - 1));
}
_handlePanResponderMove(evt, gestureState){
let dy = gestureState.dy;
if(this.isMoving) {
return;
}
// turn down
if(dy > 0) {
this._move(dy > this.index * 40 ? this.index * 40 : dy);
}else{
this._move(dy < (this.index - this.state.items.length + 1) * 40 ? (this.index - this.state.items.length + 1) * 40 : dy);
}
}
_handlePanResponderRelease(evt, gestureState){
let middleHeight = this.middleHeight;
this.index = middleHeight % 40 >= 20 ? Math.ceil(middleHeight / 40) : Math.floor(middleHeight / 40);
this._move(0);
this._onValueChange();
}
componentWillMount(){
this._panResponder = PanResponder.create({
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
onPanResponderRelease: this._handlePanResponderRelease.bind(this),
onPanResponderMove: this._handlePanResponderMove.bind(this)
});
this.isMoving = false;
this.index = this.state.selectedIndex;
}
componentWillUnmount(){
this.timer && clearInterval(this.timer);
}
_renderItems(items){
//value was used to watch the change of picker
//label was used to display
let upItems = [], middleItems = [], downItems = [];
items.forEach((item, index) => {
upItems[index] = <Text
key={'up'+index}
style={[styles.upText, this.state.itemStyle]}
onPress={() => {
this._moveTo(index);
}} >
{item.label}
</Text>;
middleItems[index] = <Text
key={'mid'+index}
style={[styles.middleText, this.state.itemStyle]}>{item.label}
</Text>;
downItems[index] = <Text
key={'down'+index}
style={[styles.downText, this.state.itemStyle]}
onPress={() => {
this._moveTo(index);
}} >
{item.label}
</Text>;
});
return { upItems, middleItems, downItems, };
}
_onValueChange(){
//the current picked label was more expected to be passed,
//but PickerIOS only passed value, so we set label to be the second argument
//add by zooble @2015-12-10
var curItem = this.state.items[this.index];
this.state.onValueChange && this.state.onValueChange(curItem.value, curItem.label);
}
render(){
let index = this.state.selectedIndex;
let length = this.state.items.length;
let items = this._renderItems(this.state.items);
let upViewStyle = {
marginTop: (3 - index) * 30,
height: length * 30,
};
let middleViewStyle = {
marginTop: -index * 40,
};
let downViewStyle = {
marginTop: (-index - 1) * 30,
height: length * 30,
};
return (
//total to be 90*2+40=220 height
<View style={[styles.container, this.state.pickerStyle]} {...this._panResponder.panHandlers}>
<View style={styles.up}>
<View style={[styles.upView, upViewStyle]} ref={(up) => { this.up = up }} >
{ items.upItems }
</View>
</View>
<View style={styles.middle}>
<View style={[styles.middleView, middleViewStyle]} ref={(middle) => { this.middle = middle }} >
{ items.middleItems }
</View>
</View>
<View style={styles.down}>
<View style={[styles.downView, downViewStyle]} ref={(down) => { this.down = down }} >
{ items.downItems }
</View>
</View>
</View>
);
}
};
let width = Dimensions.get('window').width;
let height = Dimensions.get('window').height;
let ratio = PixelRatio.get();
let styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
//this is very important
backgroundColor: null
},
up: {
height: 90,
overflow: 'hidden'
},
upView: {
justifyContent: 'flex-start',
alignItems: 'center'
},
upText: {
paddingTop: 0,
height: 30,
fontSize: 20,
color: '#000',
opacity: .5,
paddingBottom: 0,
marginTop: 0,
marginBottom: 0
},
middle: {
height: 40,
width: width,
overflow: 'hidden',
borderColor: '#aaa',
borderTopWidth: 1/ratio,
borderBottomWidth: 1/ratio
},
middleView: {
height: 40,
justifyContent: 'flex-start',
alignItems: 'center'
},
middleText: {
paddingTop: 0,
height: 40,
color: '#000',
fontSize: 28,
paddingBottom: 0,
marginTop: 0,
marginBottom: 0
},
down: {
height: 90,
overflow: 'hidden'
},
downView: {
overflow: 'hidden',
justifyContent: 'flex-start',
alignItems: 'center'
},
downText: {
paddingTop: 0,
height: 30,
fontSize: 16,
color: '#000',
opacity: .5,
paddingBottom: 0,
marginTop: 0,
marginBottom: 0
}
});