grommet 2.25.3 → 2.27.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 (160) hide show
  1. package/all/stories/AllComponents.js +19 -126
  2. package/components/Accordion/stories/CustomHeader.js +3 -1
  3. package/components/Avatar/Avatar.js +22 -9
  4. package/components/Box/stories/BackgroundThemed.js +54 -0
  5. package/components/Box/stories/CustomThemed/ThemedBackground.tsx +176 -0
  6. package/components/Calendar/Calendar.js +2 -1
  7. package/components/CheckBox/StyledCheckBox.js +9 -5
  8. package/components/CheckBoxGroup/CheckBoxGroup.js +3 -2
  9. package/components/CheckBoxGroup/index.d.ts +2 -1
  10. package/components/CheckBoxGroup/propTypes.js +1 -0
  11. package/components/DataChart/Detail.js +3 -1
  12. package/components/DataTable/index.d.ts +6 -6
  13. package/components/DateInput/DateInput.js +1 -1
  14. package/components/Diagram/Diagram.js +2 -1
  15. package/components/Drop/stories/Inline.js +1 -1
  16. package/components/Drop/stories/Overflow.js +1 -1
  17. package/components/Drop/stories/Plain.js +1 -1
  18. package/components/Drop/stories/Simple.js +1 -1
  19. package/components/Drop/stories/Styled.js +1 -1
  20. package/components/FormField/FormField.js +1 -1
  21. package/components/Heading/Heading.js +31 -2
  22. package/components/Heading/StyledHeading.js +1 -1
  23. package/components/Heading/index.d.ts +1 -0
  24. package/components/Heading/propTypes.js +1 -0
  25. package/components/Heading/stories/All.js +4 -1
  26. package/components/List/List.js +125 -41
  27. package/components/List/index.d.ts +1 -0
  28. package/components/List/propTypes.js +1 -0
  29. package/components/List/stories/Order.js +1 -0
  30. package/components/List/stories/Pinned.js +37 -0
  31. package/components/List/stories/Selection.js +1 -0
  32. package/components/List/stories/onClickItem.js +1 -0
  33. package/components/Notification/Notification.js +2 -2
  34. package/components/Notification/index.d.ts +1 -1
  35. package/components/Notification/propTypes.js +1 -1
  36. package/components/Notification/stories/CustomThemed/ThemedNotification.tsx +2 -2
  37. package/components/RadioButton/README.md +1 -279
  38. package/components/RadioButton/RadioButton.js +1 -0
  39. package/components/Select/DefaultSelectTextInput.js +43 -0
  40. package/components/Select/EmptySearchOption.js +34 -0
  41. package/components/Select/Select.js +38 -99
  42. package/components/Select/SelectContainer.js +20 -60
  43. package/components/Select/StyledSelect.js +58 -2
  44. package/components/Select/index.d.ts +11 -9
  45. package/components/Select/propTypes.js +44 -40
  46. package/components/Select/utils.js +129 -3
  47. package/components/SelectMultiple/README.md +2 -0
  48. package/components/SelectMultiple/SelectMultiple.js +476 -0
  49. package/components/SelectMultiple/SelectMultipleContainer.js +483 -0
  50. package/components/SelectMultiple/SelectMultipleValue.js +163 -0
  51. package/components/SelectMultiple/SelectionSummary.js +104 -0
  52. package/components/SelectMultiple/index.d.ts +21 -0
  53. package/components/SelectMultiple/index.js +8 -0
  54. package/components/SelectMultiple/propTypes.js +28 -0
  55. package/components/SelectMultiple/stories/Default.js +76 -0
  56. package/components/SelectMultiple/stories/Disabled.js +84 -0
  57. package/components/SelectMultiple/stories/FormField.js +66 -0
  58. package/components/SelectMultiple/stories/Limited.js +68 -0
  59. package/components/SelectMultiple/stories/LongList.js +76 -0
  60. package/components/SelectMultiple/stories/ObjectOptions.js +104 -0
  61. package/components/SelectMultiple/stories/ShowSelectedInline.js +76 -0
  62. package/components/SelectMultiple/stories/typescript/Children.tsx +106 -0
  63. package/components/Sidebar/stories/Labels.js +4 -1
  64. package/components/TextArea/stories/NonResizable.js +1 -0
  65. package/components/TextArea/stories/Simple.js +1 -0
  66. package/components/index.d.ts +1 -0
  67. package/components/index.js +8 -0
  68. package/es6/all/stories/AllComponents.js +21 -121
  69. package/es6/components/Accordion/stories/CustomHeader.js +3 -1
  70. package/es6/components/Avatar/Avatar.js +21 -9
  71. package/es6/components/Box/stories/BackgroundThemed.js +39 -0
  72. package/es6/components/Box/stories/CustomThemed/ThemedBackground.tsx +176 -0
  73. package/es6/components/Calendar/Calendar.js +2 -1
  74. package/es6/components/CheckBox/StyledCheckBox.js +9 -5
  75. package/es6/components/CheckBoxGroup/CheckBoxGroup.js +3 -2
  76. package/es6/components/CheckBoxGroup/index.d.ts +2 -1
  77. package/es6/components/CheckBoxGroup/propTypes.js +1 -0
  78. package/es6/components/DataChart/Detail.js +3 -1
  79. package/es6/components/DataTable/index.d.ts +6 -6
  80. package/es6/components/DateInput/DateInput.js +1 -1
  81. package/es6/components/Diagram/Diagram.js +2 -1
  82. package/es6/components/Drop/stories/Inline.js +1 -1
  83. package/es6/components/Drop/stories/Overflow.js +1 -1
  84. package/es6/components/Drop/stories/Plain.js +1 -1
  85. package/es6/components/Drop/stories/Simple.js +1 -1
  86. package/es6/components/Drop/stories/Styled.js +1 -1
  87. package/es6/components/FormField/FormField.js +1 -1
  88. package/es6/components/Heading/Heading.js +30 -3
  89. package/es6/components/Heading/StyledHeading.js +1 -1
  90. package/es6/components/Heading/index.d.ts +1 -0
  91. package/es6/components/Heading/propTypes.js +1 -0
  92. package/es6/components/Heading/stories/All.js +4 -1
  93. package/es6/components/List/List.js +126 -42
  94. package/es6/components/List/index.d.ts +1 -0
  95. package/es6/components/List/propTypes.js +1 -0
  96. package/es6/components/List/stories/Order.js +1 -0
  97. package/es6/components/List/stories/Pinned.js +22 -0
  98. package/es6/components/List/stories/Selection.js +1 -0
  99. package/es6/components/List/stories/onClickItem.js +1 -0
  100. package/es6/components/Notification/Notification.js +2 -2
  101. package/es6/components/Notification/index.d.ts +1 -1
  102. package/es6/components/Notification/propTypes.js +1 -1
  103. package/es6/components/Notification/stories/CustomThemed/ThemedNotification.tsx +2 -2
  104. package/es6/components/RadioButton/README.md +1 -279
  105. package/es6/components/RadioButton/RadioButton.js +1 -0
  106. package/es6/components/Select/DefaultSelectTextInput.js +31 -0
  107. package/es6/components/Select/EmptySearchOption.js +21 -0
  108. package/es6/components/Select/Select.js +34 -93
  109. package/es6/components/Select/SelectContainer.js +20 -59
  110. package/es6/components/Select/StyledSelect.js +45 -1
  111. package/es6/components/Select/index.d.ts +11 -9
  112. package/es6/components/Select/propTypes.js +41 -39
  113. package/es6/components/Select/utils.js +95 -1
  114. package/es6/components/SelectMultiple/README.md +2 -0
  115. package/es6/components/SelectMultiple/SelectMultiple.js +447 -0
  116. package/es6/components/SelectMultiple/SelectMultipleContainer.js +455 -0
  117. package/es6/components/SelectMultiple/SelectMultipleValue.js +149 -0
  118. package/es6/components/SelectMultiple/SelectionSummary.js +91 -0
  119. package/es6/components/SelectMultiple/index.d.ts +21 -0
  120. package/es6/components/SelectMultiple/index.js +1 -0
  121. package/es6/components/SelectMultiple/propTypes.js +18 -0
  122. package/es6/components/SelectMultiple/stories/Default.js +60 -0
  123. package/es6/components/SelectMultiple/stories/Disabled.js +68 -0
  124. package/es6/components/SelectMultiple/stories/FormField.js +50 -0
  125. package/es6/components/SelectMultiple/stories/Limited.js +53 -0
  126. package/es6/components/SelectMultiple/stories/LongList.js +63 -0
  127. package/es6/components/SelectMultiple/stories/ObjectOptions.js +88 -0
  128. package/es6/components/SelectMultiple/stories/ShowSelectedInline.js +61 -0
  129. package/es6/components/SelectMultiple/stories/typescript/Children.tsx +106 -0
  130. package/es6/components/Sidebar/stories/Labels.js +4 -1
  131. package/es6/components/TextArea/stories/NonResizable.js +1 -0
  132. package/es6/components/TextArea/stories/Simple.js +1 -0
  133. package/es6/components/index.d.ts +1 -0
  134. package/es6/components/index.js +1 -0
  135. package/es6/index.d.ts +1 -0
  136. package/es6/themes/base.d.ts +16 -5
  137. package/es6/themes/base.js +60 -39
  138. package/es6/themes/grommet.js +15 -0
  139. package/es6/themes/hacktoberfest2022.js +129 -0
  140. package/es6/themes/index.js +2 -1
  141. package/es6/utils/background.js +65 -12
  142. package/es6/utils/general-prop-types.js +2 -0
  143. package/es6/utils/index.d.ts +2 -0
  144. package/es6/utils/styles.js +3 -1
  145. package/grommet.min.js +1 -1
  146. package/grommet.min.js.map +1 -1
  147. package/index.d.ts +1 -0
  148. package/package.json +44 -41
  149. package/themes/base.d.ts +16 -5
  150. package/themes/base.js +61 -39
  151. package/themes/grommet.js +15 -0
  152. package/themes/hacktoberfest2022.js +137 -0
  153. package/themes/index.js +6 -2
  154. package/tools/grommet-babel-preset-es2015.js +1 -12
  155. package/tools/pack.js +1 -94
  156. package/tools/release-stable.js +1 -27
  157. package/utils/background.js +64 -12
  158. package/utils/general-prop-types.js +2 -0
  159. package/utils/index.d.ts +2 -0
  160. package/utils/styles.js +3 -1
@@ -0,0 +1,455 @@
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, getOptionIndex, arrayIncludes } 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 = getOptionIndex(allOptions, options[index], valueKey || labelKey);
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
+ }, [labelKey, 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 = getOptionIndex(disabledProp, getOptionValue(index, options, valueKey || labelKey), 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 ? arrayIncludes(value, valueKey && valueKey.reduce ? applyKey(option, valueKey) : option, valueKey || labelKey) : 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
+ var searchText = search.toLowerCase();
387
+
388
+ if (typeof optionLabel === 'string' && optionLabel.toLowerCase().indexOf(searchText) >= 0) {
389
+ // code to bold search term in matching options
390
+ var boldIndex = optionLabel.toLowerCase().indexOf(searchText);
391
+ var childBeginning = optionLabel.substring(0, boldIndex);
392
+ var childBold = optionLabel.substring(boldIndex, boldIndex + searchText.length);
393
+ childBold = /*#__PURE__*/React.createElement("b", null, childBold);
394
+ var childEnd = optionLabel.substring(boldIndex + searchText.length);
395
+ child = /*#__PURE__*/React.createElement(CheckBox, {
396
+ label: /*#__PURE__*/React.createElement(Box, {
397
+ alignSelf: "center",
398
+ width: "100%",
399
+ align: "start",
400
+ direction: "row"
401
+ }, /*#__PURE__*/React.createElement(Text, null, childBeginning, childBold, childEnd)),
402
+ pad: "xsmall",
403
+ tabIndex: "-1",
404
+ checked: optionSelected,
405
+ disabled: optionDisabled
406
+ });
407
+ }
408
+ } // if we have a child, turn on plain, and hoverIndicator
409
+
410
+
411
+ return /*#__PURE__*/React.createElement(SelectOption, {
412
+ a11yTitle: optionSelected ? optionLabel + " selected" : optionLabel + " not selected" // eslint-disable-next-line react/no-array-index-key
413
+ ,
414
+ key: index // merge optionRef and activeRef
415
+ ,
416
+ ref: function ref(node) {
417
+ // eslint-disable-next-line no-param-reassign
418
+ if (optionRef) optionRef.current = node;
419
+ if (optionActive) activeRef.current = node;
420
+ },
421
+ tabIndex: optionSelected ? '0' : '-1',
422
+ role: "option",
423
+ id: "option" + index,
424
+ "aria-setsize": options.length,
425
+ "aria-posinset": index + 1,
426
+ "aria-selected": optionSelected,
427
+ focusIndicator: false,
428
+ "aria-disabled": optionDisabled || undefined,
429
+ plain: !child ? undefined : true,
430
+ align: "start",
431
+ kind: !child ? 'option' : undefined,
432
+ active: optionActive,
433
+ selected: optionSelected,
434
+ onMouseOver: !optionDisabled ? onActiveOption(index) : undefined,
435
+ onClick: !optionDisabled ? selectOption(index) : undefined,
436
+ onFocus: function onFocus() {
437
+ return setActiveIndex(index);
438
+ },
439
+ textComponent: textComponent
440
+ }, child);
441
+ }) : /*#__PURE__*/React.createElement(EmptySearchOption, {
442
+ emptySearchMessage: emptySearchMessage,
443
+ selectOptionsStyle: selectOptionsStyle,
444
+ theme: theme
445
+ })), usingKeyboard && showA11yLimit && /*#__PURE__*/React.createElement(Box, {
446
+ height: "0px",
447
+ width: "0px",
448
+ overflow: "hidden" // announce when we reach the limit of items
449
+ // that can be selected
450
+ ,
451
+ "aria-live": "assertive",
452
+ role: "alert"
453
+ }, showA11yLimit)));
454
+ });
455
+ export { SelectMultipleContainer };
@@ -0,0 +1,149 @@
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, useDisabled, arrayIncludes, getOptionIndex } 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 optionSelected = arrayIncludes(value, optionValue, valueKey || labelKey);
29
+ var indexOptions = getOptionIndex(allOptions, i, valueKey || labelKey);
30
+ var optionLabel = getOptionLabel(indexOptions, allOptions, labelKey || valueKey);
31
+ var optionDisabled = isDisabled(indexOptions);
32
+ var valueIndex = getOptionIndex(value, optionValue, valueKey || labelKey);
33
+
34
+ if (valueIndex < theme.selectMultiple.maxInline) {
35
+ var child;
36
+
37
+ if (children) {
38
+ child = children(i, indexOptions, allOptions, {
39
+ active: false,
40
+ disabled: optionDisabled,
41
+ selected: true
42
+ });
43
+ }
44
+
45
+ return /*#__PURE__*/React.createElement(SelectOption, {
46
+ role: "option",
47
+ a11yTitle: optionSelected ? optionLabel + " selected" : optionLabel + " not selected",
48
+ "aria-setsize": value.length,
49
+ "aria-posinset": valueIndex + 1,
50
+ "aria-selected": optionSelected,
51
+ "aria-disabled": optionDisabled,
52
+ plain: true,
53
+ hoverIndicator: !optionDisabled,
54
+ fill: "horizontal",
55
+ tabIndex: "0",
56
+ onClick: function onClick(event) {
57
+ if (!optionDisabled) {
58
+ var intermediate = [].concat(value);
59
+
60
+ if (arrayIncludes(intermediate, optionValue, valueKey || labelKey)) {
61
+ onSelectChange(event, {
62
+ option: optionValue,
63
+ value: intermediate.filter(function (v) {
64
+ return typeof v === 'object' ? applyKey(v, valueKey || labelKey) === applyKey(optionValue, valueKey || labelKey) : v !== optionValue;
65
+ })
66
+ });
67
+
68
+ if (valueIndex !== intermediate.length - 1) {
69
+ setTimeout(function () {
70
+ var nextFocus = document.getElementById("selected-" + intermediate[valueIndex + 1]);
71
+ if (nextFocus) nextFocus.focus();
72
+ var result = intermediate[valueIndex + 1];
73
+ setShowA11yDiv("Unselected " + optionLabel + ". \n Focus moved to " + getOptionLabel(getOptionIndex(allOptions, result, valueKey || labelKey), allOptions, labelKey || valueKey));
74
+ }, 200); // Timeout needed to allow screen reader
75
+ // time to announce and next item to display on
76
+ // screen. Based on testing, 200ms is enough time
77
+ } else if (intermediate.length !== 1) {
78
+ setTimeout(function () {
79
+ var nextFocus = document.getElementById("selected-" + intermediate[valueIndex - 1]);
80
+ if (nextFocus) nextFocus.focus();
81
+ var result = intermediate[valueIndex - 1];
82
+ setShowA11yDiv("Unselected " + optionLabel + ". Focus moved to \n " + getOptionLabel(getOptionIndex(allOptions, result, valueKey || labelKey), allOptions, labelKey || valueKey));
83
+ }, 200); // Timeout needed to allow screen reader
84
+ // time to announce and next item to display on
85
+ // screen. Based on testing, 200ms is enough time
86
+ } else if (dropButtonRef.current) dropButtonRef.current.focus();
87
+ }
88
+ }
89
+ },
90
+ key: optionLabel,
91
+ id: "selected-" + optionValue
92
+ }, child || /*#__PURE__*/React.createElement(CheckBox, {
93
+ disabled: optionDisabled,
94
+ label: /*#__PURE__*/React.createElement(Box, {
95
+ alignSelf: "center",
96
+ align: "start"
97
+ }, optionLabel),
98
+ key: optionLabel,
99
+ pad: "xsmall",
100
+ tabIndex: "-1",
101
+ checked: optionSelected
102
+ }));
103
+ }
104
+
105
+ return undefined;
106
+ }, [valueKey, allOptions, children, dropButtonRef, isDisabled, labelKey, onSelectChange, value, theme.selectMultiple.maxInline]); // After announcing set showA11yDiv to undefined so it won't
107
+ // be read out again
108
+
109
+ useEffect(function () {
110
+ if (showA11yDiv !== undefined) {
111
+ setTimeout(function () {
112
+ setShowA11yDiv(undefined);
113
+ }, 1000);
114
+ }
115
+ }, [showA11yDiv]);
116
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Box, {
117
+ width: "100%",
118
+ role: "listbox",
119
+ "aria-multiselectable": true,
120
+ a11yTitle: "Selected Options"
121
+ }, value && allOptions.filter(function (i) {
122
+ return arrayIncludes(value, valueKey && valueKey.reduce ? applyKey(i, valueKey) : i, valueKey || labelKey);
123
+ })
124
+ /* eslint-disable-next-line array-callback-return,
125
+ consistent-return */
126
+ .map(function (i) {
127
+ return visibleValue(i);
128
+ }), showA11yDiv && /*#__PURE__*/React.createElement(Box, {
129
+ height: "0px",
130
+ width: "0px",
131
+ overflow: "hidden" // announce when an item is removed from selected options
132
+ ,
133
+ "aria-live": "assertive",
134
+ role: "alert"
135
+ }, showA11yDiv)), value && value.length > theme.selectMultiple.maxInline && /*#__PURE__*/React.createElement(Box, {
136
+ pad: {
137
+ horizontal: 'small',
138
+ bottom: 'small',
139
+ top: 'xsmall'
140
+ },
141
+ alignSelf: "start"
142
+ }, /*#__PURE__*/React.createElement(Button, {
143
+ onClick: onRequestOpen,
144
+ size: "small",
145
+ label: "+ " + (value.length - theme.selectMultiple.maxInline) + " more"
146
+ })));
147
+ };
148
+
149
+ 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, arrayIncludes } 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 (arrayIncludes(value, getOptionValue(i, options, valueKey || labelKey), 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 };