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