grommet 2.25.2 → 2.26.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.
Files changed (111) hide show
  1. package/components/Anchor/Anchor.js +1 -1
  2. package/components/Box/stories/BackgroundThemed.js +54 -0
  3. package/components/Box/stories/CustomThemed/ThemedBackground.tsx +176 -0
  4. package/components/Button/Button.js +2 -2
  5. package/components/Calendar/Calendar.js +2 -1
  6. package/components/CheckBoxGroup/CheckBoxGroup.js +3 -2
  7. package/components/CheckBoxGroup/index.d.ts +2 -1
  8. package/components/CheckBoxGroup/propTypes.js +1 -0
  9. package/components/FormField/FormField.js +1 -1
  10. package/components/Heading/Heading.js +31 -2
  11. package/components/Heading/StyledHeading.js +1 -1
  12. package/components/Heading/index.d.ts +1 -0
  13. package/components/Heading/propTypes.js +1 -0
  14. package/components/Heading/stories/All.js +4 -1
  15. package/components/Layer/LayerContainer.js +16 -9
  16. package/components/List/List.js +131 -33
  17. package/components/List/index.d.ts +1 -0
  18. package/components/List/propTypes.js +1 -0
  19. package/components/List/stories/Pinned.js +36 -0
  20. package/components/Notification/stories/CustomThemed/ThemedNotification.tsx +2 -2
  21. package/components/Select/DefaultSelectTextInput.js +43 -0
  22. package/components/Select/EmptySearchOption.js +34 -0
  23. package/components/Select/Select.js +30 -95
  24. package/components/Select/SelectContainer.js +19 -59
  25. package/components/Select/StyledSelect.js +58 -2
  26. package/components/Select/index.d.ts +11 -9
  27. package/components/Select/propTypes.js +44 -40
  28. package/components/Select/utils.js +103 -3
  29. package/components/SelectMultiple/README.md +2 -0
  30. package/components/SelectMultiple/SelectMultiple.js +476 -0
  31. package/components/SelectMultiple/SelectMultipleContainer.js +481 -0
  32. package/components/SelectMultiple/SelectMultipleValue.js +166 -0
  33. package/components/SelectMultiple/SelectionSummary.js +104 -0
  34. package/components/SelectMultiple/index.d.ts +21 -0
  35. package/components/SelectMultiple/index.js +8 -0
  36. package/components/SelectMultiple/propTypes.js +28 -0
  37. package/components/SelectMultiple/stories/Default.js +76 -0
  38. package/components/SelectMultiple/stories/Disabled.js +84 -0
  39. package/components/SelectMultiple/stories/FormField.js +66 -0
  40. package/components/SelectMultiple/stories/Limited.js +68 -0
  41. package/components/SelectMultiple/stories/LongList.js +76 -0
  42. package/components/SelectMultiple/stories/ObjectOptions.js +80 -0
  43. package/components/SelectMultiple/stories/ShowSelectedInline.js +76 -0
  44. package/components/SelectMultiple/stories/typescript/Children.tsx +106 -0
  45. package/components/index.d.ts +1 -0
  46. package/components/index.js +8 -0
  47. package/es6/components/Anchor/Anchor.js +2 -2
  48. package/es6/components/Box/stories/BackgroundThemed.js +39 -0
  49. package/es6/components/Box/stories/CustomThemed/ThemedBackground.tsx +176 -0
  50. package/es6/components/Button/Button.js +3 -3
  51. package/es6/components/Calendar/Calendar.js +2 -1
  52. package/es6/components/CheckBoxGroup/CheckBoxGroup.js +3 -2
  53. package/es6/components/CheckBoxGroup/index.d.ts +2 -1
  54. package/es6/components/CheckBoxGroup/propTypes.js +1 -0
  55. package/es6/components/FormField/FormField.js +1 -1
  56. package/es6/components/Heading/Heading.js +30 -3
  57. package/es6/components/Heading/StyledHeading.js +1 -1
  58. package/es6/components/Heading/index.d.ts +1 -0
  59. package/es6/components/Heading/propTypes.js +1 -0
  60. package/es6/components/Heading/stories/All.js +4 -1
  61. package/es6/components/Layer/LayerContainer.js +16 -9
  62. package/es6/components/List/List.js +131 -34
  63. package/es6/components/List/index.d.ts +1 -0
  64. package/es6/components/List/propTypes.js +1 -0
  65. package/es6/components/List/stories/Pinned.js +21 -0
  66. package/es6/components/Notification/stories/CustomThemed/ThemedNotification.tsx +2 -2
  67. package/es6/components/Select/DefaultSelectTextInput.js +31 -0
  68. package/es6/components/Select/EmptySearchOption.js +21 -0
  69. package/es6/components/Select/Select.js +26 -89
  70. package/es6/components/Select/SelectContainer.js +19 -58
  71. package/es6/components/Select/StyledSelect.js +45 -1
  72. package/es6/components/Select/index.d.ts +11 -9
  73. package/es6/components/Select/propTypes.js +41 -39
  74. package/es6/components/Select/utils.js +75 -1
  75. package/es6/components/SelectMultiple/README.md +2 -0
  76. package/es6/components/SelectMultiple/SelectMultiple.js +447 -0
  77. package/es6/components/SelectMultiple/SelectMultipleContainer.js +453 -0
  78. package/es6/components/SelectMultiple/SelectMultipleValue.js +152 -0
  79. package/es6/components/SelectMultiple/SelectionSummary.js +91 -0
  80. package/es6/components/SelectMultiple/index.d.ts +21 -0
  81. package/es6/components/SelectMultiple/index.js +1 -0
  82. package/es6/components/SelectMultiple/propTypes.js +18 -0
  83. package/es6/components/SelectMultiple/stories/Default.js +60 -0
  84. package/es6/components/SelectMultiple/stories/Disabled.js +68 -0
  85. package/es6/components/SelectMultiple/stories/FormField.js +50 -0
  86. package/es6/components/SelectMultiple/stories/Limited.js +53 -0
  87. package/es6/components/SelectMultiple/stories/LongList.js +63 -0
  88. package/es6/components/SelectMultiple/stories/ObjectOptions.js +64 -0
  89. package/es6/components/SelectMultiple/stories/ShowSelectedInline.js +61 -0
  90. package/es6/components/SelectMultiple/stories/typescript/Children.tsx +106 -0
  91. package/es6/components/index.d.ts +1 -0
  92. package/es6/components/index.js +1 -0
  93. package/es6/index.d.ts +1 -0
  94. package/es6/themes/base.d.ts +8 -2
  95. package/es6/themes/base.js +54 -38
  96. package/es6/themes/grommet.js +15 -0
  97. package/es6/utils/DOM.js +4 -0
  98. package/es6/utils/background.js +65 -12
  99. package/es6/utils/general-prop-types.js +2 -0
  100. package/es6/utils/index.d.ts +2 -0
  101. package/grommet.min.js +1 -1
  102. package/grommet.min.js.map +1 -1
  103. package/index.d.ts +1 -0
  104. package/package.json +7 -6
  105. package/themes/base.d.ts +8 -2
  106. package/themes/base.js +55 -38
  107. package/themes/grommet.js +15 -0
  108. package/utils/DOM.js +9 -2
  109. package/utils/background.js +64 -12
  110. package/utils/general-prop-types.js +2 -0
  111. package/utils/index.d.ts +2 -0
@@ -0,0 +1,453 @@
1
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
+
3
+ import React, { forwardRef, useCallback, useContext, useEffect, useRef, useState } from 'react';
4
+ import { ThemeContext } from 'styled-components';
5
+ import { FormUp } from 'grommet-icons/icons/FormUp';
6
+ import { setFocusWithoutScroll } from '../../utils';
7
+ import { defaultProps } from '../../default-props';
8
+ import { Box } from '../Box';
9
+ import { Button } from '../Button';
10
+ import { CheckBox } from '../CheckBox';
11
+ import { InfiniteScroll } from '../InfiniteScroll';
12
+ import { Keyboard } from '../Keyboard';
13
+ import { Text } from '../Text';
14
+ import { TextInput } from '../TextInput';
15
+ import { SelectionSummary } from './SelectionSummary';
16
+ import { StyledContainer, OptionsContainer, SelectOption } from '../Select/StyledSelect';
17
+ import { applyKey, getOptionLabel, getOptionValue, useDisabled } from '../Select/utils';
18
+ import { EmptySearchOption } from '../Select/EmptySearchOption';
19
+ var SelectMultipleContainer = /*#__PURE__*/forwardRef(function (_ref, ref) {
20
+ var _optionsRef$current;
21
+
22
+ var allOptions = _ref.allOptions,
23
+ _ref$children = _ref.children,
24
+ children = _ref$children === void 0 ? null : _ref$children,
25
+ disabledProp = _ref.disabled,
26
+ disabledKey = _ref.disabledKey,
27
+ dropHeight = _ref.dropHeight,
28
+ _ref$emptySearchMessa = _ref.emptySearchMessage,
29
+ emptySearchMessage = _ref$emptySearchMessa === void 0 ? 'No matches found' : _ref$emptySearchMessa,
30
+ help = _ref.help,
31
+ id = _ref.id,
32
+ labelKey = _ref.labelKey,
33
+ limit = _ref.limit,
34
+ onChange = _ref.onChange,
35
+ onClose = _ref.onClose,
36
+ onKeyDown = _ref.onKeyDown,
37
+ onMore = _ref.onMore,
38
+ onSearch = _ref.onSearch,
39
+ optionIndexesInValue = _ref.optionIndexesInValue,
40
+ options = _ref.options,
41
+ _ref$replace = _ref.replace,
42
+ replace = _ref$replace === void 0 ? true : _ref$replace,
43
+ searchPlaceholder = _ref.searchPlaceholder,
44
+ search = _ref.search,
45
+ setSearch = _ref.setSearch,
46
+ usingKeyboard = _ref.usingKeyboard,
47
+ _ref$value = _ref.value,
48
+ value = _ref$value === void 0 ? [] : _ref$value,
49
+ valueKey = _ref.valueKey,
50
+ showSelectedInline = _ref.showSelectedInline;
51
+ var theme = useContext(ThemeContext) || defaultProps.theme;
52
+
53
+ var _useState = useState(-1),
54
+ activeIndex = _useState[0],
55
+ setActiveIndex = _useState[1];
56
+
57
+ var _useState2 = useState(usingKeyboard),
58
+ keyboardNavigation = _useState2[0],
59
+ setKeyboardNavigation = _useState2[1];
60
+
61
+ var searchRef = useRef();
62
+ var optionsRef = useRef();
63
+
64
+ var _useState3 = useState(disabledProp),
65
+ disabled = _useState3[0],
66
+ setDisabled = _useState3[1];
67
+
68
+ var activeRef = useRef();
69
+
70
+ var _useState4 = useState(),
71
+ showA11yLimit = _useState4[0],
72
+ setShowA11yLimit = _useState4[1];
73
+
74
+ var clearRef = useRef();
75
+ var isDisabled = useDisabled(disabled, disabledKey, options, valueKey || labelKey); // for keyboard/screenreader, keep the active option in focus
76
+
77
+ useEffect(function () {
78
+ var _activeRef$current;
79
+
80
+ if (activeIndex) (_activeRef$current = activeRef.current) == null ? void 0 : _activeRef$current.focus();
81
+ }, [activeIndex]); // set initial focus
82
+
83
+ useEffect(function () {
84
+ // need to wait for Drop to be ready
85
+ var timer = setTimeout(function () {
86
+ var clearButton = clearRef.current;
87
+
88
+ if (clearButton && clearButton.focus) {
89
+ setFocusWithoutScroll(clearButton);
90
+ } else if (searchRef && searchRef.current) {
91
+ var searchInput = searchRef.current;
92
+
93
+ if (searchInput && searchInput.focus) {
94
+ setFocusWithoutScroll(searchInput);
95
+ }
96
+ } else if (activeRef.current) {
97
+ setFocusWithoutScroll(activeRef.current);
98
+ } else if (optionsRef.current) {
99
+ setActiveIndex(0);
100
+ }
101
+ }, 100); // Drop should be open after 100ms
102
+
103
+ return function () {
104
+ return clearTimeout(timer);
105
+ };
106
+ }, []);
107
+ useEffect(function () {
108
+ var optionsNode = optionsRef.current;
109
+
110
+ if (optionsNode.children) {
111
+ var optionNode = optionsNode.children[activeIndex];
112
+ if (optionNode) optionNode.focus();
113
+ }
114
+ }, [activeIndex]);
115
+ var isSelected = useCallback(function (index) {
116
+ var result;
117
+ var optionVal = getOptionValue(index, options, valueKey || labelKey);
118
+
119
+ if (value) {
120
+ if (value.length === 0) {
121
+ result = false;
122
+ } else if (typeof value[0] !== 'object') {
123
+ result = value.indexOf(optionVal) !== -1;
124
+ } else if (valueKey) {
125
+ result = value.some(function (valueItem) {
126
+ var valueValue = typeof valueKey === 'function' ? valueKey(valueItem) : valueItem[valueKey];
127
+ return valueValue === optionVal;
128
+ });
129
+ }
130
+ }
131
+
132
+ return result;
133
+ }, [value, valueKey, options, labelKey]);
134
+ var selectOption = useCallback(function (index) {
135
+ return function (event) {
136
+ if (onChange) {
137
+ var nextOptionIndexesInValue = optionIndexesInValue.slice(0);
138
+ var allOptionsIndex = allOptions.indexOf(options[index]);
139
+ var valueIndex = optionIndexesInValue.indexOf(allOptionsIndex);
140
+
141
+ if (valueIndex === -1 && (!limit || (value == null ? void 0 : value.length) < limit)) {
142
+ nextOptionIndexesInValue.push(allOptionsIndex);
143
+ } else {
144
+ nextOptionIndexesInValue.splice(valueIndex, 1);
145
+ }
146
+
147
+ var nextValue = nextOptionIndexesInValue.map(function (i) {
148
+ return valueKey && valueKey.reduce ? applyKey(allOptions[i], valueKey) : allOptions[i];
149
+ });
150
+ var nextSelected = nextOptionIndexesInValue;
151
+ onChange(event, {
152
+ option: options[index],
153
+ value: nextValue,
154
+ selected: nextSelected
155
+ });
156
+ }
157
+ };
158
+ }, [limit, onChange, optionIndexesInValue, options, allOptions, valueKey, value]);
159
+ var onNextOption = useCallback(function (event) {
160
+ event.preventDefault();
161
+ var nextActiveIndex = activeIndex + 1;
162
+
163
+ if (nextActiveIndex !== options.length) {
164
+ setActiveIndex(nextActiveIndex);
165
+ setKeyboardNavigation(true);
166
+ }
167
+ }, [activeIndex, options]);
168
+ var onPreviousOption = useCallback(function (event) {
169
+ event.preventDefault();
170
+ var nextActiveIndex = activeIndex - 1;
171
+
172
+ if (nextActiveIndex === -1) {
173
+ var searchInput = searchRef.current;
174
+
175
+ if (searchInput && searchInput.focus) {
176
+ setActiveIndex(nextActiveIndex);
177
+ setFocusWithoutScroll(searchInput);
178
+ }
179
+ }
180
+
181
+ if (nextActiveIndex >= 0) {
182
+ setActiveIndex(nextActiveIndex);
183
+ setKeyboardNavigation(true);
184
+ }
185
+ }, [activeIndex]);
186
+ var onKeyDownOption = useCallback(function (event) {
187
+ if (!onSearch) {
188
+ var nextActiveIndex = options.findIndex(function (e) {
189
+ var label;
190
+
191
+ if (typeof e === 'object') {
192
+ label = e.label || applyKey(e, labelKey);
193
+ } else {
194
+ label = e;
195
+ }
196
+
197
+ return typeof label === 'string' && label.charAt(0).toLowerCase() === event.key.toLowerCase();
198
+ });
199
+
200
+ if (nextActiveIndex >= 0) {
201
+ event.preventDefault();
202
+ setActiveIndex(nextActiveIndex);
203
+ setKeyboardNavigation(true);
204
+ }
205
+ }
206
+
207
+ if (onKeyDown) {
208
+ onKeyDown(event);
209
+ }
210
+ }, [onKeyDown, options, onSearch, labelKey]);
211
+ var onActiveOption = useCallback(function (index) {
212
+ return function () {
213
+ if (!keyboardNavigation) setActiveIndex(index);
214
+ };
215
+ }, [keyboardNavigation]);
216
+ var onSelectOption = useCallback(function (event) {
217
+ if (!isDisabled(activeIndex) && activeIndex >= 0 && activeIndex < options.length) {
218
+ event.preventDefault(); // prevent submitting forms
219
+
220
+ selectOption(activeIndex)(event);
221
+ }
222
+ }, [activeIndex, selectOption, options, isDisabled]);
223
+ var customSearchInput = theme.select.searchInput;
224
+ var SelectTextInput = customSearchInput || TextInput;
225
+ var selectOptionsStyle = theme.select.options ? _extends({}, theme.select.options.box, theme.select.options.container) : {}; // handle when limit is reached
226
+
227
+ useEffect(function () {
228
+ var originallyDisabled = function originallyDisabled(index) {
229
+ var option = allOptions[index];
230
+ var result;
231
+
232
+ if (disabledKey) {
233
+ result = applyKey(option, disabledKey);
234
+ } else if (Array.isArray(disabledProp)) {
235
+ if (typeof disabledProp[0] === 'number') {
236
+ result = disabledProp.indexOf(index) !== -1;
237
+ } else {
238
+ result = disabledProp.indexOf(getOptionValue(index, options, valueKey || labelKey)) !== -1;
239
+ }
240
+ }
241
+
242
+ return result;
243
+ };
244
+
245
+ if (value && limit) {
246
+ if (value.length === limit) {
247
+ var newDisabled = [].concat(disabledProp); // disable everything that is not selected
248
+
249
+ for (var i = 0; i < options.length; i += 1) {
250
+ if (!isSelected(i) && !originallyDisabled(i)) {
251
+ newDisabled.push(options[i]);
252
+ }
253
+ }
254
+
255
+ if (usingKeyboard) setShowA11yLimit('Selected. Maximum selection limit reached.');
256
+ setDisabled(newDisabled);
257
+ } else {
258
+ if (usingKeyboard) setShowA11yLimit(undefined);
259
+ setDisabled(disabledProp);
260
+ }
261
+ }
262
+ }, [isSelected, value, limit, disabledProp, allOptions, disabledKey, labelKey, options, usingKeyboard, valueKey]); // reset showA11yLimit after announcement is read
263
+
264
+ useEffect(function () {
265
+ if (showA11yLimit !== undefined) {
266
+ setTimeout(function () {
267
+ setShowA11yLimit(undefined);
268
+ }, 2000); // value chosen based on length of a11yLimit message
269
+ }
270
+ }, [showA11yLimit]);
271
+ var summaryContent = /*#__PURE__*/React.createElement(SelectionSummary, {
272
+ allOptions: allOptions,
273
+ clearRef: clearRef,
274
+ disabled: disabled,
275
+ disabledKey: disabledKey,
276
+ isSelected: isSelected,
277
+ labelKey: labelKey,
278
+ limit: limit,
279
+ onChange: onChange,
280
+ onMore: onMore,
281
+ options: options,
282
+ search: search,
283
+ setActiveIndex: setActiveIndex,
284
+ showSelectedInline: showSelectedInline,
285
+ value: value,
286
+ valueKey: valueKey
287
+ });
288
+ if (showSelectedInline) summaryContent = /*#__PURE__*/React.createElement(Box, {
289
+ direction: "row",
290
+ justify: "between",
291
+ flex: false
292
+ }, summaryContent, /*#__PURE__*/React.createElement(Button, {
293
+ onClick: onClose,
294
+ a11yTitle: "Close Select"
295
+ }, /*#__PURE__*/React.createElement(Box, {
296
+ fill: true,
297
+ alignSelf: "start",
298
+ pad: {
299
+ right: 'small',
300
+ top: 'small'
301
+ }
302
+ }, /*#__PURE__*/React.createElement(FormUp, null))));
303
+ return /*#__PURE__*/React.createElement(Keyboard, {
304
+ onEnter: onSelectOption,
305
+ onSpace: onSelectOption,
306
+ onUp: onPreviousOption,
307
+ onDown: onNextOption,
308
+ onKeyDown: onKeyDownOption,
309
+ onEsc: onClose
310
+ }, /*#__PURE__*/React.createElement(StyledContainer, {
311
+ ref: ref,
312
+ as: Box,
313
+ id: id ? id + "__select-drop" : undefined,
314
+ dropHeight: dropHeight,
315
+ a11yTitle: "Select dropdown"
316
+ }, summaryContent, onSearch && /*#__PURE__*/React.createElement(Box, {
317
+ pad: !customSearchInput ? 'xsmall' : undefined,
318
+ flex: false
319
+ }, /*#__PURE__*/React.createElement(Keyboard, {
320
+ onEnter: function onEnter(event) {
321
+ onNextOption(event);
322
+ }
323
+ }, /*#__PURE__*/React.createElement(SelectTextInput, {
324
+ a11yTitle: "Search to filter options.",
325
+ focusIndicator: !customSearchInput,
326
+ size: "small",
327
+ ref: searchRef,
328
+ type: "search",
329
+ value: search || '',
330
+ placeholder: searchPlaceholder,
331
+ onChange: function onChange(event) {
332
+ var nextSearch = event.target.value;
333
+ setSearch(nextSearch);
334
+ setActiveIndex(-1);
335
+ onSearch(nextSearch);
336
+ }
337
+ }))), /*#__PURE__*/React.createElement(Box, {
338
+ flex: false
339
+ }, help), /*#__PURE__*/React.createElement(OptionsContainer, {
340
+ role: "listbox",
341
+ tabIndex: "0",
342
+ ref: optionsRef,
343
+ "aria-multiselectable": true,
344
+ onMouseMove: function onMouseMove() {
345
+ return setKeyboardNavigation(false);
346
+ },
347
+ "aria-activedescendant": optionsRef == null ? void 0 : (_optionsRef$current = optionsRef.current) == null ? void 0 : _optionsRef$current.children[activeIndex]
348
+ }, options.length > 0 ? /*#__PURE__*/React.createElement(InfiniteScroll, {
349
+ items: options,
350
+ step: theme.select.step,
351
+ onMore: onMore,
352
+ replace: replace,
353
+ show: activeIndex !== -1 ? activeIndex : undefined
354
+ }, function (option, index, optionRef) {
355
+ var optionDisabled = isDisabled(index);
356
+ var optionSelected = value ? value.includes(valueKey && valueKey.reduce ? applyKey(option, valueKey) : option) : false;
357
+ var optionActive = activeIndex === index;
358
+ var optionLabel = getOptionLabel(index, options, labelKey || valueKey); // Determine whether the label is done as a child or
359
+ // as an option Button kind property.
360
+
361
+ var child;
362
+ var textComponent = false;
363
+
364
+ if (children) {
365
+ child = children(option, index, options, {
366
+ active: optionActive,
367
+ disabled: optionDisabled,
368
+ selected: optionSelected
369
+ });
370
+ if (typeof child === 'string' || child.props && child.props.children && typeof child.props.children === 'string') textComponent = true;
371
+ } else {
372
+ child = /*#__PURE__*/React.createElement(CheckBox, {
373
+ label: /*#__PURE__*/React.createElement(Box, {
374
+ alignSelf: "center",
375
+ width: "100%",
376
+ align: "start"
377
+ }, optionLabel),
378
+ pad: "xsmall",
379
+ tabIndex: "-1",
380
+ checked: optionSelected,
381
+ disabled: optionDisabled
382
+ });
383
+ }
384
+
385
+ if (!children && search) {
386
+ if (typeof optionLabel === 'string' && optionLabel.toLowerCase().indexOf(search) >= 0) {
387
+ // code to bold search term in matching options
388
+ var boldIndex = optionLabel.toLowerCase().indexOf(search);
389
+ var childBeginning = optionLabel.substring(0, boldIndex);
390
+ var childBold = optionLabel.substring(boldIndex, boldIndex + search.length);
391
+ childBold = /*#__PURE__*/React.createElement("b", null, childBold);
392
+ var childEnd = optionLabel.substring(boldIndex + search.length);
393
+ child = /*#__PURE__*/React.createElement(CheckBox, {
394
+ label: /*#__PURE__*/React.createElement(Box, {
395
+ alignSelf: "center",
396
+ width: "100%",
397
+ align: "start",
398
+ direction: "row"
399
+ }, /*#__PURE__*/React.createElement(Text, null, childBeginning, childBold, childEnd)),
400
+ pad: "xsmall",
401
+ tabIndex: "-1",
402
+ checked: optionSelected,
403
+ disabled: optionDisabled
404
+ });
405
+ }
406
+ } // if we have a child, turn on plain, and hoverIndicator
407
+
408
+
409
+ return /*#__PURE__*/React.createElement(SelectOption, {
410
+ a11yTitle: optionSelected ? optionLabel + " selected" : optionLabel + " not selected" // eslint-disable-next-line react/no-array-index-key
411
+ ,
412
+ key: index // merge optionRef and activeRef
413
+ ,
414
+ ref: function ref(node) {
415
+ // eslint-disable-next-line no-param-reassign
416
+ if (optionRef) optionRef.current = node;
417
+ if (optionActive) activeRef.current = node;
418
+ },
419
+ tabIndex: optionSelected ? '0' : '-1',
420
+ role: "option",
421
+ id: "option" + index,
422
+ "aria-setsize": options.length,
423
+ "aria-posinset": index + 1,
424
+ "aria-selected": optionSelected,
425
+ focusIndicator: false,
426
+ "aria-disabled": optionDisabled || undefined,
427
+ plain: !child ? undefined : true,
428
+ align: "start",
429
+ kind: !child ? 'option' : undefined,
430
+ active: optionActive,
431
+ selected: optionSelected,
432
+ onMouseOver: !optionDisabled ? onActiveOption(index) : undefined,
433
+ onClick: !optionDisabled ? selectOption(index) : undefined,
434
+ onFocus: function onFocus() {
435
+ return setActiveIndex(index);
436
+ },
437
+ textComponent: textComponent
438
+ }, child);
439
+ }) : /*#__PURE__*/React.createElement(EmptySearchOption, {
440
+ emptySearchMessage: emptySearchMessage,
441
+ selectOptionsStyle: selectOptionsStyle,
442
+ theme: theme
443
+ })), usingKeyboard && showA11yLimit && /*#__PURE__*/React.createElement(Box, {
444
+ height: "0px",
445
+ width: "0px",
446
+ overflow: "hidden" // announce when we reach the limit of items
447
+ // that can be selected
448
+ ,
449
+ "aria-live": "assertive",
450
+ role: "alert"
451
+ }, showA11yLimit)));
452
+ });
453
+ export { SelectMultipleContainer };
@@ -0,0 +1,152 @@
1
+ import React, { useCallback, useEffect, useState } from 'react';
2
+ import { Box } from '../Box';
3
+ import { Button } from '../Button';
4
+ import { CheckBox } from '../CheckBox';
5
+ import { SelectOption } from '../Select/StyledSelect';
6
+ import { applyKey, getOptionLabel, getOptionValue, useDisabled } from '../Select/utils';
7
+
8
+ var SelectMultipleValue = function SelectMultipleValue(_ref) {
9
+ var allOptions = _ref.allOptions,
10
+ children = _ref.children,
11
+ disabled = _ref.disabled,
12
+ disabledKey = _ref.disabledKey,
13
+ dropButtonRef = _ref.dropButtonRef,
14
+ labelKey = _ref.labelKey,
15
+ onRequestOpen = _ref.onRequestOpen,
16
+ onSelectChange = _ref.onSelectChange,
17
+ theme = _ref.theme,
18
+ value = _ref.value,
19
+ valueKey = _ref.valueKey;
20
+
21
+ var _useState = useState(false),
22
+ showA11yDiv = _useState[0],
23
+ setShowA11yDiv = _useState[1];
24
+
25
+ var isDisabled = useDisabled(disabled, disabledKey, allOptions, valueKey || labelKey);
26
+ var visibleValue = useCallback(function (i) {
27
+ var optionValue = valueKey && valueKey.reduce ? applyKey(i, valueKey) : i;
28
+ var indexOptions = allOptions.indexOf(i);
29
+ var optionLabel = getOptionLabel(indexOptions, allOptions, labelKey || valueKey);
30
+ var optionDisabled = isDisabled(indexOptions);
31
+
32
+ if (value.indexOf(optionValue) < theme.selectMultiple.maxInline) {
33
+ var child;
34
+
35
+ if (children) {
36
+ child = children(i, indexOptions, allOptions, {
37
+ active: false,
38
+ disabled: optionDisabled,
39
+ selected: true
40
+ });
41
+ }
42
+
43
+ return /*#__PURE__*/React.createElement(SelectOption, {
44
+ role: "option",
45
+ a11yTitle: value.includes(optionValue) ? optionLabel + " selected" : optionLabel + " not selected",
46
+ "aria-setsize": value.length,
47
+ "aria-posinset": value.indexOf(optionValue) + 1,
48
+ "aria-selected": value.includes(optionValue),
49
+ "aria-disabled": optionDisabled,
50
+ plain: true,
51
+ hoverIndicator: !optionDisabled,
52
+ fill: "horizontal",
53
+ tabIndex: "0",
54
+ onClick: function onClick(event) {
55
+ if (!optionDisabled) {
56
+ var intermediate = [].concat(value);
57
+ var index = value.indexOf(optionValue);
58
+
59
+ if (intermediate.includes(optionValue)) {
60
+ onSelectChange(event, {
61
+ option: optionValue,
62
+ value: intermediate.filter(function (v) {
63
+ return v !== optionValue;
64
+ })
65
+ });
66
+
67
+ if (index !== intermediate.length - 1) {
68
+ setTimeout(function () {
69
+ var nextFocus = document.getElementById("selected-" + intermediate[index + 1]);
70
+ if (nextFocus) nextFocus.focus();
71
+ var result = allOptions.find(function (obj, j) {
72
+ return getOptionValue(j, allOptions, valueKey || labelKey) === intermediate[index + 1];
73
+ });
74
+ setShowA11yDiv("Unselected " + optionLabel + ". \n Focus moved to " + getOptionLabel(allOptions.indexOf(result), allOptions, labelKey || valueKey));
75
+ }, 200); // Timeout needed to allow screen reader
76
+ // time to announce and next item to display on
77
+ // screen. Based on testing, 200ms is enough time
78
+ } else if (intermediate.length !== 1) {
79
+ setTimeout(function () {
80
+ var nextFocus = document.getElementById("selected-" + intermediate[index - 1]);
81
+ if (nextFocus) nextFocus.focus();
82
+ var result = allOptions.find(function (obj, j) {
83
+ return getOptionValue(j, allOptions, valueKey || labelKey) === intermediate[index - 1];
84
+ });
85
+ setShowA11yDiv("Unselected " + optionLabel + ". Focus moved to \n " + getOptionLabel(allOptions.indexOf(result), allOptions, labelKey || valueKey));
86
+ }, 200); // Timeout needed to allow screen reader
87
+ // time to announce and next item to display on
88
+ // screen. Based on testing, 200ms is enough time
89
+ } else if (dropButtonRef.current) dropButtonRef.current.focus();
90
+ }
91
+ }
92
+ },
93
+ key: optionLabel,
94
+ id: "selected-" + optionValue
95
+ }, child || /*#__PURE__*/React.createElement(CheckBox, {
96
+ disabled: optionDisabled,
97
+ label: /*#__PURE__*/React.createElement(Box, {
98
+ alignSelf: "center",
99
+ align: "start"
100
+ }, optionLabel),
101
+ key: optionLabel,
102
+ pad: "xsmall",
103
+ tabIndex: "-1",
104
+ checked: value.includes(optionValue)
105
+ }));
106
+ }
107
+
108
+ return undefined;
109
+ }, [valueKey, allOptions, children, dropButtonRef, isDisabled, labelKey, onSelectChange, value, theme.selectMultiple.maxInline]); // After announcing set showA11yDiv to undefined so it won't
110
+ // be read out again
111
+
112
+ useEffect(function () {
113
+ if (showA11yDiv !== undefined) {
114
+ setTimeout(function () {
115
+ setShowA11yDiv(undefined);
116
+ }, 1000);
117
+ }
118
+ }, [showA11yDiv]);
119
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Box, {
120
+ width: "100%",
121
+ role: "listbox",
122
+ "aria-multiselectable": true,
123
+ a11yTitle: "Selected Options"
124
+ }, value && allOptions.filter(function (i) {
125
+ return value.indexOf(valueKey && valueKey.reduce ? applyKey(i, valueKey) : i) !== -1;
126
+ })
127
+ /* eslint-disable-next-line array-callback-return,
128
+ consistent-return */
129
+ .map(function (i) {
130
+ return visibleValue(i);
131
+ }), showA11yDiv && /*#__PURE__*/React.createElement(Box, {
132
+ height: "0px",
133
+ width: "0px",
134
+ overflow: "hidden" // announce when an item is removed from selected options
135
+ ,
136
+ "aria-live": "assertive",
137
+ role: "alert"
138
+ }, showA11yDiv)), value && value.length > theme.selectMultiple.maxInline && /*#__PURE__*/React.createElement(Box, {
139
+ pad: {
140
+ horizontal: 'small',
141
+ bottom: 'small',
142
+ top: 'xsmall'
143
+ },
144
+ alignSelf: "start"
145
+ }, /*#__PURE__*/React.createElement(Button, {
146
+ onClick: onRequestOpen,
147
+ size: "small",
148
+ label: "+ " + (value.length - theme.selectMultiple.maxInline) + " more"
149
+ })));
150
+ };
151
+
152
+ export { SelectMultipleValue };
@@ -0,0 +1,91 @@
1
+ import React, { useCallback } from 'react';
2
+ import { Box } from '../Box';
3
+ import { Button } from '../Button';
4
+ import { Text } from '../Text';
5
+ import { applyKey, getOptionValue, useDisabled } from '../Select/utils';
6
+
7
+ var SelectionSummary = function SelectionSummary(_ref) {
8
+ var allOptions = _ref.allOptions,
9
+ clearRef = _ref.clearRef,
10
+ disabled = _ref.disabled,
11
+ disabledKey = _ref.disabledKey,
12
+ isSelected = _ref.isSelected,
13
+ labelKey = _ref.labelKey,
14
+ limit = _ref.limit,
15
+ onChange = _ref.onChange,
16
+ onMore = _ref.onMore,
17
+ options = _ref.options,
18
+ search = _ref.search,
19
+ setActiveIndex = _ref.setActiveIndex,
20
+ showSelectedInline = _ref.showSelectedInline,
21
+ value = _ref.value,
22
+ valueKey = _ref.valueKey;
23
+ var isDisabled = useDisabled(disabled, disabledKey, options, valueKey || labelKey);
24
+ var selectedValuesDisabled = useCallback(function () {
25
+ var disabledSelected = 0;
26
+
27
+ if (value) {
28
+ for (var i = 0; i < allOptions.length; i += 1) {
29
+ if (value.includes(getOptionValue(i, options, valueKey || labelKey)) && isDisabled(i)) disabledSelected += 1;
30
+ }
31
+
32
+ if (value.length === disabledSelected) return true;
33
+ }
34
+
35
+ return false;
36
+ }, [value, allOptions, options, valueKey, labelKey, isDisabled]);
37
+
38
+ if (search === '' || search === undefined) {
39
+ var showSelectAll = !!((value == null ? void 0 : value.length) === 0 || selectedValuesDisabled() || !value);
40
+ return /*#__PURE__*/React.createElement(Box, {
41
+ pad: showSelectedInline ? {
42
+ left: 'xsmall',
43
+ vertical: 'xsmall'
44
+ } : 'xsmall',
45
+ direction: "row",
46
+ justify: "between",
47
+ gap: "small",
48
+ fill: "horizontal",
49
+ flex: showSelectedInline
50
+ }, /*#__PURE__*/React.createElement(Box, {
51
+ pad: {
52
+ vertical: 'xsmall'
53
+ },
54
+ alignSelf: "center"
55
+ }, /*#__PURE__*/React.createElement(Text, {
56
+ size: "small"
57
+ }, (value == null ? void 0 : value.length) === 0 || onMore || !value ? ((value == null ? void 0 : value.length) || 0) + " selected" : ((value == null ? void 0 : value.length) || 0) + " selected of " + options.length)), (options.length && (!limit || !(!value || (value == null ? void 0 : value.length) === 0 && selectedValuesDisabled()))) > 0 && (!onMore || onMore && (value == null ? void 0 : value.length) !== 0) && /*#__PURE__*/React.createElement(Button, {
58
+ a11yTitle: showSelectAll ? "Select all " + options.length + " options" : (value == null ? void 0 : value.length) + " options selected. Clear all?",
59
+ label: showSelectAll ? 'Select All' : 'Clear All',
60
+ onClick: function onClick(event) {
61
+ if (onChange) {
62
+ var nextSelected = options.filter(function (i, index) {
63
+ return showSelectAll ? !isDisabled(index) || isSelected(index) : isDisabled(index) && isSelected(index);
64
+ });
65
+ var nextValue = nextSelected.map(function (i) {
66
+ return valueKey && valueKey.reduce ? applyKey(i, valueKey) : i;
67
+ });
68
+ onChange(event, {
69
+ option: options,
70
+ value: nextValue,
71
+ selected: nextSelected
72
+ });
73
+ }
74
+
75
+ if (limit && !showSelectAll) setActiveIndex(0);
76
+ },
77
+ onFocus: function onFocus() {
78
+ return setActiveIndex(-1);
79
+ },
80
+ ref: clearRef
81
+ }));
82
+ }
83
+
84
+ return /*#__PURE__*/React.createElement(Box, {
85
+ pad: "xsmall"
86
+ }, /*#__PURE__*/React.createElement(Text, {
87
+ size: "small"
88
+ }, ((value == null ? void 0 : value.length) || '0') + " selected"));
89
+ };
90
+
91
+ export { SelectionSummary };