-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Autocomplete.js
1248 lines (1204 loc) · 37.8 KB
/
Autocomplete.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import integerPropType from '@mui/utils/integerPropType';
import chainPropTypes from '@mui/utils/chainPropTypes';
import composeClasses from '@mui/utils/composeClasses';
import { alpha } from '@mui/system/colorManipulator';
import useAutocomplete, { createFilterOptions } from '../useAutocomplete';
import Popper from '../Popper';
import ListSubheader from '../ListSubheader';
import Paper from '../Paper';
import IconButton from '../IconButton';
import Chip from '../Chip';
import inputClasses from '../Input/inputClasses';
import inputBaseClasses from '../InputBase/inputBaseClasses';
import outlinedInputClasses from '../OutlinedInput/outlinedInputClasses';
import filledInputClasses from '../FilledInput/filledInputClasses';
import ClearIcon from '../internal/svg-icons/Close';
import ArrowDropDownIcon from '../internal/svg-icons/ArrowDropDown';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import autocompleteClasses, { getAutocompleteUtilityClass } from './autocompleteClasses';
import capitalize from '../utils/capitalize';
import useSlot from '../utils/useSlot';
const useUtilityClasses = (ownerState) => {
const {
classes,
disablePortal,
expanded,
focused,
fullWidth,
hasClearIcon,
hasPopupIcon,
inputFocused,
popupOpen,
size,
} = ownerState;
const slots = {
root: [
'root',
expanded && 'expanded',
focused && 'focused',
fullWidth && 'fullWidth',
hasClearIcon && 'hasClearIcon',
hasPopupIcon && 'hasPopupIcon',
],
inputRoot: ['inputRoot'],
input: ['input', inputFocused && 'inputFocused'],
tag: ['tag', `tagSize${capitalize(size)}`],
endAdornment: ['endAdornment'],
clearIndicator: ['clearIndicator'],
popupIndicator: ['popupIndicator', popupOpen && 'popupIndicatorOpen'],
popper: ['popper', disablePortal && 'popperDisablePortal'],
paper: ['paper'],
listbox: ['listbox'],
loading: ['loading'],
noOptions: ['noOptions'],
option: ['option'],
groupLabel: ['groupLabel'],
groupUl: ['groupUl'],
};
return composeClasses(slots, getAutocompleteUtilityClass, classes);
};
const AutocompleteRoot = styled('div', {
name: 'MuiAutocomplete',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
const { fullWidth, hasClearIcon, hasPopupIcon, inputFocused, size } = ownerState;
return [
{ [`& .${autocompleteClasses.tag}`]: styles.tag },
{ [`& .${autocompleteClasses.tag}`]: styles[`tagSize${capitalize(size)}`] },
{ [`& .${autocompleteClasses.inputRoot}`]: styles.inputRoot },
{ [`& .${autocompleteClasses.input}`]: styles.input },
{ [`& .${autocompleteClasses.input}`]: inputFocused && styles.inputFocused },
styles.root,
fullWidth && styles.fullWidth,
hasPopupIcon && styles.hasPopupIcon,
hasClearIcon && styles.hasClearIcon,
];
},
})({
[`&.${autocompleteClasses.focused} .${autocompleteClasses.clearIndicator}`]: {
visibility: 'visible',
},
/* Avoid double tap issue on iOS */
'@media (pointer: fine)': {
[`&:hover .${autocompleteClasses.clearIndicator}`]: {
visibility: 'visible',
},
},
[`& .${autocompleteClasses.tag}`]: {
margin: 3,
maxWidth: 'calc(100% - 6px)',
},
[`& .${autocompleteClasses.inputRoot}`]: {
[`.${autocompleteClasses.hasPopupIcon}&, .${autocompleteClasses.hasClearIcon}&`]: {
paddingRight: 26 + 4,
},
[`.${autocompleteClasses.hasPopupIcon}.${autocompleteClasses.hasClearIcon}&`]: {
paddingRight: 52 + 4,
},
[`& .${autocompleteClasses.input}`]: {
width: 0,
minWidth: 30,
},
},
[`& .${inputClasses.root}`]: {
paddingBottom: 1,
'& .MuiInput-input': {
padding: '4px 4px 4px 0px',
},
},
[`& .${inputClasses.root}.${inputBaseClasses.sizeSmall}`]: {
[`& .${inputClasses.input}`]: {
padding: '2px 4px 3px 0',
},
},
[`& .${outlinedInputClasses.root}`]: {
padding: 9,
[`.${autocompleteClasses.hasPopupIcon}&, .${autocompleteClasses.hasClearIcon}&`]: {
paddingRight: 26 + 4 + 9,
},
[`.${autocompleteClasses.hasPopupIcon}.${autocompleteClasses.hasClearIcon}&`]: {
paddingRight: 52 + 4 + 9,
},
[`& .${autocompleteClasses.input}`]: {
padding: '7.5px 4px 7.5px 5px',
},
[`& .${autocompleteClasses.endAdornment}`]: {
right: 9,
},
},
[`& .${outlinedInputClasses.root}.${inputBaseClasses.sizeSmall}`]: {
// Don't specify paddingRight, as it overrides the default value set when there is only
// one of the popup or clear icon as the specificity is equal so the latter one wins
paddingTop: 6,
paddingBottom: 6,
paddingLeft: 6,
[`& .${autocompleteClasses.input}`]: {
padding: '2.5px 4px 2.5px 8px',
},
},
[`& .${filledInputClasses.root}`]: {
paddingTop: 19,
paddingLeft: 8,
[`.${autocompleteClasses.hasPopupIcon}&, .${autocompleteClasses.hasClearIcon}&`]: {
paddingRight: 26 + 4 + 9,
},
[`.${autocompleteClasses.hasPopupIcon}.${autocompleteClasses.hasClearIcon}&`]: {
paddingRight: 52 + 4 + 9,
},
[`& .${filledInputClasses.input}`]: {
padding: '7px 4px',
},
[`& .${autocompleteClasses.endAdornment}`]: {
right: 9,
},
},
[`& .${filledInputClasses.root}.${inputBaseClasses.sizeSmall}`]: {
paddingBottom: 1,
[`& .${filledInputClasses.input}`]: {
padding: '2.5px 4px',
},
},
[`& .${inputBaseClasses.hiddenLabel}`]: {
paddingTop: 8,
},
[`& .${filledInputClasses.root}.${inputBaseClasses.hiddenLabel}`]: {
paddingTop: 0,
paddingBottom: 0,
[`& .${autocompleteClasses.input}`]: {
paddingTop: 16,
paddingBottom: 17,
},
},
[`& .${filledInputClasses.root}.${inputBaseClasses.hiddenLabel}.${inputBaseClasses.sizeSmall}`]: {
[`& .${autocompleteClasses.input}`]: {
paddingTop: 8,
paddingBottom: 9,
},
},
[`& .${autocompleteClasses.input}`]: {
flexGrow: 1,
textOverflow: 'ellipsis',
opacity: 0,
},
variants: [
{
props: { fullWidth: true },
style: { width: '100%' },
},
{
props: { size: 'small' },
style: {
[`& .${autocompleteClasses.tag}`]: {
margin: 2,
maxWidth: 'calc(100% - 4px)',
},
},
},
{
props: { inputFocused: true },
style: {
[`& .${autocompleteClasses.input}`]: {
opacity: 1,
},
},
},
{
props: { multiple: true },
style: {
[`& .${autocompleteClasses.inputRoot}`]: {
flexWrap: 'wrap',
},
},
},
],
});
const AutocompleteEndAdornment = styled('div', {
name: 'MuiAutocomplete',
slot: 'EndAdornment',
overridesResolver: (props, styles) => styles.endAdornment,
})({
// We use a position absolute to support wrapping tags.
position: 'absolute',
right: 0,
top: '50%',
transform: 'translate(0, -50%)',
});
const AutocompleteClearIndicator = styled(IconButton, {
name: 'MuiAutocomplete',
slot: 'ClearIndicator',
overridesResolver: (props, styles) => styles.clearIndicator,
})({
marginRight: -2,
padding: 4,
visibility: 'hidden',
});
const AutocompletePopupIndicator = styled(IconButton, {
name: 'MuiAutocomplete',
slot: 'PopupIndicator',
overridesResolver: ({ ownerState }, styles) => ({
...styles.popupIndicator,
...(ownerState.popupOpen && styles.popupIndicatorOpen),
}),
})({
padding: 2,
marginRight: -2,
variants: [
{
props: { popupOpen: true },
style: {
transform: 'rotate(180deg)',
},
},
],
});
const AutocompletePopper = styled(Popper, {
name: 'MuiAutocomplete',
slot: 'Popper',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
{ [`& .${autocompleteClasses.option}`]: styles.option },
styles.popper,
ownerState.disablePortal && styles.popperDisablePortal,
];
},
})(
memoTheme(({ theme }) => ({
zIndex: (theme.vars || theme).zIndex.modal,
variants: [
{
props: { disablePortal: true },
style: {
position: 'absolute',
},
},
],
})),
);
const AutocompletePaper = styled(Paper, {
name: 'MuiAutocomplete',
slot: 'Paper',
overridesResolver: (props, styles) => styles.paper,
})(
memoTheme(({ theme }) => ({
...theme.typography.body1,
overflow: 'auto',
})),
);
const AutocompleteLoading = styled('div', {
name: 'MuiAutocomplete',
slot: 'Loading',
overridesResolver: (props, styles) => styles.loading,
})(
memoTheme(({ theme }) => ({
color: (theme.vars || theme).palette.text.secondary,
padding: '14px 16px',
})),
);
const AutocompleteNoOptions = styled('div', {
name: 'MuiAutocomplete',
slot: 'NoOptions',
overridesResolver: (props, styles) => styles.noOptions,
})(
memoTheme(({ theme }) => ({
color: (theme.vars || theme).palette.text.secondary,
padding: '14px 16px',
})),
);
const AutocompleteListbox = styled('div', {
name: 'MuiAutocomplete',
slot: 'Listbox',
overridesResolver: (props, styles) => styles.listbox,
})(
memoTheme(({ theme }) => ({
listStyle: 'none',
margin: 0,
padding: '8px 0',
maxHeight: '40vh',
overflow: 'auto',
position: 'relative',
[`& .${autocompleteClasses.option}`]: {
minHeight: 48,
display: 'flex',
overflow: 'hidden',
justifyContent: 'flex-start',
alignItems: 'center',
cursor: 'pointer',
paddingTop: 6,
boxSizing: 'border-box',
outline: '0',
WebkitTapHighlightColor: 'transparent',
paddingBottom: 6,
paddingLeft: 16,
paddingRight: 16,
[theme.breakpoints.up('sm')]: {
minHeight: 'auto',
},
[`&.${autocompleteClasses.focused}`]: {
backgroundColor: (theme.vars || theme).palette.action.hover,
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
backgroundColor: 'transparent',
},
},
'&[aria-disabled="true"]': {
opacity: (theme.vars || theme).palette.action.disabledOpacity,
pointerEvents: 'none',
},
[`&.${autocompleteClasses.focusVisible}`]: {
backgroundColor: (theme.vars || theme).palette.action.focus,
},
'&[aria-selected="true"]': {
backgroundColor: theme.vars
? `rgba(${theme.vars.palette.primary.mainChannel} / ${theme.vars.palette.action.selectedOpacity})`
: alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity),
[`&.${autocompleteClasses.focused}`]: {
backgroundColor: theme.vars
? `rgba(${theme.vars.palette.primary.mainChannel} / calc(${theme.vars.palette.action.selectedOpacity} + ${theme.vars.palette.action.hoverOpacity}))`
: alpha(
theme.palette.primary.main,
theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity,
),
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
backgroundColor: (theme.vars || theme).palette.action.selected,
},
},
[`&.${autocompleteClasses.focusVisible}`]: {
backgroundColor: theme.vars
? `rgba(${theme.vars.palette.primary.mainChannel} / calc(${theme.vars.palette.action.selectedOpacity} + ${theme.vars.palette.action.focusOpacity}))`
: alpha(
theme.palette.primary.main,
theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity,
),
},
},
},
})),
);
const AutocompleteGroupLabel = styled(ListSubheader, {
name: 'MuiAutocomplete',
slot: 'GroupLabel',
overridesResolver: (props, styles) => styles.groupLabel,
})(
memoTheme(({ theme }) => ({
backgroundColor: (theme.vars || theme).palette.background.paper,
top: -8,
})),
);
const AutocompleteGroupUl = styled('ul', {
name: 'MuiAutocomplete',
slot: 'GroupUl',
overridesResolver: (props, styles) => styles.groupUl,
})({
padding: 0,
[`& .${autocompleteClasses.option}`]: {
paddingLeft: 24,
},
});
export { createFilterOptions };
const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiAutocomplete' });
/* eslint-disable @typescript-eslint/no-unused-vars */
const {
autoComplete = false,
autoHighlight = false,
autoSelect = false,
blurOnSelect = false,
ChipProps: ChipPropsProp,
className,
clearIcon = <ClearIcon fontSize="small" />,
clearOnBlur = !props.freeSolo,
clearOnEscape = false,
clearText = 'Clear',
closeText = 'Close',
componentsProps,
defaultValue = props.multiple ? [] : null,
disableClearable = false,
disableCloseOnSelect = false,
disabled = false,
disabledItemsFocusable = false,
disableListWrap = false,
disablePortal = false,
filterOptions,
filterSelectedOptions = false,
forcePopupIcon = 'auto',
freeSolo = false,
fullWidth = false,
getLimitTagsText = (more) => `+${more}`,
getOptionDisabled,
getOptionKey,
getOptionLabel: getOptionLabelProp,
isOptionEqualToValue,
groupBy,
handleHomeEndKeys = !props.freeSolo,
id: idProp,
includeInputInList = false,
inputValue: inputValueProp,
limitTags = -1,
ListboxComponent: ListboxComponentProp,
ListboxProps: ListboxPropsProp,
loading = false,
loadingText = 'Loading…',
multiple = false,
noOptionsText = 'No options',
onChange,
onClose,
onHighlightChange,
onInputChange,
onOpen,
open,
openOnFocus = false,
openText = 'Open',
options,
PaperComponent: PaperComponentProp,
PopperComponent: PopperComponentProp,
popupIcon = <ArrowDropDownIcon />,
readOnly = false,
renderGroup: renderGroupProp,
renderInput,
renderOption: renderOptionProp,
renderTags,
selectOnFocus = !props.freeSolo,
size = 'medium',
slots = {},
slotProps = {},
value: valueProp,
...other
} = props;
/* eslint-enable @typescript-eslint/no-unused-vars */
const {
getRootProps,
getInputProps,
getInputLabelProps,
getPopupIndicatorProps,
getClearProps,
getTagProps,
getListboxProps,
getOptionProps,
value,
dirty,
expanded,
id,
popupOpen,
focused,
focusedTag,
anchorEl,
setAnchorEl,
inputValue,
groupedOptions,
} = useAutocomplete({ ...props, componentName: 'Autocomplete' });
const hasClearIcon = !disableClearable && !disabled && dirty && !readOnly;
const hasPopupIcon = (!freeSolo || forcePopupIcon === true) && forcePopupIcon !== false;
const { onMouseDown: handleInputMouseDown } = getInputProps();
const { ref: listboxRef, ...otherListboxProps } = getListboxProps();
const defaultGetOptionLabel = (option) => option.label ?? option;
const getOptionLabel = getOptionLabelProp || defaultGetOptionLabel;
// If you modify this, make sure to keep the `AutocompleteOwnerState` type in sync.
const ownerState = {
...props,
disablePortal,
expanded,
focused,
fullWidth,
getOptionLabel,
hasClearIcon,
hasPopupIcon,
inputFocused: focusedTag === -1,
popupOpen,
size,
};
const classes = useUtilityClasses(ownerState);
const externalForwardedProps = {
slots: {
paper: PaperComponentProp,
popper: PopperComponentProp,
...slots,
},
slotProps: {
chip: ChipPropsProp,
listbox: ListboxPropsProp,
...componentsProps,
...slotProps,
},
};
const [ListboxSlot, listboxProps] = useSlot('listbox', {
elementType: AutocompleteListbox,
externalForwardedProps,
ownerState,
className: classes.listbox,
additionalProps: otherListboxProps,
ref: listboxRef,
});
const [PaperSlot, paperProps] = useSlot('paper', {
elementType: Paper,
externalForwardedProps,
ownerState,
className: classes.paper,
});
const [PopperSlot, popperProps] = useSlot('popper', {
elementType: Popper,
externalForwardedProps,
ownerState,
className: classes.popper,
additionalProps: {
disablePortal,
style: { width: anchorEl ? anchorEl.clientWidth : null },
role: 'presentation',
anchorEl,
open: popupOpen,
},
});
let startAdornment;
if (multiple && value.length > 0) {
const getCustomizedTagProps = (params) => ({
className: classes.tag,
disabled,
...getTagProps(params),
});
if (renderTags) {
startAdornment = renderTags(value, getCustomizedTagProps, ownerState);
} else {
startAdornment = value.map((option, index) => {
const { key, ...customTagProps } = getCustomizedTagProps({ index });
return (
<Chip
key={key}
label={getOptionLabel(option)}
size={size}
{...customTagProps}
{...externalForwardedProps.slotProps.chip}
/>
);
});
}
}
if (limitTags > -1 && Array.isArray(startAdornment)) {
const more = startAdornment.length - limitTags;
if (!focused && more > 0) {
startAdornment = startAdornment.splice(0, limitTags);
startAdornment.push(
<span className={classes.tag} key={startAdornment.length}>
{getLimitTagsText(more)}
</span>,
);
}
}
const defaultRenderGroup = (params) => (
<li key={params.key}>
<AutocompleteGroupLabel
className={classes.groupLabel}
ownerState={ownerState}
component="div"
>
{params.group}
</AutocompleteGroupLabel>
<AutocompleteGroupUl className={classes.groupUl} ownerState={ownerState}>
{params.children}
</AutocompleteGroupUl>
</li>
);
const renderGroup = renderGroupProp || defaultRenderGroup;
const defaultRenderOption = (props2, option) => {
// Need to clearly apply key because of https://github.com/vercel/next.js/issues/55642
const { key, ...otherProps } = props2;
return (
<li key={key} {...otherProps}>
{getOptionLabel(option)}
</li>
);
};
const renderOption = renderOptionProp || defaultRenderOption;
const renderListOption = (option, index) => {
const optionProps = getOptionProps({ option, index });
return renderOption(
{ ...optionProps, className: classes.option },
option,
{
selected: optionProps['aria-selected'],
index,
inputValue,
},
ownerState,
);
};
const clearIndicatorSlotProps = externalForwardedProps.slotProps.clearIndicator;
const popupIndicatorSlotProps = externalForwardedProps.slotProps.popupIndicator;
const renderAutocompletePopperChildren = (children) => (
<AutocompletePopper as={PopperSlot} {...popperProps}>
<AutocompletePaper as={PaperSlot} {...paperProps}>
{children}
</AutocompletePaper>
</AutocompletePopper>
);
let autocompletePopper = null;
if (groupedOptions.length > 0) {
autocompletePopper = renderAutocompletePopperChildren(
// TODO v7: remove `as` prop and move ListboxComponentProp to externalForwardedProps or remove ListboxComponentProp entirely
// https://github.com/mui/material-ui/pull/43994#issuecomment-2401945800
<ListboxSlot as={ListboxComponentProp} {...listboxProps}>
{groupedOptions.map((option, index) => {
if (groupBy) {
return renderGroup({
key: option.key,
group: option.group,
children: option.options.map((option2, index2) =>
renderListOption(option2, option.index + index2),
),
});
}
return renderListOption(option, index);
})}
</ListboxSlot>,
);
} else if (loading && groupedOptions.length === 0) {
autocompletePopper = renderAutocompletePopperChildren(
<AutocompleteLoading className={classes.loading} ownerState={ownerState}>
{loadingText}
</AutocompleteLoading>,
);
} else if (groupedOptions.length === 0 && !freeSolo && !loading) {
autocompletePopper = renderAutocompletePopperChildren(
<AutocompleteNoOptions
className={classes.noOptions}
ownerState={ownerState}
role="presentation"
onMouseDown={(event) => {
// Prevent input blur when interacting with the "no options" content
event.preventDefault();
}}
>
{noOptionsText}
</AutocompleteNoOptions>,
);
}
return (
<React.Fragment>
<AutocompleteRoot
ref={ref}
className={clsx(classes.root, className)}
ownerState={ownerState}
{...getRootProps(other)}
>
{renderInput({
id,
disabled,
fullWidth: true,
size: size === 'small' ? 'small' : undefined,
InputLabelProps: getInputLabelProps(),
InputProps: {
ref: setAnchorEl,
className: classes.inputRoot,
startAdornment,
onMouseDown: (event) => {
if (event.target === event.currentTarget) {
handleInputMouseDown(event);
}
},
...((hasClearIcon || hasPopupIcon) && {
endAdornment: (
<AutocompleteEndAdornment className={classes.endAdornment} ownerState={ownerState}>
{hasClearIcon ? (
<AutocompleteClearIndicator
{...getClearProps()}
aria-label={clearText}
title={clearText}
ownerState={ownerState}
{...clearIndicatorSlotProps}
className={clsx(classes.clearIndicator, clearIndicatorSlotProps?.className)}
>
{clearIcon}
</AutocompleteClearIndicator>
) : null}
{hasPopupIcon ? (
<AutocompletePopupIndicator
{...getPopupIndicatorProps()}
disabled={disabled}
aria-label={popupOpen ? closeText : openText}
title={popupOpen ? closeText : openText}
ownerState={ownerState}
{...popupIndicatorSlotProps}
className={clsx(classes.popupIndicator, popupIndicatorSlotProps?.className)}
>
{popupIcon}
</AutocompletePopupIndicator>
) : null}
</AutocompleteEndAdornment>
),
}),
},
inputProps: {
className: classes.input,
disabled,
readOnly,
...getInputProps(),
},
})}
</AutocompleteRoot>
{anchorEl ? autocompletePopper : null}
</React.Fragment>
);
});
Autocomplete.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* If `true`, the portion of the selected suggestion that the user hasn't typed,
* known as the completion string, appears inline after the input cursor in the textbox.
* The inline completion string is visually highlighted and has a selected state.
* @default false
*/
autoComplete: PropTypes.bool,
/**
* If `true`, the first option is automatically highlighted.
* @default false
*/
autoHighlight: PropTypes.bool,
/**
* If `true`, the selected option becomes the value of the input
* when the Autocomplete loses focus unless the user chooses
* a different option or changes the character string in the input.
*
* When using the `freeSolo` mode, the typed value will be the input value
* if the Autocomplete loses focus without highlighting an option.
* @default false
*/
autoSelect: PropTypes.bool,
/**
* Control if the input should be blurred when an option is selected:
*
* - `false` the input is not blurred.
* - `true` the input is always blurred.
* - `touch` the input is blurred after a touch event.
* - `mouse` the input is blurred after a mouse event.
* @default false
*/
blurOnSelect: PropTypes.oneOfType([PropTypes.oneOf(['mouse', 'touch']), PropTypes.bool]),
/**
* Props applied to the [`Chip`](https://mui.com/material-ui/api/chip/) element.
*/
ChipProps: PropTypes.object,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The icon to display in place of the default clear icon.
* @default <ClearIcon fontSize="small" />
*/
clearIcon: PropTypes.node,
/**
* If `true`, the input's text is cleared on blur if no value is selected.
*
* Set it to `true` if you want to help the user enter a new value.
* Set it to `false` if you want to help the user resume their search.
* @default !props.freeSolo
*/
clearOnBlur: PropTypes.bool,
/**
* If `true`, clear all values when the user presses escape and the popup is closed.
* @default false
*/
clearOnEscape: PropTypes.bool,
/**
* Override the default text for the *clear* icon button.
*
* For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
* @default 'Clear'
*/
clearText: PropTypes.string,
/**
* Override the default text for the *close popup* icon button.
*
* For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
* @default 'Close'
*/
closeText: PropTypes.string,
/**
* The props used for each slot inside.
* @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.
*/
componentsProps: PropTypes.shape({
clearIndicator: PropTypes.object,
paper: PropTypes.object,
popper: PropTypes.object,
popupIndicator: PropTypes.object,
}),
/**
* The default value. Use when the component is not controlled.
* @default props.multiple ? [] : null
*/
defaultValue: chainPropTypes(PropTypes.any, (props) => {
if (props.multiple && props.defaultValue !== undefined && !Array.isArray(props.defaultValue)) {
return new Error(
[
'MUI: The Autocomplete expects the `defaultValue` prop to be an array when `multiple={true}` or undefined.',
`However, ${props.defaultValue} was provided.`,
].join('\n'),
);
}
return null;
}),
/**
* If `true`, the input can't be cleared.
* @default false
*/
disableClearable: PropTypes.bool,
/**
* If `true`, the popup won't close when a value is selected.
* @default false
*/
disableCloseOnSelect: PropTypes.bool,
/**
* If `true`, the component is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* If `true`, will allow focus on disabled items.
* @default false
*/
disabledItemsFocusable: PropTypes.bool,
/**
* If `true`, the list box in the popup will not wrap focus.
* @default false
*/
disableListWrap: PropTypes.bool,
/**
* If `true`, the `Popper` content will be under the DOM hierarchy of the parent component.
* @default false
*/
disablePortal: PropTypes.bool,
/**
* A function that determines the filtered options to be rendered on search.
*
* @default createFilterOptions()
* @param {Value[]} options The options to render.
* @param {object} state The state of the component.
* @returns {Value[]}
*/
filterOptions: PropTypes.func,
/**
* If `true`, hide the selected options from the list box.
* @default false
*/
filterSelectedOptions: PropTypes.bool,
/**
* Force the visibility display of the popup icon.
* @default 'auto'
*/
forcePopupIcon: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.bool]),
/**
* If `true`, the Autocomplete is free solo, meaning that the user input is not bound to provided options.
* @default false
*/
freeSolo: PropTypes.bool,
/**
* If `true`, the input will take up the full width of its container.
* @default false
*/
fullWidth: PropTypes.bool,
/**
* The label to display when the tags are truncated (`limitTags`).
*
* @param {number} more The number of truncated tags.
* @returns {ReactNode}
* @default (more) => `+${more}`
*/
getLimitTagsText: PropTypes.func,
/**
* Used to determine the disabled state for a given option.
*
* @param {Value} option The option to test.
* @returns {boolean}
*/
getOptionDisabled: PropTypes.func,
/**
* Used to determine the key for a given option.
* This can be useful when the labels of options are not unique (since labels are used as keys by default).
*
* @param {Value} option The option to get the key for.
* @returns {string | number}
*/
getOptionKey: PropTypes.func,
/**
* Used to determine the string value for a given option.
* It's used to fill the input (and the list box options if `renderOption` is not provided).
*
* If used in free solo mode, it must accept both the type of the options and a string.
*
* @param {Value} option
* @returns {string}
* @default (option) => option.label ?? option
*/
getOptionLabel: PropTypes.func,
/**
* If provided, the options will be grouped under the returned string.
* The groupBy value is also used as the text for group headings when `renderGroup` is not provided.
*
* @param {Value} options The options to group.
* @returns {string}
*/
groupBy: PropTypes.func,
/**
* If `true`, the component handles the "Home" and "End" keys when the popup is open.