index.js 3.76 KB
Newer Older
xwenliang's avatar
xwenliang committed
1 2 3 4 5 6 7 8 9
'use strict';

import React, {
	StyleSheet, 
	PropTypes, 
	View, 
	Text,
	Animated,
	Platform,
xwenliang's avatar
bugs  
xwenliang committed
10 11
	Dimensions,
	PickerIOS
xwenliang's avatar
xwenliang committed
12 13 14 15 16 17 18 19 20 21 22
} from 'react-native';

import PickerAndroid from 'react-native-picker-android';

let Picker = Platform.OS === 'ios' ? PickerIOS : PickerAndroid;
let PickerItem = Picker.Item;
let width = Dimensions.get('window').width;
let height = Dimensions.get('window').height;

export default class PickerAny extends React.Component {

xwenliang's avatar
bugs  
xwenliang committed
23 24 25 26 27 28 29 30 31 32 33 34 35
	static propTypes = {
		pickerHeight: PropTypes.number,
		showDuration: PropTypes.number,
		pickerData: PropTypes.array,
		onPickerDone: PropTypes.func
	}

	static defaultProps = {
		pickerHeight: height/3,
		showDuration: 300,
		onPickerDone: ()=>{}
	}

xwenliang's avatar
xwenliang committed
36 37 38
	constructor(props, context){
		super(props, context);
		this.state = {
xwenliang's avatar
bugs  
xwenliang committed
39 40
			selectedValue: this.props.selectedValue,
			slideAnim: new Animated.Value(-this.props.pickerHeight)
xwenliang's avatar
xwenliang committed
41
		};
xwenliang's avatar
bugs  
xwenliang committed
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
	}

	_slideUp(){
		this.isMoving = true;
		Animated.timing(
			this.state.slideAnim,
			{
				toValue: 0,
				duration: this.props.showDuration,
			}
		).start((evt) => {
			if(evt.finished) {
				this.isMoving = false;
				this.isPickerShow = true;
			}
		});
	}

	_slideDown(){
		this.isMoving = true;
		Animated.timing(
			this.state.slideAnim,
			{
				toValue: -this.props.pickerHeight,
				duration: this.props.showDuration,
			}
		).start((evt) => {
			if(evt.finished) {
				this.isMoving = false;
				this.isPickerShow = false;
			}
		});
	}

	_toggle(){
		if(this.isMoving) {
			return;
		}
		if(this.isPickerShow) {
			this._slideDown();
		}
		else{
			this._slideUp();
		}
	}
	//向父组件提供方法
	toggle(){
		this._toggle();
	}
xwenliang's avatar
xwenliang committed
91 92

	_prePressHandle(callback){
xwenliang's avatar
bugs  
xwenliang committed
93 94 95
		//通知子组件往上滚
		this.pickerWheel.moveUp();
	}
xwenliang's avatar
xwenliang committed
96 97

	_nextPressHandle(callback){
xwenliang's avatar
bugs  
xwenliang committed
98 99 100 101 102 103 104 105 106
		//通知子组件往下滚
		this.pickerWheel.moveDown();
	}

	_pickerFinish(){
		let pickedValue = this.pickedValue === undefined ? this.state.selectedValue : this.pickedValue;
		this._toggle();
		this.props.onPickerDone(pickedValue);
	}
xwenliang's avatar
xwenliang committed
107 108
	
	render(){
xwenliang's avatar
bugs  
xwenliang committed
109 110 111 112
		let pickerBtn = Platform.OS === 'ios' ? null : (
			<View style={styles.pickerBtnView}>
				<Text style={styles.pickerMoveBtn} onPress={this._prePressHandle.bind(this)}>上一个</Text>
				<Text style={styles.pickerMoveBtn} onPress={this._nextPressHandle.bind(this)}>下一个</Text>
xwenliang's avatar
xwenliang committed
113
			</View>
xwenliang's avatar
bugs  
xwenliang committed
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
		);
		return (
			<Animated.View style={[styles.picker, {
				height: this.props.pickerHeight,
				bottom: this.state.slideAnim
			}]}>
				<View style={styles.pickerToolbar}>
					{pickerBtn}
					<View style={styles.pickerFinishBtn}>
						<Text style={styles.pickerFinishBtnText} 
							onPress={this._pickerFinish.bind(this)}>完成</Text>
					</View>
				</View>
				<Picker
					ref={pickerWheel => this.pickerWheel = pickerWheel }
					selectedValue={this.state.selectedValue}
					onValueChange={value => {
						this.pickedValue = value;
						this.setState({
							selectedValue: value
						});
					}} >
					{this.props.pickerData.map((value, index) => (
						<PickerItem
							key={index}
							value={value}
							label={value}
						/>)
					)}
				</Picker>
			</Animated.View>
		);
	}
xwenliang's avatar
xwenliang committed
147 148 149
};

let styles = StyleSheet.create({
xwenliang's avatar
bugs  
xwenliang committed
150
	picker: {
xwenliang's avatar
xwenliang committed
151 152 153 154
		flex: 1,
		position: 'absolute',
		bottom: 0,
		left: 0,
xwenliang's avatar
bugs  
xwenliang committed
155 156
		backgroundColor: '#bdc0c7',
		width: width,
xwenliang's avatar
xwenliang committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
		overflow: 'hidden', 
	},
	pickerToolbar: {
		height: 30,
		width: width,
		backgroundColor: '#e6e6e6',
		flexDirection: 'row',
		borderTopWidth: 1,
		borderBottomWidth: 1,
		borderColor: '#c3c3c3'
	},
	pickerBtnView: {
		flex: 1,
		flexDirection: 'row',
		justifyContent: 'flex-start',
xwenliang's avatar
bugs  
xwenliang committed
172
		alignItems: 'center'
xwenliang's avatar
xwenliang committed
173 174 175 176
	},
	pickerMoveBtn: {
		color: '#149be0',
		fontSize: 16,
xwenliang's avatar
bugs  
xwenliang committed
177
		marginLeft: 20
xwenliang's avatar
xwenliang committed
178 179 180 181 182 183
	},
	pickerFinishBtn: {
		flex: 1,
		flexDirection: 'row',
		justifyContent: 'flex-end',
		alignItems: 'center',
xwenliang's avatar
bugs  
xwenliang committed
184
		marginRight: 20
xwenliang's avatar
xwenliang committed
185 186 187 188 189 190
	},
	pickerFinishBtnText: {
		fontSize: 16,
		color: '#149be0'
	}
});