-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.tsx
408 lines (374 loc) · 11.3 KB
/
index.tsx
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { __, _n, sprintf } from '@wordpress/i18n';
import {
Component,
useState,
useMemo,
useRef,
useEffect,
} from '@wordpress/element';
import { useInstanceId } from '@wordpress/compose';
import { speak } from '@wordpress/a11y';
import { closeSmall } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { InputWrapperFlex } from './styles';
import TokenInput from '../form-token-field/token-input';
import SuggestionsList from '../form-token-field/suggestions-list';
import BaseControl from '../base-control';
import Button from '../button';
import { FlexBlock } from '../flex';
import withFocusOutside from '../higher-order/with-focus-outside';
import { useControlledValue } from '../utils/hooks';
import { normalizeTextString } from '../utils/strings';
import type { ComboboxControlOption, ComboboxControlProps } from './types';
import type { TokenInputProps } from '../form-token-field/types';
import { useDeprecated36pxDefaultSizeProp } from '../utils/use-deprecated-props';
import { withIgnoreIMEEvents } from '../utils/with-ignore-ime-events';
import { maybeWarnDeprecated36pxSize } from '../utils/deprecated-36px-size';
const noop = () => {};
interface DetectOutsideComponentProps {
onFocusOutside: ( event: React.FocusEvent ) => void;
children?: React.ReactNode;
}
const DetectOutside = withFocusOutside(
class extends Component< DetectOutsideComponentProps > {
handleFocusOutside( event: React.FocusEvent ) {
this.props.onFocusOutside( event );
}
render() {
return this.props.children;
}
}
);
const getIndexOfMatchingSuggestion = (
selectedSuggestion: ComboboxControlOption | null,
matchingSuggestions: ComboboxControlOption[]
) =>
selectedSuggestion === null
? -1
: matchingSuggestions.indexOf( selectedSuggestion );
/**
* `ComboboxControl` is an enhanced version of a [`SelectControl`](../select-control/README.md) with the addition of
* being able to search for options using a search input.
*
* ```jsx
* import { ComboboxControl } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const options = [
* {
* value: 'small',
* label: 'Small',
* },
* {
* value: 'normal',
* label: 'Normal',
* disabled: true,
* },
* {
* value: 'large',
* label: 'Large',
* disabled: false,
* },
* ];
*
* function MyComboboxControl() {
* const [ fontSize, setFontSize ] = useState();
* const [ filteredOptions, setFilteredOptions ] = useState( options );
* return (
* <ComboboxControl
* __next40pxDefaultSize
* __nextHasNoMarginBottom
* label="Font Size"
* value={ fontSize }
* onChange={ setFontSize }
* options={ filteredOptions }
* onFilterValueChange={ ( inputValue ) =>
* setFilteredOptions(
* options.filter( ( option ) =>
* option.label
* .toLowerCase()
* .startsWith( inputValue.toLowerCase() )
* )
* )
* }
* />
* );
* }
* ```
*/
function ComboboxControl( props: ComboboxControlProps ) {
const {
__nextHasNoMarginBottom = false,
__next40pxDefaultSize = false,
value: valueProp,
label,
options,
onChange: onChangeProp,
onFilterValueChange = noop,
hideLabelFromVision,
help,
allowReset = true,
className,
messages = {
selected: __( 'Item selected.' ),
},
__experimentalRenderItem,
expandOnFocus = true,
placeholder,
} = useDeprecated36pxDefaultSizeProp( props );
const [ value, setValue ] = useControlledValue( {
value: valueProp,
onChange: onChangeProp,
} );
const currentOption = options.find( ( option ) => option.value === value );
const currentLabel = currentOption?.label ?? '';
// Use a custom prefix when generating the `instanceId` to avoid having
// duplicate input IDs when rendering this component and `FormTokenField`
// in the same page (see https://github.com/WordPress/gutenberg/issues/42112).
const instanceId = useInstanceId( ComboboxControl, 'combobox-control' );
const [ selectedSuggestion, setSelectedSuggestion ] = useState(
currentOption || null
);
const [ isExpanded, setIsExpanded ] = useState( false );
const [ inputHasFocus, setInputHasFocus ] = useState( false );
const [ inputValue, setInputValue ] = useState( '' );
const inputContainer = useRef< HTMLInputElement >( null );
const matchingSuggestions = useMemo( () => {
const startsWithMatch: ComboboxControlOption[] = [];
const containsMatch: ComboboxControlOption[] = [];
const match = normalizeTextString( inputValue );
options.forEach( ( option ) => {
const index = normalizeTextString( option.label ).indexOf( match );
if ( index === 0 ) {
startsWithMatch.push( option );
} else if ( index > 0 ) {
containsMatch.push( option );
}
} );
return startsWithMatch.concat( containsMatch );
}, [ inputValue, options ] );
const onSuggestionSelected = (
newSelectedSuggestion: ComboboxControlOption
) => {
if ( newSelectedSuggestion.disabled ) {
return;
}
setValue( newSelectedSuggestion.value );
speak( messages.selected, 'assertive' );
setSelectedSuggestion( newSelectedSuggestion );
setInputValue( '' );
setIsExpanded( false );
};
const handleArrowNavigation = ( offset = 1 ) => {
const index = getIndexOfMatchingSuggestion(
selectedSuggestion,
matchingSuggestions
);
let nextIndex = index + offset;
if ( nextIndex < 0 ) {
nextIndex = matchingSuggestions.length - 1;
} else if ( nextIndex >= matchingSuggestions.length ) {
nextIndex = 0;
}
setSelectedSuggestion( matchingSuggestions[ nextIndex ] );
setIsExpanded( true );
};
const onKeyDown: React.KeyboardEventHandler< HTMLDivElement > =
withIgnoreIMEEvents( ( event ) => {
let preventDefault = false;
if ( event.defaultPrevented ) {
return;
}
switch ( event.code ) {
case 'Enter':
if ( selectedSuggestion ) {
onSuggestionSelected( selectedSuggestion );
preventDefault = true;
}
break;
case 'ArrowUp':
handleArrowNavigation( -1 );
preventDefault = true;
break;
case 'ArrowDown':
handleArrowNavigation( 1 );
preventDefault = true;
break;
case 'Escape':
setIsExpanded( false );
setSelectedSuggestion( null );
preventDefault = true;
break;
default:
break;
}
if ( preventDefault ) {
event.preventDefault();
}
} );
const onBlur = () => {
setInputHasFocus( false );
};
const onFocus = () => {
setInputHasFocus( true );
if ( expandOnFocus ) {
setIsExpanded( true );
}
onFilterValueChange( '' );
setInputValue( '' );
};
const onClick = () => {
setIsExpanded( true );
};
const onFocusOutside = () => {
setIsExpanded( false );
};
const onInputChange: TokenInputProps[ 'onChange' ] = ( event ) => {
const text = event.value;
setInputValue( text );
onFilterValueChange( text );
if ( inputHasFocus ) {
setIsExpanded( true );
}
};
const handleOnReset = () => {
setValue( null );
inputContainer.current?.focus();
};
// Stop propagation of the keydown event when pressing Enter on the Reset
// button to prevent calling the onKeydown callback on the container div
// element which actually sets the selected suggestion.
const handleResetStopPropagation: React.KeyboardEventHandler<
HTMLButtonElement
> = ( event ) => {
event.stopPropagation();
};
// Update current selections when the filter input changes.
useEffect( () => {
const hasMatchingSuggestions = matchingSuggestions.length > 0;
const hasSelectedMatchingSuggestions =
getIndexOfMatchingSuggestion(
selectedSuggestion,
matchingSuggestions
) > 0;
if ( hasMatchingSuggestions && ! hasSelectedMatchingSuggestions ) {
// If the current selection isn't present in the list of suggestions, then automatically select the first item from the list of suggestions.
setSelectedSuggestion( matchingSuggestions[ 0 ] );
}
}, [ matchingSuggestions, selectedSuggestion ] );
// Announcements.
useEffect( () => {
const hasMatchingSuggestions = matchingSuggestions.length > 0;
if ( isExpanded ) {
const message = hasMatchingSuggestions
? sprintf(
/* translators: %d: number of results. */
_n(
'%d result found, use up and down arrow keys to navigate.',
'%d results found, use up and down arrow keys to navigate.',
matchingSuggestions.length
),
matchingSuggestions.length
)
: __( 'No results.' );
speak( message, 'polite' );
}
}, [ matchingSuggestions, isExpanded ] );
maybeWarnDeprecated36pxSize( {
componentName: 'ComboboxControl',
__next40pxDefaultSize,
size: undefined,
} );
// Disable reason: There is no appropriate role which describes the
// input container intended accessible usability.
// TODO: Refactor click detection to use blur to stop propagation.
/* eslint-disable jsx-a11y/no-static-element-interactions */
return (
<DetectOutside onFocusOutside={ onFocusOutside }>
<BaseControl
__nextHasNoMarginBottom={ __nextHasNoMarginBottom }
__associatedWPComponentName="ComboboxControl"
className={ clsx( className, 'components-combobox-control' ) }
label={ label }
id={ `components-form-token-input-${ instanceId }` }
hideLabelFromVision={ hideLabelFromVision }
help={ help }
>
<div
className="components-combobox-control__suggestions-container"
tabIndex={ -1 }
onKeyDown={ onKeyDown }
>
<InputWrapperFlex
__next40pxDefaultSize={ __next40pxDefaultSize }
>
<FlexBlock>
<TokenInput
className="components-combobox-control__input"
instanceId={ instanceId }
ref={ inputContainer }
placeholder={ placeholder }
value={ isExpanded ? inputValue : currentLabel }
onFocus={ onFocus }
onBlur={ onBlur }
onClick={ onClick }
isExpanded={ isExpanded }
selectedSuggestionIndex={ getIndexOfMatchingSuggestion(
selectedSuggestion,
matchingSuggestions
) }
onChange={ onInputChange }
/>
</FlexBlock>
{ allowReset && (
<Button
size="small"
icon={ closeSmall }
// Disable reason: Focus returns to input field when reset is clicked.
// eslint-disable-next-line no-restricted-syntax
disabled={ ! value }
onClick={ handleOnReset }
onKeyDown={ handleResetStopPropagation }
label={ __( 'Reset' ) }
/>
) }
</InputWrapperFlex>
{ isExpanded && (
<SuggestionsList
instanceId={ instanceId }
// The empty string for `value` here is not actually used, but is
// just a quick way to satisfy the TypeScript requirements of SuggestionsList.
// See: https://github.com/WordPress/gutenberg/pull/47581/files#r1091089330
match={ { label: inputValue, value: '' } }
displayTransform={ ( suggestion ) =>
suggestion.label
}
suggestions={ matchingSuggestions }
selectedIndex={ getIndexOfMatchingSuggestion(
selectedSuggestion,
matchingSuggestions
) }
onHover={ setSelectedSuggestion }
onSelect={ onSuggestionSelected }
scrollIntoView
__experimentalRenderItem={
__experimentalRenderItem
}
/>
) }
</div>
</BaseControl>
</DetectOutside>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
}
export default ComboboxControl;