forked from lawnstarter/react-native-picker-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
173 lines (159 loc) · 5.24 KB
/
example.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React from 'react';
import { Alert, Text, TextInput, StyleSheet, View } from 'react-native';
import RNPickerSelect from 'react-native-picker-select';
export default class App extends React.Component {
constructor(props) {
super(props);
this.inputRefs = {};
this.state = {
favColor: undefined,
items: [
{
label: 'Red',
value: 'red',
},
{
label: 'Orange',
value: 'orange',
},
{
label: 'Blue',
value: 'blue',
},
],
favSport: undefined,
items2: [
{
label: 'Football',
value: 'football',
},
{
label: 'Baseball',
value: 'baseball',
},
{
label: 'Hockey',
value: 'hockey',
},
],
};
}
componentDidMount() {
// if the component is using the optional `value` prop, the parent
// has the abililty to both set the initial value and also update it
setTimeout(() => {
this.setState({
favColor: 'red',
});
}, 1000);
// parent can also update the `items` prop
setTimeout(() => {
this.setState({
items: this.state.items.concat([{ value: 'purple', label: 'Purple' }]),
});
}, 2000);
}
render() {
return (
<View style={styles.container}>
<Text>Name?</Text>
<TextInput
ref={(el) => {
this.inputRefs.name = el;
}}
returnKeyType="next"
enablesReturnKeyAutomatically
onSubmitEditing={() => {
this.inputRefs.picker.togglePicker();
}}
style={pickerSelectStyles.inputIOS}
blurOnSubmit={false}
/>
<View style={{ paddingVertical: 5 }} />
<Text>What’s your favorite color?</Text>
<RNPickerSelect
placeholder={{
label: 'Select a color...',
value: null,
}}
items={this.state.items}
onValueChange={(value) => {
this.setState({
favColor: value,
});
}}
onUpArrow={() => {
this.inputRefs.name.focus();
}}
onDownArrow={() => {
this.inputRefs.picker2.togglePicker();
}}
style={{ ...pickerSelectStyles }}
value={this.state.favColor}
ref={(el) => {
this.inputRefs.picker = el;
}}
/>
<View style={{ paddingVertical: 5 }} />
<Text>What’s your favorite sport?</Text>
<RNPickerSelect
placeholder={{
label: 'Select a sport...',
value: null,
}}
items={this.state.items2}
onValueChange={(value) => {
this.setState({
favSport: value,
});
}}
onUpArrow={() => {
this.inputRefs.picker.togglePicker();
}}
onDownArrow={() => {
this.inputRefs.company.focus();
}}
style={{ ...pickerSelectStyles }}
value={this.state.favSport}
ref={(el) => {
this.inputRefs.picker2 = el;
}}
/>
<View style={{ paddingVertical: 5 }} />
<Text>Company?</Text>
<TextInput
ref={(el) => {
this.inputRefs.company = el;
}}
returnKeyType="go"
enablesReturnKeyAutomatically
style={pickerSelectStyles.inputIOS}
onSubmitEditing={() => {
Alert.alert('Success', 'Form submitted', [{ text: 'Okay', onPress: null }]);
}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
paddingTop: 30,
backgroundColor: '#fff',
justifyContent: 'center',
paddingHorizontal: 10,
},
});
const pickerSelectStyles = StyleSheet.create({
inputIOS: {
fontSize: 16,
paddingTop: 13,
paddingHorizontal: 10,
paddingBottom: 12,
borderWidth: 1,
borderColor: 'gray',
borderRadius: 4,
backgroundColor: 'white',
color: 'black',
},
});