-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
OptionsSelector.js
207 lines (171 loc) · 6.81 KB
/
OptionsSelector.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
import _ from 'underscore';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import TextInputWithFocusStyles from './TextInputWithFocusStyles';
import OptionsList from './OptionsList';
import styles from '../styles/styles';
import themeColors from '../styles/themes/default';
import optionPropTypes from './optionPropTypes';
const propTypes = {
// Callback to fire when a row is tapped
onSelectRow: PropTypes.func,
// Sections for the section list
sections: PropTypes.arrayOf(PropTypes.shape({
// Title of the section
title: PropTypes.string,
// The initial index of this section given the total number of options in each section's data array
indexOffset: PropTypes.number,
// Array of options
data: PropTypes.arrayOf(optionPropTypes),
// Whether this section should show or not
shouldShow: PropTypes.bool,
})).isRequired,
// Value in the search input field
value: PropTypes.string.isRequired,
// Callback fired when text changes
onChangeText: PropTypes.func.isRequired,
// Optional placeholder text for the selector
placeholderText: PropTypes.string,
// Options that have already been selected
selectedOptions: PropTypes.arrayOf(optionPropTypes),
// Optional header message
headerMessage: PropTypes.string,
// Whether we can select multiple options
canSelectMultipleOptions: PropTypes.bool,
// Whether any section headers should be visible
hideSectionHeaders: PropTypes.bool,
// Whether to allow arrow key actions on the list
disableArrowKeysActions: PropTypes.bool,
// A flag to indicate whether to show additional optional states, such as pin and draft icons
hideAdditionalOptionStates: PropTypes.bool,
// Force the text style to be the unread style on all rows
forceTextUnreadStyle: PropTypes.bool,
// Whether to show the title tooltip
showTitleTooltip: PropTypes.bool,
};
const defaultProps = {
onSelectRow: () => {},
placeholderText: 'Name, email, or phone number',
selectedOptions: [],
headerMessage: '',
canSelectMultipleOptions: false,
hideSectionHeaders: false,
disableArrowKeysActions: false,
hideAdditionalOptionStates: false,
forceTextUnreadStyle: false,
showTitleTooltip: false,
};
class OptionsSelector extends Component {
constructor(props) {
super(props);
this.handleKeyPress = this.handleKeyPress.bind(this);
this.viewableItems = [];
this.state = {
focusedIndex: 0,
};
}
componentDidMount() {
this.textInput.focus();
}
/**
* Scrolls to the focused index within the SectionList
*
* @param {Number} sectionIndex
* @param {Number} itemIndex
*/
scrollToFocusedIndex(sectionIndex, itemIndex) {
this.list.scrollToLocation({sectionIndex, itemIndex});
}
/**
* Delegate key presses to specific callbacks
*
* @param {SyntheticEvent} e
*/
handleKeyPress(e) {
if (this.props.disableArrowKeysActions) {
return;
}
// We are mapping over all the options to combine them into a single array and also saving the section index
// index within that section so we can navigate
const allOptions = _.reduce(this.props.sections, (options, section, sectionIndex) => (
[...options, ..._.map(section.data, (option, index) => ({
...option,
index,
sectionIndex,
}))]
), []);
switch (e.nativeEvent.key) {
case 'Enter': {
this.props.onSelectRow(allOptions[this.state.focusedIndex]);
e.preventDefault();
break;
}
case 'ArrowDown': {
this.setState((prevState) => {
let newFocusedIndex = prevState.focusedIndex + 1;
// Wrap around to the top of the list
if (newFocusedIndex > allOptions.length - 1) {
newFocusedIndex = 0;
}
const {index, sectionIndex} = allOptions[newFocusedIndex];
this.scrollToFocusedIndex(sectionIndex, index);
return {focusedIndex: newFocusedIndex};
});
e.preventDefault();
break;
}
case 'ArrowUp': {
this.setState((prevState) => {
let newFocusedIndex = prevState.focusedIndex - 1;
// Wrap around to the bottom of the list
if (newFocusedIndex < 0) {
newFocusedIndex = allOptions.length - 1;
}
const {index, sectionIndex} = allOptions[newFocusedIndex];
this.scrollToFocusedIndex(sectionIndex, index);
return {focusedIndex: newFocusedIndex};
});
e.preventDefault();
break;
}
default:
}
}
render() {
return (
<View style={[styles.flex1]}>
<View style={[styles.ph5, styles.pv3]}>
<TextInputWithFocusStyles
styleFocusIn={[styles.textInputReversedFocus]}
ref={el => this.textInput = el}
style={[styles.textInput]}
value={this.props.value}
onChangeText={this.props.onChangeText}
onKeyPress={this.handleKeyPress}
placeholder={this.props.placeholderText}
placeholderTextColor={themeColors.placeholderText}
/>
</View>
<OptionsList
ref={el => this.list = el}
optionHoveredStyle={styles.hoveredComponentBG}
onSelectRow={this.props.onSelectRow}
sections={this.props.sections}
focusedIndex={this.state.focusedIndex}
selectedOptions={this.props.selectedOptions}
canSelectMultipleOptions={this.props.canSelectMultipleOptions}
hideSectionHeaders={this.props.hideSectionHeaders}
headerMessage={this.props.headerMessage}
disableFocusOptions={this.props.disableArrowKeysActions}
hideAdditionalOptionStates={this.props.hideAdditionalOptionStates}
forceTextUnreadStyle={this.props.forceTextUnreadStyle}
showTitleTooltip={this.props.showTitleTooltip}
/>
</View>
);
}
}
OptionsSelector.defaultProps = defaultProps;
OptionsSelector.propTypes = propTypes;
export default OptionsSelector;