This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
forked from lucasferreira/react-native-simpledialog-android
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Picker.js
157 lines (135 loc) · 3.46 KB
/
Picker.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
import React, {
Component
} from 'react';
import PropTypes from 'prop-types';
import {
NativeModules,
View,
Text,
TouchableNativeFeedback,
Image,
} from 'react-native';
import styles from './styles';
import dropDownImageWhite from './drop-down-arrow-white.png';
import dropDownImageBlack from './drop-down-arrow-black.png';
const THEMES = {
LIGHT: 0,
DARK: 1,
};
class Picker extends Component {
constructor(props) {
super(props);
this.state = {};
}
onPressPicker(values, labels) {
NativeModules.FixedAndroidPicker.showPickerDialog(labels).then((index) => {
this.setState({
selectedValue: values[index],
});
this.props.onValueChange(values[index], index);
}).catch((error) => {
//dialog closed
});
}
render() {
const items = this.props.items;
const labels = this.getLabels(items);
const values = this.getValues(items);
const renderDropDown = this.props.renderDropDown ?
this.props.renderDropDown : this.renderDropDown;
return (
<View
style={this.getTheme() == THEMES.LIGHT ? styles.backgroundWhite : styles.backgroundBlack}>
<TouchableNativeFeedback
underlayColor={this.getTheme() == THEMES.LIGHT ? '#FFFFFF' : '#000000'}
style={styles.padding5}
onPress={() => {
this.onPressPicker(values, labels);
}}>
{
renderDropDown(labels, values)
}
</TouchableNativeFeedback>
</View>
);
}
getTheme() {
return this.props.theme;
}
renderDropDown(labels, values) {
return (
<View
style={[
styles.flexDirectionRow,
styles.alignItemsCenter,
]}>
<Text
style={[
this.props.theme == THEMES.LIGHT ? styles.fontBlack : styles.fontWhite,
this.props.styles.label,
]}>
{
labels[values.indexOf(this.state.selectedValue ? this.state.selectedValue : this.props.selectedValue)]
}
</Text>
<Image
source={this.getDropDownSource()}
style={[
styles.dropDownImage,
styles.marginLeft5,
this.props.styles.icon,
]}
/>
</View>
);
}
getDropDownSource() {
let dropDownImageSource;
if (this.props.dropDownImageSource) {
dropDownImageSource = this.props.dropDownImageSource;
} else if (this.getTheme() == THEMES.LIGHT) {
dropDownImageSource = dropDownImageBlack;
} else {
dropDownImageSource = dropDownImageWhite;
}
return dropDownImageSource;
}
getLabels(items) {
return items.map((item) => {
return item.label;
});
}
getValues(items) {
return items.map((item) => {
return item.value;
});
}
}
Picker.propTypes = {
selectedValue: PropTypes.string.isRequired,
items: PropTypes.arrayOf(PropTypes.shape({
label: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
})).isRequired,
onValueChange: PropTypes.func.isRequired,
dropDownImageSource: PropTypes.oneOfType([
PropTypes.shape({
uri: PropTypes.string.isRequired,
}).isRequired,
PropTypes.number.isRequired,
]),
theme: PropTypes.number,
styles: PropTypes.shape({
icon: PropTypes.number,
label: PropTypes.number,
}),
renderDropDown: PropTypes.func,
};
Picker.defaultProps = {
theme: THEMES.LIGHT,
styles: {},
};
export {
THEMES as Themes,
};
export default Picker;