forked from folio-org/stripes-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectList.js
333 lines (300 loc) · 9.65 KB
/
SelectList.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* SelectList
* Used by both SingleSelect and MultiSelect controls to render their option lists
*/
import React from 'react';
import { intlShape } from 'react-intl';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import injectIntl from '../InjectIntl';
import DefaultOptionFormatter from './DefaultOptionFormatter';
import css from './Selection.css';
const propTypes = {
activeId: PropTypes.string,
controlRef: PropTypes.object,
cursoredValue: PropTypes.string,
emptyMessage: PropTypes.string,
filtered: PropTypes.bool,
filterRef: PropTypes.object,
formatter: PropTypes.func,
id: PropTypes.string,
intl: intlShape.isRequired,
label: PropTypes.string,
list: PropTypes.arrayOf(PropTypes.object),
maxHeight: PropTypes.string,
multiple: PropTypes.bool,
onChoose: PropTypes.func,
onClickItem: PropTypes.func,
onFilter: PropTypes.func,
onFilterKeyDown: PropTypes.func,
optionAlignment: PropTypes.string,
rootRef: PropTypes.object,
searchTerm: PropTypes.string,
selected: PropTypes.string,
selectedIndex: PropTypes.number,
selectedList: PropTypes.arrayOf(PropTypes.object),
visible: PropTypes.bool,
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
};
const defaultProps = {
formatter: DefaultOptionFormatter,
maxHeight: '174px',
optionAlignment: 'outside',
searchTerm: '',
filterRef: React.createRef(),
rootRef: React.createRef(),
};
class SelectList extends React.Component { // eslint-disable-line react/no-deprecated
constructor(props) {
super(props);
this._cursorID = this.initCursorID();
this.emptyId = `${this.props.id}-empty-message`;
this.container = this.props.rootRef;
this.filterField = this.props.filterRef;
this.optionList = React.createRef();
this.containerHeight = 0;
this.getClass = this.getClass.bind(this);
this.getCursorID = this.getCursorID.bind(this);
this.scrollToCursor = this.scrollToCursor.bind(this);
this.initCursorValue = this.initCursorValue.bind(this);
this.handleOptionClick = this.handleOptionClick.bind(this);
this.animationCallback = null;
this.getRootClass = this.getRootClass.bind(this);
this.renderList = this.renderList.bind(this);
this.getAlert = this.getAlert.bind(this);
this._alignmentClass = '';
switch (props.optionAlignment) {
case 'outside':
this._alignmentClass = css.optionOutside;
break;
case 'start':
this._alignmentClass = css.optionStart;
break;
case 'end':
this._alignmentClass = css.optionEnd;
break;
case 'center':
this.alignmentClass = css.optionCentered;
break;
default:
break;
}
}
componentDidMount() {
if (this.props.selected && !this.props.filtered) {
this.scrollToCursor(true);
}
}
shouldComponentUpdate(nextProps) {
if (!this.props.visible && !nextProps.visible) {
return false;
}
return true;
}
componentDidUpdate(prevProps) {
// if listbox is appearing...
if (!prevProps.visible && this.props.visible) {
if (this.props.selected) {
this.scrollToCursor(true);
}
if (this.filterField.current) { // filter field not present in 'multiple' mode.
this.filterField.current.focus();
}
} else if (this.props.filtered) {
this.scrollToCursor();
}
// like a good overlay, send focus back to our trigger when we close...
if (prevProps.visible && !this.props.visible) {
if (this.props.controlRef.current) {
this.props.controlRef.current.focus();
}
}
if (this.props.cursoredValue !== prevProps.cursoredValue) {
this.scrollToCursor();
}
if (prevProps.searchTerm !== '' && this.props.searchTerm === '') {
if (this.props.selected !== '') {
this.scrollToCursor(true);
} else {
this.scrollToCursor();
}
}
}
componentWillUnmount() {
cancelAnimationFrame(this.animationCallback);
}
initCursorID() {
if (this.props.selected) {
return `option-${this.props.selectedIndex}-${this.props.selected}`;
}
if (this.props.list.length) {
return `option-0-${this.props.list[0].value}`;
}
return '';
}
initCursorValue(props) {
if (props.selected !== '') {
if (props.filtered) {
return props.list[0];
} else {
const selectedInd = this.props.list.findIndex(o => o.value === this.props.selected);
return this.props.list[selectedInd];
}
}
return props.list[0];
}
getCursorID() {
return this._cursorID;
}
scrollToCursor(useSelected) {
if (this.props.list.length > 0) {
this.animationCallback = requestAnimationFrame(() => {
let cursored;
if (useSelected && this.props.selected !== '') {
if (this.optionList.current) {
cursored = this.optionList.current.querySelector(`.${css.selected}`);
if (cursored) {
this.optionList.current.scrollTop = cursored.offsetTop;
}
}
} else if (this.container.current) {
cursored = this.container.current.querySelector(`.${css.cursor}`);
if (cursored) {
if (
cursored.offsetTop > ((this.optionList.current.scrollTop + this.optionList.current.offsetHeight) - 30)
) {
const newScroll = cursored.offsetTop - (this.optionList.current.offsetHeight - cursored.offsetHeight);
this.optionList.current.scrollTop = newScroll;
} else if (cursored.offsetTop < this.optionList.current.scrollTop) {
this.optionList.current.scrollTop = cursored.offsetTop;
}
}
}
});
}
}
getClass(o) {
// selected class..
let selectCriteria;
if (!this.props.multiple) {
selectCriteria = o.value === this.props.selected;
} else {
selectCriteria = (this.props.selectedList.findIndex(i => i.value === o.value) !== -1);
}
return classnames(
css.option,
this._alignmentClass,
{ [`${css.cursor}`]: o.value === this.props.cursoredValue },
{ [`${css.selected}`]: selectCriteria },
);
}
getRootClass() {
return classnames(
css.selectionListRoot,
{ 'sr-only': !this.props.visible },
);
}
getActionIndicator(o) {
const selectCriteria = (this.props.selectedList.findIndex(i => i.value === o.value) !== -1);
const removeMsg = this.props.intl.formatMessage({ id: 'stripes-components.removeSelection' });
const addMsg = this.props.intl.formatMessage({ id: 'stripes-components.addSelection' });
return selectCriteria
? <span role="img" aria-label={removeMsg}>➖</span>
: <span role="img" aria-label={addMsg}>✚</span>;
}
handleOptionClick(o) {
this.props.onChoose(o.value, o.label);
this.props.onClickItem();
}
getAlert() {
if (this.props.list.length === 0) {
return (<div className={css.selectionEmptyMessage}>{this.props.emptyMessage}</div>);
}
return null;
}
renderList() {
if (this.props.list.length === 0) {
return (
<li className={css.option}>
-{this.props.intl.formatMessage({ id: 'stripes-components.selection.emptyList' })}-
</li>
);
}
const Formatter = this.props.formatter;
return this.props.list.map((o, i) => (
<li
key={`option-${i}-${o.value}`}
role="option"
tabIndex="-1"
unselectable="on"
aria-selected={o.value === this.props.selected}
id={`option-${this.props.id}-${i}-${o.value}`}
className={this.getClass(o)}
onKeyDown={this.props.onFilterKeyDown}
onClick={() => { this.handleOptionClick(o); }}
>
<Formatter option={o} searchTerm={this.props.searchTerm} />
{
this.props.multiple &&
this.getActionIndicator(o)
}
</li>
));
}
// returns the activeId, or if the list is empty, returns the id for the "empty message" list item.
getActiveId() {
if (this.props.list.length > 0) {
return this.props.activeId;
}
return this.emptyId;
}
render() {
return (
<div
hidden={this.props.visible ? undefined : 'true'}
className={this.getRootClass()}
style={{ width: this.props.width }}
id={`sl-container-${this.props.id}`}
ref={this.props.rootRef}
>
{
!this.props.multiple &&
<div className={css.selectionFilterContainer}>
<input
type="text"
role="listbox"
aria-label={this.props.intl.formatMessage(
{ id: 'stripes-components.selection.filterOptionsLabel' },
{ label: this.props.label }
)}
placeholder={this.props.intl.formatMessage(
{ id: 'stripes-components.selection.filterOptionsPlaceholder' }
)}
onChange={this.props.onFilter}
value={this.props.searchTerm}
className={css.selectionFilter}
ref={this.props.filterRef}
aria-owns={`sl-${this.props.id}`}
aria-activedescendant={this.props.activeId}
onKeyDown={this.props.onFilterKeyDown}
/>
</div>
}
<ul
role="listbox"
className={css.selectionList}
style={{ maxHeight: this.props.maxHeight }}
ref={this.optionList}
id={`sl-${this.props.id}`}
>
{this.renderList()}
</ul>
<div className={css.selectListSection} role="alert">
{this.getAlert()}
</div>
</div>
);
}
}
SelectList.propTypes = propTypes;
SelectList.defaultProps = defaultProps;
export default injectIntl(SelectList);