@automattic/vip-design-system 0.32.1 → 0.33.0
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/build/system/Form/RadioBoxGroup.js +4 -1
- package/build/system/NewForm/FormAutocomplete.js +1 -0
- package/build/system/NewForm/FormAutocompleteMultiselect.js +521 -0
- package/build/system/NewForm/FormAutocompleteMultiselect.stories.js +248 -0
- package/build/system/NewForm/FormAutocompleteMultiselect.test.js +97 -0
- package/build/system/NewForm/index.js +5 -1
- package/package.json +2 -2
- package/src/system/Form/RadioBoxGroup.js +4 -0
- package/src/system/NewForm/FormAutocomplete.js +1 -0
- package/src/system/NewForm/FormAutocompleteMultiselect.js +473 -0
- package/src/system/NewForm/FormAutocompleteMultiselect.stories.jsx +162 -0
- package/src/system/NewForm/FormAutocompleteMultiselect.test.js +39 -0
- package/src/system/NewForm/index.js +3 -1
|
@@ -49,6 +49,7 @@ var RadioOption = function RadioOption(_ref) {
|
|
|
49
49
|
|
|
50
50
|
var ref = _react["default"].useRef(null);
|
|
51
51
|
|
|
52
|
+
var describedById = "input-radio-box-" + forLabel + "-description";
|
|
52
53
|
return (0, _jsxRuntime.jsxs)("div", {
|
|
53
54
|
id: "o" + forLabel,
|
|
54
55
|
sx: (0, _extends2["default"])({
|
|
@@ -88,7 +89,8 @@ var RadioOption = function RadioOption(_ref) {
|
|
|
88
89
|
sx: {
|
|
89
90
|
mr: 3,
|
|
90
91
|
mt: 3
|
|
91
|
-
}
|
|
92
|
+
},
|
|
93
|
+
"aria-describedby": describedById
|
|
92
94
|
}, restOption)), (0, _jsxRuntime.jsxs)("div", {
|
|
93
95
|
sx: {
|
|
94
96
|
mb: 0,
|
|
@@ -108,6 +110,7 @@ var RadioOption = function RadioOption(_ref) {
|
|
|
108
110
|
fontSize: 1,
|
|
109
111
|
display: 'block'
|
|
110
112
|
},
|
|
113
|
+
id: describedById,
|
|
111
114
|
children: description
|
|
112
115
|
})]
|
|
113
116
|
})]
|
|
@@ -338,6 +338,7 @@ var FormAutocomplete = /*#__PURE__*/_react["default"].forwardRef(function (_ref,
|
|
|
338
338
|
}, []);
|
|
339
339
|
(0, _react.useEffect)(function () {
|
|
340
340
|
global.document.querySelector("#" + forLabel).addEventListener('blur', function () {
|
|
341
|
+
setInputQuery(global.document.querySelector("#" + forLabel).value);
|
|
341
342
|
resetInputState();
|
|
342
343
|
});
|
|
343
344
|
}, [forwardRef]);
|
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
exports.FormAutocompleteMultiselect = void 0;
|
|
7
|
+
|
|
8
|
+
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
|
9
|
+
|
|
10
|
+
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
11
|
+
|
|
12
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
13
|
+
|
|
14
|
+
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
15
|
+
|
|
16
|
+
var _react2 = _interopRequireDefault(require("accessible-autocomplete/react"));
|
|
17
|
+
|
|
18
|
+
var _classnames = _interopRequireDefault(require("classnames"));
|
|
19
|
+
|
|
20
|
+
var _FormSelectContent = require("./FormSelectContent");
|
|
21
|
+
|
|
22
|
+
var _FormSelectArrow = require("./FormSelectArrow");
|
|
23
|
+
|
|
24
|
+
var _Label = require("../Form/Label");
|
|
25
|
+
|
|
26
|
+
var _FormSelectSearch = require("./FormSelectSearch");
|
|
27
|
+
|
|
28
|
+
var _FormSelectLoading = require("./FormSelectLoading");
|
|
29
|
+
|
|
30
|
+
var _Input = require("../Form/Input.styles");
|
|
31
|
+
|
|
32
|
+
var _Form = require("../Form");
|
|
33
|
+
|
|
34
|
+
var _ = require("../");
|
|
35
|
+
|
|
36
|
+
var _ScreenReaderText = _interopRequireDefault(require("../ScreenReaderText"));
|
|
37
|
+
|
|
38
|
+
var _md = require("react-icons/md");
|
|
39
|
+
|
|
40
|
+
var _jsxRuntime = require("theme-ui/jsx-runtime");
|
|
41
|
+
|
|
42
|
+
var _excluded = ["autoFilter", "className", "debounce", "displayMenu", "dropdownArrow", "errorMessage", "forLabel", "getOptionLabel", "getOptionValue", "hasError", "isInline", "label", "loading", "minLength", "noOptionsMessage", "onChange", "onInputChange", "options", "required", "searchIcon", "showAllValues", "source", "value"];
|
|
43
|
+
|
|
44
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
45
|
+
|
|
46
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
47
|
+
|
|
48
|
+
var baseBorderTextColors = (0, _extends2["default"])({}, _Input.baseControlBorderStyle, {
|
|
49
|
+
backgroundColor: _Input.inputBaseBackground,
|
|
50
|
+
color: _Input.inputBaseText,
|
|
51
|
+
borderRadius: 1
|
|
52
|
+
});
|
|
53
|
+
var defaultStyles = (0, _extends2["default"])({
|
|
54
|
+
width: '100%'
|
|
55
|
+
}, baseBorderTextColors, {
|
|
56
|
+
py: 0,
|
|
57
|
+
minHeight: '36px',
|
|
58
|
+
lineHeight: '36px',
|
|
59
|
+
'&:focus': function focus(theme) {
|
|
60
|
+
return theme.outline;
|
|
61
|
+
},
|
|
62
|
+
'&:focus-visible': function focusVisible(theme) {
|
|
63
|
+
return theme.outline;
|
|
64
|
+
},
|
|
65
|
+
'&:focus-within': function focusWithin(theme) {
|
|
66
|
+
return theme.outline;
|
|
67
|
+
},
|
|
68
|
+
'&.autocomplete__input--focused': function autocomplete__inputFocused(theme) {
|
|
69
|
+
return theme.outline;
|
|
70
|
+
},
|
|
71
|
+
'& .autocomplete__input': {
|
|
72
|
+
width: '100%',
|
|
73
|
+
paddingLeft: 3,
|
|
74
|
+
py: 0,
|
|
75
|
+
borderWidth: 0,
|
|
76
|
+
color: 'text',
|
|
77
|
+
minHeight: '36px',
|
|
78
|
+
lineHeight: '36px',
|
|
79
|
+
'&:focus': {
|
|
80
|
+
outlineWidth: 0,
|
|
81
|
+
boxShadow: 'none'
|
|
82
|
+
},
|
|
83
|
+
'&:focus-visible': {
|
|
84
|
+
outlineWidth: 0,
|
|
85
|
+
boxShadow: 'none'
|
|
86
|
+
},
|
|
87
|
+
'&:focus-within': {
|
|
88
|
+
outlineWidth: 0,
|
|
89
|
+
boxShadow: 'none'
|
|
90
|
+
},
|
|
91
|
+
'&.autocomplete__input--focused': {
|
|
92
|
+
outlineWidth: 0,
|
|
93
|
+
boxShadow: 'none'
|
|
94
|
+
},
|
|
95
|
+
'&.autocomplete__input--show-all-values': {
|
|
96
|
+
paddingRight: '40px'
|
|
97
|
+
},
|
|
98
|
+
'&::placeholder': {
|
|
99
|
+
color: 'input.text.placeholder',
|
|
100
|
+
opacity: 1
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
'& .autocomplete__menu': (0, _extends2["default"])({}, baseBorderTextColors),
|
|
104
|
+
'& .autocomplete__hint, & .autocomplete__input, & .autocomplete__option': {
|
|
105
|
+
fontSize: 'inherit'
|
|
106
|
+
},
|
|
107
|
+
'& .autocomplete__wrapper': {
|
|
108
|
+
width: '100%'
|
|
109
|
+
},
|
|
110
|
+
'& .autocomplete__option': {
|
|
111
|
+
borderColor: _Input.baseControlBorderStyle.borderColor
|
|
112
|
+
},
|
|
113
|
+
'& .autocomplete__option--odd': {
|
|
114
|
+
bg: 'backgroundSecondary'
|
|
115
|
+
},
|
|
116
|
+
'& .autocomplete__option:hover, & .autocomplete__option--focused': {
|
|
117
|
+
bg: 'midnight',
|
|
118
|
+
borderColor: 'midnight'
|
|
119
|
+
},
|
|
120
|
+
'& .autocomplete__input--show-all-values': {
|
|
121
|
+
paddingRight: 0
|
|
122
|
+
},
|
|
123
|
+
'& .autocomplete__hint': {
|
|
124
|
+
border: 'none',
|
|
125
|
+
paddingLeft: 3,
|
|
126
|
+
minHeight: '27px',
|
|
127
|
+
lineHeight: '27px'
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
var inlineStyles = {
|
|
131
|
+
borderWidth: 0
|
|
132
|
+
};
|
|
133
|
+
var searchIconStyles = {
|
|
134
|
+
'& .autocomplete__input.autocomplete__input': {
|
|
135
|
+
paddingLeft: 4
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
var DefaultArrow = function DefaultArrow(config) {
|
|
140
|
+
return (0, _jsxRuntime.jsx)(_FormSelectArrow.FormSelectArrow, {
|
|
141
|
+
classNames: config.className
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
var AddSelectionStatus = function AddSelectionStatus(_ref) {
|
|
146
|
+
var status = _ref.status;
|
|
147
|
+
return (0, _jsxRuntime.jsx)("div", {
|
|
148
|
+
sx: {
|
|
149
|
+
border: '0',
|
|
150
|
+
clip: 'rect(0 0 0 0)',
|
|
151
|
+
height: '1px',
|
|
152
|
+
marginBottom: '-1px',
|
|
153
|
+
marginRight: '-1px',
|
|
154
|
+
overflow: 'hidden',
|
|
155
|
+
padding: '0',
|
|
156
|
+
position: 'absolute',
|
|
157
|
+
whiteSpace: 'nowrap',
|
|
158
|
+
width: '1px'
|
|
159
|
+
},
|
|
160
|
+
id: "vip-autocompletemultiselect-status",
|
|
161
|
+
role: "status",
|
|
162
|
+
"aria-atomic": "true",
|
|
163
|
+
"aria-live": "assertive",
|
|
164
|
+
children: status
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
AddSelectionStatus.propTypes = {
|
|
169
|
+
status: _propTypes["default"].string.isRequired
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
var SelectedOptions = function SelectedOptions(_ref2) {
|
|
173
|
+
var index = _ref2.index,
|
|
174
|
+
option = _ref2.option,
|
|
175
|
+
unselectValue = _ref2.unselectValue;
|
|
176
|
+
return (0, _jsxRuntime.jsx)("div", {
|
|
177
|
+
sx: {
|
|
178
|
+
mr: 1,
|
|
179
|
+
maxWidth: '100%'
|
|
180
|
+
},
|
|
181
|
+
children: (0, _jsxRuntime.jsxs)(_.Button, {
|
|
182
|
+
variant: "tertiary",
|
|
183
|
+
onClick: function onClick(e) {
|
|
184
|
+
e.preventDefault();
|
|
185
|
+
unselectValue(option, index);
|
|
186
|
+
},
|
|
187
|
+
sx: {
|
|
188
|
+
mt: 1,
|
|
189
|
+
fontSize: 1,
|
|
190
|
+
maxWidth: '100%'
|
|
191
|
+
},
|
|
192
|
+
children: [(0, _jsxRuntime.jsx)("div", {
|
|
193
|
+
sx: {
|
|
194
|
+
overflow: 'hidden',
|
|
195
|
+
textOverflow: 'ellipsis',
|
|
196
|
+
whiteSpace: 'nowrap'
|
|
197
|
+
},
|
|
198
|
+
"aria-hidden": "true",
|
|
199
|
+
children: option
|
|
200
|
+
}), (0, _jsxRuntime.jsxs)(_ScreenReaderText["default"], {
|
|
201
|
+
children: [option + " selected. Press Space or Enter to remove.", index === 0 ? ' Press Shift Tab to add more.' : '']
|
|
202
|
+
}), (0, _jsxRuntime.jsx)(_md.MdClose, {
|
|
203
|
+
sx: {
|
|
204
|
+
ml: 2
|
|
205
|
+
}
|
|
206
|
+
})]
|
|
207
|
+
})
|
|
208
|
+
}, index);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
SelectedOptions.propTypes = {
|
|
212
|
+
index: _propTypes["default"].number.isRequired,
|
|
213
|
+
option: _propTypes["default"].string.isRequired,
|
|
214
|
+
unselectValue: _propTypes["default"].func.isRequired
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
var FormAutocompleteMultiselect = /*#__PURE__*/_react["default"].forwardRef(function (_ref3, forwardRef) {
|
|
218
|
+
var _ref3$autoFilter = _ref3.autoFilter,
|
|
219
|
+
autoFilter = _ref3$autoFilter === void 0 ? true : _ref3$autoFilter,
|
|
220
|
+
className = _ref3.className,
|
|
221
|
+
_ref3$debounce = _ref3.debounce,
|
|
222
|
+
debounce = _ref3$debounce === void 0 ? 0 : _ref3$debounce,
|
|
223
|
+
_ref3$displayMenu = _ref3.displayMenu,
|
|
224
|
+
displayMenu = _ref3$displayMenu === void 0 ? 'overlay' : _ref3$displayMenu,
|
|
225
|
+
_ref3$dropdownArrow = _ref3.dropdownArrow,
|
|
226
|
+
dropdownArrow = _ref3$dropdownArrow === void 0 ? DefaultArrow : _ref3$dropdownArrow,
|
|
227
|
+
errorMessage = _ref3.errorMessage,
|
|
228
|
+
_ref3$forLabel = _ref3.forLabel,
|
|
229
|
+
forLabel = _ref3$forLabel === void 0 ? 'vip-autocompletemultiselect' : _ref3$forLabel,
|
|
230
|
+
getOptionLabel = _ref3.getOptionLabel,
|
|
231
|
+
getOptionValue = _ref3.getOptionValue,
|
|
232
|
+
hasError = _ref3.hasError,
|
|
233
|
+
isInline = _ref3.isInline,
|
|
234
|
+
label = _ref3.label,
|
|
235
|
+
loading = _ref3.loading,
|
|
236
|
+
_ref3$minLength = _ref3.minLength,
|
|
237
|
+
minLength = _ref3$minLength === void 0 ? 0 : _ref3$minLength,
|
|
238
|
+
_ref3$noOptionsMessag = _ref3.noOptionsMessage,
|
|
239
|
+
noOptionsMessage = _ref3$noOptionsMessag === void 0 ? function () {
|
|
240
|
+
return 'No results found. Type to search.';
|
|
241
|
+
} : _ref3$noOptionsMessag,
|
|
242
|
+
_ref3$onChange = _ref3.onChange,
|
|
243
|
+
onChange = _ref3$onChange === void 0 ? function () {} : _ref3$onChange,
|
|
244
|
+
onInputChange = _ref3.onInputChange,
|
|
245
|
+
_ref3$options = _ref3.options,
|
|
246
|
+
options = _ref3$options === void 0 ? [] : _ref3$options,
|
|
247
|
+
required = _ref3.required,
|
|
248
|
+
searchIcon = _ref3.searchIcon,
|
|
249
|
+
_ref3$showAllValues = _ref3.showAllValues,
|
|
250
|
+
showAllValues = _ref3$showAllValues === void 0 ? false : _ref3$showAllValues,
|
|
251
|
+
source = _ref3.source,
|
|
252
|
+
value = _ref3.value,
|
|
253
|
+
props = (0, _objectWithoutPropertiesLoose2["default"])(_ref3, _excluded);
|
|
254
|
+
var OPTION_ACTION = {
|
|
255
|
+
ADD: 'add',
|
|
256
|
+
REMOVE: 'remove',
|
|
257
|
+
NONE: 'none'
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
var _useState = (0, _react.useState)(false),
|
|
261
|
+
isDirty = _useState[0],
|
|
262
|
+
setIsDirty = _useState[1];
|
|
263
|
+
|
|
264
|
+
var _useState2 = (0, _react.useState)([]),
|
|
265
|
+
selectedOptions = _useState2[0],
|
|
266
|
+
setSelectedOptions = _useState2[1];
|
|
267
|
+
|
|
268
|
+
var _useState3 = (0, _react.useState)(''),
|
|
269
|
+
addStatus = _useState3[0],
|
|
270
|
+
setAddStatus = _useState3[1];
|
|
271
|
+
|
|
272
|
+
var _useState4 = (0, _react.useState)({
|
|
273
|
+
action: OPTION_ACTION.NONE,
|
|
274
|
+
option: null,
|
|
275
|
+
index: -1
|
|
276
|
+
}),
|
|
277
|
+
currentOption = _useState4[0],
|
|
278
|
+
setCurrentOption = _useState4[1];
|
|
279
|
+
|
|
280
|
+
var debounceTimeout;
|
|
281
|
+
forwardRef = forwardRef || /*#__PURE__*/_react["default"].createRef();
|
|
282
|
+
/**
|
|
283
|
+
* Reset the underlying component state to show the selected value
|
|
284
|
+
*/
|
|
285
|
+
|
|
286
|
+
var resetInputState = (0, _react.useCallback)(function () {
|
|
287
|
+
var _forwardRef;
|
|
288
|
+
|
|
289
|
+
if ((_forwardRef = forwardRef) != null && _forwardRef.current) {
|
|
290
|
+
// resets the input field to the selected value or the empty string
|
|
291
|
+
forwardRef.current.setState((0, _extends2["default"])({}, forwardRef.current.state, {
|
|
292
|
+
query: '' // selected value should not be null or the component will crash
|
|
293
|
+
|
|
294
|
+
}));
|
|
295
|
+
}
|
|
296
|
+
}, [forwardRef]);
|
|
297
|
+
|
|
298
|
+
var SelectLabel = function SelectLabel() {
|
|
299
|
+
return (0, _jsxRuntime.jsx)(_Label.Label, {
|
|
300
|
+
required: required,
|
|
301
|
+
htmlFor: forLabel,
|
|
302
|
+
children: label
|
|
303
|
+
});
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
var inlineLabel = !!(isInline && label);
|
|
307
|
+
var optionLabel = (0, _react.useCallback)(function (option) {
|
|
308
|
+
return getOptionLabel ? getOptionLabel(option) : option.label;
|
|
309
|
+
}, [getOptionLabel]);
|
|
310
|
+
var getOptionByLabel = (0, _react.useCallback)(function (inputValue) {
|
|
311
|
+
return getAllOptions.find(function (option) {
|
|
312
|
+
return "" + optionLabel(option) === "" + inputValue;
|
|
313
|
+
});
|
|
314
|
+
}, [getAllOptions, optionLabel]);
|
|
315
|
+
var getAllOptions = (0, _react.useMemo)(function () {
|
|
316
|
+
return [].concat(options.filter(function (option) {
|
|
317
|
+
return !option.options;
|
|
318
|
+
}), options.filter(function (option) {
|
|
319
|
+
return option.options;
|
|
320
|
+
}).map(function (option) {
|
|
321
|
+
return option.options;
|
|
322
|
+
})).reduce(function (a, b) {
|
|
323
|
+
return a.concat(b);
|
|
324
|
+
}, []);
|
|
325
|
+
}, [options]);
|
|
326
|
+
var onValueChange = (0, _react.useCallback)(function (inputValue) {
|
|
327
|
+
if (inputValue && !selectedOptions.includes(inputValue)) {
|
|
328
|
+
setCurrentOption({
|
|
329
|
+
action: OPTION_ACTION.ADD,
|
|
330
|
+
option: inputValue
|
|
331
|
+
});
|
|
332
|
+
setSelectedOptions([].concat(selectedOptions, [inputValue]));
|
|
333
|
+
}
|
|
334
|
+
}, [getOptionByLabel, setSelectedOptions, selectedOptions, setCurrentOption]);
|
|
335
|
+
var unselectValue = (0, _react.useCallback)(function (inputValue, index) {
|
|
336
|
+
if (inputValue) {
|
|
337
|
+
setSelectedOptions(selectedOptions.filter(function (option) {
|
|
338
|
+
return ((option == null ? void 0 : option.label) || option) !== inputValue;
|
|
339
|
+
}));
|
|
340
|
+
setCurrentOption({
|
|
341
|
+
action: OPTION_ACTION.REMOVE,
|
|
342
|
+
option: inputValue,
|
|
343
|
+
index: index
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}, [getOptionByLabel, setSelectedOptions, selectedOptions, setCurrentOption]);
|
|
347
|
+
var handleTypeChange = (0, _react.useCallback)(function (query) {
|
|
348
|
+
return options.filter(function (option) {
|
|
349
|
+
return optionLabel(option).toLowerCase().indexOf(query.toLowerCase()) >= 0;
|
|
350
|
+
});
|
|
351
|
+
}, [options]);
|
|
352
|
+
var handleInputChange = (0, _react.useCallback)(function (query) {
|
|
353
|
+
if (!debounce) {
|
|
354
|
+
return onInputChange(query);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
clearTimeout(debounceTimeout);
|
|
358
|
+
|
|
359
|
+
if (!query.length || query.length >= minLength) {
|
|
360
|
+
debounceTimeout = setTimeout(function () {
|
|
361
|
+
onInputChange(query);
|
|
362
|
+
}, debounce);
|
|
363
|
+
}
|
|
364
|
+
}, [onInputChange, debounce, minLength]);
|
|
365
|
+
var suggest = (0, _react.useCallback)(function (query, populateResults) {
|
|
366
|
+
var _data;
|
|
367
|
+
|
|
368
|
+
var data = options;
|
|
369
|
+
|
|
370
|
+
if (isDirty && onInputChange) {
|
|
371
|
+
handleInputChange(query);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (isDirty && autoFilter) {
|
|
375
|
+
data = handleTypeChange(query);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
var optionForDisplay = (_data = data) == null ? void 0 : _data.map(function (option) {
|
|
379
|
+
return optionLabel(option);
|
|
380
|
+
});
|
|
381
|
+
populateResults(optionForDisplay.filter(function (option) {
|
|
382
|
+
return !selectedOptions.includes(option);
|
|
383
|
+
}));
|
|
384
|
+
}, [autoFilter, isDirty, onInputChange, options, selectedOptions]);
|
|
385
|
+
(0, _react.useEffect)(function () {
|
|
386
|
+
global.document.querySelector('.autocomplete__input').removeAttribute('aria-activedescendant');
|
|
387
|
+
}, []);
|
|
388
|
+
(0, _react.useEffect)(function () {
|
|
389
|
+
global.document.querySelector('.autocomplete__menu').setAttribute('aria-label', label + " list");
|
|
390
|
+
}, [label]);
|
|
391
|
+
(0, _react.useEffect)(function () {
|
|
392
|
+
var input = global.document.querySelector("#" + forLabel);
|
|
393
|
+
|
|
394
|
+
if (!input || required === undefined) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
input.setAttribute('aria-required', required);
|
|
399
|
+
}, [required]);
|
|
400
|
+
(0, _react.useEffect)(function () {
|
|
401
|
+
global.document.querySelector("#" + forLabel).addEventListener('keydown', function () {
|
|
402
|
+
setIsDirty(true);
|
|
403
|
+
});
|
|
404
|
+
}, [setIsDirty]); // For accessibility, we need to add the error message to the aria-describedby attribute
|
|
405
|
+
|
|
406
|
+
(0, _react.useEffect)(function () {
|
|
407
|
+
var input = global.document.querySelector("#" + forLabel);
|
|
408
|
+
input == null ? void 0 : input.setAttribute('aria-describedby', "describe-" + forLabel + "-validation " + input.getAttribute('aria-describedby'));
|
|
409
|
+
}, []); // Update selectedOption and reset the input state on select input change
|
|
410
|
+
|
|
411
|
+
(0, _react.useEffect)(function () {
|
|
412
|
+
onChange(selectedOptions, selectedOptions.map(function (option) {
|
|
413
|
+
return (option == null ? void 0 : option.label) || option;
|
|
414
|
+
}));
|
|
415
|
+
resetInputState();
|
|
416
|
+
}, [selectedOptions]); // Update the select status for screen readers
|
|
417
|
+
|
|
418
|
+
(0, _react.useEffect)(function () {
|
|
419
|
+
if (currentOption.action === OPTION_ACTION.ADD) {
|
|
420
|
+
setAddStatus(currentOption.option + " added to the list.");
|
|
421
|
+
setCurrentOption({
|
|
422
|
+
action: OPTION_ACTION.NONE,
|
|
423
|
+
option: null
|
|
424
|
+
});
|
|
425
|
+
} else if (currentOption.index === selectedOptions.length && selectedOptions.length > 0) {
|
|
426
|
+
// Move focus to the first selected item, if the last element is removed and there are other elements in the list
|
|
427
|
+
global.document.querySelector('.vip-button-component').focus();
|
|
428
|
+
} else if (selectedOptions.length === 0) {
|
|
429
|
+
// Move focus to the input field if the last element is removed and there are no other elements in the list
|
|
430
|
+
global.document.querySelector('.autocomplete__input').focus();
|
|
431
|
+
}
|
|
432
|
+
}, [currentOption]);
|
|
433
|
+
return (0, _jsxRuntime.jsxs)("div", {
|
|
434
|
+
className: (0, _classnames["default"])('vip-form-autocomplete-component', className),
|
|
435
|
+
children: [label && !isInline && (0, _jsxRuntime.jsx)(SelectLabel, {}), (0, _jsxRuntime.jsx)("div", {
|
|
436
|
+
sx: (0, _extends2["default"])({}, defaultStyles, isInline && inlineStyles, searchIcon && searchIconStyles),
|
|
437
|
+
children: (0, _jsxRuntime.jsxs)(_FormSelectContent.FormSelectContent, {
|
|
438
|
+
isInline: inlineLabel,
|
|
439
|
+
label: inlineLabel ? (0, _jsxRuntime.jsx)(SelectLabel, {}) : null,
|
|
440
|
+
children: [searchIcon && (0, _jsxRuntime.jsx)(_FormSelectSearch.FormSelectSearch, {}), (0, _jsxRuntime.jsx)(_react2["default"], (0, _extends2["default"])({
|
|
441
|
+
id: forLabel,
|
|
442
|
+
"aria-busy": loading,
|
|
443
|
+
showAllValues: showAllValues,
|
|
444
|
+
ref: forwardRef,
|
|
445
|
+
source: source || suggest,
|
|
446
|
+
defaultValue: value,
|
|
447
|
+
displayMenu: displayMenu,
|
|
448
|
+
onConfirm: onValueChange,
|
|
449
|
+
tNoResults: noOptionsMessage,
|
|
450
|
+
required: required,
|
|
451
|
+
dropdownArrow: showAllValues ? dropdownArrow : function () {
|
|
452
|
+
return '';
|
|
453
|
+
},
|
|
454
|
+
confirmOnBlur: false
|
|
455
|
+
}, props)), addStatus && (0, _jsxRuntime.jsx)(AddSelectionStatus, {
|
|
456
|
+
status: addStatus
|
|
457
|
+
}), loading && (0, _jsxRuntime.jsx)(_FormSelectLoading.FormSelectLoading, {
|
|
458
|
+
sx: {
|
|
459
|
+
right: showAllValues ? 40 : 10
|
|
460
|
+
}
|
|
461
|
+
})]
|
|
462
|
+
})
|
|
463
|
+
}), (0, _jsxRuntime.jsxs)(_.Flex, {
|
|
464
|
+
sx: {
|
|
465
|
+
mt: 2,
|
|
466
|
+
justifyContent: 'space-between'
|
|
467
|
+
},
|
|
468
|
+
children: [hasError && errorMessage && (0, _jsxRuntime.jsx)(_Form.Validation, {
|
|
469
|
+
isValid: false,
|
|
470
|
+
describedId: forLabel,
|
|
471
|
+
children: errorMessage
|
|
472
|
+
}), (0, _jsxRuntime.jsxs)("div", {
|
|
473
|
+
sx: {
|
|
474
|
+
fontSize: 1
|
|
475
|
+
},
|
|
476
|
+
children: [selectedOptions.length, " item", selectedOptions.length > 1 ? 's' : '', " selected"]
|
|
477
|
+
})]
|
|
478
|
+
}), (0, _jsxRuntime.jsx)("div", {
|
|
479
|
+
sx: {
|
|
480
|
+
display: 'inline-flex',
|
|
481
|
+
flexWrap: 'wrap',
|
|
482
|
+
maxWidth: '100%'
|
|
483
|
+
},
|
|
484
|
+
children: selectedOptions && selectedOptions.map(function (option, idx) {
|
|
485
|
+
return (0, _jsxRuntime.jsx)(SelectedOptions, {
|
|
486
|
+
index: idx,
|
|
487
|
+
option: option,
|
|
488
|
+
unselectValue: unselectValue
|
|
489
|
+
}, idx);
|
|
490
|
+
})
|
|
491
|
+
})]
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
exports.FormAutocompleteMultiselect = FormAutocompleteMultiselect;
|
|
496
|
+
FormAutocompleteMultiselect.propTypes = {
|
|
497
|
+
autoFilter: _propTypes["default"].bool,
|
|
498
|
+
className: _propTypes["default"].any,
|
|
499
|
+
debounce: _propTypes["default"].number,
|
|
500
|
+
displayMenu: _propTypes["default"].string,
|
|
501
|
+
errorMessage: _propTypes["default"].string,
|
|
502
|
+
forLabel: _propTypes["default"].string,
|
|
503
|
+
getOptionLabel: _propTypes["default"].func,
|
|
504
|
+
getOptionValue: _propTypes["default"].func,
|
|
505
|
+
hasError: _propTypes["default"].bool,
|
|
506
|
+
isInline: _propTypes["default"].bool,
|
|
507
|
+
label: _propTypes["default"].string,
|
|
508
|
+
loading: _propTypes["default"].bool,
|
|
509
|
+
minLength: _propTypes["default"].number,
|
|
510
|
+
noOptionsMessage: _propTypes["default"].func,
|
|
511
|
+
onChange: _propTypes["default"].func,
|
|
512
|
+
onInputChange: _propTypes["default"].func,
|
|
513
|
+
options: _propTypes["default"].array,
|
|
514
|
+
required: _propTypes["default"].bool,
|
|
515
|
+
searchIcon: _propTypes["default"].bool,
|
|
516
|
+
showAllValues: _propTypes["default"].bool,
|
|
517
|
+
source: _propTypes["default"].func,
|
|
518
|
+
value: _propTypes["default"].string,
|
|
519
|
+
dropdownArrow: _propTypes["default"].node
|
|
520
|
+
};
|
|
521
|
+
FormAutocompleteMultiselect.displayName = 'FormAutocompleteMultiselect';
|