@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
|
@@ -54,6 +54,7 @@ export var useDownshiftGroup = function (_a) {
|
|
|
54
54
|
key: index,
|
|
55
55
|
index: index,
|
|
56
56
|
group: group,
|
|
57
|
+
order: acc.length,
|
|
57
58
|
options: [__assign(__assign({}, option), { indexInOwnGroup: 0 })],
|
|
58
59
|
expanded: false,
|
|
59
60
|
getExpandIconProps: function (additionExpandIconProps) {
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { __assign, __read, __rest, __spread } from "tslib";
|
|
2
|
+
import { useMemo, useRef, useState } from 'react';
|
|
3
|
+
import isString from 'lodash/isString';
|
|
4
|
+
import uniqueId from 'lodash/uniqueId';
|
|
5
|
+
import { useControlled } from '@material-ui/core/utils';
|
|
6
|
+
import { combineProps, useEventCallback, useKeyboardMoveFocus, useRefState, } from '../../../foundation';
|
|
7
|
+
import { DEFAULT_GET_OPTION_LABEL, downshiftComponentName, stringArrToRegExp, } from './DownshiftUtils';
|
|
8
|
+
var DOWNSHIFT_ID_TOKEN = 'rc-chip-';
|
|
9
|
+
/** @inner inner hook work with useDownshift */
|
|
10
|
+
export var useDownshiftTag = function (_a) {
|
|
11
|
+
var downshiftId = _a.id, _b = _a.value, selectedItemsProp = _b === void 0 ? [] : _b, onSelectChange = _a.onChange, variant = _a.variant, _c = _a.getOptionLabel, getOptionLabel = _c === void 0 ? DEFAULT_GET_OPTION_LABEL : _c, onInputChangeProp = _a.onInputChange, wrapperRef = _a.wrapperRef, inputRef = _a.inputRef, freeSolo = _a.freeSolo, maxFreeSolo = _a.maxFreeSolo, keyToTags = _a.keyToTags, onMaxFreeSolo = _a.onMaxFreeSolo, multiple = _a.multiple, labelProp = _a.label, required = _a.required, disabled = _a.disabled, getStopCreateFreeSolo = _a.getStopCreateFreeSolo, onReset = _a.onReset;
|
|
12
|
+
var isAutocomplete = variant === 'autocomplete';
|
|
13
|
+
var isSelectedFromAutocompleteRef = useRef(false);
|
|
14
|
+
var _d = __read(useControlled({
|
|
15
|
+
controlled: selectedItemsProp,
|
|
16
|
+
default: [],
|
|
17
|
+
name: downshiftComponentName,
|
|
18
|
+
}), 2), tags = _d[0], _setTags = _d[1];
|
|
19
|
+
var _e = __read(useState(false), 2), focused = _e[0], setFocused = _e[1];
|
|
20
|
+
var _f = __read(useRefState(0), 2), focusedIndexRef = _f[0], setFocusedIndex = _f[1];
|
|
21
|
+
var freeSoloCount = useMemo(function () { return tags.filter(function (x) { return x.freeSolo; }).length; }, [tags]);
|
|
22
|
+
var setTags = function (_selectedItems) {
|
|
23
|
+
_setTags(_selectedItems);
|
|
24
|
+
onSelectChange === null || onSelectChange === void 0 ? void 0 : onSelectChange(_selectedItems);
|
|
25
|
+
if (isAutocomplete && _selectedItems.length === 1) {
|
|
26
|
+
var result = getOptionLabel(_selectedItems[0]);
|
|
27
|
+
onInputChangeProp === null || onInputChangeProp === void 0 ? void 0 : onInputChangeProp(result);
|
|
28
|
+
isSelectedFromAutocompleteRef.current = true;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
var _focusTag = function (tagToFocus) {
|
|
32
|
+
var _a;
|
|
33
|
+
var textFieldElm = wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current;
|
|
34
|
+
var inputElm = inputRef.current;
|
|
35
|
+
if (tagToFocus === -1) {
|
|
36
|
+
inputElm === null || inputElm === void 0 ? void 0 : inputElm.focus();
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
(_a = textFieldElm === null || textFieldElm === void 0 ? void 0 : textFieldElm.querySelector("[data-tag-index=\"" + tagToFocus + "\"]")) === null || _a === void 0 ? void 0 : _a.focus();
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var focusTag = useEventCallback(function (tagToFocus, focus) {
|
|
43
|
+
if (focus === void 0) { focus = true; }
|
|
44
|
+
if (focusedIndexRef.current === tagToFocus)
|
|
45
|
+
return;
|
|
46
|
+
setFocusedIndex(tagToFocus);
|
|
47
|
+
if (focus)
|
|
48
|
+
_focusTag(tagToFocus);
|
|
49
|
+
});
|
|
50
|
+
/** when return `true` mean add item success */
|
|
51
|
+
var checkAndAddFreeSolo = function (_a, e) {
|
|
52
|
+
var _b = _a === void 0 ? {} : _a, selectedItem = _b.selectedItem, value = _b.value;
|
|
53
|
+
var _c;
|
|
54
|
+
var isAddItem = false;
|
|
55
|
+
var currentValue = value !== null && value !== void 0 ? value : (_c = inputRef.current) === null || _c === void 0 ? void 0 : _c.value;
|
|
56
|
+
if (!(getStopCreateFreeSolo === null || getStopCreateFreeSolo === void 0 ? void 0 : getStopCreateFreeSolo()) &&
|
|
57
|
+
!selectedItem &&
|
|
58
|
+
freeSolo &&
|
|
59
|
+
currentValue &&
|
|
60
|
+
currentValue.length > 0) {
|
|
61
|
+
if (freeSoloCount < maxFreeSolo) {
|
|
62
|
+
var items = currentValue
|
|
63
|
+
.trim()
|
|
64
|
+
.split(stringArrToRegExp(keyToTags))
|
|
65
|
+
.filter(function (x) { return x.trim() !== ''; });
|
|
66
|
+
var toLength = freeSoloCount + items.length;
|
|
67
|
+
if (toLength > maxFreeSolo) {
|
|
68
|
+
items.splice(-(toLength - maxFreeSolo));
|
|
69
|
+
onMaxFreeSolo === null || onMaxFreeSolo === void 0 ? void 0 : onMaxFreeSolo(maxFreeSolo);
|
|
70
|
+
}
|
|
71
|
+
if (items.length > 0) {
|
|
72
|
+
setTags(__spread(tags, items.map(function (label) {
|
|
73
|
+
return ({
|
|
74
|
+
id: uniqueId(DOWNSHIFT_ID_TOKEN),
|
|
75
|
+
isSuggestion: false,
|
|
76
|
+
freeSolo: true,
|
|
77
|
+
label: label,
|
|
78
|
+
});
|
|
79
|
+
})));
|
|
80
|
+
isAddItem = true;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
onMaxFreeSolo === null || onMaxFreeSolo === void 0 ? void 0 : onMaxFreeSolo(maxFreeSolo);
|
|
85
|
+
}
|
|
86
|
+
onReset === null || onReset === void 0 ? void 0 : onReset(e);
|
|
87
|
+
}
|
|
88
|
+
return isAddItem;
|
|
89
|
+
};
|
|
90
|
+
var removeTag = function (selectedItem) {
|
|
91
|
+
var selectedItemIndex = tags.indexOf(selectedItem);
|
|
92
|
+
if (selectedItemIndex > -1) {
|
|
93
|
+
setTags(__spread(tags.slice(0, selectedItemIndex), tags.slice(selectedItemIndex + 1)));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
var addTag = function (selectedItem) {
|
|
97
|
+
// * change to uniqueId
|
|
98
|
+
if (selectedItem.freeSolo) {
|
|
99
|
+
if (freeSoloCount < maxFreeSolo) {
|
|
100
|
+
selectedItem.id = uniqueId(DOWNSHIFT_ID_TOKEN);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
onMaxFreeSolo === null || onMaxFreeSolo === void 0 ? void 0 : onMaxFreeSolo(maxFreeSolo);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (multiple) {
|
|
108
|
+
setTags(__spread(tags, [selectedItem]));
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
setTags([selectedItem]);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
var handleTagKey = useKeyboardMoveFocus({
|
|
115
|
+
options: tags,
|
|
116
|
+
focusedIndexRef: focusedIndexRef,
|
|
117
|
+
infinite: 'order',
|
|
118
|
+
onFocusedIndexChange: function (event, toIndex) {
|
|
119
|
+
focusTag(toIndex);
|
|
120
|
+
event === null || event === void 0 ? void 0 : event.preventDefault();
|
|
121
|
+
},
|
|
122
|
+
}).onKeyFocusedIndexHandle;
|
|
123
|
+
/**
|
|
124
|
+
* All Get props methods
|
|
125
|
+
*/
|
|
126
|
+
var getTagListBoxProps = function (props) {
|
|
127
|
+
return __assign({ 'aria-label': isString(labelProp) ? labelProp : undefined, 'aria-required': required || false, 'aria-disabled': disabled || false }, props);
|
|
128
|
+
};
|
|
129
|
+
var getTagProps = function (_a) {
|
|
130
|
+
var index = _a.index, selectedItem = _a.selectedItem, restTagProps = __rest(_a, ["index", "selectedItem"]);
|
|
131
|
+
var tabIndex =
|
|
132
|
+
// * when tag is not focus
|
|
133
|
+
!focused &&
|
|
134
|
+
// * and is first item
|
|
135
|
+
index === 0
|
|
136
|
+
? 0
|
|
137
|
+
: -1;
|
|
138
|
+
return combineProps({
|
|
139
|
+
tabIndex: tabIndex,
|
|
140
|
+
// TODO: wait for input can complete that.
|
|
141
|
+
// role: 'option',
|
|
142
|
+
role: 'button',
|
|
143
|
+
key: downshiftId + "-selected-item-" + index,
|
|
144
|
+
'data-tag-index': index,
|
|
145
|
+
'data-item-last': index === tags.length - 1,
|
|
146
|
+
onClick: function (e) {
|
|
147
|
+
e.stopPropagation();
|
|
148
|
+
},
|
|
149
|
+
onBlur: function () { return setFocused(false); },
|
|
150
|
+
onFocus: function () {
|
|
151
|
+
focusTag(index);
|
|
152
|
+
setFocused(true);
|
|
153
|
+
},
|
|
154
|
+
onKeyDown: function (e) {
|
|
155
|
+
var focusedIndex = focusedIndexRef.current;
|
|
156
|
+
switch (e.key) {
|
|
157
|
+
case 'Backspace':
|
|
158
|
+
{
|
|
159
|
+
var lengthBeforeDelete = tags.length;
|
|
160
|
+
focusTag(
|
|
161
|
+
// * when length is 1 mean that will be remove all
|
|
162
|
+
lengthBeforeDelete === 1
|
|
163
|
+
? -1
|
|
164
|
+
: focusedIndex === 0
|
|
165
|
+
? 0
|
|
166
|
+
: focusedIndex - 1);
|
|
167
|
+
removeTag(selectedItem);
|
|
168
|
+
}
|
|
169
|
+
break;
|
|
170
|
+
case 'Delete':
|
|
171
|
+
{
|
|
172
|
+
var lengthBeforeDelete = tags.length;
|
|
173
|
+
focusTag(
|
|
174
|
+
// * when length is 1 mean that will be remove all
|
|
175
|
+
lengthBeforeDelete === 1
|
|
176
|
+
? -1
|
|
177
|
+
: focusedIndex === tags.length - 1
|
|
178
|
+
? focusedIndex - 1
|
|
179
|
+
: focusedIndex);
|
|
180
|
+
removeTag(selectedItem);
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
default:
|
|
184
|
+
handleTagKey(e);
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
onDelete: function (e) {
|
|
189
|
+
// * left button clicked
|
|
190
|
+
if (e.button === 0) {
|
|
191
|
+
removeTag(selectedItem);
|
|
192
|
+
setFocused(false);
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
}, restTagProps);
|
|
196
|
+
};
|
|
197
|
+
return {
|
|
198
|
+
focused: focused,
|
|
199
|
+
setFocused: setFocused,
|
|
200
|
+
tags: tags,
|
|
201
|
+
isSelectedFromAutocompleteRef: isSelectedFromAutocompleteRef,
|
|
202
|
+
focusedIndex: focusedIndexRef.current,
|
|
203
|
+
checkAndAddFreeSolo: checkAndAddFreeSolo,
|
|
204
|
+
getTagListBoxProps: getTagListBoxProps,
|
|
205
|
+
getTagProps: getTagProps,
|
|
206
|
+
focusTag: focusTag,
|
|
207
|
+
addTag: addTag,
|
|
208
|
+
clearTags: function () {
|
|
209
|
+
setTags([]);
|
|
210
|
+
},
|
|
211
|
+
blur: function () {
|
|
212
|
+
if (focusedIndexRef.current !== -1) {
|
|
213
|
+
focusTag(-1, false);
|
|
214
|
+
}
|
|
215
|
+
setFocused(false);
|
|
216
|
+
},
|
|
217
|
+
// reset: () => {
|
|
218
|
+
// setActiveIndex(-1);
|
|
219
|
+
// },
|
|
220
|
+
focusLast: function () {
|
|
221
|
+
var currTagsLength = tags.length;
|
|
222
|
+
if (currTagsLength > 0) {
|
|
223
|
+
focusTag(currTagsLength - 1);
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
};
|
|
@@ -27,7 +27,12 @@ export var useRefState = function (value, customUpdate) {
|
|
|
27
27
|
/**
|
|
28
28
|
* update ref value, set isUpdate to `false` will not trigger re-render
|
|
29
29
|
*/
|
|
30
|
-
var setInnerValue = useEventCallback(function (toValue,
|
|
30
|
+
var setInnerValue = useEventCallback(function (toValue,
|
|
31
|
+
/**
|
|
32
|
+
* is re-render
|
|
33
|
+
* @default true
|
|
34
|
+
*/
|
|
35
|
+
isUpdate) {
|
|
31
36
|
if (isUpdate === void 0) { isUpdate = true; }
|
|
32
37
|
innerValueRef.current = toValue;
|
|
33
38
|
if (isUpdate) {
|
|
@@ -29,7 +29,12 @@ exports.useRefState = function (value, customUpdate) {
|
|
|
29
29
|
/**
|
|
30
30
|
* update ref value, set isUpdate to `false` will not trigger re-render
|
|
31
31
|
*/
|
|
32
|
-
var setInnerValue = useEventCallback_1.useEventCallback(function (toValue,
|
|
32
|
+
var setInnerValue = useEventCallback_1.useEventCallback(function (toValue,
|
|
33
|
+
/**
|
|
34
|
+
* is re-render
|
|
35
|
+
* @default true
|
|
36
|
+
*/
|
|
37
|
+
isUpdate) {
|
|
33
38
|
if (isUpdate === void 0) { isUpdate = true; }
|
|
34
39
|
innerValueRef.current = toValue;
|
|
35
40
|
if (isUpdate) {
|