UNPKG

6.4 kBTypeScriptView Raw
1import * as React from 'react';
2import { Simplify } from '@mui/types';
3import { SelectValue, UseSelectButtonSlotProps, UseSelectListboxSlotProps } from '../useSelect';
4import { SelectOption } from '../useOption';
5import Popper, { PopperProps } from '../Popper';
6import { PolymorphicProps, SlotComponentProps, WithOptionalOwnerState } from '../utils';
7export interface SelectRootSlotPropsOverrides {
8}
9export interface SelectListboxSlotPropsOverrides {
10}
11export interface SelectPopperSlotPropsOverrides {
12}
13export interface SelectOwnProps<OptionValue extends {}, Multiple extends boolean> {
14 /**
15 * If `true`, the select element is focused during the first mount
16 * @default false
17 */
18 autoFocus?: boolean;
19 children?: React.ReactNode;
20 className?: string;
21 /**
22 * If `true`, the select will be initially open.
23 * @default false
24 */
25 defaultListboxOpen?: boolean;
26 /**
27 * The default selected value. Use when the component is not controlled.
28 */
29 defaultValue?: SelectValue<OptionValue, Multiple>;
30 /**
31 * If `true`, the select is disabled.
32 * @default false
33 */
34 disabled?: boolean;
35 /**
36 * A function to convert the currently selected value to a string.
37 * Used to set a value of a hidden input associated with the select,
38 * so that the selected value can be posted with a form.
39 */
40 getSerializedValue?: (option: SelectValue<SelectOption<OptionValue>, Multiple>) => React.InputHTMLAttributes<HTMLInputElement>['value'];
41 /**
42 * `id` attribute of the listbox element.
43 */
44 listboxId?: string;
45 /**
46 * Controls the open state of the select's listbox.
47 * @default undefined
48 */
49 listboxOpen?: boolean;
50 /**
51 * If `true`, selecting multiple values is allowed.
52 * This affects the type of the `value`, `defaultValue`, and `onChange` props.
53 *
54 * @default false
55 */
56 multiple?: Multiple;
57 /**
58 * Name of the element. For example used by the server to identify the fields in form submits.
59 * If the name is provided, the component will render a hidden input element that can be submitted to a server.
60 */
61 name?: string;
62 /**
63 * Callback fired when an option is selected.
64 */
65 onChange?: (event: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null, value: SelectValue<OptionValue, Multiple>) => void;
66 /**
67 * Callback fired when the component requests to be opened.
68 * Use in controlled mode (see listboxOpen).
69 */
70 onListboxOpenChange?: (isOpen: boolean) => void;
71 /**
72 * A function used to convert the option label to a string.
73 * It's useful when labels are elements and need to be converted to plain text
74 * to enable navigation using character keys on a keyboard.
75 *
76 * @default defaultOptionStringifier
77 */
78 getOptionAsString?: (option: SelectOption<OptionValue>) => string;
79 /**
80 * Function that customizes the rendering of the selected value.
81 */
82 renderValue?: (option: SelectValue<SelectOption<OptionValue>, Multiple>) => React.ReactNode;
83 /**
84 * The props used for each slot inside the Input.
85 * @default {}
86 */
87 slotProps?: {
88 root?: SlotComponentProps<'button', SelectRootSlotPropsOverrides, SelectOwnerState<OptionValue, Multiple>>;
89 listbox?: SlotComponentProps<'ul', SelectListboxSlotPropsOverrides, SelectOwnerState<OptionValue, Multiple>>;
90 popper?: SlotComponentProps<typeof Popper, SelectPopperSlotPropsOverrides, SelectOwnerState<OptionValue, Multiple>>;
91 };
92 /**
93 * The components used for each slot inside the Select.
94 * Either a string to use a HTML element or a component.
95 * @default {}
96 */
97 slots?: SelectSlots<OptionValue, Multiple>;
98 /**
99 * The selected value.
100 * Set to `null` to deselect all options.
101 */
102 value?: SelectValue<OptionValue, Multiple>;
103}
104export interface SelectSlots<OptionValue extends {}, Multiple extends boolean> {
105 /**
106 * The component that renders the root.
107 * @default 'button'
108 */
109 root?: React.ElementType;
110 /**
111 * The component that renders the listbox.
112 * @default 'ul'
113 */
114 listbox?: React.ElementType;
115 /**
116 * The component that renders the popper.
117 * @default Popper
118 */
119 popper?: React.ComponentType<WithOptionalOwnerState<SelectPopperSlotProps<OptionValue, Multiple>>>;
120}
121export interface SelectTypeMap<OptionValue extends {}, Multiple extends boolean, AdditionalProps = {}, RootComponentType extends React.ElementType = 'button'> {
122 props: SelectOwnProps<OptionValue, Multiple> & AdditionalProps;
123 defaultComponent: RootComponentType;
124}
125export type SelectProps<OptionValue extends {}, Multiple extends boolean, RootComponentType extends React.ElementType = SelectTypeMap<OptionValue, Multiple>['defaultComponent']> = PolymorphicProps<SelectTypeMap<OptionValue, Multiple, {}, RootComponentType>, RootComponentType>;
126export interface SelectType {
127 <OptionValue extends {}, Multiple extends boolean = false, RootComponentType extends React.ElementType = SelectTypeMap<OptionValue, Multiple>['defaultComponent']>(props: PolymorphicProps<SelectTypeMap<OptionValue, Multiple>, RootComponentType>): JSX.Element | null;
128 propTypes?: any;
129 displayName?: string | undefined;
130}
131export type SelectOwnerState<OptionValue extends {}, Multiple extends boolean> = Simplify<SelectOwnProps<OptionValue, Multiple> & {
132 active: boolean;
133 disabled: boolean;
134 focusVisible: boolean;
135 open: boolean;
136}>;
137export type SelectRootSlotProps<OptionValue extends {}, Multiple extends boolean> = Simplify<UseSelectButtonSlotProps & {
138 className?: string;
139 children?: React.ReactNode;
140 ownerState: SelectOwnerState<OptionValue, Multiple>;
141}>;
142export type SelectListboxSlotProps<OptionValue extends {}, Multiple extends boolean> = Simplify<UseSelectListboxSlotProps & {
143 className?: string;
144 children?: React.ReactNode;
145 ownerState: SelectOwnerState<OptionValue, Multiple>;
146}>;
147export type SelectPopperSlotProps<OptionValue extends {}, Multiple extends boolean> = {
148 anchorEl: PopperProps['anchorEl'];
149 children?: PopperProps['children'];
150 className?: string;
151 keepMounted: PopperProps['keepMounted'];
152 open: boolean;
153 ownerState: SelectOwnerState<OptionValue, Multiple>;
154 placement: PopperProps['placement'];
155};