-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathComboBox.tsx
More file actions
248 lines (232 loc) · 8.95 KB
/
ComboBox.tsx
File metadata and controls
248 lines (232 loc) · 8.95 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
/*
* Copyright 2022 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 {AriaComboBoxProps, useComboBox, useFilter} from 'react-aria';
import {ButtonContext} from './Button';
import {
ClassNameOrFunction,
ContextValue,
dom,
Provider,
RACValidation,
removeDataAttributes,
RenderProps,
SlotProps,
useContextProps,
useRenderProps,
useSlot,
useSlottedContext
} from './utils';
import {Collection, ComboBoxState, Node, useComboBoxState} from 'react-stately';
import {CollectionBuilder} from '@react-aria/collections';
import {FieldErrorContext} from './FieldError';
import {filterDOMProps, useResizeObserver} from '@react-aria/utils';
import {FormContext} from './Form';
import {forwardRefType, GlobalDOMAttributes, RefObject} from '@react-types/shared';
import {GroupContext} from './Group';
import {InputContext} from './Input';
import {LabelContext} from './Label';
import {ListBoxContext, ListStateContext} from './ListBox';
import {OverlayTriggerStateContext} from './Dialog';
import {PopoverContext} from './Popover';
import React, {createContext, ForwardedRef, forwardRef, useCallback, useMemo, useRef, useState} from 'react';
import {TextContext} from './Text';
export interface ComboBoxRenderProps {
/**
* Whether the combobox is currently open.
* @selector [data-open]
*/
isOpen: boolean,
/**
* Whether the combobox is disabled.
* @selector [data-disabled]
*/
isDisabled: boolean,
/**
* Whether the combobox is invalid.
* @selector [data-invalid]
*/
isInvalid: boolean,
/**
* Whether the combobox is required.
* @selector [data-required]
*/
isRequired: boolean
}
export interface ComboBoxProps<T extends object> extends Omit<AriaComboBoxProps<T>, 'children' | 'placeholder' | 'label' | 'description' | 'errorMessage' | 'validationState' | 'validationBehavior'>, RACValidation, RenderProps<ComboBoxRenderProps>, SlotProps, GlobalDOMAttributes<HTMLDivElement> {
/**
* The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state.
* @default 'react-aria-ComboBox'
*/
className?: ClassNameOrFunction<ComboBoxRenderProps>,
/** The filter function used to determine if a option should be included in the combo box list. */
defaultFilter?: (textValue: string, inputValue: string) => boolean,
/**
* Whether the text or key of the selected item is submitted as part of an HTML form.
* When `allowsCustomValue` is `true`, this option does not apply and the text is always submitted.
* @default 'key'
*/
formValue?: 'text' | 'key',
/** Whether the combo box allows the menu to be open when the collection is empty. */
allowsEmptyCollection?: boolean
}
export const ComboBoxContext = createContext<ContextValue<ComboBoxProps<any>, HTMLDivElement>>(null);
export const ComboBoxStateContext = createContext<ComboBoxState<any> | null>(null);
/**
* A combo box combines a text input with a listbox, allowing users to filter a list of options to items matching a query.
*/
export const ComboBox = /*#__PURE__*/ (forwardRef as forwardRefType)(function ComboBox<T extends object>(props: ComboBoxProps<T>, ref: ForwardedRef<HTMLDivElement>) {
[props, ref] = useContextProps(props, ref, ComboBoxContext);
let {children, isDisabled = false, isInvalid = false, isRequired = false} = props;
let content = useMemo(() => (
<ListBoxContext.Provider value={{items: props.items ?? props.defaultItems}}>
{typeof children === 'function'
? children({
isOpen: false,
isDisabled,
isInvalid,
isRequired,
defaultChildren: null
})
: children}
</ListBoxContext.Provider>
), [children, isDisabled, isInvalid, isRequired, props.items, props.defaultItems]);
return (
<CollectionBuilder content={content}>
{collection => <ComboBoxInner props={props} collection={collection} comboBoxRef={ref} />}
</CollectionBuilder>
);
});
// Contexts to clear inside the popover.
const CLEAR_CONTEXTS = [LabelContext, ButtonContext, InputContext, GroupContext, TextContext];
interface ComboBoxInnerProps<T extends object> {
props: ComboBoxProps<T>,
collection: Collection<Node<T>>,
comboBoxRef: RefObject<HTMLDivElement | null>
}
function ComboBoxInner<T extends object>({props, collection, comboBoxRef: ref}: ComboBoxInnerProps<T>) {
let {
name,
formValue = 'key',
allowsCustomValue
} = props;
if (allowsCustomValue) {
formValue = 'text';
}
let {validationBehavior: formValidationBehavior} = useSlottedContext(FormContext) || {};
let validationBehavior = props.validationBehavior ?? formValidationBehavior ?? 'native';
let {contains} = useFilter({sensitivity: 'base'});
let state = useComboBoxState({
...props,
defaultFilter: props.defaultFilter || contains,
// If props.items isn't provided, rely on collection filtering (aka listbox.items is provided or defaultItems provided to Combobox)
items: props.items,
children: undefined,
collection,
validationBehavior
});
let buttonRef = useRef<HTMLButtonElement>(null);
let inputRef = useRef<HTMLInputElement>(null);
let listBoxRef = useRef<HTMLDivElement>(null);
let popoverRef = useRef<HTMLDivElement>(null);
let [labelRef, label] = useSlot(
!props['aria-label'] && !props['aria-labelledby']
);
let {
buttonProps,
inputProps,
listBoxProps,
labelProps,
descriptionProps,
errorMessageProps,
...validation
} = useComboBox({
...removeDataAttributes(props),
label,
inputRef,
buttonRef,
listBoxRef,
popoverRef,
name: formValue === 'text' ? name : undefined,
validationBehavior
}, state);
// Make menu width match input + button
let [menuWidth, setMenuWidth] = useState<string | null>(null);
let onResize = useCallback(() => {
if (inputRef.current) {
let buttonRect = buttonRef.current?.getBoundingClientRect();
let inputRect = inputRef.current.getBoundingClientRect();
let minX = buttonRect ? Math.min(buttonRect.left, inputRect.left) : inputRect.left;
let maxX = buttonRect ? Math.max(buttonRect.right, inputRect.right) : inputRect.right;
setMenuWidth((maxX - minX) + 'px');
}
}, [buttonRef, inputRef, setMenuWidth]);
useResizeObserver({
ref: inputRef,
onResize: onResize
});
// Only expose a subset of state to renderProps function to avoid infinite render loop
let renderPropsState = useMemo(() => ({
isOpen: state.isOpen,
isDisabled: props.isDisabled || false,
isInvalid: validation.isInvalid || false,
isRequired: props.isRequired || false
}), [state.isOpen, props.isDisabled, validation.isInvalid, props.isRequired]);
let renderProps = useRenderProps({
...props,
values: renderPropsState,
defaultClassName: 'react-aria-ComboBox'
});
let DOMProps = filterDOMProps(props, {global: true});
delete DOMProps.id;
return (
<Provider
values={[
[ComboBoxStateContext, state],
[LabelContext, {...labelProps, ref: labelRef}],
[ButtonContext, {...buttonProps, ref: buttonRef, isPressed: state.isOpen}],
[InputContext, {...inputProps, ref: inputRef}],
[OverlayTriggerStateContext, state],
[PopoverContext, {
ref: popoverRef,
triggerRef: inputRef,
scrollRef: listBoxRef,
placement: 'bottom start',
isNonModal: true,
trigger: 'ComboBox',
style: {'--trigger-width': menuWidth} as React.CSSProperties,
clearContexts: CLEAR_CONTEXTS
}],
[ListBoxContext, {...listBoxProps, ref: listBoxRef}],
[ListStateContext, state],
[TextContext, {
slots: {
description: descriptionProps,
errorMessage: errorMessageProps
}
}],
[GroupContext, {isInvalid: validation.isInvalid, isDisabled: props.isDisabled || false}],
[FieldErrorContext, validation]
]}>
<dom.div
{...DOMProps}
{...renderProps}
ref={ref}
slot={props.slot || undefined}
data-focused={state.isFocused || undefined}
data-open={state.isOpen || undefined}
data-disabled={props.isDisabled || undefined}
data-invalid={validation.isInvalid || undefined}
data-required={props.isRequired || undefined} />
{name && formValue === 'key' && <input type="hidden" name={name} form={props.form} value={state.selectedKey ?? ''} />}
</Provider>
);
}