UNPKG

7.1 kBTypeScriptView Raw
1import * as React from 'react';
2import { Simplify } from '@mui/types';
3import { SelectValue, UseSelectButtonSlotProps, UseSelectListboxSlotProps } from '../useSelect';
4import { SelectOption } from '../useOption';
5import { PopupProps } from '../Unstable_Popup';
6import { PolymorphicProps, SlotComponentProps } from '../utils';
7export interface SelectRootSlotPropsOverrides {
8}
9export interface SelectListboxSlotPropsOverrides {
10}
11export interface SelectPopupSlotPropsOverrides {
12}
13export interface SelectOwnProps<OptionValue extends {}, Multiple extends boolean> {
14 /**
15 * A function used to determine if two options' values are equal.
16 * By default, reference equality is used.
17 *
18 * There is a performance impact when using the `areOptionsEqual` prop (proportional to the number of options).
19 * Therefore, it's recommented to use the default reference equality comparison whenever possible.
20 */
21 areOptionsEqual?: (a: OptionValue, b: OptionValue) => boolean;
22 /**
23 * This prop helps users to fill forms faster, especially on mobile devices.
24 * The name can be confusing, as it's more like an autofill.
25 * You can learn more about it [following the specification](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill).
26 */
27 autoComplete?: string;
28 /**
29 * If `true`, the select element is focused during the first mount
30 * @default false
31 */
32 autoFocus?: boolean;
33 children?: React.ReactNode;
34 className?: string;
35 /**
36 * If `true`, the select will be initially open.
37 * @default false
38 */
39 defaultListboxOpen?: boolean;
40 /**
41 * The default selected value. Use when the component is not controlled.
42 */
43 defaultValue?: SelectValue<OptionValue, Multiple>;
44 /**
45 * If `true`, the select is disabled.
46 * @default false
47 */
48 disabled?: boolean;
49 /**
50 * A function to convert the currently selected value to a string.
51 * Used to set a value of a hidden input associated with the select,
52 * so that the selected value can be posted with a form.
53 */
54 getSerializedValue?: (option: SelectValue<SelectOption<OptionValue>, Multiple>) => React.InputHTMLAttributes<HTMLInputElement>['value'];
55 /**
56 * `id` attribute of the listbox element.
57 */
58 listboxId?: string;
59 /**
60 * Controls the open state of the select's listbox.
61 * @default undefined
62 */
63 listboxOpen?: boolean;
64 /**
65 * If `true`, selecting multiple values is allowed.
66 * This affects the type of the `value`, `defaultValue`, and `onChange` props.
67 *
68 * @default false
69 */
70 multiple?: Multiple;
71 /**
72 * Name of the element. For example used by the server to identify the fields in form submits.
73 */
74 name?: string;
75 /**
76 * Callback fired when an option is selected.
77 */
78 onChange?: (event: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null, value: SelectValue<OptionValue, Multiple>) => void;
79 /**
80 * Callback fired when the component requests to be opened.
81 * Use in controlled mode (see listboxOpen).
82 */
83 onListboxOpenChange?: (isOpen: boolean) => void;
84 /**
85 * A function used to convert the option label to a string.
86 * It's useful when labels are elements and need to be converted to plain text
87 * to enable navigation using character keys on a keyboard.
88 *
89 * @default defaultOptionStringifier
90 */
91 getOptionAsString?: (option: SelectOption<OptionValue>) => string;
92 /**
93 * Function that customizes the rendering of the selected value.
94 */
95 renderValue?: (option: SelectValue<SelectOption<OptionValue>, Multiple>) => React.ReactNode;
96 /**
97 * Text to show when there is no selected value.
98 */
99 placeholder?: React.ReactNode;
100 /**
101 * The props used for each slot inside the Input.
102 * @default {}
103 */
104 slotProps?: {
105 root?: SlotComponentProps<'button', SelectRootSlotPropsOverrides, SelectOwnerState<OptionValue, Multiple>>;
106 listbox?: SlotComponentProps<'ul', SelectListboxSlotPropsOverrides, SelectOwnerState<OptionValue, Multiple>>;
107 popup?: SlotComponentProps<'div', SelectPopupSlotPropsOverrides & PopupProps, SelectOwnerState<OptionValue, Multiple>>;
108 };
109 /**
110 * If `true`, the Select cannot be empty when submitting form.
111 * @default false
112 */
113 required?: boolean;
114 /**
115 * The components used for each slot inside the Select.
116 * Either a string to use a HTML element or a component.
117 * @default {}
118 */
119 slots?: SelectSlots;
120 /**
121 * The selected value.
122 * Set to `null` to deselect all options.
123 */
124 value?: SelectValue<OptionValue, Multiple>;
125}
126export interface SelectSlots {
127 /**
128 * The component that renders the root.
129 * @default 'button'
130 */
131 root?: React.ElementType;
132 /**
133 * The component that renders the listbox.
134 * @default 'ul'
135 */
136 listbox?: React.ElementType;
137 /**
138 * The component that wraps the popup.
139 * @default 'div'
140 */
141 popup?: React.ElementType;
142}
143export interface SelectTypeMap<OptionValue extends {}, Multiple extends boolean, AdditionalProps = {}, RootComponentType extends React.ElementType = 'button'> {
144 props: SelectOwnProps<OptionValue, Multiple> & AdditionalProps;
145 defaultComponent: RootComponentType;
146}
147export type SelectProps<OptionValue extends {}, Multiple extends boolean, RootComponentType extends React.ElementType = SelectTypeMap<OptionValue, Multiple>['defaultComponent']> = PolymorphicProps<SelectTypeMap<OptionValue, Multiple, {}, RootComponentType>, RootComponentType>;
148export interface SelectType {
149 <OptionValue extends {}, Multiple extends boolean = false, RootComponentType extends React.ElementType = SelectTypeMap<OptionValue, Multiple>['defaultComponent']>(props: PolymorphicProps<SelectTypeMap<OptionValue, Multiple>, RootComponentType>): JSX.Element | null;
150 propTypes?: any;
151 displayName?: string | undefined;
152}
153export type SelectOwnerState<OptionValue extends {}, Multiple extends boolean> = Simplify<SelectOwnProps<OptionValue, Multiple> & {
154 active: boolean;
155 disabled: boolean;
156 focusVisible: boolean;
157 open: boolean;
158}>;
159export type SelectRootSlotProps<OptionValue extends {}, Multiple extends boolean> = Simplify<UseSelectButtonSlotProps & {
160 className?: string;
161 children?: React.ReactNode;
162 ownerState: SelectOwnerState<OptionValue, Multiple>;
163}>;
164export type SelectListboxSlotProps<OptionValue extends {}, Multiple extends boolean> = Simplify<UseSelectListboxSlotProps & {
165 className?: string;
166 children?: React.ReactNode;
167 ownerState: SelectOwnerState<OptionValue, Multiple>;
168}>;
169export type SelectPopupSlotProps<OptionValue extends {}, Multiple extends boolean> = {
170 anchor: PopupProps['anchor'];
171 children?: React.ReactNode;
172 className?: string;
173 keepMounted: PopupProps['keepMounted'];
174 open: PopupProps['open'];
175 ownerState: SelectOwnerState<OptionValue, Multiple>;
176 placement: PopupProps['placement'];
177};