index.js 12.7 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
		pickerBtnText: PropTypes.string,
25
		pickerCancelBtnText: PropTypes.string,
xwenliang's avatar
xwenliang committed
26
		pickerBtnStyle: PropTypes.any,
27 28
		pickerTitle: PropTypes.string,
		pickerTitleStyle: PropTypes.any,
xwenliang's avatar
xwenliang committed
29 30
		pickerToolBarStyle: PropTypes.any,
		pickerItemStyle: PropTypes.any,
xwenliang's avatar
bugs  
xwenliang committed
31 32
		pickerHeight: PropTypes.number,
		showDuration: PropTypes.number,
xwenliang's avatar
xwenliang committed
33 34
		pickerData: PropTypes.any.isRequired,
		selectedValue: PropTypes.any.isRequired,
35 36
		onPickerDone: PropTypes.func,
		onPickerCancel: PropTypes.func
xwenliang's avatar
bugs  
xwenliang committed
37 38 39
	}

	static defaultProps = {
xwenliang's avatar
xwenliang committed
40
		pickerBtnText: '完成',
41
		pickerCancelBtnText: '取消',
xwenliang's avatar
xwenliang committed
42
		pickerHeight: 250,
xwenliang's avatar
bugs  
xwenliang committed
43
		showDuration: 300,
44 45
		onPickerDone: ()=>{},
		onPickerCancel: ()=>{}
xwenliang's avatar
bugs  
xwenliang committed
46 47
	}

xwenliang's avatar
xwenliang committed
48 49
	constructor(props, context){
		super(props, context);
xwenliang's avatar
xwenliang committed
50 51 52 53 54 55 56 57 58 59 60
	}

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

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

xwenliang's avatar
xwenliang committed
61 62 63
	shouldComponentUpdate(nextProps, nextState, context){
		if(JSON.stringify(nextState.selectedValue) === JSON.stringify(this.state.selectedValue)){
			return false;
xwenliang's avatar
xwenliang committed
64
		}
xwenliang's avatar
xwenliang committed
65 66
		return true;
	}
xwenliang's avatar
xwenliang committed
67

xwenliang's avatar
xwenliang committed
68
	_getStateFromProps(props){
xwenliang's avatar
xwenliang committed
69
		//the pickedValue must looks like [wheelone's, wheeltwo's, ...]
xwenliang's avatar
xwenliang committed
70 71
		//this.state.selectedValue may be the result of the first pickerWheel
		let pickerBtnText = props.pickerBtnText;
72
		let pickerCancelBtnText = props.pickerCancelBtnText;
xwenliang's avatar
xwenliang committed
73
		let pickerBtnStyle = props.pickerBtnStyle;
74 75
		let pickerTitle = props.pickerTitle;
		let pickerTitleStyle = props.pickerTitleStyle;
xwenliang's avatar
xwenliang committed
76 77 78 79 80 81 82
		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;
83
		let onPickerCancel = props.onPickerCancel;
xwenliang's avatar
xwenliang committed
84

xwenliang's avatar
xwenliang committed
85 86 87
		let pickerStyle = pickerData.constructor === Array ? 'parallel' : 'cascade';
		let firstWheelData;
		let firstPickedData;
Zuxuan Liang's avatar
Debug  
Zuxuan Liang committed
88
		let secondPickedData;
xwenliang's avatar
xwenliang committed
89 90 91 92 93
		let secondWheelData;
		let secondPickedDataIndex;
		let thirdWheelData;
		let thirdPickedDataIndex;
		let cascadeData = {};
Jon's avatar
Jon committed
94 95
		let slideAnim = (this.state && this.state.slideAnim ? this.state.slideAnim : new Animated.Value(-props.pickerHeight));
		
xwenliang's avatar
xwenliang committed
96 97
		if(pickerStyle === 'parallel'){
			//compatible single wheel sence
xwenliang's avatar
xwenliang committed
98 99
			if(selectedValue.constructor !== Array){
				selectedValue = [selectedValue];
xwenliang's avatar
xwenliang committed
100 101 102 103 104 105 106 107
			}
			if(pickerData[0].constructor !== Array){
				pickerData = [pickerData];
			}
		}
		else if(pickerStyle === 'cascade'){
			//only support three stage
			firstWheelData = Object.keys(pickerData);
xwenliang's avatar
xwenliang committed
108 109 110
			firstPickedData = props.selectedValue[0];
			secondPickedData = props.selectedValue[1];
			cascadeData = this._getCascadeData(pickerData, selectedValue, firstPickedData, secondPickedData, true);
xwenliang's avatar
xwenliang committed
111
		}
xwenliang's avatar
xwenliang committed
112
		//save picked data
xwenliang's avatar
xwenliang committed
113 114 115 116
		this.pickedValue = selectedValue;
		this.pickerStyle = pickerStyle;
		return {
			pickerBtnText,
117
			pickerCancelBtnText,
xwenliang's avatar
xwenliang committed
118
			pickerBtnStyle,
119 120
			pickerTitle,
			pickerTitleStyle,
xwenliang's avatar
xwenliang committed
121 122 123 124
			pickerToolBarStyle,
			pickerItemStyle,
			pickerHeight,
			showDuration,
xwenliang's avatar
xwenliang committed
125
			pickerData,
xwenliang's avatar
xwenliang committed
126 127
			selectedValue,
			onPickerDone,
128
			onPickerCancel,
xwenliang's avatar
xwenliang committed
129 130 131 132
			//list of first wheel data
			firstWheelData,
			//first wheel selected value
			firstPickedData,
Jon's avatar
Jon committed
133
			slideAnim,
xwenliang's avatar
xwenliang committed
134 135 136 137 138 139
			//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
140
		};
xwenliang's avatar
bugs  
xwenliang committed
141 142 143 144 145 146 147 148
	}

	_slideUp(){
		this.isMoving = true;
		Animated.timing(
			this.state.slideAnim,
			{
				toValue: 0,
xwenliang's avatar
xwenliang committed
149
				duration: this.state.showDuration,
xwenliang's avatar
bugs  
xwenliang committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163
			}
		).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
164 165
				toValue: -this.state.pickerHeight,
				duration: this.state.showDuration,
xwenliang's avatar
bugs  
xwenliang committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
			}
		).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
190 191

	_prePressHandle(callback){
xwenliang's avatar
bugs  
xwenliang committed
192 193 194
		//通知子组件往上滚
		this.pickerWheel.moveUp();
	}
xwenliang's avatar
xwenliang committed
195 196

	_nextPressHandle(callback){
xwenliang's avatar
bugs  
xwenliang committed
197 198 199 200
		//通知子组件往下滚
		this.pickerWheel.moveDown();
	}

201 202 203 204 205
	_pickerCancel() {
		this._toggle();
		this.state.onPickerCancel();
	}

xwenliang's avatar
bugs  
xwenliang committed
206 207
	_pickerFinish(){
		this._toggle();
xwenliang's avatar
xwenliang committed
208
		this.state.onPickerDone(this.pickedValue);
xwenliang's avatar
xwenliang committed
209 210 211 212 213 214
	}

	_renderParallelWheel(pickerData){
		let me = this;
		return pickerData.map((item, index) => {
			return (
xwenliang's avatar
xwenliang committed
215
				<View style={styles.pickerWheel} key={index}>
xwenliang's avatar
xwenliang committed
216 217 218 219 220 221 222 223 224 225 226 227
					<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
228
								label={value.toString()}
xwenliang's avatar
xwenliang committed
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
							/>)
						)}
					</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
313
							label={value.toString()}
xwenliang's avatar
xwenliang committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327
						/>)
					)}
				</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
328
							let cascadeData = me._getCascadeData(pickerData, me.pickedValue, value, secondWheelData[0]);
xwenliang's avatar
xwenliang committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
							//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
353
								label={value.toString()}
xwenliang's avatar
xwenliang committed
354 355 356 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
							/>)
						)}
					</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
382
								label={value.toString()}
xwenliang's avatar
xwenliang committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
							/>)
						)}
					</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
433
	}
xwenliang's avatar
xwenliang committed
434 435
	
	render(){
xwenliang's avatar
xwenliang committed
436
		/*let pickerBtn = Platform.OS === 'ios' ? null : (
xwenliang's avatar
bugs  
xwenliang committed
437 438 439
			<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
440
			</View>
xwenliang's avatar
xwenliang committed
441
		);*/
442
		// let pickerBtn = null;
xwenliang's avatar
bugs  
xwenliang committed
443 444
		return (
			<Animated.View style={[styles.picker, {
xwenliang's avatar
xwenliang committed
445
				height: this.state.pickerHeight,
xwenliang's avatar
bugs  
xwenliang committed
446 447
				bottom: this.state.slideAnim
			}]}>
xwenliang's avatar
xwenliang committed
448
				<View style={[styles.pickerToolbar, this.state.pickerToolBarStyle]}>
449 450
					<View style={styles.pickerCancelBtn}>
						<Text style={[styles.pickerFinishBtnText, this.state.pickerBtnStyle]}
451
							onPress={this._pickerCancel.bind(this)}>{this.state.pickerCancelBtnText}</Text>
452 453 454 455
					</View>
					<Text style={[styles.pickerTitle, this.state.pickerTitleStyle]} numberOfLines={1}>
						{this.state.pickerTitle}
					</Text>
xwenliang's avatar
bugs  
xwenliang committed
456
					<View style={styles.pickerFinishBtn}>
xwenliang's avatar
xwenliang committed
457 458
						<Text style={[styles.pickerFinishBtnText, this.state.pickerBtnStyle]}
							onPress={this._pickerFinish.bind(this)}>{this.state.pickerBtnText}</Text>
xwenliang's avatar
bugs  
xwenliang committed
459 460
					</View>
				</View>
xwenliang's avatar
xwenliang committed
461 462 463
				<View style={styles.pickerWrap}>
					{this._renderWheel(this.state.pickerData)}
				</View>
xwenliang's avatar
bugs  
xwenliang committed
464 465 466
			</Animated.View>
		);
	}
xwenliang's avatar
xwenliang committed
467 468 469
};

let styles = StyleSheet.create({
xwenliang's avatar
bugs  
xwenliang committed
470
	picker: {
xwenliang's avatar
xwenliang committed
471
		width: width,
xwenliang's avatar
xwenliang committed
472 473 474
		position: 'absolute',
		bottom: 0,
		left: 0,
xwenliang's avatar
bugs  
xwenliang committed
475
		backgroundColor: '#bdc0c7',
xwenliang's avatar
xwenliang committed
476 477 478
		overflow: 'hidden'
	},
	pickerWrap: {
xwenliang's avatar
bugs  
xwenliang committed
479
		width: width,
xwenliang's avatar
xwenliang committed
480 481 482 483
		flexDirection: 'row'
	},
	pickerWheel: {
		flex: 1
xwenliang's avatar
xwenliang committed
484 485 486 487 488 489 490 491
	},
	pickerToolbar: {
		height: 30,
		width: width,
		backgroundColor: '#e6e6e6',
		flexDirection: 'row',
		borderTopWidth: 1,
		borderBottomWidth: 1,
xwenliang's avatar
xwenliang committed
492 493
		borderColor: '#c3c3c3',
		alignItems: 'center'
xwenliang's avatar
xwenliang committed
494 495 496 497 498
	},
	pickerBtnView: {
		flex: 1,
		flexDirection: 'row',
		justifyContent: 'flex-start',
xwenliang's avatar
bugs  
xwenliang committed
499
		alignItems: 'center'
xwenliang's avatar
xwenliang committed
500 501 502 503
	},
	pickerMoveBtn: {
		color: '#149be0',
		fontSize: 16,
xwenliang's avatar
bugs  
xwenliang committed
504
		marginLeft: 20
xwenliang's avatar
xwenliang committed
505
	},
506 507 508 509 510 511 512 513
	pickerCancelBtn: {
		flex: 1,
		flexDirection: 'row',
		justifyContent: 'flex-start',
		alignItems: 'center',
		marginLeft: 20
	},
	pickerTitle: {
xwenliang's avatar
xwenliang committed
514
		flex: 4,
515 516 517
		color: 'black',
		textAlign: 'center'
	},
xwenliang's avatar
xwenliang committed
518 519 520 521 522
	pickerFinishBtn: {
		flex: 1,
		flexDirection: 'row',
		justifyContent: 'flex-end',
		alignItems: 'center',
xwenliang's avatar
bugs  
xwenliang committed
523
		marginRight: 20
xwenliang's avatar
xwenliang committed
524 525 526 527 528 529
	},
	pickerFinishBtnText: {
		fontSize: 16,
		color: '#149be0'
	}
});