@mui/material 6.0.0-beta.0 → 6.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/Autocomplete/Autocomplete.d.ts +6 -7
  2. package/Autocomplete/Autocomplete.js +1 -1
  3. package/Badge/Badge.d.ts +1 -1
  4. package/Badge/Badge.js +2 -2
  5. package/Badge/useBadge.d.ts +13 -0
  6. package/Badge/useBadge.js +41 -0
  7. package/Badge/useBadge.types.d.ts +40 -0
  8. package/Badge/useBadge.types.js +1 -0
  9. package/Breadcrumbs/Breadcrumbs.d.ts +1 -1
  10. package/Breadcrumbs/Breadcrumbs.js +1 -1
  11. package/CHANGELOG.md +48 -0
  12. package/ClickAwayListener/ClickAwayListener.d.ts +47 -0
  13. package/ClickAwayListener/ClickAwayListener.js +178 -0
  14. package/ClickAwayListener/index.d.ts +2 -2
  15. package/ClickAwayListener/index.js +1 -1
  16. package/InputBase/InputBase.js +2 -2
  17. package/ListItem/ListItem.js +1 -1
  18. package/Menu/Menu.js +1 -1
  19. package/Modal/Modal.d.ts +1 -1
  20. package/Modal/Modal.js +1 -1
  21. package/Modal/ModalManager.d.ts +25 -0
  22. package/Modal/ModalManager.js +211 -0
  23. package/Modal/index.d.ts +1 -1
  24. package/Modal/index.js +1 -1
  25. package/Modal/useModal.d.ts +13 -0
  26. package/Modal/useModal.js +195 -0
  27. package/Modal/useModal.types.d.ts +115 -0
  28. package/Modal/useModal.types.js +1 -0
  29. package/NoSsr/NoSsr.d.ts +25 -0
  30. package/NoSsr/NoSsr.js +73 -0
  31. package/NoSsr/NoSsr.types.d.ts +18 -0
  32. package/NoSsr/NoSsr.types.js +1 -0
  33. package/NoSsr/index.d.ts +3 -2
  34. package/NoSsr/index.js +1 -1
  35. package/Popover/Popover.js +1 -1
  36. package/Popper/BasePopper.d.ts +7 -0
  37. package/Popper/BasePopper.js +370 -0
  38. package/Popper/BasePopper.types.d.ts +130 -0
  39. package/Popper/BasePopper.types.js +1 -0
  40. package/Popper/Popper.d.ts +1 -1
  41. package/Popper/Popper.js +1 -1
  42. package/Popper/index.d.ts +2 -1
  43. package/Popper/index.js +2 -1
  44. package/Popper/popperClasses.d.ts +8 -0
  45. package/Popper/popperClasses.js +7 -0
  46. package/Portal/Portal.d.ts +16 -0
  47. package/Portal/Portal.js +92 -0
  48. package/Portal/Portal.types.d.ts +23 -0
  49. package/Portal/Portal.types.js +1 -0
  50. package/Portal/index.d.ts +3 -2
  51. package/Portal/index.js +1 -1
  52. package/Slider/Slider.d.ts +2 -2
  53. package/Slider/Slider.js +3 -2
  54. package/Slider/useSlider.d.ts +14 -0
  55. package/Slider/useSlider.js +647 -0
  56. package/Slider/useSlider.types.d.ts +228 -0
  57. package/Slider/useSlider.types.js +1 -0
  58. package/Snackbar/Snackbar.d.ts +1 -1
  59. package/Snackbar/Snackbar.js +3 -3
  60. package/Snackbar/useSnackbar.d.ts +14 -0
  61. package/Snackbar/useSnackbar.js +133 -0
  62. package/Snackbar/useSnackbar.types.d.ts +60 -0
  63. package/Snackbar/useSnackbar.types.js +1 -0
  64. package/SwipeableDrawer/SwipeableDrawer.js +1 -1
  65. package/TabScrollButton/TabScrollButton.d.ts +1 -1
  66. package/TabScrollButton/TabScrollButton.js +1 -1
  67. package/TablePagination/TablePagination.js +1 -1
  68. package/Tabs/Tabs.d.ts +1 -1
  69. package/Tabs/Tabs.js +1 -1
  70. package/TextareaAutosize/TextareaAutosize.d.ts +14 -0
  71. package/TextareaAutosize/TextareaAutosize.js +222 -0
  72. package/TextareaAutosize/TextareaAutosize.types.d.ts +13 -0
  73. package/TextareaAutosize/TextareaAutosize.types.js +1 -0
  74. package/TextareaAutosize/index.d.ts +3 -2
  75. package/TextareaAutosize/index.js +1 -1
  76. package/Tooltip/Tooltip.js +1 -1
  77. package/Unstable_TrapFocus/FocusTrap.d.ts +10 -0
  78. package/Unstable_TrapFocus/FocusTrap.js +330 -0
  79. package/Unstable_TrapFocus/FocusTrap.types.d.ts +51 -0
  80. package/Unstable_TrapFocus/FocusTrap.types.js +1 -0
  81. package/Unstable_TrapFocus/index.d.ts +2 -2
  82. package/Unstable_TrapFocus/index.js +1 -1
  83. package/index.d.ts +1 -1
  84. package/index.js +2 -2
  85. package/modern/Autocomplete/Autocomplete.js +1 -1
  86. package/modern/Badge/Badge.js +2 -2
  87. package/modern/Badge/useBadge.js +41 -0
  88. package/modern/Badge/useBadge.types.js +1 -0
  89. package/modern/Breadcrumbs/Breadcrumbs.js +1 -1
  90. package/modern/ClickAwayListener/ClickAwayListener.js +178 -0
  91. package/modern/ClickAwayListener/index.js +1 -1
  92. package/modern/InputBase/InputBase.js +2 -2
  93. package/modern/ListItem/ListItem.js +1 -1
  94. package/modern/Menu/Menu.js +1 -1
  95. package/modern/Modal/Modal.js +1 -1
  96. package/modern/Modal/ModalManager.js +211 -0
  97. package/modern/Modal/index.js +1 -1
  98. package/modern/Modal/useModal.js +195 -0
  99. package/modern/Modal/useModal.types.js +1 -0
  100. package/modern/NoSsr/NoSsr.js +73 -0
  101. package/modern/NoSsr/NoSsr.types.js +1 -0
  102. package/modern/NoSsr/index.js +1 -1
  103. package/modern/Popover/Popover.js +1 -1
  104. package/modern/Popper/BasePopper.js +370 -0
  105. package/modern/Popper/BasePopper.types.js +1 -0
  106. package/modern/Popper/Popper.js +1 -1
  107. package/modern/Popper/index.js +2 -1
  108. package/modern/Popper/popperClasses.js +7 -0
  109. package/modern/Portal/Portal.js +92 -0
  110. package/modern/Portal/Portal.types.js +1 -0
  111. package/modern/Portal/index.js +1 -1
  112. package/modern/Slider/Slider.js +3 -2
  113. package/modern/Slider/useSlider.js +647 -0
  114. package/modern/Slider/useSlider.types.js +1 -0
  115. package/modern/Snackbar/Snackbar.js +3 -3
  116. package/modern/Snackbar/useSnackbar.js +133 -0
  117. package/modern/Snackbar/useSnackbar.types.js +1 -0
  118. package/modern/SwipeableDrawer/SwipeableDrawer.js +1 -1
  119. package/modern/TabScrollButton/TabScrollButton.js +1 -1
  120. package/modern/TablePagination/TablePagination.js +1 -1
  121. package/modern/Tabs/Tabs.js +1 -1
  122. package/modern/TextareaAutosize/TextareaAutosize.js +222 -0
  123. package/modern/TextareaAutosize/TextareaAutosize.types.js +1 -0
  124. package/modern/TextareaAutosize/index.js +1 -1
  125. package/modern/Tooltip/Tooltip.js +1 -1
  126. package/modern/Unstable_TrapFocus/FocusTrap.js +330 -0
  127. package/modern/Unstable_TrapFocus/FocusTrap.types.js +1 -0
  128. package/modern/Unstable_TrapFocus/index.js +1 -1
  129. package/modern/index.js +2 -2
  130. package/modern/useAutocomplete/useAutocomplete.js +976 -2
  131. package/modern/utils/PolymorphicComponent.js +1 -0
  132. package/modern/utils/areArraysEqual.js +4 -0
  133. package/modern/utils/index.js +1 -1
  134. package/modern/utils/isHostComponent.js +7 -0
  135. package/modern/utils/omitEventHandlers.js +18 -0
  136. package/modern/utils/shouldSpreadAdditionalProps.js +1 -1
  137. package/modern/utils/useSlot.js +3 -1
  138. package/node/Autocomplete/Autocomplete.js +3 -3
  139. package/node/Badge/Badge.js +5 -5
  140. package/node/Badge/useBadge.js +46 -0
  141. package/node/Badge/useBadge.types.js +5 -0
  142. package/node/Breadcrumbs/Breadcrumbs.js +2 -2
  143. package/node/ClickAwayListener/ClickAwayListener.js +184 -0
  144. package/node/ClickAwayListener/index.js +1 -1
  145. package/node/InputBase/InputBase.js +7 -7
  146. package/node/ListItem/ListItem.js +3 -3
  147. package/node/Menu/Menu.js +3 -3
  148. package/node/Modal/Modal.js +2 -2
  149. package/node/Modal/ModalManager.js +219 -0
  150. package/node/Modal/index.js +2 -2
  151. package/node/Modal/useModal.js +205 -0
  152. package/node/Modal/useModal.types.js +5 -0
  153. package/node/NoSsr/NoSsr.js +81 -0
  154. package/node/NoSsr/NoSsr.types.js +5 -0
  155. package/node/NoSsr/index.js +3 -2
  156. package/node/Popover/Popover.js +2 -2
  157. package/node/Popper/BasePopper.js +379 -0
  158. package/node/Popper/BasePopper.types.js +5 -0
  159. package/node/Popper/Popper.js +2 -2
  160. package/node/Popper/index.js +14 -1
  161. package/node/Popper/popperClasses.js +15 -0
  162. package/node/Portal/Portal.js +100 -0
  163. package/node/Portal/Portal.types.js +5 -0
  164. package/node/Portal/index.js +3 -2
  165. package/node/Slider/Slider.js +14 -13
  166. package/node/Slider/useSlider.js +670 -0
  167. package/node/Slider/useSlider.types.js +5 -0
  168. package/node/Snackbar/Snackbar.js +6 -6
  169. package/node/Snackbar/useSnackbar.js +141 -0
  170. package/node/Snackbar/useSnackbar.types.js +5 -0
  171. package/node/SwipeableDrawer/SwipeableDrawer.js +2 -2
  172. package/node/TabScrollButton/TabScrollButton.js +3 -3
  173. package/node/TablePagination/TablePagination.js +2 -2
  174. package/node/Tabs/Tabs.js +3 -3
  175. package/node/TextareaAutosize/TextareaAutosize.js +230 -0
  176. package/node/TextareaAutosize/TextareaAutosize.types.js +5 -0
  177. package/node/TextareaAutosize/index.js +3 -2
  178. package/node/Tooltip/Tooltip.js +5 -5
  179. package/node/Unstable_TrapFocus/FocusTrap.js +339 -0
  180. package/node/Unstable_TrapFocus/FocusTrap.types.js +5 -0
  181. package/node/Unstable_TrapFocus/index.js +3 -2
  182. package/node/index.js +3 -3
  183. package/node/useAutocomplete/useAutocomplete.js +985 -18
  184. package/node/utils/PolymorphicComponent.js +5 -0
  185. package/node/utils/areArraysEqual.js +10 -0
  186. package/node/utils/index.js +2 -2
  187. package/node/utils/isHostComponent.js +13 -0
  188. package/node/utils/omitEventHandlers.js +24 -0
  189. package/node/utils/shouldSpreadAdditionalProps.js +3 -2
  190. package/node/utils/useSlot.js +6 -4
  191. package/package.json +8 -8
  192. package/styles/overrides.d.ts +1 -1
  193. package/useAutocomplete/useAutocomplete.d.ts +468 -2
  194. package/useAutocomplete/useAutocomplete.js +976 -2
  195. package/utils/PolymorphicComponent.d.ts +17 -0
  196. package/utils/PolymorphicComponent.js +1 -0
  197. package/utils/areArraysEqual.d.ts +3 -0
  198. package/utils/areArraysEqual.js +4 -0
  199. package/utils/index.d.ts +2 -1
  200. package/utils/index.js +1 -1
  201. package/utils/isHostComponent.d.ts +6 -0
  202. package/utils/isHostComponent.js +7 -0
  203. package/utils/omitEventHandlers.d.ts +9 -0
  204. package/utils/omitEventHandlers.js +18 -0
  205. package/utils/shouldSpreadAdditionalProps.js +1 -1
  206. package/utils/types.d.ts +2 -1
  207. package/utils/useSlot.js +3 -1
@@ -1,4 +1,978 @@
1
1
  'use client';
2
2
 
3
- export { useAutocomplete as default } from '@mui/base/useAutocomplete';
4
- export * from '@mui/base/useAutocomplete';
3
+ /* eslint-disable no-constant-condition */
4
+ import * as React from 'react';
5
+ import { unstable_setRef as setRef, unstable_useEventCallback as useEventCallback, unstable_useControlled as useControlled, unstable_useId as useId, usePreviousProps } from '@mui/utils';
6
+
7
+ // https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
8
+ function stripDiacritics(string) {
9
+ return string.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
10
+ }
11
+ export function createFilterOptions(config = {}) {
12
+ const {
13
+ ignoreAccents = true,
14
+ ignoreCase = true,
15
+ limit,
16
+ matchFrom = 'any',
17
+ stringify,
18
+ trim = false
19
+ } = config;
20
+ return (options, {
21
+ inputValue,
22
+ getOptionLabel
23
+ }) => {
24
+ let input = trim ? inputValue.trim() : inputValue;
25
+ if (ignoreCase) {
26
+ input = input.toLowerCase();
27
+ }
28
+ if (ignoreAccents) {
29
+ input = stripDiacritics(input);
30
+ }
31
+ const filteredOptions = !input ? options : options.filter(option => {
32
+ let candidate = (stringify || getOptionLabel)(option);
33
+ if (ignoreCase) {
34
+ candidate = candidate.toLowerCase();
35
+ }
36
+ if (ignoreAccents) {
37
+ candidate = stripDiacritics(candidate);
38
+ }
39
+ return matchFrom === 'start' ? candidate.indexOf(input) === 0 : candidate.indexOf(input) > -1;
40
+ });
41
+ return typeof limit === 'number' ? filteredOptions.slice(0, limit) : filteredOptions;
42
+ };
43
+ }
44
+ const defaultFilterOptions = createFilterOptions();
45
+
46
+ // Number of options to jump in list box when `Page Up` and `Page Down` keys are used.
47
+ const pageSize = 5;
48
+ const defaultIsActiveElementInListbox = listboxRef => listboxRef.current !== null && listboxRef.current.parentElement?.contains(document.activeElement);
49
+ function useAutocomplete(props) {
50
+ const {
51
+ // eslint-disable-next-line @typescript-eslint/naming-convention
52
+ unstable_isActiveElementInListbox = defaultIsActiveElementInListbox,
53
+ // eslint-disable-next-line @typescript-eslint/naming-convention
54
+ unstable_classNamePrefix = 'Mui',
55
+ autoComplete = false,
56
+ autoHighlight = false,
57
+ autoSelect = false,
58
+ blurOnSelect = false,
59
+ clearOnBlur = !props.freeSolo,
60
+ clearOnEscape = false,
61
+ componentName = 'useAutocomplete',
62
+ defaultValue = props.multiple ? [] : null,
63
+ disableClearable = false,
64
+ disableCloseOnSelect = false,
65
+ disabled: disabledProp,
66
+ disabledItemsFocusable = false,
67
+ disableListWrap = false,
68
+ filterOptions = defaultFilterOptions,
69
+ filterSelectedOptions = false,
70
+ freeSolo = false,
71
+ getOptionDisabled,
72
+ getOptionKey,
73
+ getOptionLabel: getOptionLabelProp = option => option.label ?? option,
74
+ groupBy,
75
+ handleHomeEndKeys = !props.freeSolo,
76
+ id: idProp,
77
+ includeInputInList = false,
78
+ inputValue: inputValueProp,
79
+ isOptionEqualToValue = (option, value) => option === value,
80
+ multiple = false,
81
+ onChange,
82
+ onClose,
83
+ onHighlightChange,
84
+ onInputChange,
85
+ onOpen,
86
+ open: openProp,
87
+ openOnFocus = false,
88
+ options,
89
+ readOnly = false,
90
+ selectOnFocus = !props.freeSolo,
91
+ value: valueProp
92
+ } = props;
93
+ const id = useId(idProp);
94
+ let getOptionLabel = getOptionLabelProp;
95
+ getOptionLabel = option => {
96
+ const optionLabel = getOptionLabelProp(option);
97
+ if (typeof optionLabel !== 'string') {
98
+ if (process.env.NODE_ENV !== 'production') {
99
+ const erroneousReturn = optionLabel === undefined ? 'undefined' : `${typeof optionLabel} (${optionLabel})`;
100
+ console.error(`MUI: The \`getOptionLabel\` method of ${componentName} returned ${erroneousReturn} instead of a string for ${JSON.stringify(option)}.`);
101
+ }
102
+ return String(optionLabel);
103
+ }
104
+ return optionLabel;
105
+ };
106
+ const ignoreFocus = React.useRef(false);
107
+ const firstFocus = React.useRef(true);
108
+ const inputRef = React.useRef(null);
109
+ const listboxRef = React.useRef(null);
110
+ const [anchorEl, setAnchorEl] = React.useState(null);
111
+ const [focusedTag, setFocusedTag] = React.useState(-1);
112
+ const defaultHighlighted = autoHighlight ? 0 : -1;
113
+ const highlightedIndexRef = React.useRef(defaultHighlighted);
114
+ const [value, setValueState] = useControlled({
115
+ controlled: valueProp,
116
+ default: defaultValue,
117
+ name: componentName
118
+ });
119
+ const [inputValue, setInputValueState] = useControlled({
120
+ controlled: inputValueProp,
121
+ default: '',
122
+ name: componentName,
123
+ state: 'inputValue'
124
+ });
125
+ const [focused, setFocused] = React.useState(false);
126
+ const resetInputValue = React.useCallback((event, newValue, reason) => {
127
+ // retain current `inputValue` if new option isn't selected and `clearOnBlur` is false
128
+ // When `multiple` is enabled, `newValue` is an array of all selected items including the newly selected item
129
+ const isOptionSelected = multiple ? value.length < newValue.length : newValue !== null;
130
+ if (!isOptionSelected && !clearOnBlur) {
131
+ return;
132
+ }
133
+ let newInputValue;
134
+ if (multiple) {
135
+ newInputValue = '';
136
+ } else if (newValue == null) {
137
+ newInputValue = '';
138
+ } else {
139
+ const optionLabel = getOptionLabel(newValue);
140
+ newInputValue = typeof optionLabel === 'string' ? optionLabel : '';
141
+ }
142
+ if (inputValue === newInputValue) {
143
+ return;
144
+ }
145
+ setInputValueState(newInputValue);
146
+ if (onInputChange) {
147
+ onInputChange(event, newInputValue, reason);
148
+ }
149
+ }, [getOptionLabel, inputValue, multiple, onInputChange, setInputValueState, clearOnBlur, value]);
150
+ const [open, setOpenState] = useControlled({
151
+ controlled: openProp,
152
+ default: false,
153
+ name: componentName,
154
+ state: 'open'
155
+ });
156
+ const [inputPristine, setInputPristine] = React.useState(true);
157
+ const inputValueIsSelectedValue = !multiple && value != null && inputValue === getOptionLabel(value);
158
+ const popupOpen = open && !readOnly;
159
+ const filteredOptions = popupOpen ? filterOptions(options.filter(option => {
160
+ if (filterSelectedOptions && (multiple ? value : [value]).some(value2 => value2 !== null && isOptionEqualToValue(option, value2))) {
161
+ return false;
162
+ }
163
+ return true;
164
+ }),
165
+ // we use the empty string to manipulate `filterOptions` to not filter any options
166
+ // i.e. the filter predicate always returns true
167
+ {
168
+ inputValue: inputValueIsSelectedValue && inputPristine ? '' : inputValue,
169
+ getOptionLabel
170
+ }) : [];
171
+ const previousProps = usePreviousProps({
172
+ filteredOptions,
173
+ value,
174
+ inputValue
175
+ });
176
+ React.useEffect(() => {
177
+ const valueChange = value !== previousProps.value;
178
+ if (focused && !valueChange) {
179
+ return;
180
+ }
181
+
182
+ // Only reset the input's value when freeSolo if the component's value changes.
183
+ if (freeSolo && !valueChange) {
184
+ return;
185
+ }
186
+ resetInputValue(null, value, 'reset');
187
+ }, [value, resetInputValue, focused, previousProps.value, freeSolo]);
188
+ const listboxAvailable = open && filteredOptions.length > 0 && !readOnly;
189
+ if (process.env.NODE_ENV !== 'production') {
190
+ if (value !== null && !freeSolo && options.length > 0) {
191
+ const missingValue = (multiple ? value : [value]).filter(value2 => !options.some(option => isOptionEqualToValue(option, value2)));
192
+ if (missingValue.length > 0) {
193
+ console.warn([`MUI: The value provided to ${componentName} is invalid.`, `None of the options match with \`${missingValue.length > 1 ? JSON.stringify(missingValue) : JSON.stringify(missingValue[0])}\`.`, 'You can use the `isOptionEqualToValue` prop to customize the equality test.'].join('\n'));
194
+ }
195
+ }
196
+ }
197
+ const focusTag = useEventCallback(tagToFocus => {
198
+ if (tagToFocus === -1) {
199
+ inputRef.current.focus();
200
+ } else {
201
+ anchorEl.querySelector(`[data-tag-index="${tagToFocus}"]`).focus();
202
+ }
203
+ });
204
+
205
+ // Ensure the focusedTag is never inconsistent
206
+ React.useEffect(() => {
207
+ if (multiple && focusedTag > value.length - 1) {
208
+ setFocusedTag(-1);
209
+ focusTag(-1);
210
+ }
211
+ }, [value, multiple, focusedTag, focusTag]);
212
+ function validOptionIndex(index, direction) {
213
+ if (!listboxRef.current || index < 0 || index >= filteredOptions.length) {
214
+ return -1;
215
+ }
216
+ let nextFocus = index;
217
+ while (true) {
218
+ const option = listboxRef.current.querySelector(`[data-option-index="${nextFocus}"]`);
219
+
220
+ // Same logic as MenuList.js
221
+ const nextFocusDisabled = disabledItemsFocusable ? false : !option || option.disabled || option.getAttribute('aria-disabled') === 'true';
222
+ if (option && option.hasAttribute('tabindex') && !nextFocusDisabled) {
223
+ // The next option is available
224
+ return nextFocus;
225
+ }
226
+
227
+ // The next option is disabled, move to the next element.
228
+ // with looped index
229
+ if (direction === 'next') {
230
+ nextFocus = (nextFocus + 1) % filteredOptions.length;
231
+ } else {
232
+ nextFocus = (nextFocus - 1 + filteredOptions.length) % filteredOptions.length;
233
+ }
234
+
235
+ // We end up with initial index, that means we don't have available options.
236
+ // All of them are disabled
237
+ if (nextFocus === index) {
238
+ return -1;
239
+ }
240
+ }
241
+ }
242
+ const setHighlightedIndex = useEventCallback(({
243
+ event,
244
+ index,
245
+ reason = 'auto'
246
+ }) => {
247
+ highlightedIndexRef.current = index;
248
+
249
+ // does the index exist?
250
+ if (index === -1) {
251
+ inputRef.current.removeAttribute('aria-activedescendant');
252
+ } else {
253
+ inputRef.current.setAttribute('aria-activedescendant', `${id}-option-${index}`);
254
+ }
255
+ if (onHighlightChange) {
256
+ onHighlightChange(event, index === -1 ? null : filteredOptions[index], reason);
257
+ }
258
+ if (!listboxRef.current) {
259
+ return;
260
+ }
261
+ const prev = listboxRef.current.querySelector(`[role="option"].${unstable_classNamePrefix}-focused`);
262
+ if (prev) {
263
+ prev.classList.remove(`${unstable_classNamePrefix}-focused`);
264
+ prev.classList.remove(`${unstable_classNamePrefix}-focusVisible`);
265
+ }
266
+ let listboxNode = listboxRef.current;
267
+ if (listboxRef.current.getAttribute('role') !== 'listbox') {
268
+ listboxNode = listboxRef.current.parentElement.querySelector('[role="listbox"]');
269
+ }
270
+
271
+ // "No results"
272
+ if (!listboxNode) {
273
+ return;
274
+ }
275
+ if (index === -1) {
276
+ listboxNode.scrollTop = 0;
277
+ return;
278
+ }
279
+ const option = listboxRef.current.querySelector(`[data-option-index="${index}"]`);
280
+ if (!option) {
281
+ return;
282
+ }
283
+ option.classList.add(`${unstable_classNamePrefix}-focused`);
284
+ if (reason === 'keyboard') {
285
+ option.classList.add(`${unstable_classNamePrefix}-focusVisible`);
286
+ }
287
+
288
+ // Scroll active descendant into view.
289
+ // Logic copied from https://www.w3.org/WAI/content-assets/wai-aria-practices/patterns/combobox/examples/js/select-only.js
290
+ // In case of mouse clicks and touch (in mobile devices) we avoid scrolling the element and keep both behaviors same.
291
+ // Consider this API instead once it has a better browser support:
292
+ // .scrollIntoView({ scrollMode: 'if-needed', block: 'nearest' });
293
+ if (listboxNode.scrollHeight > listboxNode.clientHeight && reason !== 'mouse' && reason !== 'touch') {
294
+ const element = option;
295
+ const scrollBottom = listboxNode.clientHeight + listboxNode.scrollTop;
296
+ const elementBottom = element.offsetTop + element.offsetHeight;
297
+ if (elementBottom > scrollBottom) {
298
+ listboxNode.scrollTop = elementBottom - listboxNode.clientHeight;
299
+ } else if (element.offsetTop - element.offsetHeight * (groupBy ? 1.3 : 0) < listboxNode.scrollTop) {
300
+ listboxNode.scrollTop = element.offsetTop - element.offsetHeight * (groupBy ? 1.3 : 0);
301
+ }
302
+ }
303
+ });
304
+ const changeHighlightedIndex = useEventCallback(({
305
+ event,
306
+ diff,
307
+ direction = 'next',
308
+ reason = 'auto'
309
+ }) => {
310
+ if (!popupOpen) {
311
+ return;
312
+ }
313
+ const getNextIndex = () => {
314
+ const maxIndex = filteredOptions.length - 1;
315
+ if (diff === 'reset') {
316
+ return defaultHighlighted;
317
+ }
318
+ if (diff === 'start') {
319
+ return 0;
320
+ }
321
+ if (diff === 'end') {
322
+ return maxIndex;
323
+ }
324
+ const newIndex = highlightedIndexRef.current + diff;
325
+ if (newIndex < 0) {
326
+ if (newIndex === -1 && includeInputInList) {
327
+ return -1;
328
+ }
329
+ if (disableListWrap && highlightedIndexRef.current !== -1 || Math.abs(diff) > 1) {
330
+ return 0;
331
+ }
332
+ return maxIndex;
333
+ }
334
+ if (newIndex > maxIndex) {
335
+ if (newIndex === maxIndex + 1 && includeInputInList) {
336
+ return -1;
337
+ }
338
+ if (disableListWrap || Math.abs(diff) > 1) {
339
+ return maxIndex;
340
+ }
341
+ return 0;
342
+ }
343
+ return newIndex;
344
+ };
345
+ const nextIndex = validOptionIndex(getNextIndex(), direction);
346
+ setHighlightedIndex({
347
+ index: nextIndex,
348
+ reason,
349
+ event
350
+ });
351
+
352
+ // Sync the content of the input with the highlighted option.
353
+ if (autoComplete && diff !== 'reset') {
354
+ if (nextIndex === -1) {
355
+ inputRef.current.value = inputValue;
356
+ } else {
357
+ const option = getOptionLabel(filteredOptions[nextIndex]);
358
+ inputRef.current.value = option;
359
+
360
+ // The portion of the selected suggestion that has not been typed by the user,
361
+ // a completion string, appears inline after the input cursor in the textbox.
362
+ const index = option.toLowerCase().indexOf(inputValue.toLowerCase());
363
+ if (index === 0 && inputValue.length > 0) {
364
+ inputRef.current.setSelectionRange(inputValue.length, option.length);
365
+ }
366
+ }
367
+ }
368
+ });
369
+ const getPreviousHighlightedOptionIndex = () => {
370
+ const isSameValue = (value1, value2) => {
371
+ const label1 = value1 ? getOptionLabel(value1) : '';
372
+ const label2 = value2 ? getOptionLabel(value2) : '';
373
+ return label1 === label2;
374
+ };
375
+ if (highlightedIndexRef.current !== -1 && previousProps.filteredOptions && previousProps.filteredOptions.length !== filteredOptions.length && previousProps.inputValue === inputValue && (multiple ? value.length === previousProps.value.length && previousProps.value.every((val, i) => getOptionLabel(value[i]) === getOptionLabel(val)) : isSameValue(previousProps.value, value))) {
376
+ const previousHighlightedOption = previousProps.filteredOptions[highlightedIndexRef.current];
377
+ if (previousHighlightedOption) {
378
+ return filteredOptions.findIndex(option => {
379
+ return getOptionLabel(option) === getOptionLabel(previousHighlightedOption);
380
+ });
381
+ }
382
+ }
383
+ return -1;
384
+ };
385
+ const syncHighlightedIndex = React.useCallback(() => {
386
+ if (!popupOpen) {
387
+ return;
388
+ }
389
+
390
+ // Check if the previously highlighted option still exists in the updated filtered options list and if the value and inputValue haven't changed
391
+ // If it exists and the value and the inputValue haven't changed, just update its index, otherwise continue execution
392
+ const previousHighlightedOptionIndex = getPreviousHighlightedOptionIndex();
393
+ if (previousHighlightedOptionIndex !== -1) {
394
+ highlightedIndexRef.current = previousHighlightedOptionIndex;
395
+ return;
396
+ }
397
+ const valueItem = multiple ? value[0] : value;
398
+
399
+ // The popup is empty, reset
400
+ if (filteredOptions.length === 0 || valueItem == null) {
401
+ changeHighlightedIndex({
402
+ diff: 'reset'
403
+ });
404
+ return;
405
+ }
406
+ if (!listboxRef.current) {
407
+ return;
408
+ }
409
+
410
+ // Synchronize the value with the highlighted index
411
+ if (valueItem != null) {
412
+ const currentOption = filteredOptions[highlightedIndexRef.current];
413
+
414
+ // Keep the current highlighted index if possible
415
+ if (multiple && currentOption && value.findIndex(val => isOptionEqualToValue(currentOption, val)) !== -1) {
416
+ return;
417
+ }
418
+ const itemIndex = filteredOptions.findIndex(optionItem => isOptionEqualToValue(optionItem, valueItem));
419
+ if (itemIndex === -1) {
420
+ changeHighlightedIndex({
421
+ diff: 'reset'
422
+ });
423
+ } else {
424
+ setHighlightedIndex({
425
+ index: itemIndex
426
+ });
427
+ }
428
+ return;
429
+ }
430
+
431
+ // Prevent the highlighted index to leak outside the boundaries.
432
+ if (highlightedIndexRef.current >= filteredOptions.length - 1) {
433
+ setHighlightedIndex({
434
+ index: filteredOptions.length - 1
435
+ });
436
+ return;
437
+ }
438
+
439
+ // Restore the focus to the previous index.
440
+ setHighlightedIndex({
441
+ index: highlightedIndexRef.current
442
+ });
443
+ // Ignore filteredOptions (and options, isOptionEqualToValue, getOptionLabel) not to break the scroll position
444
+ // eslint-disable-next-line react-hooks/exhaustive-deps
445
+ }, [
446
+ // Only sync the highlighted index when the option switch between empty and not
447
+ filteredOptions.length,
448
+ // Don't sync the highlighted index with the value when multiple
449
+ // eslint-disable-next-line react-hooks/exhaustive-deps
450
+ multiple ? false : value, filterSelectedOptions, changeHighlightedIndex, setHighlightedIndex, popupOpen, inputValue, multiple]);
451
+ const handleListboxRef = useEventCallback(node => {
452
+ setRef(listboxRef, node);
453
+ if (!node) {
454
+ return;
455
+ }
456
+ syncHighlightedIndex();
457
+ });
458
+ if (process.env.NODE_ENV !== 'production') {
459
+ // eslint-disable-next-line react-hooks/rules-of-hooks
460
+ React.useEffect(() => {
461
+ if (!inputRef.current || inputRef.current.nodeName !== 'INPUT') {
462
+ if (inputRef.current && inputRef.current.nodeName === 'TEXTAREA') {
463
+ console.warn([`A textarea element was provided to ${componentName} where input was expected.`, `This is not a supported scenario but it may work under certain conditions.`, `A textarea keyboard navigation may conflict with Autocomplete controls (for example enter and arrow keys).`, `Make sure to test keyboard navigation and add custom event handlers if necessary.`].join('\n'));
464
+ } else {
465
+ console.error([`MUI: Unable to find the input element. It was resolved to ${inputRef.current} while an HTMLInputElement was expected.`, `Instead, ${componentName} expects an input element.`, '', componentName === 'useAutocomplete' ? 'Make sure you have bound getInputProps correctly and that the normal ref/effect resolutions order is guaranteed.' : 'Make sure you have customized the input component correctly.'].join('\n'));
466
+ }
467
+ }
468
+ }, [componentName]);
469
+ }
470
+ React.useEffect(() => {
471
+ syncHighlightedIndex();
472
+ }, [syncHighlightedIndex]);
473
+ const handleOpen = event => {
474
+ if (open) {
475
+ return;
476
+ }
477
+ setOpenState(true);
478
+ setInputPristine(true);
479
+ if (onOpen) {
480
+ onOpen(event);
481
+ }
482
+ };
483
+ const handleClose = (event, reason) => {
484
+ if (!open) {
485
+ return;
486
+ }
487
+ setOpenState(false);
488
+ if (onClose) {
489
+ onClose(event, reason);
490
+ }
491
+ };
492
+ const handleValue = (event, newValue, reason, details) => {
493
+ if (multiple) {
494
+ if (value.length === newValue.length && value.every((val, i) => val === newValue[i])) {
495
+ return;
496
+ }
497
+ } else if (value === newValue) {
498
+ return;
499
+ }
500
+ if (onChange) {
501
+ onChange(event, newValue, reason, details);
502
+ }
503
+ setValueState(newValue);
504
+ };
505
+ const isTouch = React.useRef(false);
506
+ const selectNewValue = (event, option, reasonProp = 'selectOption', origin = 'options') => {
507
+ let reason = reasonProp;
508
+ let newValue = option;
509
+ if (multiple) {
510
+ newValue = Array.isArray(value) ? value.slice() : [];
511
+ if (process.env.NODE_ENV !== 'production') {
512
+ const matches = newValue.filter(val => isOptionEqualToValue(option, val));
513
+ if (matches.length > 1) {
514
+ console.error([`MUI: The \`isOptionEqualToValue\` method of ${componentName} does not handle the arguments correctly.`, `The component expects a single value to match a given option but found ${matches.length} matches.`].join('\n'));
515
+ }
516
+ }
517
+ const itemIndex = newValue.findIndex(valueItem => isOptionEqualToValue(option, valueItem));
518
+ if (itemIndex === -1) {
519
+ newValue.push(option);
520
+ } else if (origin !== 'freeSolo') {
521
+ newValue.splice(itemIndex, 1);
522
+ reason = 'removeOption';
523
+ }
524
+ }
525
+ resetInputValue(event, newValue, reason);
526
+ handleValue(event, newValue, reason, {
527
+ option
528
+ });
529
+ if (!disableCloseOnSelect && (!event || !event.ctrlKey && !event.metaKey)) {
530
+ handleClose(event, reason);
531
+ }
532
+ if (blurOnSelect === true || blurOnSelect === 'touch' && isTouch.current || blurOnSelect === 'mouse' && !isTouch.current) {
533
+ inputRef.current.blur();
534
+ }
535
+ };
536
+ function validTagIndex(index, direction) {
537
+ if (index === -1) {
538
+ return -1;
539
+ }
540
+ let nextFocus = index;
541
+ while (true) {
542
+ // Out of range
543
+ if (direction === 'next' && nextFocus === value.length || direction === 'previous' && nextFocus === -1) {
544
+ return -1;
545
+ }
546
+ const option = anchorEl.querySelector(`[data-tag-index="${nextFocus}"]`);
547
+
548
+ // Same logic as MenuList.js
549
+ if (!option || !option.hasAttribute('tabindex') || option.disabled || option.getAttribute('aria-disabled') === 'true') {
550
+ nextFocus += direction === 'next' ? 1 : -1;
551
+ } else {
552
+ return nextFocus;
553
+ }
554
+ }
555
+ }
556
+ const handleFocusTag = (event, direction) => {
557
+ if (!multiple) {
558
+ return;
559
+ }
560
+ if (inputValue === '') {
561
+ handleClose(event, 'toggleInput');
562
+ }
563
+ let nextTag = focusedTag;
564
+ if (focusedTag === -1) {
565
+ if (inputValue === '' && direction === 'previous') {
566
+ nextTag = value.length - 1;
567
+ }
568
+ } else {
569
+ nextTag += direction === 'next' ? 1 : -1;
570
+ if (nextTag < 0) {
571
+ nextTag = 0;
572
+ }
573
+ if (nextTag === value.length) {
574
+ nextTag = -1;
575
+ }
576
+ }
577
+ nextTag = validTagIndex(nextTag, direction);
578
+ setFocusedTag(nextTag);
579
+ focusTag(nextTag);
580
+ };
581
+ const handleClear = event => {
582
+ ignoreFocus.current = true;
583
+ setInputValueState('');
584
+ if (onInputChange) {
585
+ onInputChange(event, '', 'clear');
586
+ }
587
+ handleValue(event, multiple ? [] : null, 'clear');
588
+ };
589
+ const handleKeyDown = other => event => {
590
+ if (other.onKeyDown) {
591
+ other.onKeyDown(event);
592
+ }
593
+ if (event.defaultMuiPrevented) {
594
+ return;
595
+ }
596
+ if (focusedTag !== -1 && ['ArrowLeft', 'ArrowRight'].indexOf(event.key) === -1) {
597
+ setFocusedTag(-1);
598
+ focusTag(-1);
599
+ }
600
+
601
+ // Wait until IME is settled.
602
+ if (event.which !== 229) {
603
+ switch (event.key) {
604
+ case 'Home':
605
+ if (popupOpen && handleHomeEndKeys) {
606
+ // Prevent scroll of the page
607
+ event.preventDefault();
608
+ changeHighlightedIndex({
609
+ diff: 'start',
610
+ direction: 'next',
611
+ reason: 'keyboard',
612
+ event
613
+ });
614
+ }
615
+ break;
616
+ case 'End':
617
+ if (popupOpen && handleHomeEndKeys) {
618
+ // Prevent scroll of the page
619
+ event.preventDefault();
620
+ changeHighlightedIndex({
621
+ diff: 'end',
622
+ direction: 'previous',
623
+ reason: 'keyboard',
624
+ event
625
+ });
626
+ }
627
+ break;
628
+ case 'PageUp':
629
+ // Prevent scroll of the page
630
+ event.preventDefault();
631
+ changeHighlightedIndex({
632
+ diff: -pageSize,
633
+ direction: 'previous',
634
+ reason: 'keyboard',
635
+ event
636
+ });
637
+ handleOpen(event);
638
+ break;
639
+ case 'PageDown':
640
+ // Prevent scroll of the page
641
+ event.preventDefault();
642
+ changeHighlightedIndex({
643
+ diff: pageSize,
644
+ direction: 'next',
645
+ reason: 'keyboard',
646
+ event
647
+ });
648
+ handleOpen(event);
649
+ break;
650
+ case 'ArrowDown':
651
+ // Prevent cursor move
652
+ event.preventDefault();
653
+ changeHighlightedIndex({
654
+ diff: 1,
655
+ direction: 'next',
656
+ reason: 'keyboard',
657
+ event
658
+ });
659
+ handleOpen(event);
660
+ break;
661
+ case 'ArrowUp':
662
+ // Prevent cursor move
663
+ event.preventDefault();
664
+ changeHighlightedIndex({
665
+ diff: -1,
666
+ direction: 'previous',
667
+ reason: 'keyboard',
668
+ event
669
+ });
670
+ handleOpen(event);
671
+ break;
672
+ case 'ArrowLeft':
673
+ handleFocusTag(event, 'previous');
674
+ break;
675
+ case 'ArrowRight':
676
+ handleFocusTag(event, 'next');
677
+ break;
678
+ case 'Enter':
679
+ if (highlightedIndexRef.current !== -1 && popupOpen) {
680
+ const option = filteredOptions[highlightedIndexRef.current];
681
+ const disabled = getOptionDisabled ? getOptionDisabled(option) : false;
682
+
683
+ // Avoid early form validation, let the end-users continue filling the form.
684
+ event.preventDefault();
685
+ if (disabled) {
686
+ return;
687
+ }
688
+ selectNewValue(event, option, 'selectOption');
689
+
690
+ // Move the selection to the end.
691
+ if (autoComplete) {
692
+ inputRef.current.setSelectionRange(inputRef.current.value.length, inputRef.current.value.length);
693
+ }
694
+ } else if (freeSolo && inputValue !== '' && inputValueIsSelectedValue === false) {
695
+ if (multiple) {
696
+ // Allow people to add new values before they submit the form.
697
+ event.preventDefault();
698
+ }
699
+ selectNewValue(event, inputValue, 'createOption', 'freeSolo');
700
+ }
701
+ break;
702
+ case 'Escape':
703
+ if (popupOpen) {
704
+ // Avoid Opera to exit fullscreen mode.
705
+ event.preventDefault();
706
+ // Avoid the Modal to handle the event.
707
+ event.stopPropagation();
708
+ handleClose(event, 'escape');
709
+ } else if (clearOnEscape && (inputValue !== '' || multiple && value.length > 0)) {
710
+ // Avoid Opera to exit fullscreen mode.
711
+ event.preventDefault();
712
+ // Avoid the Modal to handle the event.
713
+ event.stopPropagation();
714
+ handleClear(event);
715
+ }
716
+ break;
717
+ case 'Backspace':
718
+ // Remove the value on the left of the "cursor"
719
+ if (multiple && !readOnly && inputValue === '' && value.length > 0) {
720
+ const index = focusedTag === -1 ? value.length - 1 : focusedTag;
721
+ const newValue = value.slice();
722
+ newValue.splice(index, 1);
723
+ handleValue(event, newValue, 'removeOption', {
724
+ option: value[index]
725
+ });
726
+ }
727
+ break;
728
+ case 'Delete':
729
+ // Remove the value on the right of the "cursor"
730
+ if (multiple && !readOnly && inputValue === '' && value.length > 0 && focusedTag !== -1) {
731
+ const index = focusedTag;
732
+ const newValue = value.slice();
733
+ newValue.splice(index, 1);
734
+ handleValue(event, newValue, 'removeOption', {
735
+ option: value[index]
736
+ });
737
+ }
738
+ break;
739
+ default:
740
+ }
741
+ }
742
+ };
743
+ const handleFocus = event => {
744
+ setFocused(true);
745
+ if (openOnFocus && !ignoreFocus.current) {
746
+ handleOpen(event);
747
+ }
748
+ };
749
+ const handleBlur = event => {
750
+ // Ignore the event when using the scrollbar with IE11
751
+ if (unstable_isActiveElementInListbox(listboxRef)) {
752
+ inputRef.current.focus();
753
+ return;
754
+ }
755
+ setFocused(false);
756
+ firstFocus.current = true;
757
+ ignoreFocus.current = false;
758
+ if (autoSelect && highlightedIndexRef.current !== -1 && popupOpen) {
759
+ selectNewValue(event, filteredOptions[highlightedIndexRef.current], 'blur');
760
+ } else if (autoSelect && freeSolo && inputValue !== '') {
761
+ selectNewValue(event, inputValue, 'blur', 'freeSolo');
762
+ } else if (clearOnBlur) {
763
+ resetInputValue(event, value, 'blur');
764
+ }
765
+ handleClose(event, 'blur');
766
+ };
767
+ const handleInputChange = event => {
768
+ const newValue = event.target.value;
769
+ if (inputValue !== newValue) {
770
+ setInputValueState(newValue);
771
+ setInputPristine(false);
772
+ if (onInputChange) {
773
+ onInputChange(event, newValue, 'input');
774
+ }
775
+ }
776
+ if (newValue === '') {
777
+ if (!disableClearable && !multiple) {
778
+ handleValue(event, null, 'clear');
779
+ }
780
+ } else {
781
+ handleOpen(event);
782
+ }
783
+ };
784
+ const handleOptionMouseMove = event => {
785
+ const index = Number(event.currentTarget.getAttribute('data-option-index'));
786
+ if (highlightedIndexRef.current !== index) {
787
+ setHighlightedIndex({
788
+ event,
789
+ index,
790
+ reason: 'mouse'
791
+ });
792
+ }
793
+ };
794
+ const handleOptionTouchStart = event => {
795
+ setHighlightedIndex({
796
+ event,
797
+ index: Number(event.currentTarget.getAttribute('data-option-index')),
798
+ reason: 'touch'
799
+ });
800
+ isTouch.current = true;
801
+ };
802
+ const handleOptionClick = event => {
803
+ const index = Number(event.currentTarget.getAttribute('data-option-index'));
804
+ selectNewValue(event, filteredOptions[index], 'selectOption');
805
+ isTouch.current = false;
806
+ };
807
+ const handleTagDelete = index => event => {
808
+ const newValue = value.slice();
809
+ newValue.splice(index, 1);
810
+ handleValue(event, newValue, 'removeOption', {
811
+ option: value[index]
812
+ });
813
+ };
814
+ const handlePopupIndicator = event => {
815
+ if (open) {
816
+ handleClose(event, 'toggleInput');
817
+ } else {
818
+ handleOpen(event);
819
+ }
820
+ };
821
+
822
+ // Prevent input blur when interacting with the combobox
823
+ const handleMouseDown = event => {
824
+ // Prevent focusing the input if click is anywhere outside the Autocomplete
825
+ if (!event.currentTarget.contains(event.target)) {
826
+ return;
827
+ }
828
+ if (event.target.getAttribute('id') !== id) {
829
+ event.preventDefault();
830
+ }
831
+ };
832
+
833
+ // Focus the input when interacting with the combobox
834
+ const handleClick = event => {
835
+ // Prevent focusing the input if click is anywhere outside the Autocomplete
836
+ if (!event.currentTarget.contains(event.target)) {
837
+ return;
838
+ }
839
+ inputRef.current.focus();
840
+ if (selectOnFocus && firstFocus.current && inputRef.current.selectionEnd - inputRef.current.selectionStart === 0) {
841
+ inputRef.current.select();
842
+ }
843
+ firstFocus.current = false;
844
+ };
845
+ const handleInputMouseDown = event => {
846
+ if (!disabledProp && (inputValue === '' || !open)) {
847
+ handlePopupIndicator(event);
848
+ }
849
+ };
850
+ let dirty = freeSolo && inputValue.length > 0;
851
+ dirty = dirty || (multiple ? value.length > 0 : value !== null);
852
+ let groupedOptions = filteredOptions;
853
+ if (groupBy) {
854
+ // used to keep track of key and indexes in the result array
855
+ const indexBy = new Map();
856
+ let warn = false;
857
+ groupedOptions = filteredOptions.reduce((acc, option, index) => {
858
+ const group = groupBy(option);
859
+ if (acc.length > 0 && acc[acc.length - 1].group === group) {
860
+ acc[acc.length - 1].options.push(option);
861
+ } else {
862
+ if (process.env.NODE_ENV !== 'production') {
863
+ if (indexBy.get(group) && !warn) {
864
+ console.warn(`MUI: The options provided combined with the \`groupBy\` method of ${componentName} returns duplicated headers.`, 'You can solve the issue by sorting the options with the output of `groupBy`.');
865
+ warn = true;
866
+ }
867
+ indexBy.set(group, true);
868
+ }
869
+ acc.push({
870
+ key: index,
871
+ index,
872
+ group,
873
+ options: [option]
874
+ });
875
+ }
876
+ return acc;
877
+ }, []);
878
+ }
879
+ if (disabledProp && focused) {
880
+ handleBlur();
881
+ }
882
+ return {
883
+ getRootProps: (other = {}) => ({
884
+ 'aria-owns': listboxAvailable ? `${id}-listbox` : null,
885
+ ...other,
886
+ onKeyDown: handleKeyDown(other),
887
+ onMouseDown: handleMouseDown,
888
+ onClick: handleClick
889
+ }),
890
+ getInputLabelProps: () => ({
891
+ id: `${id}-label`,
892
+ htmlFor: id
893
+ }),
894
+ getInputProps: () => ({
895
+ id,
896
+ value: inputValue,
897
+ onBlur: handleBlur,
898
+ onFocus: handleFocus,
899
+ onChange: handleInputChange,
900
+ onMouseDown: handleInputMouseDown,
901
+ // if open then this is handled imperatively so don't let react override
902
+ // only have an opinion about this when closed
903
+ 'aria-activedescendant': popupOpen ? '' : null,
904
+ 'aria-autocomplete': autoComplete ? 'both' : 'list',
905
+ 'aria-controls': listboxAvailable ? `${id}-listbox` : undefined,
906
+ 'aria-expanded': listboxAvailable,
907
+ // Disable browser's suggestion that might overlap with the popup.
908
+ // Handle autocomplete but not autofill.
909
+ autoComplete: 'off',
910
+ ref: inputRef,
911
+ autoCapitalize: 'none',
912
+ spellCheck: 'false',
913
+ role: 'combobox',
914
+ disabled: disabledProp
915
+ }),
916
+ getClearProps: () => ({
917
+ tabIndex: -1,
918
+ type: 'button',
919
+ onClick: handleClear
920
+ }),
921
+ getPopupIndicatorProps: () => ({
922
+ tabIndex: -1,
923
+ type: 'button',
924
+ onClick: handlePopupIndicator
925
+ }),
926
+ getTagProps: ({
927
+ index
928
+ }) => ({
929
+ key: index,
930
+ 'data-tag-index': index,
931
+ tabIndex: -1,
932
+ ...(!readOnly && {
933
+ onDelete: handleTagDelete(index)
934
+ })
935
+ }),
936
+ getListboxProps: () => ({
937
+ role: 'listbox',
938
+ id: `${id}-listbox`,
939
+ 'aria-labelledby': `${id}-label`,
940
+ ref: handleListboxRef,
941
+ onMouseDown: event => {
942
+ // Prevent blur
943
+ event.preventDefault();
944
+ }
945
+ }),
946
+ getOptionProps: ({
947
+ index,
948
+ option
949
+ }) => {
950
+ const selected = (multiple ? value : [value]).some(value2 => value2 != null && isOptionEqualToValue(option, value2));
951
+ const disabled = getOptionDisabled ? getOptionDisabled(option) : false;
952
+ return {
953
+ key: getOptionKey?.(option) ?? getOptionLabel(option),
954
+ tabIndex: -1,
955
+ role: 'option',
956
+ id: `${id}-option-${index}`,
957
+ onMouseMove: handleOptionMouseMove,
958
+ onClick: handleOptionClick,
959
+ onTouchStart: handleOptionTouchStart,
960
+ 'data-option-index': index,
961
+ 'aria-disabled': disabled,
962
+ 'aria-selected': selected
963
+ };
964
+ },
965
+ id,
966
+ inputValue,
967
+ value,
968
+ dirty,
969
+ expanded: popupOpen && anchorEl,
970
+ popupOpen,
971
+ focused: focused || focusedTag !== -1,
972
+ anchorEl,
973
+ setAnchorEl,
974
+ focusedTag,
975
+ groupedOptions
976
+ };
977
+ }
978
+ export default useAutocomplete;