UNPKG

12.2 kBTypeScriptView Raw
1import * as React from 'react';
2import { SxProps } from '@mui/system';
3import { OverridableStringUnion } from '@mui/types';
4import { IconButtonProps, InternalStandardProps as StandardProps, Theme } from '@mui/material';
5import { ChipProps, ChipTypeMap } from '@mui/material/Chip';
6import { PaperProps } from '@mui/material/Paper';
7import { PopperProps } from '@mui/material/Popper';
8import useAutocomplete, {
9 AutocompleteChangeDetails,
10 AutocompleteChangeReason,
11 AutocompleteCloseReason,
12 AutocompleteInputChangeReason,
13 AutocompleteValue,
14 createFilterOptions,
15 UseAutocompleteProps,
16 AutocompleteFreeSoloValueMapping,
17} from '../useAutocomplete';
18import { AutocompleteClasses } from './autocompleteClasses';
19import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
20
21export interface AutocompletePaperSlotPropsOverrides {}
22export interface AutocompletePopperSlotPropsOverrides {}
23
24export {
25 AutocompleteChangeDetails,
26 AutocompleteChangeReason,
27 AutocompleteCloseReason,
28 AutocompleteInputChangeReason,
29 AutocompleteValue,
30 createFilterOptions,
31};
32
33export type AutocompleteOwnerState<
34 Value,
35 Multiple extends boolean | undefined,
36 DisableClearable extends boolean | undefined,
37 FreeSolo extends boolean | undefined,
38 ChipComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
39> = AutocompleteProps<Value, Multiple, DisableClearable, FreeSolo, ChipComponent> & {
40 disablePortal: boolean;
41 expanded: boolean;
42 focused: boolean;
43 fullWidth: boolean;
44 getOptionLabel: (option: Value | AutocompleteFreeSoloValueMapping<FreeSolo>) => string;
45 hasClearIcon: boolean;
46 hasPopupIcon: boolean;
47 inputFocused: boolean;
48 popupOpen: boolean;
49 size: OverridableStringUnion<'small' | 'medium', AutocompletePropsSizeOverrides>;
50};
51
52export type AutocompleteRenderGetTagProps = ({ index }: { index: number }) => {
53 key: number;
54 className: string;
55 disabled: boolean;
56 'data-tag-index': number;
57 tabIndex: -1;
58 onDelete: (event: any) => void;
59};
60
61export interface AutocompleteRenderOptionState {
62 inputValue: string;
63 index: number;
64 selected: boolean;
65}
66
67export interface AutocompleteRenderGroupParams {
68 key: string;
69 group: string;
70 children?: React.ReactNode;
71}
72
73export interface AutocompleteRenderInputParams {
74 id: string;
75 disabled: boolean;
76 fullWidth: boolean;
77 size: 'small' | undefined;
78 InputLabelProps: ReturnType<ReturnType<typeof useAutocomplete>['getInputLabelProps']>;
79 InputProps: {
80 ref: React.Ref<any>;
81 className: string;
82 startAdornment: React.ReactNode;
83 endAdornment: React.ReactNode;
84 onMouseDown: React.MouseEventHandler;
85 };
86 inputProps: ReturnType<ReturnType<typeof useAutocomplete>['getInputProps']>;
87}
88
89export interface AutocompletePropsSizeOverrides {}
90
91export interface AutocompleteSlots {
92 /**
93 * The component used to render the listbox.
94 * @default 'ul'
95 */
96 listbox: React.JSXElementConstructor<React.HTMLAttributes<HTMLElement>>;
97 /**
98 * The component used to render the body of the popup.
99 * @default Paper
100 */
101 paper: React.JSXElementConstructor<PaperProps & AutocompletePaperSlotPropsOverrides>;
102 /**
103 * The component used to position the popup.
104 * @default Popper
105 */
106 popper: React.JSXElementConstructor<PopperProps & AutocompletePopperSlotPropsOverrides>;
107}
108
109export type AutocompleteSlotsAndSlotProps<
110 Value,
111 Multiple extends boolean | undefined,
112 DisableClearable extends boolean | undefined,
113 FreeSolo extends boolean | undefined,
114 ChipComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
115> = CreateSlotsAndSlotProps<
116 AutocompleteSlots,
117 {
118 chip: SlotProps<
119 React.ElementType<Partial<ChipProps<ChipComponent>>>,
120 {},
121 AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
122 >;
123 clearIndicator: SlotProps<
124 React.ElementType<Partial<IconButtonProps>>,
125 {},
126 AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
127 >;
128 /**
129 * Props applied to the Listbox element.
130 */
131 listbox: SlotProps<
132 React.ElementType<
133 ReturnType<ReturnType<typeof useAutocomplete>['getListboxProps']> & {
134 sx?: SxProps<Theme>;
135 ref?: React.Ref<Element>;
136 }
137 >,
138 {},
139 AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
140 >;
141 paper: SlotProps<
142 React.ElementType<Partial<PaperProps>>,
143 AutocompletePaperSlotPropsOverrides,
144 AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
145 >;
146 popper: SlotProps<
147 React.ElementType<Partial<PopperProps>>,
148 AutocompletePopperSlotPropsOverrides,
149 AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
150 >;
151 popupIndicator: SlotProps<
152 React.ElementType<Partial<IconButtonProps>>,
153 {},
154 AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
155 >;
156 }
157>;
158
159export interface AutocompleteProps<
160 Value,
161 Multiple extends boolean | undefined,
162 DisableClearable extends boolean | undefined,
163 FreeSolo extends boolean | undefined,
164 ChipComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
165> extends UseAutocompleteProps<Value, Multiple, DisableClearable, FreeSolo>,
166 StandardProps<React.HTMLAttributes<HTMLDivElement>, 'defaultValue' | 'onChange' | 'children'>,
167 AutocompleteSlotsAndSlotProps<Value, Multiple, DisableClearable, FreeSolo, ChipComponent> {
168 /**
169 * Props applied to the [`Chip`](https://mui.com/material-ui/api/chip/) element.
170 */
171 ChipProps?: ChipProps<ChipComponent>;
172 /**
173 * Override or extend the styles applied to the component.
174 */
175 classes?: Partial<AutocompleteClasses>;
176 /**
177 * The icon to display in place of the default clear icon.
178 * @default <ClearIcon fontSize="small" />
179 */
180 clearIcon?: React.ReactNode;
181 /**
182 * Override the default text for the *clear* icon button.
183 *
184 * For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
185 * @default 'Clear'
186 */
187 clearText?: string;
188 /**
189 * Override the default text for the *close popup* icon button.
190 *
191 * For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
192 * @default 'Close'
193 */
194 closeText?: string;
195 /**
196 * The props used for each slot inside.
197 * @deprecated Use the `slotProps` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
198 */
199 componentsProps?: {
200 clearIndicator?: Partial<IconButtonProps>;
201 paper?: PaperProps;
202 popper?: Partial<PopperProps>;
203 popupIndicator?: Partial<IconButtonProps>;
204 };
205 /**
206 * If `true`, the component is disabled.
207 * @default false
208 */
209 disabled?: boolean;
210 /**
211 * If `true`, the `Popper` content will be under the DOM hierarchy of the parent component.
212 * @default false
213 */
214 disablePortal?: boolean;
215 /**
216 * Force the visibility display of the popup icon.
217 * @default 'auto'
218 */
219 forcePopupIcon?: true | false | 'auto';
220 /**
221 * If `true`, the input will take up the full width of its container.
222 * @default false
223 */
224 fullWidth?: boolean;
225 /**
226 * The label to display when the tags are truncated (`limitTags`).
227 *
228 * @param {number} more The number of truncated tags.
229 * @returns {ReactNode}
230 * @default (more) => `+${more}`
231 */
232 getLimitTagsText?: (more: number) => React.ReactNode;
233 /**
234 * The component used to render the listbox.
235 * @default 'ul'
236 */
237 ListboxComponent?: React.JSXElementConstructor<React.HTMLAttributes<HTMLElement>>;
238 /**
239 * Props applied to the Listbox element.
240 */
241 ListboxProps?: ReturnType<ReturnType<typeof useAutocomplete>['getListboxProps']> & {
242 sx?: SxProps<Theme>;
243 ref?: React.Ref<Element>;
244 };
245 /**
246 * If `true`, the component is in a loading state.
247 * This shows the `loadingText` in place of suggestions (only if there are no suggestions to show, for example `options` are empty).
248 * @default false
249 */
250 loading?: boolean;
251 /**
252 * Text to display when in a loading state.
253 *
254 * For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
255 * @default 'Loading…'
256 */
257 loadingText?: React.ReactNode;
258 /**
259 * The maximum number of tags that will be visible when not focused.
260 * Set `-1` to disable the limit.
261 * @default -1
262 */
263 limitTags?: number;
264 /**
265 * Text to display when there are no options.
266 *
267 * For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
268 * @default 'No options'
269 */
270 noOptionsText?: React.ReactNode;
271 onKeyDown?: (
272 event: React.KeyboardEvent<HTMLDivElement> & { defaultMuiPrevented?: boolean },
273 ) => void;
274 /**
275 * Override the default text for the *open popup* icon button.
276 *
277 * For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
278 * @default 'Open'
279 */
280 openText?: string;
281 /**
282 * The component used to render the body of the popup.
283 * @default Paper
284 */
285 PaperComponent?: React.JSXElementConstructor<React.HTMLAttributes<HTMLElement>>;
286 /**
287 * The component used to position the popup.
288 * @default Popper
289 */
290 PopperComponent?: React.JSXElementConstructor<PopperProps>;
291 /**
292 * The icon to display in place of the default popup icon.
293 * @default <ArrowDropDownIcon />
294 */
295 popupIcon?: React.ReactNode;
296 /**
297 * If `true`, the component becomes readonly. It is also supported for multiple tags where the tag cannot be deleted.
298 * @default false
299 */
300 readOnly?: boolean;
301 /**
302 * Render the group.
303 *
304 * @param {AutocompleteRenderGroupParams} params The group to render.
305 * @returns {ReactNode}
306 */
307 renderGroup?: (params: AutocompleteRenderGroupParams) => React.ReactNode;
308 /**
309 * Render the input.
310 *
311 * @param {object} params
312 * @returns {ReactNode}
313 */
314 renderInput: (params: AutocompleteRenderInputParams) => React.ReactNode;
315 /**
316 * Render the option, use `getOptionLabel` by default.
317 *
318 * @param {object} props The props to apply on the li element.
319 * @param {Value} option The option to render.
320 * @param {object} state The state of each option.
321 * @param {object} ownerState The state of the Autocomplete component.
322 * @returns {ReactNode}
323 */
324 renderOption?: (
325 props: React.HTMLAttributes<HTMLLIElement> & { key: any },
326 option: Value,
327 state: AutocompleteRenderOptionState,
328 ownerState: AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>,
329 ) => React.ReactNode;
330 /**
331 * Render the selected value.
332 *
333 * @param {Value[]} value The `value` provided to the component.
334 * @param {function} getTagProps A tag props getter.
335 * @param {object} ownerState The state of the Autocomplete component.
336 * @returns {ReactNode}
337 */
338 renderTags?: (
339 value: Value[],
340 getTagProps: AutocompleteRenderGetTagProps,
341 ownerState: AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>,
342 ) => React.ReactNode;
343 /**
344 * The size of the component.
345 * @default 'medium'
346 */
347 size?: OverridableStringUnion<'small' | 'medium', AutocompletePropsSizeOverrides>;
348 /**
349 * The system prop that allows defining system overrides as well as additional CSS styles.
350 */
351 sx?: SxProps<Theme>;
352}
353
354/**
355 *
356 * Demos:
357 *
358 * - [Autocomplete](https://mui.com/material-ui/react-autocomplete/)
359 *
360 * API:
361 *
362 * - [Autocomplete API](https://mui.com/material-ui/api/autocomplete/)
363 */
364export default function Autocomplete<
365 Value,
366 Multiple extends boolean | undefined = false,
367 DisableClearable extends boolean | undefined = false,
368 FreeSolo extends boolean | undefined = false,
369 ChipComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
370>(
371 props: AutocompleteProps<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>,
372): React.JSX.Element;