-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.js
211 lines (181 loc) · 7.55 KB
/
index.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import _ from 'underscore';
import React, {PureComponent} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import RNPickerSelect from 'react-native-picker-select';
import Icon from '../Icon';
import * as Expensicons from '../Icon/Expensicons';
import FormHelpMessage from '../FormHelpMessage';
import Text from '../Text';
import styles from '../../styles/styles';
import themeColors from '../../styles/themes/default';
import pickerStyles from './pickerStyles';
import {ScrollContext} from '../ScrollViewWithContext';
const propTypes = {
/** Picker label */
label: PropTypes.string,
/** Should the picker appear disabled? */
isDisabled: PropTypes.bool,
/** Input value */
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/** The items to display in the list of selections */
items: PropTypes.arrayOf(PropTypes.shape({
/** The value of the item that is being selected */
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
/** The text to display for the item */
label: PropTypes.string.isRequired,
})).isRequired,
/** Something to show as the placeholder before something is selected */
placeholder: PropTypes.shape({
/** The value of the placeholder item, usually an empty string */
value: PropTypes.string,
/** The text to be displayed as the placeholder */
label: PropTypes.string,
}),
/** Error text to display */
errorText: PropTypes.string,
/** Customize the Picker container */
// eslint-disable-next-line react/forbid-prop-types
containerStyles: PropTypes.arrayOf(PropTypes.object),
/** The ID used to uniquely identify the input in a Form */
inputID: PropTypes.string,
/** Saves a draft of the input value when used in a form */
// eslint-disable-next-line react/no-unused-prop-types
shouldSaveDraft: PropTypes.bool,
/** A callback method that is called when the value changes and it receives the selected value as an argument */
onInputChange: PropTypes.func.isRequired,
/** Size of a picker component */
size: PropTypes.oneOf(['normal', 'small']),
/** An icon to display with the picker */
icon: PropTypes.func,
/** Callback called when click or tap out of Picker */
onBlur: PropTypes.func,
/** Ref to be forwarded to RNPickerSelect component, provided by forwardRef, not parent component. */
innerRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.element,
}),
]),
};
const defaultProps = {
label: '',
isDisabled: false,
errorText: '',
containerStyles: [],
inputID: undefined,
shouldSaveDraft: false,
value: undefined,
placeholder: {},
size: 'normal',
icon: size => (
<Icon
src={Expensicons.DownArrow}
// eslint-disable-next-line react/jsx-props-no-spreading
{...(size === 'small' ? {width: styles.pickerSmall.icon.width, height: styles.pickerSmall.icon.height} : {})}
/>
),
onBlur: () => {},
innerRef: () => {},
};
class Picker extends PureComponent {
constructor(props) {
super(props);
this.state = {
isOpen: false,
};
this.onInputChange = this.onInputChange.bind(this);
// Windows will reuse the text color of the select for each one of the options
// so we might need to color accordingly so it doesn't blend with the background.
this.placeholder = _.isEmpty(this.props.placeholder) ? {} : {
...this.props.placeholder,
color: themeColors.pickerOptionsTextColor,
};
}
componentDidMount() {
this.setDefaultValue();
}
componentDidUpdate(prevProps) {
if (prevProps.items === this.props.items) {
return;
}
this.setDefaultValue();
}
/**
* Forms use inputID to set values. But Picker passes an index as the second parameter to onInputChange
* We are overriding this behavior to make Picker work with Form
* @param {String} value
* @param {Number} index
*/
onInputChange(value, index) {
if (this.props.inputID) {
this.props.onInputChange(value);
return;
}
this.props.onInputChange(value, index);
}
setDefaultValue() {
// When there is only 1 element in the selector, we do the user a favor and automatically select it for them
// so they don't have to spend extra time selecting the only possible value.
if (this.props.value || !this.props.items || this.props.items.length !== 1 || !this.props.onInputChange) {
return;
}
this.props.onInputChange(this.props.items[0].value, 0);
}
render() {
const hasError = !_.isEmpty(this.props.errorText);
return (
<>
<View
style={[
styles.pickerContainer,
this.props.isDisabled && styles.inputDisabled,
...this.props.containerStyles,
this.state.isOpen && styles.borderColorFocus,
hasError && styles.borderColorDanger,
]}
>
{this.props.label && (
<Text style={[styles.pickerLabel, styles.textLabelSupporting]}>{this.props.label}</Text>
)}
<RNPickerSelect
onValueChange={this.onInputChange}
// We add a text color to prevent white text on white background dropdown items on Windows
items={_.map(this.props.items, item => ({...item, color: themeColors.pickerOptionsTextColor}))}
style={this.props.size === 'normal' ? pickerStyles(this.props.isDisabled) : styles.pickerSmall}
useNativeAndroidPickerStyle={false}
placeholder={this.placeholder}
value={this.props.value}
Icon={() => this.props.icon(this.props.size)}
disabled={this.props.isDisabled}
fixAndroidTouchableBug
onOpen={() => this.setState({isOpen: true})}
onClose={() => this.setState({isOpen: false})}
textInputProps={{allowFontScaling: false}}
pickerProps={{
onFocus: () => this.setState({isOpen: true}),
onBlur: () => {
this.setState({isOpen: false});
this.props.onBlur();
},
}}
ref={(el) => {
if (!_.isFunction(this.props.innerRef)) {
return;
}
this.props.innerRef(el);
}}
scrollViewRef={this.context && this.context.scrollViewRef}
scrollViewContentOffsetY={this.context && this.context.contentOffsetY}
/>
</View>
<FormHelpMessage message={this.props.errorText} />
</>
);
}
}
Picker.propTypes = propTypes;
Picker.defaultProps = defaultProps;
Picker.contextType = ScrollContext;
// eslint-disable-next-line react/jsx-props-no-spreading
export default React.forwardRef((props, ref) => <Picker {...props} innerRef={ref} key={props.inputID} />);