-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MobileComboBox.tsx
560 lines (524 loc) · 18.5 KB
/
MobileComboBox.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import AlertMedium from '@spectrum-icons/ui/AlertMedium';
import {AriaButtonProps} from '@react-types/button';
import buttonStyles from '@adobe/spectrum-css-temp/components/button/vars.css';
import CheckmarkMedium from '@spectrum-icons/ui/CheckmarkMedium';
import ChevronDownMedium from '@spectrum-icons/ui/ChevronDownMedium';
import {classNames, unwrapDOMRef, useFocusableRef} from '@react-spectrum/utils';
import {ClearButton} from '@react-spectrum/button';
import {ComboBoxState, useComboBoxState} from '@react-stately/combobox';
import comboboxStyles from './combobox.css';
import {DismissButton, useOverlayTrigger} from '@react-aria/overlays';
import {Field} from '@react-spectrum/label';
import {FocusableRef, FocusableRefValue, RefObject, ValidationState} from '@react-types/shared';
import {FocusRing, focusSafely, FocusScope} from '@react-aria/focus';
// @ts-ignore
import intlMessages from '../intl/*.json';
import labelStyles from '@adobe/spectrum-css-temp/components/fieldlabel/vars.css';
import {ListBoxBase, useListBoxLayout} from '@react-spectrum/listbox';
import {mergeProps, useFormReset, useId} from '@react-aria/utils';
import {ProgressCircle} from '@react-spectrum/progress';
import React, {HTMLAttributes, InputHTMLAttributes, ReactElement, ReactNode, useCallback, useEffect, useRef, useState} from 'react';
import searchStyles from '@adobe/spectrum-css-temp/components/search/vars.css';
import {setInteractionModality, useHover} from '@react-aria/interactions';
import {SpectrumComboBoxProps} from '@react-types/combobox';
import styles from '@adobe/spectrum-css-temp/components/inputgroup/vars.css';
import {TextFieldBase} from '@react-spectrum/textfield';
import textfieldStyles from '@adobe/spectrum-css-temp/components/textfield/vars.css';
import {Tray} from '@react-spectrum/overlays';
import {useButton} from '@react-aria/button';
import {useComboBox} from '@react-aria/combobox';
import {useDialog} from '@react-aria/dialog';
import {useField} from '@react-aria/label';
import {useFilter, useLocalizedStringFormatter} from '@react-aria/i18n';
import {useFormValidation} from '@react-aria/form';
import {useProviderProps} from '@react-spectrum/provider';
export const MobileComboBox = React.forwardRef(function MobileComboBox<T extends object>(props: SpectrumComboBoxProps<T>, ref: FocusableRef<HTMLElement>) {
props = useProviderProps(props);
let {
isQuiet,
isDisabled,
isReadOnly,
isRequired,
validationBehavior,
name,
formValue = 'text',
allowsCustomValue
} = props;
if (allowsCustomValue) {
formValue = 'text';
}
let {contains} = useFilter({sensitivity: 'base'});
let state = useComboBoxState({
...props,
defaultFilter: contains,
allowsEmptyCollection: true,
// Needs to be false here otherwise we double up on commitSelection/commitCustomValue calls when
// user taps on underlay (i.e. initial tap will call setFocused(false) -> commitSelection/commitCustomValue via onBlur,
// then the closing of the tray will call setFocused(false) again due to cleanup effect)
shouldCloseOnBlur: false
});
let buttonRef = useRef<HTMLElement>(undefined);
let domRef = useFocusableRef(ref, buttonRef);
let {triggerProps, overlayProps} = useOverlayTrigger({type: 'listbox'}, state, buttonRef);
let inputRef = useRef<HTMLInputElement>(null);
useFormValidation({
...props,
focus: () => buttonRef.current.focus()
}, state, inputRef);
let {isInvalid, validationErrors, validationDetails} = state.displayValidation;
let validationState = props.validationState || (isInvalid ? 'invalid' : null);
let errorMessage = props.errorMessage ?? validationErrors.join(' ');
let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField({
...props,
labelElementType: 'span',
isInvalid,
errorMessage
});
// Focus the button and show focus ring when clicking on the label
labelProps.onClick = () => {
if (!props.isDisabled) {
buttonRef.current.focus();
setInteractionModality('keyboard');
}
};
let inputProps: InputHTMLAttributes<HTMLInputElement> = {
type: 'hidden',
name,
value: formValue === 'text' ? state.inputValue : state.selectedKey
};
if (validationBehavior === 'native') {
// Use a hidden <input type="text"> rather than <input type="hidden">
// so that an empty value blocks HTML form submission when the field is required.
inputProps.type = 'text';
inputProps.hidden = true;
inputProps.required = isRequired;
// Ignore react warning.
inputProps.onChange = () => {};
}
useFormReset(inputRef, inputProps.value, formValue === 'text' ? state.setInputValue : state.setSelectedKey);
return (
<>
<Field
{...props}
labelProps={labelProps}
descriptionProps={descriptionProps}
errorMessageProps={errorMessageProps}
validationState={validationState}
isInvalid={isInvalid}
validationErrors={validationErrors}
validationDetails={validationDetails}
elementType="span"
ref={domRef}
includeNecessityIndicatorInAccessibilityName>
<ComboBoxButton
{...mergeProps(triggerProps, fieldProps, {autoFocus: props.autoFocus})}
ref={buttonRef}
isQuiet={isQuiet}
isDisabled={isDisabled}
isPlaceholder={!state.inputValue}
validationState={validationState}
onPress={() => !isReadOnly && state.open(null, 'manual')}>
{state.inputValue || props.placeholder || ''}
</ComboBoxButton>
</Field>
<input {...inputProps} ref={inputRef} />
<Tray state={state} isFixedHeight {...overlayProps}>
<ComboBoxTray
{...props}
onClose={state.close}
overlayProps={overlayProps}
state={state} />
</Tray>
</>
);
});
interface ComboBoxButtonProps extends AriaButtonProps {
isQuiet?: boolean,
isDisabled?: boolean,
isPlaceholder?: boolean,
validationState?: ValidationState,
children?: ReactNode,
style?: React.CSSProperties,
className?: string
}
const ComboBoxButton = React.forwardRef(function ComboBoxButton(props: ComboBoxButtonProps, ref: RefObject<HTMLElement | null>) {
let {
isQuiet,
isDisabled,
isPlaceholder,
validationState,
children,
style,
className
} = props;
let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/combobox');
let valueId = useId();
let invalidId = useId();
let validationIcon = validationState === 'invalid'
? <AlertMedium id={invalidId} aria-label={stringFormatter.format('invalid')} />
: <CheckmarkMedium />;
let validation = React.cloneElement(validationIcon, {
UNSAFE_className: classNames(
textfieldStyles,
'spectrum-Textfield-validationIcon',
classNames(
styles,
'spectrum-InputGroup-input-validationIcon'
)
)
});
let {hoverProps, isHovered} = useHover({});
let {buttonProps, isPressed} = useButton({
...props,
'aria-labelledby': [
props['aria-labelledby'],
props['aria-label'] && !props['aria-labelledby'] ? props.id : null,
valueId,
validationState === 'invalid' ? invalidId : null
].filter(Boolean).join(' '),
elementType: 'div'
}, ref);
return (
(<FocusRing
focusClass={classNames(styles, 'is-focused')}
focusRingClass={classNames(styles, 'focus-ring')}>
<div
{...mergeProps(hoverProps, buttonProps)}
aria-haspopup="dialog"
ref={ref as RefObject<HTMLDivElement | null>}
style={{...style, outline: 'none'}}
className={
classNames(
styles,
'spectrum-InputGroup',
{
'spectrum-InputGroup--quiet': isQuiet,
'is-disabled': isDisabled,
'spectrum-InputGroup--invalid': validationState === 'invalid' && !isDisabled,
'is-hovered': isHovered
},
classNames(
comboboxStyles,
'mobile-combobox'
),
className
)
}>
<div
className={
classNames(
textfieldStyles,
'spectrum-Textfield',
{
'spectrum-Textfield--invalid': validationState === 'invalid' && !isDisabled,
'spectrum-Textfield--valid': validationState === 'valid' && !isDisabled,
'spectrum-Textfield--quiet': isQuiet
},
classNames(
styles,
'spectrum-InputGroup-field'
)
)
}>
<div
className={
classNames(
textfieldStyles,
'spectrum-Textfield-input',
{
'is-hovered': isHovered,
'is-placeholder': isPlaceholder,
'is-disabled': isDisabled
},
classNames(
styles,
'spectrum-InputGroup-input',
classNames(labelStyles, 'spectrum-Field-field')
),
classNames(
comboboxStyles,
'mobile-input'
)
)
}>
<span
id={valueId}
className={
classNames(
comboboxStyles,
'mobile-value'
)
}>
{children}
</span>
</div>
{validationState && !isDisabled ? validation : null}
</div>
<div
className={
classNames(
buttonStyles,
'spectrum-FieldButton',
{
'spectrum-FieldButton--quiet': isQuiet,
'is-active': isPressed,
'is-disabled': isDisabled,
'spectrum-FieldButton--invalid': validationState === 'invalid' && !isDisabled,
'is-hovered': isHovered
},
classNames(
styles,
'spectrum-FieldButton'
)
)
}>
<ChevronDownMedium UNSAFE_className={classNames(styles, 'spectrum-Dropdown-chevron')} />
</div>
</div>
</FocusRing>)
);
});
interface ComboBoxTrayProps extends SpectrumComboBoxProps<unknown> {
state: ComboBoxState<unknown>,
overlayProps: HTMLAttributes<HTMLElement>,
loadingIndicator?: ReactElement,
onClose: () => void
}
function ComboBoxTray(props: ComboBoxTrayProps) {
let {
// completionMode = 'suggest',
state,
isDisabled,
validationState,
label,
overlayProps,
loadingState,
onLoadMore,
onClose
} = props;
let timeout = useRef(null);
let [showLoading, setShowLoading] = useState(false);
let inputRef = useRef<HTMLInputElement>(undefined);
let buttonRef = useRef<FocusableRefValue<HTMLElement>>(undefined);
let popoverRef = useRef<HTMLDivElement>(undefined);
let listBoxRef = useRef<HTMLDivElement>(undefined);
let isLoading = loadingState === 'loading' || loadingState === 'loadingMore';
let layout = useListBoxLayout();
let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/combobox');
let {inputProps, listBoxProps, labelProps} = useComboBox(
{
...props,
// completionMode,
layoutDelegate: layout,
buttonRef: unwrapDOMRef(buttonRef),
popoverRef: popoverRef,
listBoxRef,
inputRef,
// Handled outside the tray.
name: undefined
},
state
);
React.useEffect(() => {
focusSafely(inputRef.current);
}, []);
React.useEffect(() => {
// When the tray closes, set state.isFocused (i.e. the tray input's focus tracker) to false.
// This is to prevent state.isFocused from being set to true when the tray closes via tapping on the underlay
// (FocusScope attempts to restore focus to the tray input when tapping outside the tray due to "contain")
// Have to do this manually since React doesn't call onBlur when a component is unmounted: https://github.com/facebook/react/issues/12363
if (!state.isOpen && state.isFocused) {
state.setFocused(false);
}
});
let {dialogProps} = useDialog({
'aria-labelledby': useId(labelProps.id)
}, popoverRef);
// Override the role of the input to "searchbox" instead of "combobox".
// Since the listbox is always visible, the combobox role doesn't really give us anything.
// VoiceOver on iOS reads "double tap to collapse" when focused on the input rather than
// "double tap to edit text", as with a textbox or searchbox. We'd like double tapping to
// open the virtual keyboard rather than closing the tray.
// Unlike "combobox", "aria-expanded" is not a valid attribute on "searchbox".
inputProps.role = 'searchbox';
inputProps['aria-haspopup'] = 'listbox';
delete inputProps['aria-expanded'];
delete inputProps.onTouchEnd;
let clearButton = (
<ClearButton
preventFocus
aria-label={stringFormatter.format('clear')}
excludeFromTabOrder
onPress={() => {
state.setInputValue('');
inputRef.current.focus();
}}
UNSAFE_className={
classNames(
searchStyles,
'spectrum-ClearButton'
)
}
isDisabled={isDisabled} />
);
let loadingCircle = (
<ProgressCircle
aria-label={stringFormatter.format('loading')}
size="S"
isIndeterminate
UNSAFE_className={classNames(
searchStyles,
'spectrum-Search-circleLoader',
classNames(
textfieldStyles,
'spectrum-Textfield-circleLoader'
)
)} />
);
// Close the software keyboard on scroll to give the user a bigger area to scroll.
// But only do this if scrolling with touch, otherwise it can cause issues with touch
// screen readers.
let isTouchDown = useRef(false);
let onTouchStart = () => {
isTouchDown.current = true;
};
let onTouchEnd = () => {
isTouchDown.current = false;
};
let onScroll = useCallback(() => {
if (!inputRef.current || document.activeElement !== inputRef.current || !isTouchDown.current) {
return;
}
popoverRef.current.focus();
}, [inputRef, popoverRef, isTouchDown]);
let inputValue = inputProps.value;
let lastInputValue = useRef(inputValue);
useEffect(() => {
if (loadingState === 'filtering' && !showLoading) {
if (timeout.current === null) {
timeout.current = setTimeout(() => {
setShowLoading(true);
}, 500);
}
// If user is typing, clear the timer and restart since it is a new request
if (inputValue !== lastInputValue.current) {
clearTimeout(timeout.current);
timeout.current = setTimeout(() => {
setShowLoading(true);
}, 500);
}
} else if (loadingState !== 'filtering') {
// If loading is no longer happening, clear any timers and hide the loading circle
setShowLoading(false);
clearTimeout(timeout.current);
timeout.current = null;
}
lastInputValue.current = inputValue;
}, [loadingState, inputValue, showLoading]);
let onKeyDown = (e) => {
// Close virtual keyboard if user hits Enter w/o any focused options
if (e.key === 'Enter' && state.selectionManager.focusedKey == null) {
popoverRef.current.focus();
} else {
inputProps.onKeyDown(e);
}
};
return (
<FocusScope restoreFocus contain>
<div
{...mergeProps(overlayProps, dialogProps)}
ref={popoverRef}
className={
classNames(
comboboxStyles,
'tray-dialog'
)
}>
<DismissButton onDismiss={onClose} />
<TextFieldBase
label={label}
labelProps={labelProps}
inputProps={{...inputProps, onKeyDown}}
inputRef={inputRef}
isDisabled={isDisabled}
isLoading={showLoading && loadingState === 'filtering'}
loadingIndicator={loadingState != null && loadingCircle}
validationState={validationState}
labelAlign="start"
labelPosition="top"
wrapperChildren={(state.inputValue !== '' || loadingState === 'filtering' || validationState != null) && !props.isReadOnly && clearButton}
UNSAFE_className={
classNames(
searchStyles,
'spectrum-Search',
'spectrum-Textfield',
'spectrum-Search--loadable',
{
'spectrum-Search--invalid': validationState === 'invalid' && !isDisabled,
'spectrum-Search--valid': validationState === 'valid' && !isDisabled
},
classNames(
comboboxStyles,
'tray-textfield',
{
'has-label': !!props.label
}
)
)
}
inputClassName={
classNames(
comboboxStyles,
'tray-textfield-input',
classNames(
searchStyles,
'spectrum-Search-input'
)
)
}
validationIconClassName={
classNames(
searchStyles,
'spectrum-Search-validationIcon'
)
} />
<ListBoxBase
{...listBoxProps}
domProps={{onTouchStart, onTouchEnd}}
disallowEmptySelection
shouldSelectOnPressUp
focusOnPointerEnter
layout={layout}
state={state}
shouldUseVirtualFocus
renderEmptyState={() => loadingState !== 'loading' && (
<span className={classNames(comboboxStyles, 'no-results')}>
{stringFormatter.format('noResults')}
</span>
)}
UNSAFE_className={
classNames(
comboboxStyles,
'tray-listbox'
)
}
ref={listBoxRef}
onScroll={onScroll}
onLoadMore={onLoadMore}
isLoading={isLoading} />
<DismissButton onDismiss={onClose} />
</div>
</FocusScope>
);
}