UNPKG

6.49 kBTypeScriptView Raw
1import * as React from 'react';
2import { ListAction, ListState, UseListRootSlotProps } from '../useList';
3import { SelectOption } from '../useOption/useOption.types';
4import { EventHandlers } from '../utils/types';
5import { SelectProviderValue } from './SelectProvider';
6import { MuiCancellableEventHandler } from '../utils/muiCancellableEvent';
7export type SelectChangeEventType = React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<Element> | React.FocusEvent<Element, Element> | null;
8export type SelectValue<Value, Multiple> = Multiple extends true ? Value[] : Value | null;
9export interface SelectOptionDefinition<Value> {
10 value: Value;
11 disabled?: boolean;
12 label: string;
13}
14export interface UseSelectParameters<OptionValue, Multiple extends boolean = false> {
15 /**
16 * If `true`, the select will be open by default.
17 * @default false
18 */
19 defaultOpen?: boolean;
20 /**
21 * The default selected value. Use when the component is not controlled.
22 */
23 defaultValue?: SelectValue<OptionValue, Multiple>;
24 /**
25 * If `true`, the select is disabled.
26 * @default false
27 */
28 disabled?: boolean;
29 /**
30 * The ref of the trigger button element.
31 */
32 buttonRef?: React.Ref<Element>;
33 /**
34 * The `id` attribute of the listbox element.
35 */
36 listboxId?: string;
37 /**
38 * The ref of the listbox element.
39 */
40 listboxRef?: React.Ref<Element>;
41 /**
42 * If `true`, the end user can select multiple values.
43 * This affects the type of the `value`, `defaultValue`, and `onChange` props.
44 *
45 * @default false
46 */
47 multiple?: Multiple;
48 /**
49 * Callback fired when an option is selected.
50 */
51 onChange?: (event: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null, value: SelectValue<OptionValue, Multiple>) => void;
52 /**
53 * Callback fired when an option is highlighted.
54 */
55 onHighlightChange?: (event: React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<Element> | React.FocusEvent<Element, Element> | null, highlighted: OptionValue | null) => void;
56 /**
57 * Callback fired when the listbox is opened or closed.
58 */
59 onOpenChange?: (open: boolean) => void;
60 /**
61 * Controls the open state of the select's listbox.
62 * This is the controlled equivalent of the `defaultOpen` prop.
63 */
64 open?: boolean;
65 /**
66 * An alternative way to specify the options.
67 * If this parameter is set, options defined as JSX children are ignored.
68 */
69 options?: SelectOptionDefinition<OptionValue>[];
70 /**
71 * A function used to convert the option label to a string.
72 * This is useful when labels are elements and need to be converted to plain text
73 * to enable keyboard navigation with character keys.
74 *
75 * @default defaultOptionStringifier
76 */
77 getOptionAsString?: (option: SelectOption<OptionValue>) => string;
78 /**
79 * The selected value.
80 * Set to `null` to deselect all options.
81 */
82 value?: SelectValue<OptionValue, Multiple>;
83}
84interface UseSelectButtonSlotEventHandlers {
85 onClick: MuiCancellableEventHandler<React.MouseEvent>;
86}
87export type UseSelectButtonSlotProps<TOther = {}> = UseListRootSlotProps<Omit<TOther, keyof UseSelectButtonSlotEventHandlers>> & UseSelectButtonSlotEventHandlers & {
88 'aria-expanded': React.AriaAttributes['aria-expanded'];
89 'aria-controls': React.AriaAttributes['aria-controls'];
90 role: React.HTMLAttributes<Element>['role'];
91 ref: React.RefCallback<Element> | null;
92};
93interface UseSelectListboxSlotEventHandlers {
94 onMouseDown: React.MouseEventHandler;
95}
96export type UseSelectListboxSlotProps<TOther = {}> = Omit<TOther, keyof UseSelectListboxSlotEventHandlers> & UseSelectListboxSlotEventHandlers & {
97 'aria-multiselectable': React.AriaAttributes['aria-multiselectable'];
98 id: string | undefined;
99 ref: React.RefCallback<Element> | null;
100 role: React.HTMLAttributes<Element>['role'];
101};
102export interface UseSelectReturnValue<Value, Multiple> {
103 /**
104 * If `true`, the trigger button is active (pressed).
105 */
106 buttonActive: boolean;
107 /**
108 * If `true`, the trigger button has a visible focus.
109 */
110 buttonFocusVisible: boolean;
111 /**
112 * Ref to the button slot DOM node.
113 */
114 buttonRef: React.RefCallback<Element> | null;
115 /**
116 * If `true`, the select is disabled.
117 */
118 disabled: boolean;
119 /**
120 * Action dispatcher for the select component.
121 * Allows to programmatically control the select.
122 */
123 dispatch: (action: ListAction<Value> | SelectAction) => void;
124 /**
125 * Resolver for the button slot's props.
126 * @param otherHandlers event handlers for the button slot
127 * @returns props that should be spread on the button slot
128 */
129 getButtonProps: <OtherHandlers extends EventHandlers = {}>(otherHandlers?: OtherHandlers) => UseSelectButtonSlotProps<OtherHandlers>;
130 /**
131 * Resolver for the listbox slot's props.
132 * @param otherHandlers event handlers for the listbox slot
133 * @returns props that should be spread on the listbox slot
134 */
135 getListboxProps: <OtherHandlers extends EventHandlers = {}>(otherHandlers?: OtherHandlers) => UseSelectListboxSlotProps<OtherHandlers>;
136 /**
137 * A function that returns the metadata of an option with a given value.
138 *
139 * @param optionValue the value of the option
140 * @returns
141 */
142 getOptionMetadata: (optionValue: Value) => SelectOption<Value> | undefined;
143 /**
144 * A value to be passed to the `SelectProvider` component.
145 */
146 contextValue: SelectProviderValue<Value>;
147 /**
148 * The value of the highlighted option.
149 */
150 highlightedOption: Value | null;
151 /**
152 * Ref to the listbox slot DOM node.
153 */
154 listboxRef: React.RefCallback<Element> | null;
155 /**
156 * If `true`, the listbox is open.
157 */
158 open: boolean;
159 /**
160 * Values of all the registered options.
161 */
162 options: Value[];
163 /**
164 * The value of the selected option(s).
165 */
166 value: SelectValue<Value, Multiple>;
167}
168export declare const SelectActionTypes: {
169 readonly buttonClick: "buttonClick";
170};
171export interface ButtonClickAction {
172 type: typeof SelectActionTypes.buttonClick;
173 event: React.MouseEvent;
174}
175export type SelectAction = ButtonClickAction;
176export interface SelectInternalState<OptionValue> extends ListState<OptionValue> {
177 open: boolean;
178}
179export {};