index.js 11.5 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
} from 'react-native';

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

let Picker = Platform.OS === 'ios' ? PickerIOS : PickerAndroid;
xwenliang's avatar
xwenliang committed
17
let PickerItem = Picker.Item;
xwenliang's avatar
xwenliang committed
18 19 20 21 22
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
	static propTypes = {
xwenliang's avatar
xwenliang committed
24 25 26 27
		pickerBtnText: PropTypes.string,
		pickerBtnStyle: PropTypes.any,
		pickerToolBarStyle: PropTypes.any,
		pickerItemStyle: PropTypes.any,
xwenliang's avatar
bugs  
xwenliang committed
28 29
		pickerHeight: PropTypes.number,
		showDuration: PropTypes.number,
xwenliang's avatar
xwenliang committed
30 31
		pickerData: PropTypes.any.isRequired,
		selectedValue: PropTypes.any.isRequired,
xwenliang's avatar
bugs  
xwenliang committed
32 33 34 35
		onPickerDone: PropTypes.func
	}

	static defaultProps = {
xwenliang's avatar
xwenliang committed
36 37
		pickerBtnText: '完成',
		pickerHeight: 250,
xwenliang's avatar
bugs  
xwenliang committed
38 39 40 41
		showDuration: 300,
		onPickerDone: ()=>{}
	}

xwenliang's avatar
xwenliang committed
42 43
	constructor(props, context){
		super(props, context);
xwenliang's avatar
xwenliang committed
44 45 46 47 48 49 50 51 52 53 54 55
	}

	componentWillMount(){
		this.state = this._getStateFromProps(this.props);
	}

	componentWillReceiveProps(newProps){
		let newState = this._getStateFromProps(newProps);
		this.setState(newState);
	}

	_getStateFromProps(props){
xwenliang's avatar
xwenliang committed
56
		//the pickedValue must looks like [wheelone's, wheeltwo's, ...]
xwenliang's avatar
xwenliang committed
57 58 59 60 61 62 63 64 65 66 67
		//this.state.selectedValue may be the result of the first pickerWheel
		let pickerBtnText = props.pickerBtnText;
		let pickerBtnStyle = props.pickerBtnStyle;
		let pickerToolBarStyle = props.pickerToolBarStyle;
		let pickerItemStyle = props.pickerItemStyle;
		let pickerHeight = props.pickerHeight;
		let showDuration = props.showDuration;
		let pickerData = props.pickerData;
		let selectedValue = props.selectedValue;
		let onPickerDone = props.onPickerDone;

xwenliang's avatar
xwenliang committed
68 69 70
		let pickerStyle = pickerData.constructor === Array ? 'parallel' : 'cascade';
		let firstWheelData;
		let firstPickedData;
Zuxuan Liang's avatar
Debug  
Zuxuan Liang committed
71
		let secondPickedData;
xwenliang's avatar
xwenliang committed
72 73 74 75 76
		let secondWheelData;
		let secondPickedDataIndex;
		let thirdWheelData;
		let thirdPickedDataIndex;
		let cascadeData = {};
Jon's avatar
Jon committed
77 78
		let slideAnim = (this.state && this.state.slideAnim ? this.state.slideAnim : new Animated.Value(-props.pickerHeight));
		
xwenliang's avatar
xwenliang committed
79 80
		if(pickerStyle === 'parallel'){
			//compatible single wheel sence
xwenliang's avatar
xwenliang committed
81 82
			if(selectedValue.constructor !== Array){
				selectedValue = [selectedValue];
xwenliang's avatar
xwenliang committed
83 84 85 86 87 88 89 90
			}
			if(pickerData[0].constructor !== Array){
				pickerData = [pickerData];
			}
		}
		else if(pickerStyle === 'cascade'){
			//only support three stage
			firstWheelData = Object.keys(pickerData);
xwenliang's avatar
xwenliang committed
91 92 93
			firstPickedData = props.selectedValue[0];
			secondPickedData = props.selectedValue[1];
			cascadeData = this._getCascadeData(pickerData, selectedValue, firstPickedData, secondPickedData, true);
xwenliang's avatar
xwenliang committed
94
		}
xwenliang's avatar
xwenliang committed
95 96 97 98 99 100 101 102 103 104
		//保存了已经选择到的数据
		this.pickedValue = selectedValue;
		this.pickerStyle = pickerStyle;
		return {
			pickerBtnText,
			pickerBtnStyle,
			pickerToolBarStyle,
			pickerItemStyle,
			pickerHeight,
			showDuration,
xwenliang's avatar
xwenliang committed
105
			pickerData,
xwenliang's avatar
xwenliang committed
106 107
			selectedValue,
			onPickerDone,
xwenliang's avatar
xwenliang committed
108 109 110 111
			//list of first wheel data
			firstWheelData,
			//first wheel selected value
			firstPickedData,
Jon's avatar
Jon committed
112
			slideAnim,
xwenliang's avatar
xwenliang committed
113 114 115 116 117 118
			//list of second wheel data and pickedDataIndex
			secondWheelData: cascadeData.secondWheelData,
			secondPickedDataIndex: cascadeData.secondPickedDataIndex,
			//third wheel selected value and pickedDataIndex
			thirdWheelData: cascadeData.thirdWheelData,
			thirdPickedDataIndex: cascadeData.thirdPickedDataIndex
xwenliang's avatar
xwenliang committed
119
		};
xwenliang's avatar
bugs  
xwenliang committed
120 121 122 123 124 125 126 127
	}

	_slideUp(){
		this.isMoving = true;
		Animated.timing(
			this.state.slideAnim,
			{
				toValue: 0,
xwenliang's avatar
xwenliang committed
128
				duration: this.state.showDuration,
xwenliang's avatar
bugs  
xwenliang committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142
			}
		).start((evt) => {
			if(evt.finished) {
				this.isMoving = false;
				this.isPickerShow = true;
			}
		});
	}

	_slideDown(){
		this.isMoving = true;
		Animated.timing(
			this.state.slideAnim,
			{
xwenliang's avatar
xwenliang committed
143 144
				toValue: -this.state.pickerHeight,
				duration: this.state.showDuration,
xwenliang's avatar
bugs  
xwenliang committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
			}
		).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
169 170

	_prePressHandle(callback){
xwenliang's avatar
bugs  
xwenliang committed
171 172 173
		//通知子组件往上滚
		this.pickerWheel.moveUp();
	}
xwenliang's avatar
xwenliang committed
174 175

	_nextPressHandle(callback){
xwenliang's avatar
bugs  
xwenliang committed
176 177 178 179 180 181
		//通知子组件往下滚
		this.pickerWheel.moveDown();
	}

	_pickerFinish(){
		this._toggle();
xwenliang's avatar
xwenliang committed
182
		this.state.onPickerDone(this.pickedValue);
xwenliang's avatar
xwenliang committed
183 184 185 186 187 188
	}

	_renderParallelWheel(pickerData){
		let me = this;
		return pickerData.map((item, index) => {
			return (
xwenliang's avatar
xwenliang committed
189
				<View style={styles.pickerWheel} key={index}>
xwenliang's avatar
xwenliang committed
190 191 192 193 194 195 196 197 198 199 200 201
					<Picker
						selectedValue={me.state.selectedValue[index]}
						onValueChange={value => {
							me.pickedValue.splice(index, 1, value);
							me.setState({
								selectedValue: me.pickedValue
							});
						}} >
						{item.map((value, index) => (
							<PickerItem
								key={index}
								value={value}
xwenliang's avatar
xwenliang committed
202
								label={value.toString()}
xwenliang's avatar
xwenliang committed
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
							/>)
						)}
					</Picker>
				</View>
			);
		});
	}

	_getCascadeData(pickerData, pickedValue, firstPickedData, secondPickedData, onInit){
		let secondWheelData;
		let secondPickedDataIndex;
		let thirdWheelData;
		let thirdPickedDataIndex;
		//only support two and three stage
		for(let key in pickerData){
			//two stage
			if(pickerData[key].constructor === Array){
				secondWheelData = pickerData[firstPickedData];
				if(onInit){
					secondWheelData.forEach(function(v, k){
						if(v === pickedValue[1]){
							secondPickedDataIndex = k;
						}
					}.bind(this));
				}
				else{
					secondPickedDataIndex = 0;
				}
				break;
			}
			//three stage
			else{
				secondWheelData = Object.keys(pickerData[firstPickedData]);
				if(onInit){
					secondWheelData.forEach(function(v, k){
						if(v === pickedValue[1]){
							secondPickedDataIndex = k;
						}
					}.bind(this));
				}
				else{
					secondPickedDataIndex = 0;
				}
				thirdWheelData = pickerData[firstPickedData][secondPickedData];
				if(onInit){
					thirdWheelData.forEach(function(v, k){
						if(v === pickedValue[2]){
							thirdPickedDataIndex = k;
						}
					})
				}
				else{
					thirdPickedDataIndex = 0;
				}
				break;
			}
		}

		return {
			secondWheelData,
			secondPickedDataIndex,
			thirdWheelData,
			thirdPickedDataIndex
		}
	}

	_renderCascadeWheel(pickerData){
		let me = this;
		let thirdWheel = me.state.thirdWheelData && (
			<View style={styles.pickerWheel}>
				<Picker
					ref={'thirdWheel'}
					selectedValue={me.state.thirdPickedDataIndex}
					onValueChange={(index) => {
						//on ios platform 'this' refers to Picker?
						me.pickedValue.splice(2, 1, me.state.thirdWheelData[index]);
						me.setState({
							thirdPickedDataIndex: index
						});
					}} >
					{me.state.thirdWheelData.map((value, index) => (
						<PickerItem
							key={index}
							value={index}
xwenliang's avatar
xwenliang committed
287
							label={value.toString()}
xwenliang's avatar
xwenliang committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301
						/>)
					)}
				</Picker>
			</View>
		);

		return (
			<View style={styles.pickerWrap}>
				<View style={styles.pickerWheel}>
					<Picker
						ref={'firstWheel'}
						selectedValue={me.state.firstPickedData}
						onValueChange={value => {
							let secondWheelData = Object.keys(pickerData[value]);
Zuxuan Liang's avatar
Debug  
Zuxuan Liang committed
302
							let cascadeData = me._getCascadeData(pickerData, me.pickedValue, value, secondWheelData[0]);
xwenliang's avatar
xwenliang committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
							//when onPicked, this.pickedValue will pass to the parent
							//when firstWheel changed, second and third will also change
							if(cascadeData.thirdWheelData){
								me.pickedValue.splice(0, 3, value, cascadeData.secondWheelData[0], cascadeData.thirdWheelData[0]);
							}
							else{
								me.pickedValue.splice(0, 2, value, cascadeData.secondWheelData[0]);
							}
							
							me.setState({
								selectedValue: value,
								firstPickedData: value,
								secondWheelData: cascadeData.secondWheelData,
								secondPickedDataIndex: 0,
								thirdWheelData: cascadeData.thirdWheelData,
								thirdPickedDataIndex: 0
							});
							me.refs.secondWheel && me.refs.secondWheel.moveTo && me.refs.secondWheel.moveTo(0);
							me.refs.thirdWheel && me.refs.thirdWheel.moveTo && me.refs.thirdWheel.moveTo(0);
						}} >
						{me.state.firstWheelData.map((value, index) => (
							<PickerItem
								key={index}
								value={value}
xwenliang's avatar
xwenliang committed
327
								label={value.toString()}
xwenliang's avatar
xwenliang committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
							/>)
						)}
					</Picker>
				</View>
				<View style={styles.pickerWheel}>
					<Picker
						ref={'secondWheel'}
						selectedValue={me.state.secondPickedDataIndex}
						onValueChange={(index) => {
							let thirdWheelData = pickerData[me.state.firstPickedData][me.state.secondWheelData[index]];
							if(thirdWheelData){
								me.pickedValue.splice(1, 2, me.state.secondWheelData[index], thirdWheelData[0]);	
							}
							else{
								me.pickedValue.splice(1, 1, me.state.secondWheelData[index]);
							}
							
							me.setState({
								secondPickedDataIndex: index,
								thirdWheelData,
								thirdPickedDataIndex: 0
							});
							me.refs.thirdWheel && me.refs.thirdWheel.moveTo && me.refs.thirdWheel.moveTo(0);
						}} >
						{me.state.secondWheelData.map((value, index) => (
							<PickerItem
								key={index}
								value={index}
xwenliang's avatar
xwenliang committed
356
								label={value.toString()}
xwenliang's avatar
xwenliang committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
							/>)
						)}
					</Picker>
				</View>
				{thirdWheel}
			</View>
		);
	}

	_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;
		if(this.pickerStyle === 'parallel'){
			wheel = this._renderParallelWheel(pickerData);
		}
		else if(this.pickerStyle === 'cascade'){
			wheel = this._renderCascadeWheel(pickerData);
		}
		return wheel;
xwenliang's avatar
bugs  
xwenliang committed
407
	}
xwenliang's avatar
xwenliang committed
408 409
	
	render(){
xwenliang's avatar
xwenliang committed
410
		/*let pickerBtn = Platform.OS === 'ios' ? null : (
xwenliang's avatar
bugs  
xwenliang committed
411 412 413
			<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
414
			</View>
xwenliang's avatar
xwenliang committed
415 416
		);*/
		let pickerBtn = null;
xwenliang's avatar
bugs  
xwenliang committed
417 418
		return (
			<Animated.View style={[styles.picker, {
xwenliang's avatar
xwenliang committed
419
				height: this.state.pickerHeight,
xwenliang's avatar
bugs  
xwenliang committed
420 421
				bottom: this.state.slideAnim
			}]}>
xwenliang's avatar
xwenliang committed
422
				<View style={[styles.pickerToolbar, this.state.pickerToolBarStyle]}>
xwenliang's avatar
bugs  
xwenliang committed
423 424
					{pickerBtn}
					<View style={styles.pickerFinishBtn}>
xwenliang's avatar
xwenliang committed
425 426
						<Text style={[styles.pickerFinishBtnText, this.state.pickerBtnStyle]}
							onPress={this._pickerFinish.bind(this)}>{this.state.pickerBtnText}</Text>
xwenliang's avatar
bugs  
xwenliang committed
427 428
					</View>
				</View>
xwenliang's avatar
xwenliang committed
429 430 431
				<View style={styles.pickerWrap}>
					{this._renderWheel(this.state.pickerData)}
				</View>
xwenliang's avatar
bugs  
xwenliang committed
432 433 434
			</Animated.View>
		);
	}
xwenliang's avatar
xwenliang committed
435 436 437
};

let styles = StyleSheet.create({
xwenliang's avatar
bugs  
xwenliang committed
438
	picker: {
xwenliang's avatar
xwenliang committed
439
		width: width,
xwenliang's avatar
xwenliang committed
440 441 442
		position: 'absolute',
		bottom: 0,
		left: 0,
xwenliang's avatar
bugs  
xwenliang committed
443
		backgroundColor: '#bdc0c7',
xwenliang's avatar
xwenliang committed
444 445 446
		overflow: 'hidden'
	},
	pickerWrap: {
xwenliang's avatar
bugs  
xwenliang committed
447
		width: width,
xwenliang's avatar
xwenliang committed
448 449 450 451
		flexDirection: 'row'
	},
	pickerWheel: {
		flex: 1
xwenliang's avatar
xwenliang committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465
	},
	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
466
		alignItems: 'center'
xwenliang's avatar
xwenliang committed
467 468 469 470
	},
	pickerMoveBtn: {
		color: '#149be0',
		fontSize: 16,
xwenliang's avatar
bugs  
xwenliang committed
471
		marginLeft: 20
xwenliang's avatar
xwenliang committed
472 473 474 475 476 477
	},
	pickerFinishBtn: {
		flex: 1,
		flexDirection: 'row',
		justifyContent: 'flex-end',
		alignItems: 'center',
xwenliang's avatar
bugs  
xwenliang committed
478
		marginRight: 20
xwenliang's avatar
xwenliang committed
479 480 481 482 483 484
	},
	pickerFinishBtnText: {
		fontSize: 16,
		color: '#149be0'
	}
});