@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
@@ -0,0 +1,229 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var tslib_1 = require("tslib");
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
+ var utils_1 = require("@material-ui/core/utils");
8
+ var foundation_1 = require("../../../foundation");
9
+ var DownshiftUtils_1 = require("./DownshiftUtils");
10
+ var DOWNSHIFT_ID_TOKEN = 'rc-chip-';
11
+ /** @inner inner hook work with useDownshift */
12
+ exports.useDownshiftTag = function (_a) {
13
+ 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 ? DownshiftUtils_1.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;
14
+ var isAutocomplete = variant === 'autocomplete';
15
+ var isSelectedFromAutocompleteRef = react_1.useRef(false);
16
+ var _d = tslib_1.__read(utils_1.useControlled({
17
+ controlled: selectedItemsProp,
18
+ default: [],
19
+ name: DownshiftUtils_1.downshiftComponentName,
20
+ }), 2), tags = _d[0], _setTags = _d[1];
21
+ var _e = tslib_1.__read(react_1.useState(false), 2), focused = _e[0], setFocused = _e[1];
22
+ var _f = tslib_1.__read(foundation_1.useRefState(0), 2), focusedIndexRef = _f[0], setFocusedIndex = _f[1];
23
+ var freeSoloCount = react_1.useMemo(function () { return tags.filter(function (x) { return x.freeSolo; }).length; }, [tags]);
24
+ var setTags = function (_selectedItems) {
25
+ _setTags(_selectedItems);
26
+ onSelectChange === null || onSelectChange === void 0 ? void 0 : onSelectChange(_selectedItems);
27
+ if (isAutocomplete && _selectedItems.length === 1) {
28
+ var result = getOptionLabel(_selectedItems[0]);
29
+ onInputChangeProp === null || onInputChangeProp === void 0 ? void 0 : onInputChangeProp(result);
30
+ isSelectedFromAutocompleteRef.current = true;
31
+ }
32
+ };
33
+ var _focusTag = function (tagToFocus) {
34
+ var _a;
35
+ var textFieldElm = wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current;
36
+ var inputElm = inputRef.current;
37
+ if (tagToFocus === -1) {
38
+ inputElm === null || inputElm === void 0 ? void 0 : inputElm.focus();
39
+ }
40
+ else {
41
+ (_a = textFieldElm === null || textFieldElm === void 0 ? void 0 : textFieldElm.querySelector("[data-tag-index=\"" + tagToFocus + "\"]")) === null || _a === void 0 ? void 0 : _a.focus();
42
+ }
43
+ };
44
+ var focusTag = foundation_1.useEventCallback(function (tagToFocus, focus) {
45
+ if (focus === void 0) { focus = true; }
46
+ if (focusedIndexRef.current === tagToFocus)
47
+ return;
48
+ setFocusedIndex(tagToFocus);
49
+ if (focus)
50
+ _focusTag(tagToFocus);
51
+ });
52
+ /** when return `true` mean add item success */
53
+ var checkAndAddFreeSolo = function (_a, e) {
54
+ var _b = _a === void 0 ? {} : _a, selectedItem = _b.selectedItem, value = _b.value;
55
+ var _c;
56
+ var isAddItem = false;
57
+ var currentValue = value !== null && value !== void 0 ? value : (_c = inputRef.current) === null || _c === void 0 ? void 0 : _c.value;
58
+ if (!(getStopCreateFreeSolo === null || getStopCreateFreeSolo === void 0 ? void 0 : getStopCreateFreeSolo()) &&
59
+ !selectedItem &&
60
+ freeSolo &&
61
+ currentValue &&
62
+ currentValue.length > 0) {
63
+ if (freeSoloCount < maxFreeSolo) {
64
+ var items = currentValue
65
+ .trim()
66
+ .split(DownshiftUtils_1.stringArrToRegExp(keyToTags))
67
+ .filter(function (x) { return x.trim() !== ''; });
68
+ var toLength = freeSoloCount + items.length;
69
+ if (toLength > maxFreeSolo) {
70
+ items.splice(-(toLength - maxFreeSolo));
71
+ onMaxFreeSolo === null || onMaxFreeSolo === void 0 ? void 0 : onMaxFreeSolo(maxFreeSolo);
72
+ }
73
+ if (items.length > 0) {
74
+ setTags(tslib_1.__spread(tags, items.map(function (label) {
75
+ return ({
76
+ id: uniqueId_1.default(DOWNSHIFT_ID_TOKEN),
77
+ isSuggestion: false,
78
+ freeSolo: true,
79
+ label: label,
80
+ });
81
+ })));
82
+ isAddItem = true;
83
+ }
84
+ }
85
+ else {
86
+ onMaxFreeSolo === null || onMaxFreeSolo === void 0 ? void 0 : onMaxFreeSolo(maxFreeSolo);
87
+ }
88
+ onReset === null || onReset === void 0 ? void 0 : onReset(e);
89
+ }
90
+ return isAddItem;
91
+ };
92
+ var removeTag = function (selectedItem) {
93
+ var selectedItemIndex = tags.indexOf(selectedItem);
94
+ if (selectedItemIndex > -1) {
95
+ setTags(tslib_1.__spread(tags.slice(0, selectedItemIndex), tags.slice(selectedItemIndex + 1)));
96
+ }
97
+ };
98
+ var addTag = function (selectedItem) {
99
+ // * change to uniqueId
100
+ if (selectedItem.freeSolo) {
101
+ if (freeSoloCount < maxFreeSolo) {
102
+ selectedItem.id = uniqueId_1.default(DOWNSHIFT_ID_TOKEN);
103
+ }
104
+ else {
105
+ onMaxFreeSolo === null || onMaxFreeSolo === void 0 ? void 0 : onMaxFreeSolo(maxFreeSolo);
106
+ return;
107
+ }
108
+ }
109
+ if (multiple) {
110
+ setTags(tslib_1.__spread(tags, [selectedItem]));
111
+ }
112
+ else {
113
+ setTags([selectedItem]);
114
+ }
115
+ };
116
+ var handleTagKey = foundation_1.useKeyboardMoveFocus({
117
+ options: tags,
118
+ focusedIndexRef: focusedIndexRef,
119
+ infinite: 'order',
120
+ onFocusedIndexChange: function (event, toIndex) {
121
+ focusTag(toIndex);
122
+ event === null || event === void 0 ? void 0 : event.preventDefault();
123
+ },
124
+ }).onKeyFocusedIndexHandle;
125
+ /**
126
+ * All Get props methods
127
+ */
128
+ var getTagListBoxProps = function (props) {
129
+ return tslib_1.__assign({ 'aria-label': isString_1.default(labelProp) ? labelProp : undefined, 'aria-required': required || false, 'aria-disabled': disabled || false }, props);
130
+ };
131
+ var getTagProps = function (_a) {
132
+ var index = _a.index, selectedItem = _a.selectedItem, restTagProps = tslib_1.__rest(_a, ["index", "selectedItem"]);
133
+ var tabIndex =
134
+ // * when tag is not focus
135
+ !focused &&
136
+ // * and is first item
137
+ index === 0
138
+ ? 0
139
+ : -1;
140
+ return foundation_1.combineProps({
141
+ tabIndex: tabIndex,
142
+ // TODO: wait for input can complete that.
143
+ // role: 'option',
144
+ role: 'button',
145
+ key: downshiftId + "-selected-item-" + index,
146
+ 'data-tag-index': index,
147
+ 'data-item-last': index === tags.length - 1,
148
+ onClick: function (e) {
149
+ e.stopPropagation();
150
+ },
151
+ onBlur: function () { return setFocused(false); },
152
+ onFocus: function () {
153
+ focusTag(index);
154
+ setFocused(true);
155
+ },
156
+ onKeyDown: function (e) {
157
+ var focusedIndex = focusedIndexRef.current;
158
+ switch (e.key) {
159
+ case 'Backspace':
160
+ {
161
+ var lengthBeforeDelete = tags.length;
162
+ focusTag(
163
+ // * when length is 1 mean that will be remove all
164
+ lengthBeforeDelete === 1
165
+ ? -1
166
+ : focusedIndex === 0
167
+ ? 0
168
+ : focusedIndex - 1);
169
+ removeTag(selectedItem);
170
+ }
171
+ break;
172
+ case 'Delete':
173
+ {
174
+ var lengthBeforeDelete = tags.length;
175
+ focusTag(
176
+ // * when length is 1 mean that will be remove all
177
+ lengthBeforeDelete === 1
178
+ ? -1
179
+ : focusedIndex === tags.length - 1
180
+ ? focusedIndex - 1
181
+ : focusedIndex);
182
+ removeTag(selectedItem);
183
+ }
184
+ break;
185
+ default:
186
+ handleTagKey(e);
187
+ break;
188
+ }
189
+ },
190
+ onDelete: function (e) {
191
+ // * left button clicked
192
+ if (e.button === 0) {
193
+ removeTag(selectedItem);
194
+ setFocused(false);
195
+ }
196
+ },
197
+ }, restTagProps);
198
+ };
199
+ return {
200
+ focused: focused,
201
+ setFocused: setFocused,
202
+ tags: tags,
203
+ isSelectedFromAutocompleteRef: isSelectedFromAutocompleteRef,
204
+ focusedIndex: focusedIndexRef.current,
205
+ checkAndAddFreeSolo: checkAndAddFreeSolo,
206
+ getTagListBoxProps: getTagListBoxProps,
207
+ getTagProps: getTagProps,
208
+ focusTag: focusTag,
209
+ addTag: addTag,
210
+ clearTags: function () {
211
+ setTags([]);
212
+ },
213
+ blur: function () {
214
+ if (focusedIndexRef.current !== -1) {
215
+ focusTag(-1, false);
216
+ }
217
+ setFocused(false);
218
+ },
219
+ // reset: () => {
220
+ // setActiveIndex(-1);
221
+ // },
222
+ focusLast: function () {
223
+ var currTagsLength = tags.length;
224
+ if (currTagsLength > 0) {
225
+ focusTag(currTagsLength - 1);
226
+ }
227
+ },
228
+ };
229
+ };
@@ -63,7 +63,7 @@ export interface PortalController<P extends {} = {}, F = any, D extends {} = {}>
63
63
  */
64
64
  readonly data?: D;
65
65
  }
66
- export declare type UpdatePropsHandler<P extends {} = {}> = (preProps?: UncontrolledProps<P>) => UncontrolledProps<P>;
66
+ export declare type UpdatePropsHandler<P extends {} = {}> = (preProps?: UncontrolledProps<P>) => Partial<UncontrolledProps<P>>;
67
67
  export declare type ControlledProps<P extends {} = {}, F = undefined> = P & {
68
68
  onClose: (feedback?: F) => void;
69
69
  open: boolean;
@@ -1,7 +1,7 @@
1
1
  import { __assign, __makeTemplateObject, __read, __rest } from "tslib";
2
2
  import React, { forwardRef, useMemo, useRef, useState, } from 'react';
3
3
  import clsx from 'clsx';
4
- import { checkDefaultPrevented, combineClasses, combineProps, logInDev, omit, styled, useEventCallback, useForkRef, useTheme, useThemeProps, } from '../../../foundation';
4
+ import { checkDefaultPrevented, combineClasses, combineProps, logInDev, omit, styled, useEver, useEventCallback, useForkRef, useTheme, useThemeProps, } from '../../../foundation';
5
5
  import { RcMenu } from '../../Menu/Menu';
6
6
  import { RcButton } from '../Button';
7
7
  import { RcButtonGroup } from '../ButtonGroup';
@@ -25,6 +25,7 @@ var _RcSplitButton = forwardRef(function (inProps, ref) {
25
25
  var innerRef = useRef(null);
26
26
  var splitRef = useForkRef(innerRef, ref);
27
27
  var isMenuOpen = !!anchorEl;
28
+ var isMenuEverOpen = useEver(isMenuOpen);
28
29
  var _h = useMemo(function () {
29
30
  var result = combineClasses(RcSplitButtonClasses, classesProp);
30
31
  return {
@@ -156,7 +157,7 @@ var _RcSplitButton = forwardRef(function (inProps, ref) {
156
157
  React.createElement(RcButton, __assign({}, _ActionButtonProps)),
157
158
  React.createElement(RcButton, __assign({ "aria-haspopup": "listbox" }, _ControlButtonProps),
158
159
  React.createElement(StyledArrowIcon, __assign({ open: isMenuOpen, size: variantIs(['round', 'plainIcon']) ? size : toButtonSize }, ArrowIconProps)))),
159
- React.createElement(RcMenu, __assign({}, MenuProps, { anchorEl: anchorEl, open: isMenuOpen, onClose: handleClose }), children)));
160
+ isMenuEverOpen && (React.createElement(RcMenu, __assign({}, MenuProps, { anchorEl: anchorEl, open: isMenuOpen, onClose: handleClose }), children))));
160
161
  });
161
162
  var RcSplitButton = styled(_RcSplitButton)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n"], ["\n ", "\n"])), splitButtonStyle);
162
163
  RcSplitButton.displayName = 'RcSplitButton';
@@ -62,7 +62,7 @@ var _RcDownshift = memo(forwardRef(function (inProps, ref) {
62
62
  onKeyDown: onKeyDownProp,
63
63
  disabledItemsHighlightable: disabledItemsHighlightable,
64
64
  onGroupExpanded: onGroupExpanded,
65
- }), focusInput = _z.focusInput, optionItems = _z.optionItems, optionsGroupList = _z.optionsGroupList, selectedItems = _z.selectedItems, highlightedIndex = _z.highlightedIndex, getToggleButtonProps = _z.getToggleButtonProps, getTagProps = _z.getTagProps, getTagListBoxProps = _z.getTagListBoxProps, getMenuProps = _z.getMenuProps, getPopperProps = _z.getPopperProps, getInputProps = _z.getInputProps, getInputAriaProps = _z.getInputAriaProps, getLabelProps = _z.getLabelProps, getItemProps = _z.getItemProps, isOpen = _z.isOpen, inputValue = _z.inputValue, isTagsFocus = _z.isTagsFocus, activeIndex = _z.activeIndex, setActiveIndex = _z.setActiveIndex, setHighlightedIndex = _z.setHighlightedIndex, onInputChange = _z.onInputChange, keepHighlightedIndex = _z.keepHighlightedIndex, changeHighlightedIndexReason = _z.changeHighlightedIndexReason, closeMenu = _z.closeMenu, openMenu = _z.openMenu, reset = _z.reset, getClearButtonProps = _z.getClearButtonProps, noOptionItem = _z.noOptionItem, getNoOptionsProps = _z.getNoOptionsProps, isKeepHighlightedIndex = _z.isKeepHighlightedIndex, isFocused = _z.isFocused;
65
+ }), focusInput = _z.focusInput, optionItems = _z.optionItems, optionsGroupList = _z.optionsGroupList, selectedItems = _z.tags, highlightedIndex = _z.highlightedIndex, getToggleButtonProps = _z.getToggleButtonProps, getTagProps = _z.getTagProps, getTagListBoxProps = _z.getTagListBoxProps, getMenuProps = _z.getMenuProps, getPopperProps = _z.getPopperProps, getInputProps = _z.getInputProps, getInputAriaProps = _z.getInputAriaProps, getLabelProps = _z.getLabelProps, getItemProps = _z.getItemProps, isOpen = _z.isOpen, inputValue = _z.inputValue, activeIndex = _z.activeIndex, setActiveIndex = _z.setActiveIndex, setHighlightedIndex = _z.setHighlightedIndex, onInputChange = _z.onInputChange, keepHighlightedIndex = _z.keepHighlightedIndex, changeHighlightedIndexReason = _z.changeHighlightedIndexReason, closeMenu = _z.closeMenu, openMenu = _z.openMenu, reset = _z.reset, getClearButtonProps = _z.getClearButtonProps, noOptionItem = _z.noOptionItem, getNoOptionsProps = _z.getNoOptionsProps, isKeepHighlightedIndex = _z.isKeepHighlightedIndex, isDownshiftFocused = _z.focused;
66
66
  var open = Boolean((isOpen || noOptionItem) && textFieldRef.current);
67
67
  var _0 = getInputProps(), onBlur = _0.onBlur, InputProps = __rest(_0, ["onBlur"]);
68
68
  var helperText = isNew
@@ -124,7 +124,6 @@ var _RcDownshift = memo(forwardRef(function (inProps, ref) {
124
124
  } })));
125
125
  });
126
126
  })();
127
- var isDownshiftFocused = focused !== null && focused !== void 0 ? focused : (isTagsFocus || isFocused ? true : undefined);
128
127
  var endAdornment = (toggleButton || clearBtn) && (React.createElement(EndAdornment, null,
129
128
  clearBtn && (React.createElement(ClearIconButton, __assign({}, getClearButtonProps(clearButtonProps)))),
130
129
  toggleButton && (React.createElement(ArrowDownButton, __assign({ variant: "plain", "aria-hidden": true, color: isDownshiftFocused
@@ -21,7 +21,7 @@ var SuggestionList = forwardRef(function (inProps, ref) {
21
21
  var highlightedIndex = props.highlightedIndex, options = props.options, getItemProps = props.getItemProps, getMenuProps = props.getMenuProps, renderOption = props.renderOption, inputValue = props.inputValue, groupVariant = props.groupVariant, groupExpanded = props.groupExpanded, renderGroup = props.renderGroup, optionsGroupList = props.optionsGroupList, getOptionDisabled = props.getOptionDisabled, MenuItem = props.MenuItem, changeHighlightedIndexReason = props.changeHighlightedIndexReason, isKeepHighlightedIndex = props.isKeepHighlightedIndex, componentsProp = props.components, onUpdatePopper = props.onUpdatePopper, getOptionLabel = props.getOptionLabel, padding = props.padding, _a = props.maxContainerHeight, maxContainerHeight = _a === void 0 ? '100%' : _a, classNameProp = props.className, classesProp = props.classes, rest = __rest(props, ["highlightedIndex", "options", "getItemProps", "getMenuProps", "renderOption", "inputValue", "groupVariant", "groupExpanded", "renderGroup", "optionsGroupList", "getOptionDisabled", "MenuItem", "changeHighlightedIndexReason", "isKeepHighlightedIndex", "components", "onUpdatePopper", "getOptionLabel", "padding", "maxContainerHeight", "className", "classes"]);
22
22
  var vlRef = useRef(null);
23
23
  var forkVlRef = useForkRef(ref, vlRef);
24
- var isTitleGroup = groupVariant === 'normal';
24
+ var isTitleMode = groupVariant === 'normal';
25
25
  var listRef = useRef(null);
26
26
  var itemData = options;
27
27
  var itemCount = options.length;
@@ -79,7 +79,7 @@ var SuggestionList = forwardRef(function (inProps, ref) {
79
79
  changeHighlightedIndexReason !== 'mouse') {
80
80
  scrollToHighlightedIndex(prevHighlightedIndex, highlightedIndex,
81
81
  // when title group topHighlightIndex to be 1, first item is group title
82
- isTitleGroup ? 1 : 0);
82
+ isTitleMode ? 1 : 0);
83
83
  }
84
84
  });
85
85
  var handleScrolling = function (scrolling) {
@@ -98,9 +98,8 @@ var SuggestionList = forwardRef(function (inProps, ref) {
98
98
  var _b, _c, _d, _e;
99
99
  var currGroup = option.group;
100
100
  var isGroupTitle = option === (currGroup === null || currGroup === void 0 ? void 0 : currGroup.options[0]);
101
- var groupIndex = (optionsGroupList === null || optionsGroupList === void 0 ? void 0 : optionsGroupList.findIndex(function (x) { return x.group === (currGroup === null || currGroup === void 0 ? void 0 : currGroup.group); })) || 0;
102
101
  var isFixedGroupExpanded = typeof groupExpanded === 'boolean';
103
- var expandIconProps = !isTitleGroup && !isFixedGroupExpanded && isGroupTitle
102
+ var expandIconProps = !isTitleMode && !isFixedGroupExpanded && isGroupTitle
104
103
  ? (_c = (_b = option.group) === null || _b === void 0 ? void 0 : _b.getExpandIconProps) === null || _c === void 0 ? void 0 : _c.call(_b, {
105
104
  className: clsx(classes.toggle, (_a = {},
106
105
  _a[classes.expanded] = (_d = option.group) === null || _d === void 0 ? void 0 : _d.expanded,
@@ -112,7 +111,9 @@ var SuggestionList = forwardRef(function (inProps, ref) {
112
111
  className: isGroupTitle ? classes.groupTitle : undefined,
113
112
  });
114
113
  var selected = highlightedIndex === index;
115
- var resultProps = __assign(__assign(__assign({}, option), itemProps), { 'aria-setsize': itemCount - (isTitleGroup ? (optionsGroupList === null || optionsGroupList === void 0 ? void 0 : optionsGroupList.length) || 0 : 0), 'aria-posinset': index - (isTitleGroup ? groupIndex : 0), key: itemProps.id });
114
+ var resultProps = __assign(__assign(__assign({}, option), itemProps), { key: itemProps.id });
115
+ var indexInOwnGroup = resultProps.indexInOwnGroup;
116
+ delete resultProps.indexInOwnGroup;
116
117
  // * when item is not disabled, that should check outside is that disabled
117
118
  if (!resultProps.freeSolo && !resultProps.disabled && getOptionDisabled) {
118
119
  resultProps.disabled = getOptionDisabled(option);
@@ -121,6 +122,7 @@ var SuggestionList = forwardRef(function (inProps, ref) {
121
122
  inputValue: inputValue,
122
123
  selected: selected,
123
124
  index: index,
125
+ indexInOwnGroup: indexInOwnGroup,
124
126
  };
125
127
  if (renderGroup && isGroupTitle) {
126
128
  return renderGroup(resultProps, __assign(__assign({}, state), { expanded: (_e = option.group) === null || _e === void 0 ? void 0 : _e.expanded, expandIconProps: expandIconProps }));
@@ -1,9 +1,8 @@
1
- import { __read, __rest } from "tslib";
2
- import { useMemo, useRef } from 'react';
3
- import uniqueId from 'lodash/uniqueId';
1
+ import { __assign, __read, __rest } from "tslib";
2
+ import { useCallback, useEffect, useMemo, useRef, } from 'react';
4
3
  import { useControlled } from '@material-ui/core/utils';
5
- import { combineProps, useForceUpdate, useKeyboardMoveFocus, useResultRef, } from '../../../../foundation';
6
- import { DEFAULT_GET_OPTION_LABEL, isItemCanSelected, } from '../../utils';
4
+ import { combineProps, useForceUpdate, useId, useKeyboardMoveFocus, } from '../../../../foundation';
5
+ import { DEFAULT_GET_OPTION_LABEL, isItemCanSelected, } from '../../utils/DownshiftUtils';
7
6
  import { useDownshiftGroup } from '../../utils/useDownshiftGroup';
8
7
  var DEFAULT_HIGHLIGHTED_INDEX = -1;
9
8
  var componentName = 'useSuggestionList';
@@ -11,7 +10,8 @@ var componentName = 'useSuggestionList';
11
10
  * provide suggestion list with search function and expandable group with virtualized list
12
11
  */
13
12
  export var useSuggestionList = function (_a) {
14
- var inputValueProp = _a.inputValue, _b = _a.getOptionLabel, getOptionLabel = _b === void 0 ? DEFAULT_GET_OPTION_LABEL : _b, filterOptions = _a.filterOptions, disabledItemsHighlightable = _a.disabledItemsHighlightable, options = _a.options, onInputChangeProp = _a.onInputChange, inputRef = _a.inputRef, onKeyDownProp = _a.onKeyDown, onSelect = _a.onSelect, onClear = _a.onClear, getOptionDisabled = _a.getOptionDisabled, groupBy = _a.groupBy, onGroupExpanded = _a.onGroupExpanded, groupVariant = _a.groupVariant, groupExpanded = _a.groupExpanded, groupDefaultExpanded = _a.groupDefaultExpanded, getExpandIconProps = _a.getExpandIconProps;
13
+ var id = _a.id, inputValueProp = _a.inputValue, _b = _a.getOptionLabel, getOptionLabel = _b === void 0 ? DEFAULT_GET_OPTION_LABEL : _b, filterOptions = _a.filterOptions, disabledItemsHighlightable = _a.disabledItemsHighlightable, options = _a.options, onInputChangeProp = _a.onInputChange, inputRef = _a.inputRef, onKeyDownProp = _a.onKeyDown, onSelect = _a.onSelect, onClear = _a.onClear, getOptionDisabled = _a.getOptionDisabled, groupBy = _a.groupBy, onGroupExpanded = _a.onGroupExpanded, groupVariant = _a.groupVariant, groupExpanded = _a.groupExpanded, groupDefaultExpanded = _a.groupDefaultExpanded, getExpandIconProps = _a.getExpandIconProps, processFilteredResult = _a.processFilteredResult;
14
+ var isTitleMode = groupVariant === 'normal';
15
15
  var _c = __read(useControlled({
16
16
  controlled: inputValueProp,
17
17
  default: '',
@@ -22,44 +22,44 @@ export var useSuggestionList = function (_a) {
22
22
  setInputValue(newValue);
23
23
  };
24
24
  var focusInput = function () { var _a; return (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus(); };
25
- var suggestionListId = useResultRef(function () {
26
- return uniqueId("suggestion-list-");
27
- }).current;
28
- // * use -2 for us know that is init state, for recalculate defaultHighlightedIndex
25
+ var currUniqueId = useId(id || 'suggestion-list', !id);
29
26
  var highlightedIndexRef = useRef(DEFAULT_HIGHLIGHTED_INDEX);
30
- var changeHighlightedIndexReason = useRef();
27
+ var changeHighlightedIndexReasonRef = useRef();
31
28
  var forceUpdate = useForceUpdate();
29
+ var getFilteredItems = useCallback(function (items) {
30
+ if (filterOptions) {
31
+ return filterOptions(items, {
32
+ inputValue: inputValue,
33
+ getOptionLabel: getOptionLabel,
34
+ selectedItems: [],
35
+ });
36
+ }
37
+ return items;
38
+ }, [filterOptions, getOptionLabel, inputValue]);
32
39
  var filteredResult = useMemo(function () {
33
- var getFilteredItems = function (items) {
34
- if (filterOptions) {
35
- return filterOptions(items, {
36
- inputValue: inputValue,
37
- getOptionLabel: getOptionLabel,
38
- selectedItems: [],
39
- });
40
- }
41
- return items;
42
- };
43
40
  // * only when isOpen calculate the filtered result
44
41
  var results = getFilteredItems(options);
42
+ results = processFilteredResult
43
+ ? processFilteredResult(results, inputValue)
44
+ : results;
45
45
  return results;
46
- }, [filterOptions, getOptionLabel, inputValue, options]);
46
+ }, [getFilteredItems, inputValue, options, processFilteredResult]);
47
47
  var _d = useDownshiftGroup({
48
- groupBy: groupBy,
48
+ id: currUniqueId,
49
49
  options: options,
50
50
  filteredResult: filteredResult,
51
- getExpandIconProps: getExpandIconProps,
52
51
  groupExpanded: groupExpanded,
53
52
  groupDefaultExpanded: groupDefaultExpanded,
54
- onGroupExpanded: onGroupExpanded,
55
53
  groupVariant: groupVariant,
54
+ groupBy: groupBy,
55
+ getExpandIconProps: getExpandIconProps,
56
+ onGroupExpanded: onGroupExpanded,
56
57
  getOptionDisabled: getOptionDisabled,
57
- id: suggestionListId,
58
58
  }), groupedResult = _d.groupedResult, handleGroupExpandedChange = _d.handleGroupExpandedChange, optionsGroupList = _d.optionsGroupList;
59
59
  var optionItems = groupBy ? groupedResult : filteredResult;
60
60
  var setHighlightedIndex = function (index, _a) {
61
61
  var _b = _a.reRender, reRender = _b === void 0 ? false : _b, reason = _a.reason;
62
- changeHighlightedIndexReason.current = reason;
62
+ changeHighlightedIndexReasonRef.current = reason;
63
63
  if (highlightedIndexRef.current !== index) {
64
64
  highlightedIndexRef.current = index;
65
65
  if (reRender)
@@ -77,25 +77,25 @@ export var useSuggestionList = function (_a) {
77
77
  return ((!!item && item.freeSolo) ||
78
78
  (isItemCanSelected(item) && !(getOptionDisabled === null || getOptionDisabled === void 0 ? void 0 : getOptionDisabled(item))));
79
79
  };
80
- var selectItemFn = function (e, selectedItem) {
80
+ var selectItem = function (e, selectedItem) {
81
81
  if (getIsItemCanSelected(selectedItem)) {
82
82
  onSelect === null || onSelect === void 0 ? void 0 : onSelect(e, selectedItem);
83
83
  return true;
84
84
  }
85
85
  return false;
86
86
  };
87
- var resetState = function () {
87
+ var clearInput = function () {
88
88
  if (inputRef.current && inputRef.current.value.length > 0) {
89
89
  updateInputValue('');
90
90
  }
91
91
  };
92
92
  var reset = function (isFocus) {
93
- resetState();
93
+ clearInput();
94
94
  onInputChangeProp === null || onInputChangeProp === void 0 ? void 0 : onInputChangeProp('');
95
95
  if (isFocus)
96
96
  focusInput();
97
97
  };
98
- var onKeyFocusedIndexHandle = useKeyboardMoveFocus({
98
+ var _e = useKeyboardMoveFocus({
99
99
  options: optionItems,
100
100
  focusedIndexRef: highlightedIndexRef,
101
101
  infinite: true,
@@ -108,29 +108,44 @@ export var useSuggestionList = function (_a) {
108
108
  : function (child) {
109
109
  return !getIsItemCanSelected(child);
110
110
  },
111
- }).onKeyFocusedIndexHandle;
111
+ }), onKeyFocusedIndexHandle = _e.onKeyFocusedIndexHandle, getNextFocusableOption = _e.getNextFocusableOption;
112
112
  var getItemProps = function (_a) {
113
- var item = _a.item, index = _a.index, itemRest = __rest(_a, ["item", "index"]);
114
- return combineProps({
115
- id: suggestionListId + "-option-" + index,
116
- role: 'option',
117
- onClick: function (e) {
118
- selectItemFn(e, item);
119
- },
120
- onMouseDown: function (e) {
113
+ var item = _a.item, _b = _a.index, index = _b === void 0 ? 0 : _b, itemRest = __rest(_a, ["item", "index"]);
114
+ var currGroup = item.group;
115
+ var isGroupTitle = item === (currGroup === null || currGroup === void 0 ? void 0 : currGroup.options[0]);
116
+ var itemCount = optionItems.length;
117
+ var groupOrder = (currGroup === null || currGroup === void 0 ? void 0 : currGroup.order) || 0;
118
+ return combineProps(__assign({ id: currUniqueId + "-option-" + index, onClick: function (e) {
119
+ selectItem(e, item);
120
+ }, onMouseDown: function (e) {
121
121
  e.preventDefault();
122
122
  e.stopPropagation();
123
- },
124
- onMouseOver: function () {
125
- if (highlightedIndexRef.current !== index) {
123
+ }, onMouseOver: function () {
124
+ if (highlightedIndexRef.current !== index &&
125
+ getIsItemCanSelected(item)) {
126
126
  setHighlightedIndex(index, { reason: 'mouse', reRender: true });
127
127
  }
128
- },
129
- }, itemRest);
128
+ } }, (isTitleMode && isGroupTitle
129
+ ? {}
130
+ : {
131
+ role: 'option',
132
+ 'aria-setsize': itemCount - (isTitleMode ? (optionsGroupList === null || optionsGroupList === void 0 ? void 0 : optionsGroupList.length) || 0 : 0),
133
+ 'aria-posinset': index - (isTitleMode ? groupOrder + 1 : 0),
134
+ })), itemRest);
135
+ };
136
+ var handleF10KeyDown = function (e) {
137
+ var highlightedIndex = highlightedIndexRef.current;
138
+ if (e.shiftKey) {
139
+ var currOption = optionItems[highlightedIndex];
140
+ var currentGroup = currOption.group;
141
+ if (currentGroup && currentGroup.options.length > 1) {
142
+ handleGroupExpandedChange(currentGroup.group);
143
+ }
144
+ }
130
145
  };
131
146
  var getInputProps = function (props) {
132
147
  return combineProps({
133
- id: suggestionListId + "-input",
148
+ id: currUniqueId + "-input",
134
149
  autoComplete: 'off',
135
150
  onChange: function (e) {
136
151
  var changeValue = e.target.value;
@@ -139,18 +154,12 @@ export var useSuggestionList = function (_a) {
139
154
  onKeyDown: function (e) {
140
155
  var _a;
141
156
  onKeyDownProp === null || onKeyDownProp === void 0 ? void 0 : onKeyDownProp(e, highlightedIndexRef.current);
142
- var highlightedIndex = highlightedIndexRef.current;
143
157
  switch (e.key) {
144
158
  case 'F10':
145
- if (e.shiftKey) {
146
- var currOption = optionItems[highlightedIndex];
147
- var currentGroup = currOption.group;
148
- if (currentGroup && currentGroup.options.length > 1) {
149
- handleGroupExpandedChange(currentGroup.group);
150
- }
151
- }
159
+ handleF10KeyDown(e);
152
160
  break;
153
- case 'Enter':
161
+ case 'Enter': {
162
+ var highlightedIndex = highlightedIndexRef.current;
154
163
  if (e.which === 229)
155
164
  return;
156
165
  if (highlightedIndex !== DEFAULT_HIGHLIGHTED_INDEX) {
@@ -161,13 +170,14 @@ export var useSuggestionList = function (_a) {
161
170
  onClick === null || onClick === void 0 ? void 0 : onClick(e);
162
171
  }
163
172
  else {
164
- selectItemFn(e, currOption);
173
+ selectItem(e, currOption);
165
174
  }
166
175
  e.stopPropagation();
167
176
  }
168
177
  // always preventDefault for not inset all enter into textarea
169
178
  e.preventDefault();
170
179
  break;
180
+ }
171
181
  default:
172
182
  onKeyFocusedIndexHandle(e);
173
183
  break;
@@ -181,57 +191,65 @@ export var useSuggestionList = function (_a) {
181
191
  onContainerClick: focusInput,
182
192
  role: 'combobox',
183
193
  'aria-autocomplete': 'list',
184
- // TODO
185
194
  'aria-expanded': true,
186
195
  'aria-haspopup': true,
187
- 'aria-owns': suggestionListId + "-menu",
196
+ 'aria-owns': currUniqueId + "-menu",
188
197
  'aria-activedescendant': highlightedIndexRef.current > -1
189
- ? suggestionListId + "-option-" + highlightedIndexRef.current
198
+ ? currUniqueId + "-option-" + highlightedIndexRef.current
190
199
  : undefined,
191
200
  }, props);
192
201
  };
193
202
  var getLabelProps = function (props) {
194
203
  return combineProps({
195
- htmlFor: suggestionListId + "-input",
196
- id: suggestionListId + "-label",
204
+ htmlFor: currUniqueId + "-input",
205
+ id: currUniqueId + "-label",
197
206
  }, props);
198
207
  };
199
208
  var getMenuProps = function (restMenuProps) {
200
209
  return combineProps({
201
- 'aria-labelledby': suggestionListId + "-label",
202
- id: suggestionListId + "-menu",
210
+ 'aria-labelledby': currUniqueId + "-label",
211
+ id: currUniqueId + "-menu",
203
212
  role: 'listbox',
204
213
  }, restMenuProps);
205
214
  };
206
215
  var getClearButtonProps = function (props) {
207
216
  return combineProps({
208
- id: suggestionListId + "-clear-button",
217
+ id: currUniqueId + "-clear-button",
209
218
  onClick: function (e) {
210
219
  onClear === null || onClear === void 0 ? void 0 : onClear(e);
211
220
  reset(true);
212
221
  },
213
222
  }, props);
214
223
  };
215
- var resultObj = {
224
+ useEffect(function () {
225
+ changeHighlightedIndexReasonRef.current = undefined;
226
+ });
227
+ return {
228
+ reset: reset,
229
+ clearInput: clearInput,
230
+ forceUpdate: forceUpdate,
216
231
  focusInput: focusInput,
232
+ updateInputValue: updateInputValue,
233
+ selectItem: selectItem,
234
+ onKeyFocusedIndexHandle: onKeyFocusedIndexHandle,
217
235
  getClearButtonProps: getClearButtonProps,
218
236
  getLabelProps: getLabelProps,
219
237
  getMenuProps: getMenuProps,
220
238
  getInputProps: getInputProps,
221
239
  getInputAriaProps: getInputAriaProps,
222
240
  getItemProps: getItemProps,
241
+ highlightedIndexRef: highlightedIndexRef,
223
242
  highlightedIndex: highlightedIndexRef.current,
224
243
  optionItems: optionItems,
225
244
  inputValue: inputValue,
226
245
  onInputChange: handleInputChange,
227
246
  setHighlightedIndex: setHighlightedIndex,
228
- changeHighlightedIndexReason: changeHighlightedIndexReason.current,
229
- reset: reset,
230
- forceUpdate: forceUpdate,
247
+ changeHighlightedIndexReasonRef: changeHighlightedIndexReasonRef,
248
+ changeHighlightedIndexReason: changeHighlightedIndexReasonRef.current,
231
249
  optionsGroupList: optionsGroupList,
232
- /** current suggestion list id */
233
- id: suggestionListId,
250
+ /** current unique id */
251
+ id: currUniqueId,
252
+ getNextFocusableOption: getNextFocusableOption,
253
+ handleF10KeyDown: handleF10KeyDown,
234
254
  };
235
- changeHighlightedIndexReason.current = undefined;
236
- return resultObj;
237
255
  };
@@ -38,3 +38,8 @@ export var RcAutocompleteDefaultFilterOptions = function (options, _a) {
38
38
  }
39
39
  return options.filter(function (item) { return getOptionLabel === null || getOptionLabel === void 0 ? void 0 : getOptionLabel(item).toLowerCase().startsWith((inputValue === null || inputValue === void 0 ? void 0 : inputValue.toLowerCase()) || ''); });
40
40
  };
41
+ export var downshiftComponentName = 'RcDownshift';
42
+ export function stringArrToRegExp(keyToTags) {
43
+ // eslint-disable-next-line security/detect-non-literal-regexp
44
+ return new RegExp((keyToTags === null || keyToTags === void 0 ? void 0 : keyToTags.join('|')) || '', 'g');
45
+ }
@@ -1,3 +1,5 @@
1
1
  export * from './DownshiftUtils';
2
2
  export * from './useDownshift';
3
3
  export * from './useDownshiftError';
4
+ export * from './useDownshiftGroup';
5
+ export * from './useDownshiftTag';