-
Notifications
You must be signed in to change notification settings - Fork 91
/
SelectionContainer.js
299 lines (257 loc) · 8.87 KB
/
SelectionContainer.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
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
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import PropTypes from 'prop-types';
import scrollTo from 'dom-helpers/util/scrollTo';
import { isRtl, withTheme, getDocument } from '@zendeskgarden/react-theming';
import ControlledComponent from '../utils/ControlledComponent';
import composeEventHandlers from '../utils/composeEventHandlers';
import IdManager from '../utils/IdManager';
import KEY_CODES from '../constants/KEY_CODES';
import SingleSelectionModel from '../utils/SingleSelectionModel';
const KEYBOARD_DIRECTION = {
HORIZONTAL: 'horizontal',
VERTICAL: 'vertical',
BOTH: 'both'
};
export class SelectionContainer extends ControlledComponent {
static propTypes = {
/**
* @param {Object} renderProps
* @param {Function} renderProps.getContainerProps - Props to be spread onto container element
* @param {Function} renderProps.getItemProps - Props to be spread onto each selectable element. `({item})` is required. Use `index` attribute for custom ordering.
* @param {Any} renderProps.focusedKey - Unique key of currently focused item
* @param {Any} renderProps.selectedKey - Unique key of currently selected item
* @param {Function} renderProps.focusSelectionModel - The SingleSelectionModel that controls the focus state
*/
children: PropTypes.func,
/**
* Whether the up/down or left/right arrow keys should be used for keyboard navigation
*/
direction: PropTypes.oneOf([
KEYBOARD_DIRECTION.HORIZONTAL,
KEYBOARD_DIRECTION.VERTICAL,
KEYBOARD_DIRECTION.BOTH
]),
/**
* Default item to assign as focused if container is focused (-1 is equivalent to last item)
*/
defaultFocusedIndex: PropTypes.number,
/**
* Unique key of currently focused item
*/
focusedKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Unique key of currently selected item
*/
selectedKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Callback for all state objects. Used when in 'controlled' mode.
**/
onStateChange: PropTypes.func,
/**
* The root ID to use for descendants. A unique ID is created if none is provided.
**/
id: PropTypes.string,
/**
* Same as children
**/
render: PropTypes.func
};
static defaultProps = {
defaultFocusedIndex: 0,
direction: KEYBOARD_DIRECTION.HORIZONTAL
};
constructor(...args) {
super(...args);
this.state = {
focusedKey: undefined,
selectedKey: undefined,
id: IdManager.generateId('garden-selection-container')
};
this.focusSelectionModel = new SingleSelectionModel();
this.focusSelectionModel.onSelectionChanged = this.onFocusSelectionModelChange;
}
componentDidUpdate(prevProps, prevState) {
const current = this.props.focusedKey === undefined ? this.state : this.props;
const prev = prevProps.focusedKey === undefined ? prevState : prevProps;
const doc = getDocument ? getDocument(this.props) : document;
/**
* We must programatically scroll the newly focused element into view.
* Side-effect of the `aria-activedescendant` accessibility strategy.
*/
if (typeof current.focusedKey !== 'undefined' && current.focusedKey !== prev.focusedKey) {
const itemNode = doc.getElementById(this.getItemId(current.focusedKey));
/* istanbul ignore if */
if (itemNode) {
scrollTo(itemNode);
}
}
}
keyDownEventHandlers = {
[KEY_CODES.ENTER]: event => {
event.preventDefault();
const { focusedKey } = this.getControlledState();
this.selectItem(focusedKey, focusedKey);
},
[KEY_CODES.SPACE]: event => {
event.preventDefault();
const { focusedKey } = this.getControlledState();
this.selectItem(focusedKey, focusedKey);
},
[KEY_CODES.END]: event => {
event.preventDefault();
this.focusSelectionModel.selectLast();
},
[KEY_CODES.HOME]: event => {
event.preventDefault();
this.focusSelectionModel.selectFirst();
},
[KEY_CODES.LEFT]: event => {
const { direction } = this.props;
if (direction !== KEYBOARD_DIRECTION.VERTICAL) {
event.preventDefault();
if (isRtl(this.props)) {
this.focusSelectionModel.selectNext();
} else {
this.focusSelectionModel.selectPrevious();
}
}
},
[KEY_CODES.RIGHT]: event => {
const { direction } = this.props;
if (direction !== KEYBOARD_DIRECTION.VERTICAL) {
event.preventDefault();
if (isRtl(this.props)) {
this.focusSelectionModel.selectPrevious();
} else {
this.focusSelectionModel.selectNext();
}
}
},
[KEY_CODES.UP]: event => {
const { direction } = this.props;
if (direction !== KEYBOARD_DIRECTION.HORIZONTAL) {
event.preventDefault();
this.focusSelectionModel.selectPrevious();
}
},
[KEY_CODES.DOWN]: event => {
const { direction } = this.props;
if (direction !== KEYBOARD_DIRECTION.HORIZONTAL) {
event.preventDefault();
this.focusSelectionModel.selectNext();
}
}
};
onFocusSelectionModelChange = ({ newSelection }) => {
const focusedKey = this.indexKeyMap[newSelection];
this.setControlledState({ focusedKey });
};
selectItem = (selectedKey, focusedKey) => {
this.setControlledState({ selectedKey, focusedKey });
};
getContainerId = () => `${this.getControlledState().id}--container`;
getContainerProps = ({
id = this.getContainerId(),
role = 'listbox',
tabIndex = 0,
onKeyDown,
onFocus,
onBlur,
onMouseDown,
...other
} = {}) => {
const { focusedKey } = this.getControlledState();
const { defaultFocusedIndex } = this.props;
return {
id,
role,
tabIndex,
'aria-activedescendant': this.getItemId(focusedKey),
onKeyDown: composeEventHandlers(onKeyDown, event => {
const keyHandler = this.keyDownEventHandlers[event.keyCode];
keyHandler && keyHandler(event);
}),
onFocus: composeEventHandlers(onFocus, () => {
if (!this.containerMouseDown) {
if (typeof focusedKey === 'undefined') {
let selectedIndex = this.keyIndexMap[this.getControlledState().selectedKey];
if (typeof selectedIndex === 'undefined') {
selectedIndex =
defaultFocusedIndex === -1
? (selectedIndex = Object.keys(this.keyIndexMap).length - 1)
: defaultFocusedIndex;
}
this.focusSelectionModel.select(selectedIndex);
}
}
}),
onMouseDown: composeEventHandlers(onMouseDown, () => {
this.containerMouseDown = true;
/**
* This is necessary to recognize focus events caused by keyboard vs mouseDown.
* Due to React event ordering this is always called before onFocus.
*/
setTimeout(() => {
this.containerMouseDown = false;
}, 0);
}),
onBlur: composeEventHandlers(onBlur, () => {
this.focusSelectionModel.clearSelection();
}),
...other
};
};
getItemId = key =>
typeof key === 'undefined' ? null : `${this.getControlledState().id}--item-${key}`;
getItemProps = (
{ key, id = this.getItemId(key), role = 'option', onClick, index, ...props } = {},
{ selectedAriaKey = 'aria-selected' } = {}
) => {
if (typeof key === 'undefined') {
throw new Error(
'"key" must be defined within getItemProps regardless of being used within a .map()'
);
}
const { selectedKey, focusedKey } = this.getControlledState();
const isSelectedItem = key === selectedKey;
const isFocusedItem = key === focusedKey;
const currentIndex = index === undefined ? this.focusSelectionModel.numItems : index;
this.indexKeyMap[currentIndex] = key;
this.keyIndexMap[key] = currentIndex;
if (isFocusedItem || (typeof focusedKey === 'undefined' && isSelectedItem)) {
this.focusSelectionModel.selectedIndex = currentIndex;
}
this.focusSelectionModel.numItems++;
return {
id,
key,
role,
[selectedAriaKey]: isSelectedItem,
onClick: composeEventHandlers(onClick, () => {
this.selectItem(key, undefined);
}),
...props
};
};
render() {
const { children, render = children } = this.props;
const { focusedKey, selectedKey } = this.getControlledState();
this.focusSelectionModel.reset();
this.indexKeyMap = {};
this.keyIndexMap = {};
this.items = [];
return render({
getContainerProps: this.getContainerProps,
getItemProps: this.getItemProps,
focusedKey,
selectedKey,
focusSelectionModel: this.focusSelectionModel
});
}
}
export default withTheme(SelectionContainer);