Commit e2b20005 authored by xwenliang's avatar xwenliang

v2.0.0

parent b79df94b
...@@ -24,6 +24,7 @@ const shortSide = width > height ? height : width; ...@@ -24,6 +24,7 @@ const shortSide = width > height ? height : width;
export default class PickerAny extends React.Component { export default class PickerAny extends React.Component {
static propTypes = { static propTypes = {
style: PropTypes.any,
pickerBtnText: PropTypes.string, pickerBtnText: PropTypes.string,
pickerCancelBtnText: PropTypes.string, pickerCancelBtnText: PropTypes.string,
pickerBtnStyle: PropTypes.any, pickerBtnStyle: PropTypes.any,
...@@ -31,8 +32,7 @@ export default class PickerAny extends React.Component { ...@@ -31,8 +32,7 @@ export default class PickerAny extends React.Component {
pickerTitleStyle: PropTypes.any, pickerTitleStyle: PropTypes.any,
pickerToolBarStyle: PropTypes.any, pickerToolBarStyle: PropTypes.any,
pickerItemStyle: PropTypes.any, pickerItemStyle: PropTypes.any,
pickerWidth: PropTypes.number, showMask: PropTypes.bool,
pickerHeight: PropTypes.number,
showDuration: PropTypes.number, showDuration: PropTypes.number,
pickerData: PropTypes.any.isRequired, pickerData: PropTypes.any.isRequired,
selectedValue: PropTypes.any.isRequired, selectedValue: PropTypes.any.isRequired,
...@@ -42,14 +42,16 @@ export default class PickerAny extends React.Component { ...@@ -42,14 +42,16 @@ export default class PickerAny extends React.Component {
}; };
static defaultProps = { static defaultProps = {
pickerBtnText: '完成', style: {
pickerCancelBtnText: '取消', width: width
pickerHeight: 250, },
pickerBtnText: 'Done',
pickerCancelBtnText: 'Cancel',
showMask: false,
showDuration: 300, showDuration: 300,
onPickerDone: ()=>{}, onPickerDone: ()=>{},
onPickerCancel: ()=>{}, onPickerCancel: ()=>{},
onValueChange: ()=>{}, onValueChange: ()=>{}
pickerWidth: width > height ? height:width
}; };
constructor(props, context){ constructor(props, context){
...@@ -66,34 +68,29 @@ export default class PickerAny extends React.Component { ...@@ -66,34 +68,29 @@ export default class PickerAny extends React.Component {
} }
shouldComponentUpdate(nextProps, nextState, context){ shouldComponentUpdate(nextProps, nextState, context){
if(nextState.pickerWidth !== this.state.pickerWidth) {
return true;
}
if(JSON.stringify(nextState.selectedValue) === JSON.stringify(this.state.selectedValue)){
return false;
}
return true; return true;
} }
_getStateFromProps(props){ _getStateFromProps(props){
//the pickedValue must looks like [wheelone's, wheeltwo's, ...] //the pickedValue must looks like [wheelone's, wheeltwo's, ...]
//this.state.selectedValue may be the result of the first pickerWheel //this.state.selectedValue may be the result of the first pickerWheel
let style = props.style; let {
let pickerBtnText = props.pickerBtnText; style,
let pickerCancelBtnText = props.pickerCancelBtnText; pickerBtnText,
let pickerBtnStyle = props.pickerBtnStyle; pickerCancelBtnText,
let pickerTitle = props.pickerTitle; pickerBtnStyle,
let pickerTitleStyle = props.pickerTitleStyle; pickerTitle,
let pickerToolBarStyle = props.pickerToolBarStyle; pickerTitleStyle,
let pickerItemStyle = props.pickerItemStyle; pickerToolBarStyle,
let pickerWidth = props.pickerWidth; pickerItemStyle,
let pickerHeight = props.pickerHeight; showDuration,
let showDuration = props.showDuration; showMask,
let pickerData = props.pickerData; pickerData,
let selectedValue = props.selectedValue; selectedValue,
let onPickerDone = props.onPickerDone; onPickerDone,
let onPickerCancel = props.onPickerCancel; onPickerCancel,
let onValueChange = props.onValueChange; onValueChange
} = props;
let pickerStyle = pickerData.constructor === Array ? 'parallel' : 'cascade'; let pickerStyle = pickerData.constructor === Array ? 'parallel' : 'cascade';
let firstWheelData; let firstWheelData;
...@@ -104,7 +101,7 @@ export default class PickerAny extends React.Component { ...@@ -104,7 +101,7 @@ export default class PickerAny extends React.Component {
let thirdWheelData; let thirdWheelData;
let thirdPickedDataIndex; let thirdPickedDataIndex;
let cascadeData = {}; let cascadeData = {};
let slideAnim = (this.state && this.state.slideAnim ? this.state.slideAnim : new Animated.Value(-props.pickerHeight)); let slideAnim = (this.state && this.state.slideAnim ? this.state.slideAnim : new Animated.Value(-height));
if(pickerStyle === 'parallel'){ if(pickerStyle === 'parallel'){
//compatible single wheel sence //compatible single wheel sence
...@@ -134,9 +131,8 @@ export default class PickerAny extends React.Component { ...@@ -134,9 +131,8 @@ export default class PickerAny extends React.Component {
pickerTitleStyle, pickerTitleStyle,
pickerToolBarStyle, pickerToolBarStyle,
pickerItemStyle, pickerItemStyle,
pickerWidth,
pickerHeight,
showDuration, showDuration,
showMask,
pickerData, pickerData,
selectedValue, selectedValue,
onPickerDone, onPickerDone,
...@@ -177,7 +173,7 @@ export default class PickerAny extends React.Component { ...@@ -177,7 +173,7 @@ export default class PickerAny extends React.Component {
Animated.timing( Animated.timing(
this.state.slideAnim, this.state.slideAnim,
{ {
toValue: -this.state.pickerHeight, toValue: -height,
duration: this.state.showDuration, duration: this.state.showDuration,
} }
).start((evt) => { ).start((evt) => {
...@@ -227,7 +223,7 @@ export default class PickerAny extends React.Component { ...@@ -227,7 +223,7 @@ export default class PickerAny extends React.Component {
this.pickerWheel.moveDown(); this.pickerWheel.moveDown();
} }
_pickerCancel() { _pickerCancel(){
this._toggle(); this._toggle();
this.state.onPickerCancel(); this.state.onPickerCancel();
} }
...@@ -351,7 +347,7 @@ export default class PickerAny extends React.Component { ...@@ -351,7 +347,7 @@ export default class PickerAny extends React.Component {
); );
return ( return (
<View style={[styles.pickerWrap, { width: this.state.pickerWidth }]}> <View style={[styles.pickerWrap, {width: this.state.style.width || width}]}>
<View style={styles.pickerWheel}> <View style={styles.pickerWheel}>
<Picker <Picker
ref={'firstWheel'} ref={'firstWheel'}
...@@ -426,38 +422,6 @@ export default class PickerAny extends React.Component { ...@@ -426,38 +422,6 @@ export default class PickerAny extends React.Component {
} }
_renderWheel(pickerData){ _renderWheel(pickerData){
/*
some sences:
1. single wheel:
[1,2,3,4]
2. two or more:
[
[1,2,3,4],
[5,6,7,8],
...
]
3. two stage cascade:
{
a: [1,2,3,4],
b: [5,6,7,8],
...
}
4. three stage cascade:
{
a: {
a1: [1,2,3,4],
a2: [5,6,7,8],
a3: [9,10,11,12]
},
b: {
b1: [1,2,3,4],
b2: [5,6,7,8],
b3: [9,10,12,12]
}
...
}
we call 1、2 parallel and 3、4 cascade
*/
let wheel = null; let wheel = null;
if(this.pickerStyle === 'parallel'){ if(this.pickerStyle === 'parallel'){
wheel = this._renderParallelWheel(pickerData); wheel = this._renderParallelWheel(pickerData);
...@@ -469,13 +433,22 @@ export default class PickerAny extends React.Component { ...@@ -469,13 +433,22 @@ export default class PickerAny extends React.Component {
} }
render(){ render(){
let mask = this.state.showMask ? (
<View style={styles.mask} >
<Text style={{width: width, height: height}} onPress={this._pickerCancel.bind(this)}></Text>
</View>
) : null;
return ( return (
<Animated.View style={[styles.picker, { <Animated.View style={[styles.picker, {
width: longSide, width: longSide,
height: this.state.pickerHeight, height: this.state.showMask ? height : this.state.style.height,
bottom: this.state.slideAnim bottom: this.state.slideAnim
}, this.state.style]}> }]}>
<View style={[styles.pickerToolbar, this.state.pickerToolBarStyle, { width: this.state.pickerWidth }]}> {mask}
<View style={[styles.pickerBox, this.state.style]}>
<View style={[styles.pickerToolbar, this.state.pickerToolBarStyle, {width: this.state.style.width || width}]}>
<View style={styles.pickerCancelBtn}> <View style={styles.pickerCancelBtn}>
<Text style={[styles.pickerFinishBtnText, this.state.pickerBtnStyle]} <Text style={[styles.pickerFinishBtnText, this.state.pickerBtnStyle]}
onPress={this._pickerCancel.bind(this)}>{this.state.pickerCancelBtnText}</Text> onPress={this._pickerCancel.bind(this)}>{this.state.pickerCancelBtnText}</Text>
...@@ -488,9 +461,10 @@ export default class PickerAny extends React.Component { ...@@ -488,9 +461,10 @@ export default class PickerAny extends React.Component {
onPress={this._pickerFinish.bind(this)}>{this.state.pickerBtnText}</Text> onPress={this._pickerFinish.bind(this)}>{this.state.pickerBtnText}</Text>
</View> </View>
</View> </View>
<View style={[styles.pickerWrap, { width: this.state.pickerWidth }]}> <View style={[styles.pickerWrap, {width: this.state.style.width || width}]}>
{this._renderWheel(this.state.pickerData)} {this._renderWheel(this.state.pickerData)}
</View> </View>
</View>
</Animated.View> </Animated.View>
); );
} }
...@@ -501,9 +475,21 @@ let styles = StyleSheet.create({ ...@@ -501,9 +475,21 @@ let styles = StyleSheet.create({
position: 'absolute', position: 'absolute',
bottom: 0, bottom: 0,
left: 0, left: 0,
backgroundColor: '#bdc0c7', backgroundColor: 'transparent',
overflow: 'hidden' overflow: 'hidden'
}, },
pickerBox: {
position: 'absolute',
bottom: 0,
left: 0
},
mask: {
position: 'absolute',
top: 0,
backgroundColor: 'transparent',
height: height,
width: width
},
pickerWrap: { pickerWrap: {
flexDirection: 'row' flexDirection: 'row'
}, },
......
{ {
"name": "react-native-picker", "name": "react-native-picker",
"version": "1.1.1", "version": "2.0.0",
"description": "react-native-picker", "description": "react-native-picker",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment