@rescui/select 0.15.1 → 0.15.2-RUI-291-Update-next-js-to-the-latest-version-8f54dec0a.66
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/lib/_virtual/index.css.js +12 -12
- package/lib/common-prop-types.d.ts +29 -35
- package/lib/common-prop-types.js +4 -3
- package/lib/constants.d.ts +6 -0
- package/lib/constants.js +7 -0
- package/lib/hooks/use-a11y.d.ts +30 -0
- package/lib/hooks/use-a11y.js +56 -0
- package/lib/hooks/use-actual-size-from-css.d.ts +10 -0
- package/lib/hooks/use-actual-size-from-css.js +27 -0
- package/lib/hooks/use-focus.d.ts +10 -0
- package/lib/hooks/use-focus.js +17 -0
- package/lib/hooks/use-hover.d.ts +10 -0
- package/lib/hooks/use-hover.js +17 -0
- package/lib/hooks/use-label-focus.d.ts +9 -0
- package/lib/hooks/use-label-focus.js +22 -0
- package/lib/hooks/use-multi-handlers.d.ts +11 -0
- package/lib/hooks/use-multi-handlers.js +44 -0
- package/lib/hooks/use-options.d.ts +13 -0
- package/lib/hooks/use-options.js +90 -0
- package/lib/hooks/use-primary-contrast-css-var-transfer.d.ts +11 -0
- package/lib/hooks/use-primary-contrast-css-var-transfer.js +29 -0
- package/lib/hooks/use-safe-id.d.ts +6 -0
- package/lib/hooks/use-safe-id.js +15 -0
- package/lib/index.css +714 -305
- package/lib/index.d.ts +4 -6
- package/lib/index.js +3 -3
- package/lib/multiselect.d.ts +3 -0
- package/lib/multiselect.js +309 -0
- package/lib/native-select.d.ts +4 -0
- package/lib/native-select.js +208 -0
- package/lib/parts/foldable.d.ts +34 -33
- package/lib/parts/foldable.js +5 -7
- package/lib/parts/form-trigger-wrapper.d.ts +34 -34
- package/lib/parts/form-trigger-wrapper.js +16 -34
- package/lib/parts/hidden-select.d.ts +13 -0
- package/lib/parts/hidden-select.js +45 -0
- package/lib/parts/icons.d.ts +17 -0
- package/lib/parts/icons.js +69 -0
- package/lib/parts/list-box.d.ts +13 -0
- package/lib/parts/list-box.js +120 -0
- package/lib/parts/multi-trigger.d.ts +12 -0
- package/lib/parts/multi-trigger.js +70 -0
- package/lib/parts/native-select-options-with-groups.d.ts +8 -0
- package/lib/parts/native-select-options-with-groups.js +76 -0
- package/lib/parts/popup-content.d.ts +20 -0
- package/lib/parts/popup-content.js +68 -0
- package/lib/public-api.p.css +543 -307
- package/lib/public-api.pcss +543 -307
- package/lib/select-basic.d.ts +3 -0
- package/lib/select-basic.js +204 -0
- package/lib/select-searchable.d.ts +3 -0
- package/lib/select-searchable.js +245 -0
- package/lib/select.d.ts +28 -92
- package/lib/select.js +51 -351
- package/lib/styles/common.p.module.css.js +32 -0
- package/lib/styles/form-trigger.p.module.css.js +15 -0
- package/lib/styles/list-box.p.module.css.js +16 -0
- package/lib/types.d.ts +202 -34
- package/lib/utils/calculate-max-height-by-items.d.ts +13 -0
- package/lib/utils/calculate-max-height-by-items.js +22 -0
- package/lib/utils/get-empty-message.d.ts +6 -0
- package/lib/utils/get-empty-message.js +32 -0
- package/lib/utils/get-option-value.d.ts +2 -0
- package/lib/utils/get-option-value.js +11 -0
- package/lib/utils/get-real-items-cnt.d.ts +2 -0
- package/lib/utils/get-real-items-cnt.js +19 -0
- package/lib/utils/get-text-from-nodes.d.ts +2 -0
- package/lib/utils/get-text-from-nodes.js +26 -0
- package/lib/utils/is-option-a-group.d.ts +2 -0
- package/lib/utils/is-option-a-group.js +5 -0
- package/lib/utils/item-to-string.d.ts +2 -0
- package/lib/utils/item-to-string.js +7 -0
- package/lib/utils/merge-props.d.ts +19 -0
- package/lib/utils/merge-props.js +104 -0
- package/lib/utils/throttle.d.ts +1 -0
- package/package.json +12 -12
- package/lib/key-codes.d.ts +0 -7
- package/lib/key-codes.js +0 -8
- package/lib/parts/common-select.p.module.css.js +0 -15
- package/lib/parts/form-trigger.p.module.css.js +0 -15
- package/lib/parts/multiselect.d.ts +0 -86
- package/lib/parts/multiselect.js +0 -455
- package/lib/parts/native-select.d.ts +0 -36
- package/lib/parts/native-select.js +0 -257
- package/lib/parts/native-select.p.module.css.js +0 -9
- package/lib/parts/throttle.d.ts +0 -1
- package/lib/parts/trigger.d.ts +0 -37
- package/lib/parts/trigger.js +0 -255
- package/lib/parts/trigger.p.module.css.js +0 -31
- package/lib/parts/use-backward-compatible-id.d.ts +0 -4
- package/lib/parts/use-backward-compatible-id.js +0 -14
- package/lib/parts/use-observable-force-rerender.d.ts +0 -2
- package/lib/parts/use-observable-force-rerender.js +0 -16
- package/lib/utils.d.ts +0 -22
- package/lib/utils.js +0 -201
- /package/lib/{parts → utils}/throttle.js +0 -0
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export
|
|
3
|
-
export {
|
|
4
|
-
export type { NativeSelectProps } from './
|
|
5
|
-
export { default as Multiselect } from './parts/multiselect';
|
|
6
|
-
export type { MultiselectProps } from './parts/multiselect';
|
|
1
|
+
export { Select } from './select';
|
|
2
|
+
export { Multiselect } from './multiselect';
|
|
3
|
+
export { NativeSelect } from './native-select';
|
|
4
|
+
export type { SelectProps, MultiselectProps, NativeSelectProps, SelectOption, Key } from './types';
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import './index.css';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
2
|
+
export { Select } from './select.js';
|
|
3
|
+
export { Multiselect } from './multiselect.js';
|
|
4
|
+
export { NativeSelect } from './native-select.js';
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { MultiselectProps } from './types';
|
|
3
|
+
export declare function Multiselect({ options, value, onChange, error, label, note, size, mode, theme: themeFromProps, placement, isSingleLine, boldLabel, reserveErrorLine, maxVisibleItems, isClearable, isLoading, isDisabled, name, noOptionsMessage, className, id, style, placeholder, ...rest }: MultiselectProps): React.JSX.Element;
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import React, { useMemo, useRef } from 'react';
|
|
3
|
+
import cn from 'classnames';
|
|
4
|
+
import { useSelect } from 'downshift';
|
|
5
|
+
import { useThemeWithUndefined } from '@rescui/ui-contexts';
|
|
6
|
+
import scrollIntoView from 'scroll-into-view-if-needed';
|
|
7
|
+
import { useLabelFocus } from './hooks/use-label-focus.js';
|
|
8
|
+
import { useOptions } from './hooks/use-options.js';
|
|
9
|
+
import { useMultiHandlers } from './hooks/use-multi-handlers.js';
|
|
10
|
+
import { useA11y } from './hooks/use-a11y.js';
|
|
11
|
+
import { Icons } from './parts/icons.js';
|
|
12
|
+
import { MultiTrigger } from './parts/multi-trigger.js';
|
|
13
|
+
import { HiddenSelect } from './parts/hidden-select.js';
|
|
14
|
+
import { FormTriggerWrapper } from './parts/form-trigger-wrapper.js';
|
|
15
|
+
import { PopupContent } from './parts/popup-content.js';
|
|
16
|
+
import { getEmptyMessage } from './utils/get-empty-message.js';
|
|
17
|
+
import { mergeProps } from './utils/merge-props.js';
|
|
18
|
+
import { itemToString } from './utils/item-to-string.js';
|
|
19
|
+
import { getOptionValue } from './utils/get-option-value.js';
|
|
20
|
+
import { getRealItemsCnt } from './utils/get-real-items-cnt.js';
|
|
21
|
+
import styles from './styles/common.p.module.css.js';
|
|
22
|
+
const THEME_STYLES = {
|
|
23
|
+
light: styles.themeLight,
|
|
24
|
+
dark: styles.themeDark
|
|
25
|
+
};
|
|
26
|
+
const SIZE_STYLES = {
|
|
27
|
+
s: styles.sizeS,
|
|
28
|
+
m: styles.sizeM,
|
|
29
|
+
l: styles.sizeL
|
|
30
|
+
};
|
|
31
|
+
const MODE_STYLES = {
|
|
32
|
+
classic: styles.classic,
|
|
33
|
+
rock: styles.rock
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const stateReducer = (state, actionAndChanges) => {
|
|
37
|
+
const {
|
|
38
|
+
changes,
|
|
39
|
+
type
|
|
40
|
+
} = actionAndChanges;
|
|
41
|
+
|
|
42
|
+
switch (type) {
|
|
43
|
+
case useSelect.stateChangeTypes.ToggleButtonKeyDownEnter:
|
|
44
|
+
case useSelect.stateChangeTypes.ToggleButtonKeyDownSpaceButton:
|
|
45
|
+
case useSelect.stateChangeTypes.ItemClick:
|
|
46
|
+
return { ...changes,
|
|
47
|
+
isOpen: true,
|
|
48
|
+
// Keep menu open after selection.
|
|
49
|
+
highlightedIndex: state.highlightedIndex
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
default:
|
|
53
|
+
return { ...changes,
|
|
54
|
+
selectedItem: null
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}; // eslint-disable-next-line complexity
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
function Multiselect(_ref) {
|
|
61
|
+
let {
|
|
62
|
+
options = [],
|
|
63
|
+
value,
|
|
64
|
+
onChange,
|
|
65
|
+
error,
|
|
66
|
+
label,
|
|
67
|
+
note,
|
|
68
|
+
size = 'm',
|
|
69
|
+
mode = 'classic',
|
|
70
|
+
theme: themeFromProps,
|
|
71
|
+
placement = 'bottom',
|
|
72
|
+
isSingleLine = false,
|
|
73
|
+
boldLabel = true,
|
|
74
|
+
reserveErrorLine = false,
|
|
75
|
+
maxVisibleItems = 5,
|
|
76
|
+
isClearable = false,
|
|
77
|
+
isLoading = false,
|
|
78
|
+
isDisabled = false,
|
|
79
|
+
name,
|
|
80
|
+
noOptionsMessage,
|
|
81
|
+
className,
|
|
82
|
+
id,
|
|
83
|
+
style,
|
|
84
|
+
placeholder,
|
|
85
|
+
...rest
|
|
86
|
+
} = _ref;
|
|
87
|
+
const selectedItems = useMemo(() => value ?? [], [value]);
|
|
88
|
+
const theme = useThemeWithUndefined(themeFromProps);
|
|
89
|
+
const triggerRef = useRef(null);
|
|
90
|
+
const menuRef = useRef(null);
|
|
91
|
+
const dropdownRef = useRef(null);
|
|
92
|
+
const {
|
|
93
|
+
selectedMap,
|
|
94
|
+
removeItem: removeSelectedItem,
|
|
95
|
+
addItem: addSelectedItem,
|
|
96
|
+
removeAllItems: reset
|
|
97
|
+
} = useMultiHandlers({
|
|
98
|
+
selectedItems,
|
|
99
|
+
onChange
|
|
100
|
+
});
|
|
101
|
+
const {
|
|
102
|
+
flattenedOptions,
|
|
103
|
+
listBoxItems
|
|
104
|
+
} = useOptions(options);
|
|
105
|
+
const isListBoxEmpty = getRealItemsCnt(listBoxItems) === 0;
|
|
106
|
+
const {
|
|
107
|
+
fieldProps,
|
|
108
|
+
errorMessageProps,
|
|
109
|
+
descriptionProps,
|
|
110
|
+
baseId
|
|
111
|
+
} = useA11y({
|
|
112
|
+
prefix: 'multiselect',
|
|
113
|
+
error,
|
|
114
|
+
description: note,
|
|
115
|
+
isDisabled
|
|
116
|
+
});
|
|
117
|
+
const {
|
|
118
|
+
isOpen,
|
|
119
|
+
getToggleButtonProps,
|
|
120
|
+
getLabelProps,
|
|
121
|
+
getMenuProps,
|
|
122
|
+
highlightedIndex,
|
|
123
|
+
getItemProps,
|
|
124
|
+
setHighlightedIndex
|
|
125
|
+
} = useSelect({
|
|
126
|
+
items: flattenedOptions,
|
|
127
|
+
itemToString,
|
|
128
|
+
itemToKey: getOptionValue,
|
|
129
|
+
id: baseId,
|
|
130
|
+
|
|
131
|
+
onIsOpenChange(newIsOpen) {
|
|
132
|
+
if (newIsOpen) {
|
|
133
|
+
// set the first selected item as a highlighted item if the popup is opened
|
|
134
|
+
const firstSelectedKey = [...selectedMap.keys()][0];
|
|
135
|
+
|
|
136
|
+
if (firstSelectedKey) {
|
|
137
|
+
setHighlightedIndex(flattenedOptions.findIndex(item => firstSelectedKey === getOptionValue(item)));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
stateReducer,
|
|
143
|
+
// there's no actual selection happening in the hook, we're doing it custom via controlled props.
|
|
144
|
+
selectedItem: null,
|
|
145
|
+
// defining our scroll behavior because on the first menu render the hook
|
|
146
|
+
// doesn't know menuRef and use page as the scroll boundary
|
|
147
|
+
scrollIntoView: (listItemEl, listEl) => {
|
|
148
|
+
scrollIntoView(listItemEl, {
|
|
149
|
+
scrollMode: 'if-needed',
|
|
150
|
+
block: 'nearest',
|
|
151
|
+
boundary: listEl ?? menuRef.current
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
onSelectedItemChange: _ref2 => {
|
|
155
|
+
let {
|
|
156
|
+
selectedItem
|
|
157
|
+
} = _ref2;
|
|
158
|
+
|
|
159
|
+
if (!selectedItem) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const changedValue = getOptionValue(selectedItem);
|
|
164
|
+
|
|
165
|
+
if (selectedMap.has(changedValue)) {
|
|
166
|
+
removeSelectedItem(selectedItem);
|
|
167
|
+
} else {
|
|
168
|
+
addSelectedItem(selectedItem);
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
isItemDisabled(item) {
|
|
173
|
+
return item && item.disabled;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
});
|
|
177
|
+
const menuProps = getMenuProps({
|
|
178
|
+
ref: menuRef,
|
|
179
|
+
'aria-multiselectable': isListBoxEmpty ? undefined : 'true'
|
|
180
|
+
}, {
|
|
181
|
+
suppressRefError: true
|
|
182
|
+
});
|
|
183
|
+
const triggerProps = getToggleButtonProps({
|
|
184
|
+
ref: triggerRef,
|
|
185
|
+
// eslint-disable-next-line complexity
|
|
186
|
+
onKeyDown: event => {
|
|
187
|
+
// prevent default behavior of Downshift when pressing Ctrl
|
|
188
|
+
if (event.ctrlKey || event.metaKey) {
|
|
189
|
+
event.preventDownshiftDefault = true;
|
|
190
|
+
return;
|
|
191
|
+
} // shift + arrow up/down selection (when popup is open)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
if (isOpen && event.shiftKey && (event.key === 'ArrowUp' || event.key === 'ArrowDown')) {
|
|
195
|
+
const newHighlightedIndex = event.key === 'ArrowUp' ? Math.max(highlightedIndex - 1, 0) : Math.min(highlightedIndex + 1, flattenedOptions.length - 1);
|
|
196
|
+
|
|
197
|
+
if (newHighlightedIndex === highlightedIndex) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
addSelectedItem(flattenedOptions[newHighlightedIndex]);
|
|
202
|
+
} // backspace removing the last selected item (when popup is closed)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
if (!isOpen && event.key === 'Backspace') {
|
|
206
|
+
removeSelectedItem(selectedItems[selectedItems.length - 1]);
|
|
207
|
+
} // delete removing all selected items (when popup is closed)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
if (!isOpen && event.key === 'Delete') {
|
|
211
|
+
reset();
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
// fix screen-reader focus on an opened popup with an empty list
|
|
215
|
+
...(isOpen && isListBoxEmpty ? {
|
|
216
|
+
'aria-activedescendant': menuProps.id
|
|
217
|
+
} : {})
|
|
218
|
+
});
|
|
219
|
+
const {
|
|
220
|
+
isFocusedByLabel,
|
|
221
|
+
labelClickProps,
|
|
222
|
+
triggerFocusProps: labelFocusForTriggerProps
|
|
223
|
+
} = useLabelFocus();
|
|
224
|
+
const emptyListMessage = getEmptyMessage({
|
|
225
|
+
isLoading,
|
|
226
|
+
isNothingFound: false,
|
|
227
|
+
noOptionsMessage
|
|
228
|
+
});
|
|
229
|
+
const listBoxState = {
|
|
230
|
+
selectedMap,
|
|
231
|
+
highlightedIndex,
|
|
232
|
+
getItemProps,
|
|
233
|
+
menuProps
|
|
234
|
+
};
|
|
235
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
236
|
+
className: cn(className, styles.root, THEME_STYLES[theme], SIZE_STYLES[size], MODE_STYLES[mode], isDisabled ? styles.disabled : styles.enabled, Boolean(error) && styles.error),
|
|
237
|
+
id: id,
|
|
238
|
+
style: style,
|
|
239
|
+
"data-test": "multiselect"
|
|
240
|
+
}, /*#__PURE__*/React.createElement(FormTriggerWrapper, {
|
|
241
|
+
theme: theme,
|
|
242
|
+
size: size,
|
|
243
|
+
disabled: isDisabled,
|
|
244
|
+
label: label,
|
|
245
|
+
labelProps: mergeProps(getLabelProps(), labelClickProps),
|
|
246
|
+
boldLabel: boldLabel,
|
|
247
|
+
error: error,
|
|
248
|
+
reserveErrorLine: reserveErrorLine,
|
|
249
|
+
errorMessageProps: errorMessageProps,
|
|
250
|
+
note: note,
|
|
251
|
+
descriptionProps: descriptionProps
|
|
252
|
+
}, /*#__PURE__*/React.createElement(HiddenSelect, {
|
|
253
|
+
name: name,
|
|
254
|
+
value: selectedItems,
|
|
255
|
+
isMultiple: true,
|
|
256
|
+
options: flattenedOptions
|
|
257
|
+
}), /*#__PURE__*/React.createElement(PopupContent, {
|
|
258
|
+
theme: theme,
|
|
259
|
+
size: size,
|
|
260
|
+
mode: mode,
|
|
261
|
+
dropdownRef: dropdownRef,
|
|
262
|
+
triggerRef: triggerRef,
|
|
263
|
+
placement: placement,
|
|
264
|
+
isOpen: isOpen,
|
|
265
|
+
listBoxState: listBoxState,
|
|
266
|
+
maxVisibleItems: maxVisibleItems,
|
|
267
|
+
emptyListMessage: emptyListMessage,
|
|
268
|
+
listBoxItems: listBoxItems,
|
|
269
|
+
dropdownUpdateDeps: [selectedItems],
|
|
270
|
+
trigger: _ref3 => {
|
|
271
|
+
let {
|
|
272
|
+
ref: triggerRefForDropdown
|
|
273
|
+
} = _ref3;
|
|
274
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
275
|
+
className: styles.triggerContainer,
|
|
276
|
+
"data-open": isOpen,
|
|
277
|
+
"data-label-focused": isFocusedByLabel
|
|
278
|
+
}, /*#__PURE__*/React.createElement("div", { ...rest,
|
|
279
|
+
...mergeProps(fieldProps, // aria-invalid & aria-describedby, rest are overridden by triggerProps
|
|
280
|
+
triggerProps, labelFocusForTriggerProps, {
|
|
281
|
+
ref: triggerRefForDropdown
|
|
282
|
+
}),
|
|
283
|
+
tabIndex: isDisabled ? undefined : 0,
|
|
284
|
+
className: cn(styles.trigger, !isSingleLine && styles.canBeMultiline),
|
|
285
|
+
"data-test": "multiselect__trigger"
|
|
286
|
+
}, /*#__PURE__*/React.createElement(MultiTrigger, {
|
|
287
|
+
selectedItems: selectedItems,
|
|
288
|
+
valueProps: {},
|
|
289
|
+
isSingleLine: isSingleLine,
|
|
290
|
+
isDisabled: isDisabled,
|
|
291
|
+
placeholder: placeholder,
|
|
292
|
+
triggerRef: triggerRef,
|
|
293
|
+
removeSelectedItem: removeSelectedItem
|
|
294
|
+
}), /*#__PURE__*/React.createElement(Icons, {
|
|
295
|
+
isLoading: isLoading,
|
|
296
|
+
canHaveClearIcon: isClearable && !isDisabled && selectedItems.length > 0,
|
|
297
|
+
onClear: () => {
|
|
298
|
+
reset();
|
|
299
|
+
triggerRef.current?.focus({
|
|
300
|
+
focusVisible: false
|
|
301
|
+
});
|
|
302
|
+
},
|
|
303
|
+
triggerId: triggerProps.id
|
|
304
|
+
})));
|
|
305
|
+
}
|
|
306
|
+
})));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export { Multiselect };
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import PropTypes from 'prop-types';
|
|
4
|
+
import cn from 'classnames';
|
|
5
|
+
import { useThemeWithUndefined } from '@rescui/ui-contexts';
|
|
6
|
+
import { ErrorMessage } from '@rescui/form-field';
|
|
7
|
+
import { nativeOptionType, nativeOptionsGroupType, noOptionsMessageType } from './common-prop-types.js';
|
|
8
|
+
import { useA11y } from './hooks/use-a11y.js';
|
|
9
|
+
import { useHover } from './hooks/use-hover.js';
|
|
10
|
+
import { useFocus } from './hooks/use-focus.js';
|
|
11
|
+
import { FormTriggerWrapper } from './parts/form-trigger-wrapper.js';
|
|
12
|
+
import { NativeSelectOptionsWithGroups } from './parts/native-select-options-with-groups.js';
|
|
13
|
+
import { Icons } from './parts/icons.js';
|
|
14
|
+
import { isOptionAGroup } from './utils/is-option-a-group.js';
|
|
15
|
+
import { getEmptyMessage } from './utils/get-empty-message.js';
|
|
16
|
+
import { mergeProps } from './utils/merge-props.js';
|
|
17
|
+
import styles from './styles/common.p.module.css.js';
|
|
18
|
+
|
|
19
|
+
function findSelectedOption(options, selectedValue) {
|
|
20
|
+
let result = null;
|
|
21
|
+
|
|
22
|
+
const checkOption = option => {
|
|
23
|
+
if (result === null || option.value === selectedValue) {
|
|
24
|
+
result = option;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
options.forEach(optionOrGroup => {
|
|
29
|
+
if (isOptionAGroup(optionOrGroup)) {
|
|
30
|
+
const group = optionOrGroup;
|
|
31
|
+
group.options?.forEach(checkOption);
|
|
32
|
+
} else {
|
|
33
|
+
checkOption(optionOrGroup);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const SIZE_STYLES = {
|
|
40
|
+
s: styles.sizeS,
|
|
41
|
+
m: styles.sizeM,
|
|
42
|
+
l: styles.sizeL
|
|
43
|
+
};
|
|
44
|
+
const THEME_STYLES = {
|
|
45
|
+
light: styles.themeLight,
|
|
46
|
+
dark: styles.themeDark
|
|
47
|
+
}; // eslint-disable-next-line complexity
|
|
48
|
+
|
|
49
|
+
const NativeSelect = _ref => {
|
|
50
|
+
let {
|
|
51
|
+
options = [],
|
|
52
|
+
size = 'm',
|
|
53
|
+
theme: themeFromProps,
|
|
54
|
+
className = null,
|
|
55
|
+
id,
|
|
56
|
+
style,
|
|
57
|
+
value,
|
|
58
|
+
onChange,
|
|
59
|
+
placeholder,
|
|
60
|
+
isLoading = false,
|
|
61
|
+
isDisabled = false,
|
|
62
|
+
isClearable = false,
|
|
63
|
+
trigger,
|
|
64
|
+
label,
|
|
65
|
+
note,
|
|
66
|
+
error,
|
|
67
|
+
reserveErrorLine = false,
|
|
68
|
+
noOptionsMessage,
|
|
69
|
+
name,
|
|
70
|
+
boldLabel = true,
|
|
71
|
+
...rest
|
|
72
|
+
} = _ref;
|
|
73
|
+
const triggerRef = React.useRef(null);
|
|
74
|
+
const {
|
|
75
|
+
labelProps,
|
|
76
|
+
fieldProps,
|
|
77
|
+
errorMessageProps,
|
|
78
|
+
descriptionProps
|
|
79
|
+
} = useA11y({
|
|
80
|
+
prefix: 'native-select',
|
|
81
|
+
error,
|
|
82
|
+
description: note,
|
|
83
|
+
label,
|
|
84
|
+
fieldRef: triggerRef,
|
|
85
|
+
isDisabled
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const reset = () => onChange(null);
|
|
89
|
+
|
|
90
|
+
const theme = useThemeWithUndefined(themeFromProps);
|
|
91
|
+
|
|
92
|
+
const handleChange = event => {
|
|
93
|
+
const newValue = event.target.value;
|
|
94
|
+
const selectedOption = findSelectedOption(options, newValue);
|
|
95
|
+
|
|
96
|
+
if (onChange) {
|
|
97
|
+
onChange(selectedOption);
|
|
98
|
+
}
|
|
99
|
+
}; // delete it when triggerFunc is migrated to the css-states instead of js-props for hover/focus
|
|
100
|
+
// replace it with ":focus + &" selector in that case
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
const {
|
|
104
|
+
isFocused,
|
|
105
|
+
focusProps
|
|
106
|
+
} = useFocus({
|
|
107
|
+
isDisabled
|
|
108
|
+
}); // same as above, could be replaced with ":hover + &" selector
|
|
109
|
+
|
|
110
|
+
const {
|
|
111
|
+
isHovered,
|
|
112
|
+
hoverProps
|
|
113
|
+
} = useHover({
|
|
114
|
+
isDisabled
|
|
115
|
+
});
|
|
116
|
+
const emptyListMessage = getEmptyMessage({
|
|
117
|
+
isLoading,
|
|
118
|
+
isNothingFound: false,
|
|
119
|
+
noOptionsMessage
|
|
120
|
+
});
|
|
121
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
122
|
+
className: cn(className, styles.root, SIZE_STYLES[size], THEME_STYLES[theme], styles.classic, isDisabled ? styles.disabled : styles.enabled, Boolean(error) && styles.error),
|
|
123
|
+
id: id,
|
|
124
|
+
style: style,
|
|
125
|
+
"data-test": "native-select"
|
|
126
|
+
}, /*#__PURE__*/React.createElement(FormTriggerWrapper, {
|
|
127
|
+
theme: theme,
|
|
128
|
+
size: size,
|
|
129
|
+
disabled: isDisabled,
|
|
130
|
+
label: label,
|
|
131
|
+
labelProps: mergeProps(labelProps),
|
|
132
|
+
boldLabel: boldLabel,
|
|
133
|
+
error: error,
|
|
134
|
+
errorMessageProps: errorMessageProps,
|
|
135
|
+
reserveErrorLine: reserveErrorLine,
|
|
136
|
+
note: note,
|
|
137
|
+
descriptionProps: descriptionProps
|
|
138
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
139
|
+
className: styles.triggerContainer
|
|
140
|
+
}, /*#__PURE__*/React.createElement("select", { ...rest,
|
|
141
|
+
...mergeProps(fieldProps, focusProps, hoverProps, {
|
|
142
|
+
onKeyDown: event => {
|
|
143
|
+
// backspace and backspace removing the last selected item (when popup is closed)
|
|
144
|
+
if (isClearable && (event.key === 'Backspace' || event.key === 'Delete')) {
|
|
145
|
+
reset();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}),
|
|
149
|
+
ref: triggerRef,
|
|
150
|
+
"data-test": "native-select_select",
|
|
151
|
+
"data-rs-internal": 'select-native__select',
|
|
152
|
+
className: styles.select,
|
|
153
|
+
disabled: isDisabled ? true : undefined,
|
|
154
|
+
onChange: handleChange,
|
|
155
|
+
name: name,
|
|
156
|
+
value: value?.value ?? ''
|
|
157
|
+
}, /*#__PURE__*/React.createElement(NativeSelectOptionsWithGroups, {
|
|
158
|
+
optionsWithGroups: options,
|
|
159
|
+
placeholder: placeholder,
|
|
160
|
+
emptyListMessage: emptyListMessage
|
|
161
|
+
})), trigger ? trigger({
|
|
162
|
+
focused: isFocused,
|
|
163
|
+
hovered: isHovered,
|
|
164
|
+
selectedLabel: value?.label,
|
|
165
|
+
selectedValue: value?.value,
|
|
166
|
+
placeholder
|
|
167
|
+
}) : /*#__PURE__*/React.createElement("div", {
|
|
168
|
+
className: cn(styles.trigger),
|
|
169
|
+
"data-test": 'native-select_trigger',
|
|
170
|
+
"data-rs-internal": 'select-native-trigger'
|
|
171
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
172
|
+
className: cn(styles.triggerText, !value?.value && styles.labelNotSelected),
|
|
173
|
+
"data-test": 'native-select_trigger_text',
|
|
174
|
+
"aria-hidden": "true"
|
|
175
|
+
}, value?.label ?? placeholder), /*#__PURE__*/React.createElement(Icons, {
|
|
176
|
+
isLoading: isLoading,
|
|
177
|
+
triggerId: fieldProps.id,
|
|
178
|
+
onClear: () => {
|
|
179
|
+
reset();
|
|
180
|
+
triggerRef.current?.focus({
|
|
181
|
+
focusVisible: false
|
|
182
|
+
});
|
|
183
|
+
},
|
|
184
|
+
canHaveClearIcon: isClearable && !isDisabled && Boolean(value?.value)
|
|
185
|
+
})))));
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
NativeSelect.propTypes = {
|
|
189
|
+
className: PropTypes.string,
|
|
190
|
+
style: PropTypes.object,
|
|
191
|
+
id: PropTypes.string,
|
|
192
|
+
options: PropTypes.arrayOf(PropTypes.oneOfType([nativeOptionType, nativeOptionsGroupType])),
|
|
193
|
+
value: nativeOptionType,
|
|
194
|
+
onChange: PropTypes.func,
|
|
195
|
+
size: PropTypes.oneOf(['m', 's', 'l']),
|
|
196
|
+
theme: PropTypes.oneOf(['light', 'dark']),
|
|
197
|
+
placeholder: PropTypes.string,
|
|
198
|
+
isDisabled: PropTypes.bool,
|
|
199
|
+
isLoading: PropTypes.bool,
|
|
200
|
+
trigger: PropTypes.func,
|
|
201
|
+
error: ErrorMessage.propTypes.children,
|
|
202
|
+
label: PropTypes.node,
|
|
203
|
+
note: PropTypes.node,
|
|
204
|
+
noOptionsMessage: noOptionsMessageType,
|
|
205
|
+
name: PropTypes.string,
|
|
206
|
+
boldLabel: PropTypes.bool
|
|
207
|
+
};
|
|
208
|
+
export { NativeSelect, NativeSelect as default };
|
package/lib/parts/foldable.d.ts
CHANGED
|
@@ -1,33 +1,34 @@
|
|
|
1
|
-
import React, { Component } from 'react';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
1
|
+
import React, { Component } from 'react';
|
|
2
|
+
import type { JSX } from 'react';
|
|
3
|
+
export type FoldableProps = {
|
|
4
|
+
className?: string;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
renderHiddenCounter?: (num: number, ref: React.Ref<HTMLElement>) => React.ReactNode;
|
|
7
|
+
};
|
|
8
|
+
type FoldableState = {
|
|
9
|
+
lastVisible: number;
|
|
10
|
+
renderingStage: number;
|
|
11
|
+
prevChildren: React.ReactNode;
|
|
12
|
+
};
|
|
13
|
+
export default class Foldable extends Component<FoldableProps, FoldableState> {
|
|
14
|
+
containerRef: React.RefObject<HTMLDivElement>;
|
|
15
|
+
plusNRef: React.RefObject<HTMLElement>;
|
|
16
|
+
childrenEls: HTMLElement[];
|
|
17
|
+
childrenWidths: number[];
|
|
18
|
+
resizeObserver: ResizeObserver;
|
|
19
|
+
tagsResizeObserver: ResizeObserver;
|
|
20
|
+
constructor(props: FoldableProps);
|
|
21
|
+
static getDerivedStateFromProps(props: FoldableProps, state: FoldableState): {
|
|
22
|
+
prevChildren: React.ReactNode;
|
|
23
|
+
renderingStage: number;
|
|
24
|
+
};
|
|
25
|
+
observeChildren(): void;
|
|
26
|
+
componentDidMount(): void;
|
|
27
|
+
componentDidUpdate(): void;
|
|
28
|
+
componentWillUnmount(): void;
|
|
29
|
+
measureChildren: () => void;
|
|
30
|
+
recalculateLastVisibleAndSetStage: (nextStage?: number) => void;
|
|
31
|
+
renderChildren(lastVisible: number): React.FunctionComponentElement<any>[];
|
|
32
|
+
render(): JSX.Element;
|
|
33
|
+
}
|
|
34
|
+
export {};
|
package/lib/parts/foldable.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
"use client";
|
|
2
1
|
import React, { Component } from 'react';
|
|
3
|
-
import { throttle } from '
|
|
2
|
+
import { throttle } from '../utils/throttle.js';
|
|
4
3
|
|
|
5
4
|
function lastVisibleIndex(lens, maxLen, gap) {
|
|
6
5
|
let extra = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
|
|
@@ -195,12 +194,11 @@ class Foldable extends Component {
|
|
|
195
194
|
const children = React.Children.toArray(_this.props.children);
|
|
196
195
|
const showLastChildIfFit = index === children.length - 2 && extra - gap - 1 > _this.childrenWidths[_this.childrenWidths.length - 1];
|
|
197
196
|
const newLastVisible = index + (showLastChildIfFit ? 1 : 0);
|
|
198
|
-
const newState = {
|
|
199
|
-
lastVisible: newLastVisible,
|
|
200
|
-
renderingStage: nextStage ? nextStage : _this.state.renderingStage
|
|
201
|
-
};
|
|
202
197
|
|
|
203
|
-
_this.setState(
|
|
198
|
+
_this.setState(prevState => ({ ...prevState,
|
|
199
|
+
lastVisible: newLastVisible,
|
|
200
|
+
renderingStage: nextStage ?? prevState.renderingStage
|
|
201
|
+
}));
|
|
204
202
|
};
|
|
205
203
|
})();
|
|
206
204
|
|