-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathComboBox.tsx
More file actions
59 lines (56 loc) · 2.06 KB
/
ComboBox.tsx
File metadata and controls
59 lines (56 loc) · 2.06 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
import React from 'react';
import {
ComboBox as SpectrumComboBox,
type SpectrumComboBoxProps,
} from '@adobe/react-spectrum';
import type { DOMRef } from '@react-types/shared';
import cl from 'classnames';
import { useMergeRef } from '@deephaven/react-hooks';
import type { NormalizedItem } from '../utils';
import { type PickerPropsT, usePickerProps } from '../picker';
export type ComboBoxProps = PickerPropsT<SpectrumComboBoxProps<NormalizedItem>>;
export type { MenuTriggerAction } from '@react-types/combobox';
export { SpectrumComboBox };
// `forwardRef`'s inferred prop type incorrectly drops required `children`
// because of upstream Spectrum type issues, so use `any` for the inner
// render function and re-cast the result to expose the correct
// `ComboBoxProps` to consumers.
const ComboBoxInternal = React.forwardRef(function ComboBox(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{ UNSAFE_className, ...props }: any,
ref: DOMRef<HTMLDivElement>
): JSX.Element {
const {
defaultSelectedKey,
disabledKeys,
selectedKey,
ref: scrollRef,
...comboBoxProps
} = usePickerProps(props);
const pickerRef = useMergeRef(ref, scrollRef);
return (
<SpectrumComboBox
// eslint-disable-next-line react/jsx-props-no-spreading
{...comboBoxProps}
UNSAFE_className={cl('dh-combobox', UNSAFE_className)}
ref={pickerRef}
// Type assertions are necessary here since Spectrum types don't account
// for number and boolean key values even though they are valid runtime
// values.
defaultSelectedKey={
defaultSelectedKey as SpectrumComboBoxProps<NormalizedItem>['defaultSelectedKey']
}
disabledKeys={
disabledKeys as SpectrumComboBoxProps<NormalizedItem>['disabledKeys']
}
selectedKey={
selectedKey as SpectrumComboBoxProps<NormalizedItem>['selectedKey']
}
/>
);
});
ComboBoxInternal.displayName = 'ComboBox';
export const ComboBox =
ComboBoxInternal as unknown as React.ForwardRefExoticComponent<
ComboBoxProps & React.RefAttributes<unknown>
>;