-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathComboBox.tsx
394 lines (376 loc) · 12.2 KB
/
ComboBox.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
/*
* Copyright 2024 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 {
ComboBox as AriaComboBox,
ComboBoxProps as AriaComboBoxProps,
ListBoxSection as AriaListBoxSection,
PopoverProps as AriaPopoverProps,
Button,
ButtonRenderProps,
ContextValue,
InputContext,
ListBox,
ListBoxItem,
ListBoxItemProps,
ListBoxProps,
Provider,
SectionProps
} from 'react-aria-components';
import {baseColor, style} from '../style' with {type: 'macro'};
import {centerBaseline} from './CenterBaseline';
import {
checkmark,
description,
Divider,
icon,
iconCenterWrapper,
label,
menuitem,
section,
sectionHeader,
sectionHeading
} from './Menu';
import CheckmarkIcon from '../ui-icons/Checkmark';
import ChevronIcon from '../ui-icons/Chevron';
import {createContext, CSSProperties, forwardRef, ReactNode, Ref, useCallback, useContext, useImperativeHandle, useRef, useState} from 'react';
import {createFocusableRef} from '@react-spectrum/utils';
import {field, fieldInput, getAllowedOverrides, StyleProps} from './style-utils' with {type: 'macro'};
import {FieldErrorIcon, FieldGroup, FieldLabel, HelpText, Input} from './Field';
import {FormContext, useFormProps} from './Form';
import {forwardRefType} from './types';
import {HeaderContext, HeadingContext, Text, TextContext} from './Content';
import {HelpTextProps, SpectrumLabelableProps} from '@react-types/shared';
import {IconContext} from './Icon';
import {menu} from './Picker';
import {mergeRefs, useResizeObserver} from '@react-aria/utils';
import {Placement} from 'react-aria';
import {PopoverBase} from './Popover';
import {pressScale} from './pressScale';
import {TextFieldRef} from '@react-types/textfield';
import {useSpectrumContextProps} from './useSpectrumContextProps';
export interface ComboboxStyleProps {
/**
* The size of the Combobox.
*
* @default 'M'
*/
size?: 'S' | 'M' | 'L' | 'XL'
}
export interface ComboBoxProps<T extends object> extends
Omit<AriaComboBoxProps<T>, 'children' | 'style' | 'className' | 'defaultFilter' | 'allowsEmptyCollection'>,
ComboboxStyleProps,
StyleProps,
SpectrumLabelableProps,
HelpTextProps,
Pick<ListBoxProps<T>, 'items'>,
Pick<AriaPopoverProps, 'shouldFlip'> {
/** The contents of the collection. */
children: ReactNode | ((item: T) => ReactNode),
/**
* Direction the menu will render relative to the Picker.
*
* @default 'bottom'
*/
direction?: 'bottom' | 'top',
/**
* Alignment of the menu relative to the input target.
*
* @default 'start'
*/
align?: 'start' | 'end',
/** Width of the menu. By default, matches width of the trigger. Note that the minimum width of the dropdown is always equal to the trigger's width. */
menuWidth?: number
}
export const ComboBoxContext = createContext<ContextValue<Partial<ComboBoxProps<any>>, TextFieldRef>>(null);
const inputButton = style<ButtonRenderProps & {isOpen: boolean, size: 'S' | 'M' | 'L' | 'XL'}>({
display: 'flex',
outlineStyle: 'none',
textAlign: 'center',
borderStyle: 'none',
borderRadius: 'control-sm',
alignItems: 'center',
justifyContent: 'center',
size: {
size: {
S: 16,
M: 20,
L: 24,
XL: 32
}
},
marginStart: 'text-to-control',
aspectRatio: 'square',
flexShrink: 0,
transition: {
default: 'default',
forcedColors: 'none'
},
backgroundColor: {
default: baseColor('gray-100'),
isOpen: 'gray-200',
isDisabled: 'disabled',
forcedColors: {
default: 'ButtonText',
isHovered: 'Highlight',
isOpen: 'Highlight',
isDisabled: 'GrayText'
}
},
color: {
default: 'neutral',
isDisabled: 'disabled',
forcedColors: {
default: 'ButtonFace'
}
}
});
const iconStyles = style({
flexShrink: 0,
rotate: 90,
'--iconPrimary': {
type: 'fill',
value: 'currentColor'
}
});
let InternalComboboxContext = createContext<{size: 'S' | 'M' | 'L' | 'XL'}>({size: 'M'});
/**
* ComboBox allow users to choose a single option from a collapsible list of options when space is limited.
*/
export const ComboBox = /*#__PURE__*/ (forwardRef as forwardRefType)(function ComboBox<T extends object>(props: ComboBoxProps<T>, ref: Ref<TextFieldRef>) {
[props, ref] = useSpectrumContextProps(props, ref, ComboBoxContext);
let inputRef = useRef<HTMLInputElement>(null);
let domRef = useRef<HTMLDivElement>(null);
let buttonRef = useRef<HTMLButtonElement>(null);
let formContext = useContext(FormContext);
props = useFormProps(props);
let {
direction = 'bottom',
align = 'start',
shouldFlip = true,
menuWidth,
label,
description: descriptionMessage,
errorMessage,
children,
items,
size = 'M',
labelPosition = 'top',
labelAlign = 'start',
necessityIndicator,
UNSAFE_className = '',
UNSAFE_style,
...pickerProps
} = props;
// Expose imperative interface for ref
useImperativeHandle(ref, () => ({
...createFocusableRef(domRef, inputRef),
select() {
if (inputRef.current) {
inputRef.current.select();
}
},
getInputElement() {
return inputRef.current;
}
}));
// Better way to encode this into a style? need to account for flipping
let menuOffset: number;
if (size === 'S') {
menuOffset = 6;
} else if (size === 'M') {
menuOffset = 6;
} else if (size === 'L') {
menuOffset = 7;
} else {
menuOffset = 8;
}
let triggerRef = useRef<HTMLDivElement>(null);
// Make menu width match input + button
let [triggerWidth, setTriggerWidth] = useState<string | null>(null);
let onResize = useCallback(() => {
if (triggerRef.current) {
let inputRect = triggerRef.current.getBoundingClientRect();
let minX = inputRect.left;
let maxX = inputRect.right;
setTriggerWidth((maxX - minX) + 'px');
}
}, [triggerRef, setTriggerWidth]);
useResizeObserver({
ref: triggerRef,
onResize: onResize
});
return (
<AriaComboBox
{...pickerProps}
style={UNSAFE_style}
className={UNSAFE_className + style(field(), getAllowedOverrides())({
isInForm: !!formContext,
labelPosition,
size
}, props.styles)}>
{({isDisabled, isOpen, isRequired, isInvalid}) => (
<>
<InternalComboboxContext.Provider value={{size}}>
<FieldLabel
isDisabled={isDisabled}
isRequired={isRequired}
size={size}
labelPosition={labelPosition}
labelAlign={labelAlign}
necessityIndicator={necessityIndicator}
contextualHelp={props.contextualHelp}>
{label}
</FieldLabel>
<FieldGroup
ref={triggerRef}
role="presentation"
isDisabled={isDisabled}
isInvalid={isInvalid}
size={size}
styles={style({
...fieldInput(),
paddingStart: 'edge-to-text',
// better way to do this one? it's not actually half, they are
// [9, 4], [12, 6], [15, 8], [18, 8]
// also noticed that our measurement is including the border, making the padding too much
paddingEnd: '[calc(self(height, self(minHeight)) * 3 / 16)]'
})({size})}>
<InputContext.Consumer>
{ctx => (
<InputContext.Provider value={{...ctx, ref: mergeRefs((ctx as any)?.ref, inputRef)}}>
<Input />
</InputContext.Provider>
)}
</InputContext.Consumer>
{isInvalid && <FieldErrorIcon isDisabled={isDisabled} />}
<Button
ref={buttonRef}
// Prevent press scale from sticking while ComboBox is open.
// @ts-ignore
isPressed={false}
style={renderProps => pressScale(buttonRef)(renderProps)}
className={renderProps => inputButton({
...renderProps,
size,
isOpen
})}>
<ChevronIcon
size={size}
className={iconStyles} />
</Button>
</FieldGroup>
<HelpText
size={size}
isDisabled={isDisabled}
isInvalid={isInvalid}
description={descriptionMessage}>
{errorMessage}
</HelpText>
<PopoverBase
hideArrow
triggerRef={triggerRef}
offset={menuOffset}
placement={`${direction} ${align}` as Placement}
shouldFlip={shouldFlip}
UNSAFE_style={{
width: menuWidth ? `${menuWidth}px` : undefined,
// manually subtract border as we can't set Popover to border-box, it causes the contents to spill out
'--trigger-width': `calc(${triggerWidth} - 2px)`
} as CSSProperties}
styles={style({
minWidth: '[var(--trigger-width)]',
width: '[var(--trigger-width)]'
})}>
<Provider
values={[
[HeaderContext, {styles: sectionHeader({size})}],
[HeadingContext, {styles: sectionHeading}],
[TextContext, {
slots: {
'description': {styles: description({size})}
}
}]
]}>
<ListBox
items={items}
className={menu({size})}>
{children}
</ListBox>
</Provider>
</PopoverBase>
</InternalComboboxContext.Provider>
</>
)}
</AriaComboBox>
);
});
export interface ComboBoxItemProps extends Omit<ListBoxItemProps, 'children' | 'style' | 'className'>, StyleProps {
children: ReactNode
}
const checkmarkIconSize = {
S: 'XS',
M: 'M',
L: 'L',
XL: 'XL'
} as const;
export function ComboBoxItem(props: ComboBoxItemProps): ReactNode {
let ref = useRef(null);
let isLink = props.href != null;
let {size} = useContext(InternalComboboxContext);
return (
<ListBoxItem
{...props}
ref={ref}
textValue={props.textValue || (typeof props.children === 'string' ? props.children as string : undefined)}
style={pressScale(ref, props.UNSAFE_style)}
className={renderProps => (props.UNSAFE_className || '') + menuitem({...renderProps, size, isLink}, props.styles)}>
{(renderProps) => {
let {children} = props;
return (
<>
<Provider
values={[
[IconContext, {
slots: {
icon: {render: centerBaseline({slot: 'icon', styles: iconCenterWrapper}), styles: icon}
}
}],
[TextContext, {
slots: {
label: {styles: label({size})},
description: {styles: description({...renderProps, size})}
}
}]
]}>
{!isLink && <CheckmarkIcon size={checkmarkIconSize[size]} className={checkmark({...renderProps, size})} />}
{typeof children === 'string' ? <Text slot="label">{children}</Text> : children}
</Provider>
</>
);
}}
</ListBoxItem>
);
}
export interface ComboBoxSectionProps<T extends object> extends SectionProps<T> {}
export function ComboBoxSection<T extends object>(props: ComboBoxSectionProps<T>): ReactNode {
let {size} = useContext(InternalComboboxContext);
return (
<>
<AriaListBoxSection
{...props}
className={section({size})}>
{props.children}
</AriaListBoxSection>
<Divider />
</>
);
}