Skip to content

Commit e2b2000

Browse files
committed
v2.0.0
1 parent b79df94 commit e2b2000

File tree

2 files changed

+70
-84
lines changed

2 files changed

+70
-84
lines changed

index.js

Lines changed: 69 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ const shortSide = width > height ? height : width;
2424
export default class PickerAny extends React.Component {
2525

2626
static propTypes = {
27+
style: PropTypes.any,
2728
pickerBtnText: PropTypes.string,
2829
pickerCancelBtnText: PropTypes.string,
2930
pickerBtnStyle: PropTypes.any,
3031
pickerTitle: PropTypes.string,
3132
pickerTitleStyle: PropTypes.any,
3233
pickerToolBarStyle: PropTypes.any,
3334
pickerItemStyle: PropTypes.any,
34-
pickerWidth: PropTypes.number,
35-
pickerHeight: PropTypes.number,
35+
showMask: PropTypes.bool,
3636
showDuration: PropTypes.number,
3737
pickerData: PropTypes.any.isRequired,
3838
selectedValue: PropTypes.any.isRequired,
@@ -42,14 +42,16 @@ export default class PickerAny extends React.Component {
4242
};
4343

4444
static defaultProps = {
45-
pickerBtnText: '完成',
46-
pickerCancelBtnText: '取消',
47-
pickerHeight: 250,
45+
style: {
46+
width: width
47+
},
48+
pickerBtnText: 'Done',
49+
pickerCancelBtnText: 'Cancel',
50+
showMask: false,
4851
showDuration: 300,
4952
onPickerDone: ()=>{},
5053
onPickerCancel: ()=>{},
51-
onValueChange: ()=>{},
52-
pickerWidth: width > height ? height:width
54+
onValueChange: ()=>{}
5355
};
5456

5557
constructor(props, context){
@@ -66,34 +68,29 @@ export default class PickerAny extends React.Component {
6668
}
6769

6870
shouldComponentUpdate(nextProps, nextState, context){
69-
if(nextState.pickerWidth !== this.state.pickerWidth) {
70-
return true;
71-
}
72-
if(JSON.stringify(nextState.selectedValue) === JSON.stringify(this.state.selectedValue)){
73-
return false;
74-
}
7571
return true;
7672
}
7773

7874
_getStateFromProps(props){
7975
//the pickedValue must looks like [wheelone's, wheeltwo's, ...]
8076
//this.state.selectedValue may be the result of the first pickerWheel
81-
let style = props.style;
82-
let pickerBtnText = props.pickerBtnText;
83-
let pickerCancelBtnText = props.pickerCancelBtnText;
84-
let pickerBtnStyle = props.pickerBtnStyle;
85-
let pickerTitle = props.pickerTitle;
86-
let pickerTitleStyle = props.pickerTitleStyle;
87-
let pickerToolBarStyle = props.pickerToolBarStyle;
88-
let pickerItemStyle = props.pickerItemStyle;
89-
let pickerWidth = props.pickerWidth;
90-
let pickerHeight = props.pickerHeight;
91-
let showDuration = props.showDuration;
92-
let pickerData = props.pickerData;
93-
let selectedValue = props.selectedValue;
94-
let onPickerDone = props.onPickerDone;
95-
let onPickerCancel = props.onPickerCancel;
96-
let onValueChange = props.onValueChange;
77+
let {
78+
style,
79+
pickerBtnText,
80+
pickerCancelBtnText,
81+
pickerBtnStyle,
82+
pickerTitle,
83+
pickerTitleStyle,
84+
pickerToolBarStyle,
85+
pickerItemStyle,
86+
showDuration,
87+
showMask,
88+
pickerData,
89+
selectedValue,
90+
onPickerDone,
91+
onPickerCancel,
92+
onValueChange
93+
} = props;
9794

9895
let pickerStyle = pickerData.constructor === Array ? 'parallel' : 'cascade';
9996
let firstWheelData;
@@ -104,7 +101,7 @@ export default class PickerAny extends React.Component {
104101
let thirdWheelData;
105102
let thirdPickedDataIndex;
106103
let cascadeData = {};
107-
let slideAnim = (this.state && this.state.slideAnim ? this.state.slideAnim : new Animated.Value(-props.pickerHeight));
104+
let slideAnim = (this.state && this.state.slideAnim ? this.state.slideAnim : new Animated.Value(-height));
108105

109106
if(pickerStyle === 'parallel'){
110107
//compatible single wheel sence
@@ -134,9 +131,8 @@ export default class PickerAny extends React.Component {
134131
pickerTitleStyle,
135132
pickerToolBarStyle,
136133
pickerItemStyle,
137-
pickerWidth,
138-
pickerHeight,
139134
showDuration,
135+
showMask,
140136
pickerData,
141137
selectedValue,
142138
onPickerDone,
@@ -177,7 +173,7 @@ export default class PickerAny extends React.Component {
177173
Animated.timing(
178174
this.state.slideAnim,
179175
{
180-
toValue: -this.state.pickerHeight,
176+
toValue: -height,
181177
duration: this.state.showDuration,
182178
}
183179
).start((evt) => {
@@ -227,7 +223,7 @@ export default class PickerAny extends React.Component {
227223
this.pickerWheel.moveDown();
228224
}
229225

230-
_pickerCancel() {
226+
_pickerCancel(){
231227
this._toggle();
232228
this.state.onPickerCancel();
233229
}
@@ -351,7 +347,7 @@ export default class PickerAny extends React.Component {
351347
);
352348

353349
return (
354-
<View style={[styles.pickerWrap, { width: this.state.pickerWidth }]}>
350+
<View style={[styles.pickerWrap, {width: this.state.style.width || width}]}>
355351
<View style={styles.pickerWheel}>
356352
<Picker
357353
ref={'firstWheel'}
@@ -426,38 +422,6 @@ export default class PickerAny extends React.Component {
426422
}
427423

428424
_renderWheel(pickerData){
429-
/*
430-
some sences:
431-
1. single wheel:
432-
[1,2,3,4]
433-
2. two or more:
434-
[
435-
[1,2,3,4],
436-
[5,6,7,8],
437-
...
438-
]
439-
3. two stage cascade:
440-
{
441-
a: [1,2,3,4],
442-
b: [5,6,7,8],
443-
...
444-
}
445-
4. three stage cascade:
446-
{
447-
a: {
448-
a1: [1,2,3,4],
449-
a2: [5,6,7,8],
450-
a3: [9,10,11,12]
451-
},
452-
b: {
453-
b1: [1,2,3,4],
454-
b2: [5,6,7,8],
455-
b3: [9,10,12,12]
456-
}
457-
...
458-
}
459-
we call 1、2 parallel and 3、4 cascade
460-
*/
461425
let wheel = null;
462426
if(this.pickerStyle === 'parallel'){
463427
wheel = this._renderParallelWheel(pickerData);
@@ -469,28 +433,38 @@ export default class PickerAny extends React.Component {
469433
}
470434

471435
render(){
436+
437+
let mask = this.state.showMask ? (
438+
<View style={styles.mask} >
439+
<Text style={{width: width, height: height}} onPress={this._pickerCancel.bind(this)}></Text>
440+
</View>
441+
) : null;
442+
472443
return (
473444
<Animated.View style={[styles.picker, {
474445
width: longSide,
475-
height: this.state.pickerHeight,
446+
height: this.state.showMask ? height : this.state.style.height,
476447
bottom: this.state.slideAnim
477-
}, this.state.style]}>
478-
<View style={[styles.pickerToolbar, this.state.pickerToolBarStyle, { width: this.state.pickerWidth }]}>
479-
<View style={styles.pickerCancelBtn}>
480-
<Text style={[styles.pickerFinishBtnText, this.state.pickerBtnStyle]}
481-
onPress={this._pickerCancel.bind(this)}>{this.state.pickerCancelBtnText}</Text>
448+
}]}>
449+
{mask}
450+
<View style={[styles.pickerBox, this.state.style]}>
451+
<View style={[styles.pickerToolbar, this.state.pickerToolBarStyle, {width: this.state.style.width || width}]}>
452+
<View style={styles.pickerCancelBtn}>
453+
<Text style={[styles.pickerFinishBtnText, this.state.pickerBtnStyle]}
454+
onPress={this._pickerCancel.bind(this)}>{this.state.pickerCancelBtnText}</Text>
455+
</View>
456+
<Text style={[styles.pickerTitle, this.state.pickerTitleStyle]} numberOfLines={1}>
457+
{this.state.pickerTitle}
458+
</Text>
459+
<View style={styles.pickerFinishBtn}>
460+
<Text style={[styles.pickerFinishBtnText, this.state.pickerBtnStyle]}
461+
onPress={this._pickerFinish.bind(this)}>{this.state.pickerBtnText}</Text>
462+
</View>
482463
</View>
483-
<Text style={[styles.pickerTitle, this.state.pickerTitleStyle]} numberOfLines={1}>
484-
{this.state.pickerTitle}
485-
</Text>
486-
<View style={styles.pickerFinishBtn}>
487-
<Text style={[styles.pickerFinishBtnText, this.state.pickerBtnStyle]}
488-
onPress={this._pickerFinish.bind(this)}>{this.state.pickerBtnText}</Text>
464+
<View style={[styles.pickerWrap, {width: this.state.style.width || width}]}>
465+
{this._renderWheel(this.state.pickerData)}
489466
</View>
490467
</View>
491-
<View style={[styles.pickerWrap, { width: this.state.pickerWidth }]}>
492-
{this._renderWheel(this.state.pickerData)}
493-
</View>
494468
</Animated.View>
495469
);
496470
}
@@ -501,9 +475,21 @@ let styles = StyleSheet.create({
501475
position: 'absolute',
502476
bottom: 0,
503477
left: 0,
504-
backgroundColor: '#bdc0c7',
478+
backgroundColor: 'transparent',
505479
overflow: 'hidden'
506480
},
481+
pickerBox: {
482+
position: 'absolute',
483+
bottom: 0,
484+
left: 0
485+
},
486+
mask: {
487+
position: 'absolute',
488+
top: 0,
489+
backgroundColor: 'transparent',
490+
height: height,
491+
width: width
492+
},
507493
pickerWrap: {
508494
flexDirection: 'row'
509495
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-picker",
3-
"version": "1.1.1",
3+
"version": "2.0.0",
44
"description": "react-native-picker",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)