@mui/material 5.16.1 → 5.16.3

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