@ringcentral/juno 1.12.4-beta.5847-6a74475d → 1.12.4

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 (39) hide show
  1. package/components/Buttons/SplitButton/SplitButton.js +2 -1
  2. package/components/Downshift/Downshift.d.ts +3 -1
  3. package/components/Downshift/Downshift.js +1 -2
  4. package/components/Downshift/SuggestionList/SuggestionList.js +7 -5
  5. package/components/Downshift/SuggestionList/utils/useSuggestionList.d.ts +20 -7
  6. package/components/Downshift/SuggestionList/utils/useSuggestionList.js +88 -70
  7. package/components/Downshift/utils/DownshiftUtils.d.ts +2 -0
  8. package/components/Downshift/utils/DownshiftUtils.js +6 -0
  9. package/components/Downshift/utils/SelectItem.d.ts +3 -1
  10. package/components/Downshift/utils/index.d.ts +2 -0
  11. package/components/Downshift/utils/index.js +2 -0
  12. package/components/Downshift/utils/useDownshift.d.ts +13 -272
  13. package/components/Downshift/utils/useDownshift.js +132 -429
  14. package/components/Downshift/utils/useDownshiftGroup.js +4 -2
  15. package/components/Downshift/utils/useDownshiftTag.d.ts +554 -0
  16. package/components/Downshift/utils/useDownshiftTag.js +229 -0
  17. package/components/PortalHost/PortalManager/types.d.ts +1 -1
  18. package/es6/components/Buttons/SplitButton/SplitButton.js +3 -2
  19. package/es6/components/Downshift/Downshift.js +1 -2
  20. package/es6/components/Downshift/SuggestionList/SuggestionList.js +7 -5
  21. package/es6/components/Downshift/SuggestionList/utils/useSuggestionList.js +90 -72
  22. package/es6/components/Downshift/utils/DownshiftUtils.js +5 -0
  23. package/es6/components/Downshift/utils/index.js +2 -0
  24. package/es6/components/Downshift/utils/useDownshift.js +135 -432
  25. package/es6/components/Downshift/utils/useDownshiftGroup.js +4 -2
  26. package/es6/components/Downshift/utils/useDownshiftTag.js +227 -0
  27. package/es6/foundation/hooks/index.js +1 -0
  28. package/es6/foundation/hooks/useEver/index.js +1 -0
  29. package/es6/foundation/hooks/useEver/useEver.js +30 -0
  30. package/es6/foundation/hooks/useRefState/useRefState.js +6 -1
  31. package/foundation/hooks/index.d.ts +1 -0
  32. package/foundation/hooks/index.js +1 -0
  33. package/foundation/hooks/useEver/index.d.ts +1 -0
  34. package/foundation/hooks/useEver/index.js +4 -0
  35. package/foundation/hooks/useEver/useEver.d.ts +13 -0
  36. package/foundation/hooks/useEver/useEver.js +32 -0
  37. package/foundation/hooks/useRefState/useRefState.js +6 -1
  38. package/foundation/theme/ThemeProvider.d.ts +5 -3
  39. package/package.json +1 -1
@@ -2,71 +2,63 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var tslib_1 = require("tslib");
4
4
  var react_1 = require("react");
5
- var isString_1 = tslib_1.__importDefault(require("lodash/isString"));
6
- var uniqueId_1 = tslib_1.__importDefault(require("lodash/uniqueId"));
7
5
  var utils_1 = require("@material-ui/core/utils");
8
6
  var foundation_1 = require("../../../foundation");
7
+ var useSuggestionList_1 = require("../SuggestionList/utils/useSuggestionList");
9
8
  var DownshiftUtils_1 = require("./DownshiftUtils");
10
- var useDownshiftGroup_1 = require("./useDownshiftGroup");
11
- var DOWNSHIFT_ID_TOKEN = 'rc-chip-';
9
+ var useDownshiftTag_1 = require("./useDownshiftTag");
12
10
  var DOWNSHIFT_ID_NO_RESULT_TOKEN = 'rc-chip-empty';
13
11
  var DEFAULT_HIGHLIGHTED_INDEX = -1;
14
- function stringArrToRegExp(keyToTags) {
15
- // eslint-disable-next-line security/detect-non-literal-regexp
16
- return new RegExp((keyToTags === null || keyToTags === void 0 ? void 0 : keyToTags.join('|')) || '', 'g');
17
- }
18
- var componentName = 'RcDownshift';
19
12
  exports.useDownshift = function (_a) {
20
- var multipleProp = _a.multiple, variant = _a.variant, labelProp = _a.label, inputValueProp = _a.inputValue, _b = _a.getOptionLabel, getOptionLabel = _b === void 0 ? DownshiftUtils_1.DEFAULT_GET_OPTION_LABEL : _b, _c = _a.keyToTags, keyToTags = _c === void 0 ? DownshiftUtils_1.DEFAULT_KEY_TO_CHIPS : _c, filterOptions = _a.filterOptions, disabledItemsHighlightable = _a.disabledItemsHighlightable, options = _a.options, freeSolo = _a.freeSolo, onInputChangeProp = _a.onInputChange, maxFreeSolo = _a.maxFreeSolo, onMaxFreeSolo = _a.onMaxFreeSolo, openOnFocus = _a.openOnFocus, disableCloseOnSelect = _a.disableCloseOnSelect, initialIsOpen = _a.initialIsOpen, autoSelect = _a.autoSelect, inputContainerRef = _a.inputContainerRef, inputRef = _a.inputRef, wrapperRef = _a.wrapperRef, onKeyDownProp = _a.onKeyDown, onSelectChange = _a.onChange, _d = _a.value, selectedItemsProp = _d === void 0 ? [] : _d, disabled = _a.disabled, renderNoOptions = _a.renderNoOptions, onClear = _a.onClear, autoHighlight = _a.autoHighlight, getOptionDisabled = _a.getOptionDisabled, addNoOptionItem = _a.addNoOptionItem, groupBy = _a.groupBy, onGroupExpanded = _a.onGroupExpanded, groupVariant = _a.groupVariant, groupExpanded = _a.groupExpanded, groupDefaultExpanded = _a.groupDefaultExpanded, getExpandIconProps = _a.getExpandIconProps, required = _a.required, openProp = _a.open, onOpen = _a.onOpen, onClose = _a.onClose, focusedProp = _a.focused;
13
+ var multipleProp = _a.multiple, variant = _a.variant, labelProp = _a.label, inputValueProp = _a.inputValue, _b = _a.getOptionLabel, getOptionLabel = _b === void 0 ? DownshiftUtils_1.DEFAULT_GET_OPTION_LABEL : _b, _c = _a.keyToTags, keyToTags = _c === void 0 ? DownshiftUtils_1.DEFAULT_KEY_TO_CHIPS : _c, filterOptionsProp = _a.filterOptions, disabledItemsHighlightable = _a.disabledItemsHighlightable, options = _a.options, freeSolo = _a.freeSolo, onInputChangeProp = _a.onInputChange, maxFreeSolo = _a.maxFreeSolo, onMaxFreeSolo = _a.onMaxFreeSolo, openOnFocus = _a.openOnFocus, disableCloseOnSelect = _a.disableCloseOnSelect, initialIsOpen = _a.initialIsOpen, autoSelect = _a.autoSelect, inputContainerRef = _a.inputContainerRef, inputRef = _a.inputRef, wrapperRef = _a.wrapperRef, onKeyDownProp = _a.onKeyDown, onSelectChange = _a.onChange, _d = _a.value, selectedItemsProp = _d === void 0 ? [] : _d, disabled = _a.disabled, renderNoOptions = _a.renderNoOptions, onClear = _a.onClear, autoHighlight = _a.autoHighlight, getOptionDisabled = _a.getOptionDisabled, addNoOptionItem = _a.addNoOptionItem, groupBy = _a.groupBy, onGroupExpanded = _a.onGroupExpanded, groupVariant = _a.groupVariant, groupExpanded = _a.groupExpanded, groupDefaultExpanded = _a.groupDefaultExpanded, getExpandIconProps = _a.getExpandIconProps, required = _a.required, openProp = _a.open, onOpen = _a.onOpen, onClose = _a.onClose, focused = _a.focused;
21
14
  var isAutocomplete = variant === 'autocomplete';
22
- // * when that is autocomplete, that will never be multiple
23
- var multiple = isAutocomplete ? false : multipleProp;
24
- var isSelectedFromAutocompleteRef = react_1.useRef(false);
25
- var isInputValueChangedRef = react_1.useRef(false);
15
+ var downshiftId = foundation_1.useId('downshift', true);
26
16
  var _e = tslib_1.__read(utils_1.useControlled({
27
- controlled: focusedProp,
17
+ controlled: focused,
28
18
  default: false,
29
- name: componentName,
30
- }), 2), isFocused = _e[0], setIsFocused = _e[1];
19
+ name: DownshiftUtils_1.downshiftComponentName,
20
+ }), 2), inputFocused = _e[0], setInputFocused = _e[1];
31
21
  var _f = tslib_1.__read(utils_1.useControlled({
32
22
  controlled: openProp,
33
23
  default: initialIsOpen || false,
34
- name: componentName,
24
+ name: DownshiftUtils_1.downshiftComponentName,
35
25
  }), 2), isOpen = _f[0], setIsOpen = _f[1];
36
- var _g = tslib_1.__read(react_1.useState(false), 2), isTagsFocus = _g[0], setIsTagsFocus = _g[1];
37
- var _h = tslib_1.__read(react_1.useState(0), 2), activeIndex = _h[0], setActiveIndex = _h[1];
38
- var _j = tslib_1.__read(utils_1.useControlled({
39
- controlled: inputValueProp,
40
- default: '',
41
- name: componentName,
42
- }), 2), inputValue = _j[0], setInputValue = _j[1];
43
- var updateInputValue = function (newValue) {
44
- onInputChangeProp === null || onInputChangeProp === void 0 ? void 0 : onInputChangeProp(newValue);
45
- setInputValue(newValue);
46
- };
47
- var _k = tslib_1.__read(utils_1.useControlled({
48
- controlled: selectedItemsProp,
49
- default: [],
50
- name: componentName,
51
- }), 2), selectedItems = _k[0], setSelectedItems = _k[1];
52
- var focusInput = function () { var _a; return (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus(); };
53
- var downshiftId = foundation_1.useResultRef(function () { return uniqueId_1.default("downshift-"); }).current;
54
- // * use -2 for us know that is init state, for recalculate defaultHighlightedIndex
55
- var highlightedIndexRef = react_1.useRef(DEFAULT_HIGHLIGHTED_INDEX);
26
+ var isInputValueChangedRef = react_1.useRef(false);
56
27
  var isCompositionRef = react_1.useRef(false);
57
- var noOptionItemRef = react_1.useRef();
28
+ var noOptionItemRef = react_1.useRef(null);
58
29
  var stopAutoSelectRef = react_1.useRef(false);
59
30
  var fromPasteString = react_1.useRef('');
60
31
  var keepHighlightedIndexRef = react_1.useRef(false);
61
- var changeHighlightedIndexReason = react_1.useRef();
62
- var readOnly = !isAutocomplete && !multiple && selectedItems.length >= 1
63
- ? true
64
- : undefined;
65
- var forceUpdate = foundation_1.useForceUpdate();
66
- var _l = foundation_1.useSleep(), sleep = _l.sleep, getSleeping = _l.getSleeping;
67
- var filteredResult = react_1.useMemo(function () {
32
+ var _g = foundation_1.useSleep(), sleep = _g.sleep, getSleeping = _g.getSleeping;
33
+ // * when that is autocomplete, that will never be multiple
34
+ var multiple = isAutocomplete ? false : multipleProp;
35
+ var _h = useDownshiftTag_1.useDownshiftTag({
36
+ id: downshiftId,
37
+ value: selectedItemsProp,
38
+ onChange: onSelectChange,
39
+ getOptionLabel: getOptionLabel,
40
+ onInputChange: onInputChangeProp,
41
+ variant: variant,
42
+ wrapperRef: wrapperRef,
43
+ inputRef: inputRef,
44
+ freeSolo: freeSolo,
45
+ maxFreeSolo: maxFreeSolo,
46
+ keyToTags: keyToTags,
47
+ onMaxFreeSolo: onMaxFreeSolo,
48
+ multiple: multiple,
49
+ label: labelProp,
50
+ required: required,
51
+ disabled: disabled,
52
+ // * when is composition, stop freeSolo create new item
53
+ getStopCreateFreeSolo: function () { return isCompositionRef.current; },
54
+ onReset: function () {
55
+ resetState();
56
+ },
57
+ }), isSelectedFromAutocompleteRef = _h.isSelectedFromAutocompleteRef, tags = _h.tags, tagFocused = _h.focused, addTag = _h.addTag, checkAndAddFreeSolo = _h.checkAndAddFreeSolo, focusedTagIndex = _h.focusedIndex, focusTag = _h.focusTag, focusLastTag = _h.focusLast, clearTags = _h.clearTags, blurTags = _h.blur, getTagProps = _h.getTagProps, getTagListBoxProps = _h.getTagListBoxProps;
58
+ var processFilteredResult = react_1.useCallback(function (results, inputValue) {
59
+ if (inputValue === void 0) { inputValue = ''; }
68
60
  var getInputValueAsItem = function () {
69
- var label = inputValue.trim() || '';
61
+ var label = inputValue.trim();
70
62
  if (label.length > 0) {
71
63
  var item = {
72
64
  id: DOWNSHIFT_ID_NO_RESULT_TOKEN,
@@ -76,22 +68,10 @@ exports.useDownshift = function (_a) {
76
68
  };
77
69
  return item;
78
70
  }
79
- return undefined;
80
- };
81
- var getFilteredItems = function (items) {
82
- if (filterOptions) {
83
- return filterOptions(items, {
84
- inputValue: inputValue,
85
- inputChanged: isInputValueChangedRef.current,
86
- getOptionLabel: getOptionLabel,
87
- selectedItems: selectedItems,
88
- });
89
- }
90
- return items;
71
+ return null;
91
72
  };
92
73
  // * only when isOpen calculate the filtered result
93
- var results = getFilteredItems(options);
94
- noOptionItemRef.current = undefined;
74
+ noOptionItemRef.current = null;
95
75
  if (addNoOptionItem) {
96
76
  var item = getInputValueAsItem();
97
77
  noOptionItemRef.current = item;
@@ -116,68 +96,58 @@ exports.useDownshift = function (_a) {
116
96
  }
117
97
  }
118
98
  return results;
119
- }, [
120
- addNoOptionItem,
121
- filterOptions,
122
- freeSolo,
123
- getOptionLabel,
124
- inputValue,
125
- options,
126
- renderNoOptions,
127
- selectedItems,
128
- ]);
129
- var _m = useDownshiftGroup_1.useDownshiftGroup({
130
- groupBy: groupBy,
99
+ }, [addNoOptionItem, freeSolo, renderNoOptions]);
100
+ var filterOptions = react_1.useCallback(function (items, state) {
101
+ if (filterOptionsProp) {
102
+ return filterOptionsProp(items, tslib_1.__assign(tslib_1.__assign({}, state), { inputChanged: isInputValueChangedRef.current, selectedItems: tags }));
103
+ }
104
+ return items;
105
+ }, [filterOptionsProp, tags]);
106
+ var _j = useSuggestionList_1.useSuggestionList({
107
+ id: downshiftId,
108
+ inputRef: inputRef,
109
+ inputValue: inputValueProp,
131
110
  options: options,
132
- filteredResult: filteredResult,
133
- getExpandIconProps: getExpandIconProps,
111
+ disabledItemsHighlightable: disabledItemsHighlightable,
112
+ groupVariant: groupVariant,
134
113
  groupExpanded: groupExpanded,
135
114
  groupDefaultExpanded: groupDefaultExpanded,
115
+ getOptionLabel: getOptionLabel,
116
+ filterOptions: filterOptions,
117
+ processFilteredResult: processFilteredResult,
118
+ onInputChange: onInputChangeProp,
119
+ onKeyDown: onKeyDownProp,
120
+ onClear: onClear,
121
+ getOptionDisabled: getOptionDisabled,
122
+ groupBy: groupBy,
136
123
  onGroupExpanded: onGroupExpanded,
137
- // TODO: single release for that breaking change
138
- // getOptionDisabled,
139
- groupVariant: groupVariant,
140
- id: downshiftId,
141
- }), groupedResult = _m.groupedResult, handleGroupExpandedChange = _m.handleGroupExpandedChange, optionsGroupList = _m.optionsGroupList;
142
- var optionItems = groupBy ? groupedResult : filteredResult;
143
- var freeSoloCount = react_1.useMemo(function () { return selectedItems.filter(function (x) { return x.freeSolo; }).length; }, [selectedItems]);
144
- var handleSelectedItems = function (_selectedItems) {
145
- setSelectedItems(_selectedItems);
146
- onSelectChange === null || onSelectChange === void 0 ? void 0 : onSelectChange(_selectedItems);
147
- if (isAutocomplete && _selectedItems.length === 1) {
148
- var result = getOptionLabel(_selectedItems[0]);
149
- onInputChangeProp === null || onInputChangeProp === void 0 ? void 0 : onInputChangeProp(result);
150
- isSelectedFromAutocompleteRef.current = true;
151
- }
152
- };
153
- var setHighlightedIndex = function (index, _a) {
154
- var _b = _a.reRender, reRender = _b === void 0 ? false : _b, reason = _a.reason;
155
- changeHighlightedIndexReason.current = reason;
156
- if (highlightedIndexRef.current !== index) {
157
- highlightedIndexRef.current = index;
158
- if (reRender)
159
- forceUpdate();
160
- }
161
- };
162
- var focusTag = function (tagToFocus) {
163
- var _a;
164
- var textFieldElm = wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current;
165
- var inputElm = inputRef.current;
166
- if (tagToFocus === -1) {
167
- inputElm === null || inputElm === void 0 ? void 0 : inputElm.focus();
168
- }
169
- else {
170
- (_a = textFieldElm === null || textFieldElm === void 0 ? void 0 : textFieldElm.querySelector("[data-tag-index=\"" + tagToFocus + "\"]")) === null || _a === void 0 ? void 0 : _a.focus();
171
- }
172
- };
173
- var handleFocusTag = foundation_1.useEventCallback(function (tagToFocus, focus) {
174
- if (focus === void 0) { focus = true; }
175
- if (activeIndex === tagToFocus)
176
- return;
177
- setActiveIndex(tagToFocus);
178
- if (focus)
179
- focusTag(tagToFocus);
180
- });
124
+ getExpandIconProps: getExpandIconProps,
125
+ onSelect: function (e, selectedItem) {
126
+ addTag(selectedItem);
127
+ resetState(e);
128
+ inputContainerScrollToBottom();
129
+ if (disableCloseOnSelect) {
130
+ if (autoHighlight) {
131
+ var currIndex = highlightedIndexRef.current;
132
+ var nextValidIndex = getNextFocusableOption();
133
+ var toIndex =
134
+ // * if that index is bigger than current index or nextValidIndex is last index,
135
+ // * cut 1 (because current item will be remove)
136
+ nextValidIndex > currIndex ||
137
+ nextValidIndex === optionItems.length - 1
138
+ ? nextValidIndex - 1
139
+ : nextValidIndex;
140
+ setHighlightedIndex(toIndex, {
141
+ reRender: true,
142
+ });
143
+ }
144
+ else {
145
+ highlightedIndexRef.current = DEFAULT_HIGHLIGHTED_INDEX;
146
+ }
147
+ }
148
+ },
149
+ }), inputValue = _j.inputValue, optionItems = _j.optionItems, optionsGroupList = _j.optionsGroupList, highlightedIndexRef = _j.highlightedIndexRef, changeHighlightedIndexReasonRef = _j.changeHighlightedIndexReasonRef, forceUpdate = _j.forceUpdate, selectItem = _j.selectItem, handleF10KeyDown = _j.handleF10KeyDown, onKeyFocusedIndexHandle = _j.onKeyFocusedIndexHandle, setHighlightedIndex = _j.setHighlightedIndex, updateInputValue = _j.updateInputValue, clearInput = _j.clearInput, getNextFocusableOption = _j.getNextFocusableOption, getItemProps = _j.getItemProps, focusInput = _j.focusInput, resetSuggestionList = _j.reset, getSuggestionListInputProps = _j.getInputProps, getSuggestionListInputAriaProps = _j.getInputAriaProps, getSuggestionListClearButtonProps = _j.getClearButtonProps, getLabelProps = _j.getLabelProps, getMenuProps = _j.getMenuProps;
150
+ var readOnly = !isAutocomplete && !multiple && tags.length >= 1 ? true : undefined;
181
151
  var closeMenu = function (e, reason) {
182
152
  keepHighlightedIndexRef.current = false;
183
153
  isInputValueChangedRef.current = false;
@@ -203,7 +173,7 @@ exports.useDownshift = function (_a) {
203
173
  setHighlightedIndex(DEFAULT_HIGHLIGHTED_INDEX, { reason: 'auto' });
204
174
  // !!! Get regexp every time for prevent bug with browser
205
175
  // https://stackoverflow.com/questions/3827456/what-is-wrong-with-my-date-regex/3827500#3827500
206
- if (stringArrToRegExp(keyToTags).test((newValue || '').trim())) {
176
+ if (DownshiftUtils_1.stringArrToRegExp(keyToTags).test((newValue || '').trim())) {
207
177
  var resultHaveAdd = checkAndAddFreeSolo({ value: newValue }, e);
208
178
  if (!disableCloseOnSelect) {
209
179
  closeMenu(e, 'toggleInput');
@@ -219,48 +189,6 @@ exports.useDownshift = function (_a) {
219
189
  openMenu(e);
220
190
  return false;
221
191
  };
222
- var getIsItemCanSelected = function (item) {
223
- return ((!!item && item.freeSolo) ||
224
- (DownshiftUtils_1.isItemCanSelected(item) && !(getOptionDisabled === null || getOptionDisabled === void 0 ? void 0 : getOptionDisabled(item))));
225
- };
226
- /** when return `true` mean add item success */
227
- var checkAndAddFreeSolo = function (_a, e) {
228
- var _b = _a === void 0 ? {} : _a, selectedItem = _b.selectedItem, value = _b.value;
229
- var _c;
230
- var isAddItem = false;
231
- var currentValue = value !== null && value !== void 0 ? value : (_c = inputRef.current) === null || _c === void 0 ? void 0 : _c.value;
232
- if (!selectedItem &&
233
- freeSolo &&
234
- !isCompositionRef.current &&
235
- currentValue &&
236
- currentValue.length > 0) {
237
- if (freeSoloCount < maxFreeSolo) {
238
- var items = currentValue
239
- .trim()
240
- .split(stringArrToRegExp(keyToTags))
241
- .filter(function (x) { return x.trim() !== ''; });
242
- var toLength = freeSoloCount + items.length;
243
- if (toLength > maxFreeSolo) {
244
- items.splice(-(toLength - maxFreeSolo));
245
- onMaxFreeSolo === null || onMaxFreeSolo === void 0 ? void 0 : onMaxFreeSolo(maxFreeSolo);
246
- }
247
- if (items.length > 0) {
248
- handleSelectedItems(tslib_1.__spread(selectedItems, items.map(function (label) { return ({
249
- id: uniqueId_1.default(DOWNSHIFT_ID_TOKEN),
250
- isSuggestion: false,
251
- freeSolo: true,
252
- label: label,
253
- }); })));
254
- isAddItem = true;
255
- }
256
- }
257
- else {
258
- onMaxFreeSolo === null || onMaxFreeSolo === void 0 ? void 0 : onMaxFreeSolo(maxFreeSolo);
259
- }
260
- resetState(e);
261
- }
262
- return isAddItem;
263
- };
264
192
  var inputContainerScrollToBottom = function () {
265
193
  requestAnimationFrame(function () {
266
194
  var inputContainer = inputContainerRef === null || inputContainerRef === void 0 ? void 0 : inputContainerRef.current;
@@ -269,107 +197,24 @@ exports.useDownshift = function (_a) {
269
197
  }
270
198
  });
271
199
  };
272
- var selectItemFn = function (selectedItem, e) {
273
- if (getIsItemCanSelected(selectedItem)) {
274
- addSelectedItem(selectedItem);
275
- resetState(e);
276
- inputContainerScrollToBottom();
277
- if (disableCloseOnSelect) {
278
- if (autoHighlight) {
279
- var currIndex = highlightedIndexRef.current;
280
- var nextValidIndex = getNextFocusableOption();
281
- var toIndex =
282
- // * if that index is bigger than current index or nextValidIndex is last index,
283
- // * cut 1 (because current item will be remove)
284
- nextValidIndex > currIndex ||
285
- nextValidIndex === optionItems.length - 1
286
- ? nextValidIndex - 1
287
- : nextValidIndex;
288
- setHighlightedIndex(toIndex, {
289
- reRender: true,
290
- });
291
- }
292
- else {
293
- highlightedIndexRef.current = DEFAULT_HIGHLIGHTED_INDEX;
294
- }
295
- }
296
- return true;
297
- }
298
- return false;
299
- };
300
- var removeSelectedItem = function (selectedItem) {
301
- var selectedItemIndex = selectedItems.indexOf(selectedItem);
302
- if (selectedItemIndex > -1) {
303
- handleSelectedItems(tslib_1.__spread(selectedItems.slice(0, selectedItemIndex), selectedItems.slice(selectedItemIndex + 1)));
304
- }
305
- };
306
- var addSelectedItem = function (selectedItem) {
307
- // * change to uniqueId
308
- if (selectedItem.freeSolo) {
309
- noOptionItemRef.current = undefined;
310
- if (freeSoloCount < maxFreeSolo) {
311
- selectedItem.id = uniqueId_1.default(DOWNSHIFT_ID_TOKEN);
312
- }
313
- else {
314
- onMaxFreeSolo === null || onMaxFreeSolo === void 0 ? void 0 : onMaxFreeSolo(maxFreeSolo);
315
- return;
316
- }
317
- }
318
- if (multiple) {
319
- handleSelectedItems(tslib_1.__spread(selectedItems, [selectedItem]));
320
- }
321
- else {
322
- handleSelectedItems([selectedItem]);
323
- }
324
- };
325
200
  var resetState = function (e) {
326
- if (
327
201
  // * when autocomplete select mode not reset input value
328
- !isSelectedFromAutocompleteRef.current &&
329
- inputRef.current &&
330
- inputRef.current.value.length > 0) {
331
- updateInputValue('');
202
+ if (!isSelectedFromAutocompleteRef.current) {
203
+ clearInput();
332
204
  }
205
+ blurTags();
333
206
  isSelectedFromAutocompleteRef.current = false;
207
+ noOptionItemRef.current = null;
334
208
  isInputValueChangedRef.current = false;
335
- setActiveIndex(-1);
336
209
  if (!disableCloseOnSelect) {
337
210
  closeMenu(e, 'select-option');
338
211
  }
339
212
  };
340
213
  var reset = function (isFocus) {
341
214
  resetState();
342
- setIsTagsFocus(false);
343
- onInputChangeProp === null || onInputChangeProp === void 0 ? void 0 : onInputChangeProp('');
344
215
  onSelectChange === null || onSelectChange === void 0 ? void 0 : onSelectChange([]);
345
- if (isFocus)
346
- focusInput();
216
+ resetSuggestionList(isFocus);
347
217
  };
348
- var activeIndexRef = react_1.useRef(activeIndex);
349
- activeIndexRef.current = activeIndex;
350
- var handleTagKey = foundation_1.useKeyboardMoveFocus({
351
- options: selectedItems,
352
- focusedIndexRef: activeIndexRef,
353
- infinite: 'order',
354
- onFocusedIndexChange: function (event, toIndex) {
355
- handleFocusTag(toIndex);
356
- event === null || event === void 0 ? void 0 : event.preventDefault();
357
- },
358
- }).onKeyFocusedIndexHandle;
359
- var _o = foundation_1.useKeyboardMoveFocus({
360
- options: optionItems,
361
- focusedIndexRef: highlightedIndexRef,
362
- infinite: true,
363
- onFocusedIndexChange: function (event, toIndex) {
364
- setHighlightedIndex(toIndex, { reason: 'keyboard', reRender: true });
365
- event === null || event === void 0 ? void 0 : event.preventDefault();
366
- },
367
- getOptionDisabled: disabledItemsHighlightable
368
- ? undefined
369
- : function (child) {
370
- return !getIsItemCanSelected(child);
371
- },
372
- }), onKeyFocusedIndexHandle = _o.onKeyFocusedIndexHandle, getNextFocusableOption = _o.getNextFocusableOption;
373
218
  var keepHighlightedIndex = function () {
374
219
  keepHighlightedIndexRef.current = true;
375
220
  };
@@ -388,8 +233,8 @@ exports.useDownshift = function (_a) {
388
233
  if (isOpen) {
389
234
  if (highlightedIndexRef.current === DEFAULT_HIGHLIGHTED_INDEX) {
390
235
  var toIndex = defaultHighlightedIndex;
391
- if (isAutocomplete && selectedItems.length === 1) {
392
- var itemText_1 = getOptionLabel(selectedItems[0]);
236
+ if (isAutocomplete && tags.length === 1) {
237
+ var itemText_1 = getOptionLabel(tags[0]);
393
238
  var fIndex = optionItems.findIndex(function (x) { return getOptionLabel(x) === itemText_1; });
394
239
  if (fIndex > -1) {
395
240
  toIndex = fIndex;
@@ -411,8 +256,8 @@ exports.useDownshift = function (_a) {
411
256
  }
412
257
  }
413
258
  var handleAutocompleteText = function () {
414
- if (isAutocomplete && selectedItems.length > 0) {
415
- var result = getOptionLabel(selectedItems[0]);
259
+ if (isAutocomplete && tags.length > 0) {
260
+ var result = getOptionLabel(tags[0]);
416
261
  onInputChangeProp === null || onInputChangeProp === void 0 ? void 0 : onInputChangeProp(result);
417
262
  }
418
263
  };
@@ -424,103 +269,9 @@ exports.useDownshift = function (_a) {
424
269
  handleAutocompleteText();
425
270
  // eslint-disable-next-line react-hooks/exhaustive-deps
426
271
  }, []);
427
- /**
428
- * All Get props methods
429
- */
430
- var getTagListBoxProps = function (props) {
431
- return tslib_1.__assign({ 'aria-label': isString_1.default(labelProp) ? labelProp : undefined, 'aria-required': required || false, 'aria-disabled': disabled || false }, props);
432
- };
433
- var getItemProps = function (_a) {
434
- var item = _a.item, index = _a.index, itemRest = tslib_1.__rest(_a, ["item", "index"]);
435
- return foundation_1.combineProps({
436
- id: downshiftId + "-option-" + index,
437
- role: 'option',
438
- onClick: function (e) {
439
- selectItemFn(item, e);
440
- },
441
- onMouseDown: function (e) {
442
- e.preventDefault();
443
- e.stopPropagation();
444
- },
445
- onMouseOver: function () {
446
- if (highlightedIndexRef.current !== index) {
447
- setHighlightedIndex(index, { reason: 'mouse', reRender: true });
448
- }
449
- },
450
- }, itemRest);
451
- };
452
- var getTagProps = function (_a) {
453
- var index = _a.index, selectedItem = _a.selectedItem, restTagProps = tslib_1.__rest(_a, ["index", "selectedItem"]);
454
- var tabIndex =
455
- // * when tag is not focus
456
- !isTagsFocus &&
457
- // * and is first item
458
- index === 0
459
- ? 0
460
- : -1;
461
- return foundation_1.combineProps({
462
- tabIndex: tabIndex,
463
- // TODO: wait for input can complete that.
464
- // role: 'option',
465
- role: 'button',
466
- key: downshiftId + "-selected-item-" + index,
467
- 'data-tag-index': index,
468
- 'data-item-last': index === selectedItems.length - 1,
469
- onClick: function (e) {
470
- e.stopPropagation();
471
- },
472
- onBlur: function () { return setIsTagsFocus(false); },
473
- onFocus: function () {
474
- handleFocusTag(index);
475
- setIsTagsFocus(true);
476
- },
477
- onKeyDown: function (e) {
478
- switch (e.key) {
479
- case 'Backspace':
480
- {
481
- var lengthBeforeDelete = selectedItems.length;
482
- handleFocusTag(
483
- // * when length is 1 mean that will be remove all
484
- lengthBeforeDelete === 1
485
- ? -1
486
- : activeIndex === 0
487
- ? 0
488
- : activeIndex - 1);
489
- removeSelectedItem(selectedItem);
490
- }
491
- break;
492
- case 'Delete':
493
- {
494
- var lengthBeforeDelete = selectedItems.length;
495
- handleFocusTag(
496
- // * when length is 1 mean that will be remove all
497
- lengthBeforeDelete === 1
498
- ? -1
499
- : activeIndex === selectedItems.length - 1
500
- ? activeIndex - 1
501
- : activeIndex);
502
- removeSelectedItem(selectedItem);
503
- }
504
- break;
505
- default:
506
- handleTagKey(e);
507
- break;
508
- }
509
- },
510
- onDelete: function (e) {
511
- // * left button clicked
512
- if (e.button === 0) {
513
- removeSelectedItem(selectedItem);
514
- setIsTagsFocus(false);
515
- }
516
- },
517
- }, restTagProps);
518
- };
519
272
  var getInputProps = function (props) {
520
- return foundation_1.combineProps({
521
- id: downshiftId + "-input",
522
- autoComplete: 'off',
523
- onPaste: function (e) {
273
+ var suggestionListItemProps = getSuggestionListInputProps(props);
274
+ return foundation_1.combineProps(tslib_1.__assign(tslib_1.__assign({}, suggestionListItemProps), { onPaste: function (e) {
524
275
  var _a;
525
276
  if (freeSolo) {
526
277
  var clipboardData = e.clipboardData;
@@ -535,20 +286,18 @@ exports.useDownshift = function (_a) {
535
286
  pasteText +
536
287
  currText.slice(position.end);
537
288
  }
538
- },
539
- onChange: function (e) {
289
+ }, onChange: function (e) {
540
290
  var changeValue = fromPasteString.current || e.target.value;
541
291
  handleInputChange(changeValue, undefined, e);
542
292
  // * when input value clear all, clear selected item
543
293
  if (isAutocomplete && changeValue.length === 0) {
544
- handleSelectedItems([]);
294
+ clearTags();
545
295
  }
546
296
  fromPasteString.current = '';
547
297
  isInputValueChangedRef.current = true;
548
- },
549
- onFocus: function (e) {
298
+ }, onFocus: function (e) {
550
299
  var _a;
551
- setIsFocused(true);
300
+ setInputFocused(true);
552
301
  if (openOnFocus)
553
302
  openMenu(e);
554
303
  // * when autocomplete mode, always select all text when focus
@@ -557,25 +306,20 @@ exports.useDownshift = function (_a) {
557
306
  }
558
307
  // * reset stopAutoSelect in focus input
559
308
  stopAutoSelectRef.current = false;
560
- if (activeIndex !== -1) {
561
- handleFocusTag(-1, false);
562
- }
563
- },
564
- onBlur: function (e) {
565
- setIsFocused(false);
309
+ blurTags();
310
+ }, onBlur: function (e) {
311
+ setInputFocused(false);
566
312
  if (autoSelect && !stopAutoSelectRef.current) {
567
313
  if (!freeSolo)
568
- selectItemFn(optionItems[highlightedIndexRef.current], e);
314
+ selectItem(e, optionItems[highlightedIndexRef.current]);
569
315
  checkAndAddFreeSolo({}, e);
570
316
  }
571
317
  // * when blur should restore text when have selected item
572
318
  handleAutocompleteText();
573
319
  closeMenu(e, 'blur');
574
- },
575
- onCompositionStart: function () {
320
+ }, onCompositionStart: function () {
576
321
  isCompositionRef.current = true;
577
- },
578
- onCompositionEnd: function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
322
+ }, onCompositionEnd: function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
579
323
  return tslib_1.__generator(this, function (_a) {
580
324
  switch (_a.label) {
581
325
  case 0:
@@ -588,20 +332,17 @@ exports.useDownshift = function (_a) {
588
332
  return [2 /*return*/];
589
333
  }
590
334
  });
591
- }); },
592
- onKeyDown: function (e) {
335
+ }); }, onKeyDown: function (e) {
593
336
  keepHighlightedIndexRef.current = false;
594
337
  onKeyDownProp === null || onKeyDownProp === void 0 ? void 0 : onKeyDownProp(e, highlightedIndexRef.current);
595
338
  var _a = foundation_1.getSelectionPosition(inputRef), isSelectRange = _a.isSelectRange, position = _a.position;
596
- var currTagsLength = selectedItems.length;
339
+ var currTagsLength = tags.length;
597
340
  if (!isSelectRange &&
598
341
  position.start === 0 &&
599
342
  currTagsLength > 0 &&
600
343
  e.key === 'Backspace') {
601
- if (currTagsLength > 0) {
602
- stopAutoSelectRef.current = true;
603
- handleFocusTag(currTagsLength - 1);
604
- }
344
+ stopAutoSelectRef.current = true;
345
+ focusLastTag();
605
346
  }
606
347
  else {
607
348
  var highlightedIndex = highlightedIndexRef.current;
@@ -633,7 +374,7 @@ exports.useDownshift = function (_a) {
633
374
  if (!e.shiftKey && !isCompositionRef.current) {
634
375
  var currentHighlightItem = optionItems[highlightedIndex];
635
376
  if (isOpen && currentHighlightItem) {
636
- if (selectItemFn(currentHighlightItem, e)) {
377
+ if (selectItem(e, currentHighlightItem)) {
637
378
  e.preventDefault();
638
379
  e.stopPropagation();
639
380
  }
@@ -660,20 +401,14 @@ exports.useDownshift = function (_a) {
660
401
  else if (highlightedIndex !== DEFAULT_HIGHLIGHTED_INDEX &&
661
402
  isOpen &&
662
403
  !isCompositionRef.current) {
663
- selectItemFn(optionItems[highlightedIndex], e);
404
+ selectItem(e, optionItems[highlightedIndex]);
664
405
  e.stopPropagation();
665
406
  }
666
407
  // always preventDefault for not inset all enter into textarea
667
408
  e.preventDefault();
668
409
  break;
669
410
  case 'F10':
670
- if (e.shiftKey) {
671
- var currOption = optionItems[highlightedIndex];
672
- var currentGroup = currOption.group;
673
- if (currentGroup && currentGroup.options.length > 1) {
674
- handleGroupExpandedChange(currentGroup.group);
675
- }
676
- }
411
+ handleF10KeyDown(e);
677
412
  break;
678
413
  case 'ArrowUp':
679
414
  case 'ArrowDown':
@@ -682,7 +417,7 @@ exports.useDownshift = function (_a) {
682
417
  return;
683
418
  }
684
419
  // * check state for is that should move key in option items
685
- if (isAutocomplete && selectedItems.length > 0 && !isOpen) {
420
+ if (isAutocomplete && tags.length > 0 && !isOpen) {
686
421
  openMenu(e);
687
422
  e.preventDefault();
688
423
  break;
@@ -697,30 +432,11 @@ exports.useDownshift = function (_a) {
697
432
  break;
698
433
  }
699
434
  }
700
- },
701
- }, props);
435
+ } }), props);
702
436
  };
703
437
  var getInputAriaProps = function (props) {
704
- return foundation_1.combineProps({
705
- readOnly: readOnly,
706
- // * provide for when container click focus on input
707
- onContainerClick: focusInput,
708
- unselectable: readOnly ? 'on' : undefined,
709
- role: 'combobox',
710
- 'aria-autocomplete': 'list',
711
- 'aria-expanded': !!isOpen,
712
- 'aria-haspopup': true,
713
- 'aria-owns': isOpen ? downshiftId + "-menu" : undefined,
714
- 'aria-activedescendant': isOpen && highlightedIndexRef.current > -1
715
- ? downshiftId + "-option-" + highlightedIndexRef.current
716
- : undefined,
717
- }, props);
718
- };
719
- var getLabelProps = function (props) {
720
- return foundation_1.combineProps({
721
- htmlFor: downshiftId + "-input",
722
- id: downshiftId + "-label",
723
- }, props);
438
+ var suggestionListInputAriaProps = getSuggestionListInputAriaProps(props);
439
+ return foundation_1.combineProps(tslib_1.__assign(tslib_1.__assign({}, suggestionListInputAriaProps), { readOnly: readOnly, unselectable: readOnly ? 'on' : undefined, 'aria-owns': isOpen ? downshiftId + "-menu" : undefined, 'aria-expanded': !!isOpen }), props);
724
440
  };
725
441
  var getPopperProps = function (restPopperProps) {
726
442
  var _a;
@@ -734,13 +450,6 @@ exports.useDownshift = function (_a) {
734
450
  },
735
451
  }, restPopperProps);
736
452
  };
737
- var getMenuProps = function (restMenuProps) {
738
- return foundation_1.combineProps({
739
- 'aria-labelledby': downshiftId + "-label",
740
- id: downshiftId + "-menu",
741
- role: 'listbox',
742
- }, restMenuProps);
743
- };
744
453
  var getToggleButtonProps = function (props) {
745
454
  return foundation_1.combineProps({
746
455
  disabled: (!isOpen && optionItems.length === 0) || disabled || readOnly,
@@ -762,13 +471,11 @@ exports.useDownshift = function (_a) {
762
471
  }, props);
763
472
  };
764
473
  var getClearButtonProps = function (props) {
765
- return foundation_1.combineProps({
766
- id: downshiftId + "-clear-button",
767
- onClick: function (e) {
474
+ var suggestionListClearButtonProps = getSuggestionListClearButtonProps(props);
475
+ return foundation_1.combineProps(tslib_1.__assign(tslib_1.__assign({}, suggestionListClearButtonProps), { onClick: function (e) {
768
476
  onClear === null || onClear === void 0 ? void 0 : onClear(e);
769
477
  reset(true);
770
- },
771
- }, props);
478
+ } }), props);
772
479
  };
773
480
  var getNoOptionsProps = function (additionProps) {
774
481
  return foundation_1.combineProps({
@@ -778,17 +485,14 @@ exports.useDownshift = function (_a) {
778
485
  role: 'presentation',
779
486
  }, additionProps);
780
487
  };
781
- var resultObj = {
488
+ return {
782
489
  focusInput: focusInput,
783
- setIsTagsFocus: setIsTagsFocus,
784
490
  readOnly: readOnly,
785
491
  getTagProps: getTagProps,
786
492
  getClearButtonProps: getClearButtonProps,
787
- removeSelectedItem: removeSelectedItem,
788
- activeIndex: activeIndex,
789
- setActiveIndex: handleFocusTag,
493
+ activeIndex: focusedTagIndex,
494
+ setActiveIndex: focusTag,
790
495
  isOpen: isOpen,
791
- isTagsFocus: isTagsFocus,
792
496
  getToggleButtonProps: getToggleButtonProps,
793
497
  getPopperProps: getPopperProps,
794
498
  getLabelProps: getLabelProps,
@@ -800,23 +504,22 @@ exports.useDownshift = function (_a) {
800
504
  getNoOptionsProps: getNoOptionsProps,
801
505
  noOptionItem: noOptionItemRef.current,
802
506
  highlightedIndex: highlightedIndexRef.current,
803
- selectedItems: selectedItems,
507
+ tags: tags,
804
508
  optionItems: optionItems,
805
509
  inputValue: inputValue,
806
510
  onInputChange: handleInputChange,
807
511
  keepHighlightedIndex: keepHighlightedIndex,
808
512
  isKeepHighlightedIndex: keepHighlightedIndexRef.current,
809
513
  setHighlightedIndex: setHighlightedIndex,
810
- changeHighlightedIndexReason: changeHighlightedIndexReason.current,
514
+ changeHighlightedIndexReason: changeHighlightedIndexReasonRef.current,
811
515
  closeMenu: closeMenu,
812
516
  openMenu: openMenu,
813
517
  reset: reset,
814
518
  forceUpdate: forceUpdate,
815
519
  optionsGroupList: optionsGroupList,
816
- isFocused: isFocused,
520
+ /** that state of current downshift focused, both tag or input focused */
521
+ focused: focused !== null && focused !== void 0 ? focused : (tagFocused || inputFocused ? true : undefined),
817
522
  id: downshiftId,
818
523
  inputChanged: isInputValueChangedRef.current,
819
524
  };
820
- changeHighlightedIndexReason.current = undefined;
821
- return resultObj;
822
525
  };