forked from surajitsarkar19/react-native-picker-view
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAndroidNumberPicker.js
More file actions
73 lines (59 loc) · 1.87 KB
/
Copy pathAndroidNumberPicker.js
File metadata and controls
73 lines (59 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
import React, {Component} from 'react';
import {requireNativeComponent, View} from 'react-native';
import PropTypes from 'prop-types';
export default class AndroidNumberPicker extends Component {
static defaultProps = {
selectedIndex: 0,
height: 200,
enableInput: true
};
static propTypes = {
...View.propTypes,
height: PropTypes.number,
selectedIndex: PropTypes.number,
values: PropTypes.arrayOf(PropTypes.string).isRequired,
onSelect: PropTypes.func,
enableInput: PropTypes.bool
};
constructor(props) {
super(props);
this.state = this._stateFromProps(props);
this._onChange = this._onChange.bind(this);
this.picker = null;
}
componentWillReceiveProps(props) {
//this.props = props;
this.setState(this._stateFromProps(props))
}
_stateFromProps(props) {
return {
selectedIndex: props.selectedIndex,
values: props.values
};
}
_onChange(event) {
if (this.props.onSelect)
this.props.onSelect(event.nativeEvent.value);
if (this.picker && this.state.selectedIndex !== event.nativeEvent.value)
this.picker.setNativeProps({selected: this.state.selectedIndex});
}
render() {
let {style, ...otherProps} = this.props;
return (
<NativeNumberPicker
ref={(ref)=>{this.picker = ref}}
selected={this.state.selectedIndex}
values={this.state.values}
onChange={this._onChange}
style={[{height: this.props.height}, style && style]}
{...otherProps}
/>
);
}
}
let NativeNumberPicker = requireNativeComponent('SRSPickerView', AndroidNumberPicker, {
nativeOnly: {
onChange: true,
selected: true,
}
});