Repository navigation
Expand file tree
/
Copy pathOptions.js
More file actions
312 lines (274 loc) · 9.12 KB
/
Copy pathOptions.js
File metadata and controls
312 lines (274 loc) · 9.12 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// @flow
import React, { Component } from 'react';
import type {
ComponentType,
// $FlowFixMe
SyntheticKeyboardEvent,
// $FlowFixMe
SyntheticMouseEvent,
// $FlowFixMe
SyntheticEvent,
Element,
Ref
} from 'react';
// internal components
import { withTheme } from './HOC/withTheme';
// internal utility functions
import { composeTheme, addThemeId, didThemePropsChange } from '../utils/themes';
import { composeFunctions } from '../utils/props';
// import constants
import { IDENTIFIERS } from '../themes/API';
type Props = {
context: {
theme: Object,
ROOT_THEME_API: Object
},
isOpen: boolean,
isOpeningUpward: boolean,
noResults: boolean,
noResultsMessage: string | Element<any>,
onBlur: Function,
onChange: Function,
onClose: Function,
options: Array<any>,
optionRenderer: Function,
optionsRef: Ref<any>,
render: Function,
resetOnClose: boolean,
selectedOption: any,
skin: ComponentType<any>,
selectedOptions: Array<any>,
targetRef: Ref<*>,
theme: Object, // if passed by user, it will take precedence over this.props.context.theme
themeId: string,
themeOverrides: Object,
toggleOpen: Function
};
type State = {
composedTheme: Object,
highlightedOptionIndex: number
};
class OptionsBase extends Component<Props, State> {
optionsElement: ?Element<any>;
static defaultProps = {
isOpen: false,
isOpeningUpward: false,
noResultsMessage: 'No results',
resetOnClose: false,
theme: null,
themeId: IDENTIFIERS.OPTIONS,
themeOverrides: {},
toggleOpen() {}
};
constructor(props: Props) {
super(props);
const { context, themeId, theme, themeOverrides } = props;
this.state = {
composedTheme: composeTheme(
addThemeId(theme || context.theme, themeId),
addThemeId(themeOverrides, themeId),
context.ROOT_THEME_API
),
highlightedOptionIndex: 0
};
}
componentDidMount() {
if (this.props.isOpen) {
document.addEventListener('keydown', this._handleKeyDown, false);
}
}
componentWillReceiveProps(nextProps: Props) {
if (!this.props.isOpen && nextProps.isOpen) {
document.addEventListener('keydown', this._handleKeyDown, false);
} else if (this.props.isOpen && !nextProps.isOpen) {
document.removeEventListener('keydown', this._handleKeyDown, false);
}
didThemePropsChange(this.props, nextProps, this.setState.bind(this));
}
componentWillUnmount() {
document.removeEventListener('keydown', this._handleKeyDown, false);
}
close = () => {
const { onClose, resetOnClose, toggleOpen, isOpen } = this.props;
if (isOpen) { toggleOpen(); }
this.setState({
highlightedOptionIndex: resetOnClose
? 0
: this.state.highlightedOptionIndex
});
if (onClose) { onClose(); }
};
getHighlightedOptionIndex = () => {
// If nothing is higlighted, highlight selected option
// In case nothing is selected, highlight first option
const { options, isOpeningUpward } = this.props;
const currentIndex = this.state.highlightedOptionIndex;
let index = 0;
if (currentIndex !== null) {
index = currentIndex;
}
if (isOpeningUpward) return options.length - 1 - index;
return index;
};
setHighlightedOptionIndex = (optionIndex: number) => {
if (
!this.isHighlightedOption(optionIndex) &&
this.isDisabledOption(optionIndex)
) {
this.setState({ highlightedOptionIndex: optionIndex });
}
};
isSelectedOption = (optionIndex: number) => {
const { options, isOpeningUpward } = this.props;
const index = isOpeningUpward ? options.length - 1 - optionIndex : optionIndex;
const option = options[index];
return option && this.props.selectedOption === option;
};
isHighlightedOption = (optionIndex: number) => this.state.highlightedOptionIndex === optionIndex;
isDisabledOption = (optionIndex: number) => {
const { options } = this.props;
const option = options[optionIndex];
return option && !option.isDisabled;
};
handleClickOnOption = (option: ?Object, event: SyntheticEvent<>) => {
if (option) {
if (option.isDisabled) return;
if (this.props.onChange) this.props.onChange(option, event);
}
if (this.props.onBlur) this.props.onBlur(event);
this.close();
};
// returns an object containing props, theme, and method handlers
// associated with rendering this.props.options, the user can call
// this in the body of the renderOptions function
getOptionProps = ({
onClick,
onMouseEnter,
...rest
}: { onClick: Function, onMouseEnter: Function } = {}) => {
const { isOpen, themeId, options, selectedOptions } = this.props;
const { composedTheme } = this.state;
const {
isHighlightedOption,
isDisabledOption,
handleClickOnOption,
setHighlightedOptionIndex
} = this;
return {
options,
selectedOptions,
isOpen,
isHighlightedOption,
isDisabledOption,
theme: composedTheme[themeId],
onClick: (option: ?Object, event: SyntheticEvent<>) =>
// the user's custom onClick event handler is composed with
// the internal functionality of Options (this.handleClickOnOption)
composeFunctions(onClick, handleClickOnOption)(option, event),
onMouseEnter: (index: number, event: SyntheticMouseEvent<>) =>
// user's custom onMouseEnter is composed with this.setHighlightedOptionIndex
composeFunctions(onMouseEnter, setHighlightedOptionIndex)(index, event),
...rest
};
};
// ========= PRIVATE HELPERS =========
_handleSelectionOnKeyDown = (event: SyntheticKeyboardEvent<>) => {
const { options } = this.props;
if (options.length) {
const { isOpeningUpward } = this.props;
const currentIndex = this.state.highlightedOptionIndex;
const reverseIndex = options.length - 1 - currentIndex;
const highlightedOption =
options[isOpeningUpward ? reverseIndex : currentIndex];
this.handleClickOnOption(highlightedOption, event);
} else {
event.preventDefault();
}
};
_handleHighlightMove = (currentIndex: number, direction: string) => {
const { options } = this.props;
if (options.length) {
const lowerIndexBound = 0;
const upperIndexBound = options.length - 1;
let newIndex = direction === 'up' ? currentIndex - 1 : currentIndex + 1;
// Make sure new index is within options bounds
newIndex = Math.max(lowerIndexBound, Math.min(newIndex, upperIndexBound));
if (options[newIndex].isDisabled) {
// Try to jump over disabled options
const canMoveUp = newIndex > lowerIndexBound;
const canMoveDown = newIndex < upperIndexBound;
if (
(direction === 'up' && canMoveUp) ||
(direction === 'down' && canMoveDown)
) {
this._handleHighlightMove(newIndex, direction);
}
} else {
this.setHighlightedOptionIndex(newIndex);
}
}
};
// this needs to get passed to OptionsSkin and attached to each Option Li
_handleKeyDown = (event: SyntheticKeyboardEvent<>) => {
const highlightOptionIndex = this.state.highlightedOptionIndex;
switch (event.keyCode) {
case 9: // Tab key: selects currently highlighted option
event.preventDefault();
this._handleSelectionOnKeyDown(event);
break;
case 13: // Enter key: selects currently highlighted option
event.preventDefault();
this._handleSelectionOnKeyDown(event);
break;
case 32: // Space key: selects currently highlighted option
event.preventDefault();
this._handleSelectionOnKeyDown(event);
break;
case 27: // Escape key: closes options if open
this.close();
break;
case 38: // Up Arrow key: moves highlighted selection 'up' 1 index
event.preventDefault(); // prevent caret move
this._handleHighlightMove(highlightOptionIndex, 'up');
break;
case 40: // Down Arrow key: moves highlighted selection 'down' 1 index
event.preventDefault(); // prevent caret move
this._handleHighlightMove(highlightOptionIndex, 'down');
break;
default:
this.props.resetOnClose && this.setHighlightedOptionIndex(0);
}
};
render() {
// destructuring props ensures only the "...rest" get passed down
const {
skin: OptionsSkin,
targetRef,
theme,
themeOverrides,
onChange,
context,
optionsRef,
isOpen,
...rest
} = this.props;
const { composedTheme, highlightedOptionIndex } = this.state;
return (
<OptionsSkin
getHighlightedOptionIndex={this.getHighlightedOptionIndex}
getOptionProps={this.getOptionProps}
handleClickOnOption={this.handleClickOnOption}
highlightedOptionIndex={highlightedOptionIndex}
isHighlightedOption={this.isHighlightedOption}
isOpen={isOpen}
isSelectedOption={this.isSelectedOption}
optionsRef={optionsRef}
setHighlightedOptionIndex={this.setHighlightedOptionIndex}
targetRef={targetRef}
theme={composedTheme}
{...rest}
/>
);
}
}
export const Options = withTheme(OptionsBase);