@rescui/select 0.1.1
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/LICENSE.txt +202 -0
- package/README.md +3 -0
- package/lib/common-prop-types.d.ts +25 -0
- package/lib/common-prop-types.js +18 -0
- package/lib/index.css +742 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +4 -0
- package/lib/key-codes.d.ts +7 -0
- package/lib/key-codes.js +8 -0
- package/lib/parts/common-select.module.pcss.js +4 -0
- package/lib/parts/foldable.d.ts +34 -0
- package/lib/parts/foldable.js +297 -0
- package/lib/parts/form-trigger-wrapper.d.ts +13 -0
- package/lib/parts/form-trigger-wrapper.js +87 -0
- package/lib/parts/form-trigger.module.pcss.js +9 -0
- package/lib/parts/multiselect.d.ts +80 -0
- package/lib/parts/multiselect.js +399 -0
- package/lib/parts/native-select.d.ts +29 -0
- package/lib/parts/native-select.js +282 -0
- package/lib/parts/native-select.module.pcss.js +9 -0
- package/lib/parts/trigger.d.ts +37 -0
- package/lib/parts/trigger.js +307 -0
- package/lib/parts/trigger.module.pcss.js +30 -0
- package/lib/select.d.ts +86 -0
- package/lib/select.js +391 -0
- package/lib/state/multiselect/actions.d.ts +36 -0
- package/lib/state/multiselect/actions.js +45 -0
- package/lib/state/multiselect/reducer.d.ts +21 -0
- package/lib/state/multiselect/reducer.js +203 -0
- package/lib/state/multiselect/selectors.d.ts +6 -0
- package/lib/state/multiselect/selectors.js +39 -0
- package/lib/state/select/actions.d.ts +81 -0
- package/lib/state/select/actions.js +87 -0
- package/lib/state/select/reducer.d.ts +22 -0
- package/lib/state/select/reducer.js +199 -0
- package/lib/state/select/selectors.d.ts +20 -0
- package/lib/state/select/selectors.js +114 -0
- package/lib/types.d.ts +51 -0
- package/lib/utils.d.ts +8 -0
- package/lib/utils.js +102 -0
- package/package.json +46 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime-corejs3/helpers/esm/toConsumableArray";
|
|
2
|
+
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
|
|
3
|
+
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
|
|
4
|
+
|
|
5
|
+
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) { symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
6
|
+
|
|
7
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { Object.defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
8
|
+
|
|
9
|
+
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
|
|
10
|
+
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
|
|
11
|
+
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
|
|
12
|
+
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
|
|
13
|
+
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
|
|
14
|
+
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
|
|
15
|
+
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
|
|
16
|
+
import "core-js/modules/es.object.to-string.js";
|
|
17
|
+
import "core-js/modules/es.array.join.js";
|
|
18
|
+
import "core-js/modules/web.dom-collections.for-each.js";
|
|
19
|
+
import React, { useState } from 'react';
|
|
20
|
+
import PropTypes from 'prop-types';
|
|
21
|
+
import cn from 'classnames';
|
|
22
|
+
import { useTheme } from '@rescui/ui-contexts';
|
|
23
|
+
import { optionType, optionsGroupType, noOptionsMessageType } from '../common-prop-types.js';
|
|
24
|
+
import SelectTrigger from './trigger.js';
|
|
25
|
+
import FormTriggerWrapper from './form-trigger-wrapper.js';
|
|
26
|
+
import styles from './native-select.module.pcss.js';
|
|
27
|
+
var DEFAULT_NO_OPTIONS_MESSAGES = {
|
|
28
|
+
noOptions: 'No options',
|
|
29
|
+
loading: 'Loading...'
|
|
30
|
+
};
|
|
31
|
+
var PLACEHOLDER_VALUE = '__placeholder__';
|
|
32
|
+
|
|
33
|
+
function isString(s) {
|
|
34
|
+
return Object.prototype.toString.call(s) === '[object String]';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
var isOptionsGroup = function isOptionsGroup(optionOrGroup) {
|
|
38
|
+
return typeof optionOrGroup.options !== 'undefined';
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
var collectOptionGroupValues = function collectOptionGroupValues(options) {
|
|
42
|
+
return _mapInstanceProperty(options).call(options, function (_ref) {
|
|
43
|
+
var value = _ref.value;
|
|
44
|
+
return value;
|
|
45
|
+
}).join('-');
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
var Option = function Option(_ref2) {
|
|
49
|
+
var value = _ref2.value,
|
|
50
|
+
label = _ref2.label,
|
|
51
|
+
_ref2$disabled = _ref2.disabled,
|
|
52
|
+
disabled = _ref2$disabled === void 0 ? false : _ref2$disabled;
|
|
53
|
+
return /*#__PURE__*/React.createElement("option", {
|
|
54
|
+
value: value,
|
|
55
|
+
disabled: disabled
|
|
56
|
+
}, label);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
var OptionGroup = function OptionGroup(_ref3) {
|
|
60
|
+
var _ref3$options = _ref3.options,
|
|
61
|
+
options = _ref3$options === void 0 ? [] : _ref3$options,
|
|
62
|
+
_ref3$label = _ref3.label,
|
|
63
|
+
label = _ref3$label === void 0 ? '' : _ref3$label,
|
|
64
|
+
_ref3$disabled = _ref3.disabled,
|
|
65
|
+
disabled = _ref3$disabled === void 0 ? false : _ref3$disabled;
|
|
66
|
+
return /*#__PURE__*/React.createElement("optgroup", {
|
|
67
|
+
label: label,
|
|
68
|
+
disabled: disabled
|
|
69
|
+
}, _mapInstanceProperty(options).call(options, function (_ref4) {
|
|
70
|
+
var value = _ref4.value,
|
|
71
|
+
optionLabel = _ref4.label,
|
|
72
|
+
optionDisabled = _ref4.disabled;
|
|
73
|
+
return /*#__PURE__*/React.createElement(Option, {
|
|
74
|
+
key: value,
|
|
75
|
+
value: value,
|
|
76
|
+
label: optionLabel,
|
|
77
|
+
disabled: optionDisabled
|
|
78
|
+
});
|
|
79
|
+
}));
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
var OptionsWithGroups = function OptionsWithGroups(_ref5) {
|
|
83
|
+
var optionsWithGroups = _ref5.optionsWithGroups;
|
|
84
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, _mapInstanceProperty(optionsWithGroups).call(optionsWithGroups, function (optionOrGroup) {
|
|
85
|
+
if (isOptionsGroup(optionOrGroup)) {
|
|
86
|
+
var _context;
|
|
87
|
+
|
|
88
|
+
var group = optionOrGroup;
|
|
89
|
+
return /*#__PURE__*/React.createElement(OptionGroup, {
|
|
90
|
+
key: _concatInstanceProperty(_context = "group--".concat(group.label, "--")).call(_context, collectOptionGroupValues(group.options)),
|
|
91
|
+
options: group.options,
|
|
92
|
+
disabled: group.disabled,
|
|
93
|
+
label: group.label
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
var option = optionOrGroup;
|
|
98
|
+
return /*#__PURE__*/React.createElement(Option, {
|
|
99
|
+
key: option.value,
|
|
100
|
+
value: option.value,
|
|
101
|
+
label: optionOrGroup.label,
|
|
102
|
+
disabled: optionOrGroup.disabled
|
|
103
|
+
});
|
|
104
|
+
}));
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
function findSelectedOption(options, selectedValue) {
|
|
108
|
+
var result = null;
|
|
109
|
+
|
|
110
|
+
var checkOption = function checkOption(option) {
|
|
111
|
+
if (result === null || option.value === selectedValue) {
|
|
112
|
+
result = option;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
options.forEach(function (optionOrGroup) {
|
|
117
|
+
if (isOptionsGroup(optionOrGroup)) {
|
|
118
|
+
var _group$options;
|
|
119
|
+
|
|
120
|
+
var group = optionOrGroup;
|
|
121
|
+
(_group$options = group.options) === null || _group$options === void 0 ? void 0 : _group$options.forEach(checkOption);
|
|
122
|
+
} else {
|
|
123
|
+
checkOption(optionOrGroup);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function getNoOptionsMessage(noOptionsMessage, isLoading) {
|
|
130
|
+
var _ref6 = isString(noOptionsMessage) ? _objectSpread(_objectSpread({}, DEFAULT_NO_OPTIONS_MESSAGES), {}, {
|
|
131
|
+
noOptions: noOptionsMessage
|
|
132
|
+
}) : _objectSpread(_objectSpread({}, DEFAULT_NO_OPTIONS_MESSAGES), noOptionsMessage),
|
|
133
|
+
noOptions = _ref6.noOptions,
|
|
134
|
+
loading = _ref6.loading;
|
|
135
|
+
|
|
136
|
+
if (isLoading) {
|
|
137
|
+
return loading;
|
|
138
|
+
} else {
|
|
139
|
+
return noOptions;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
var SIZE_STYLES = {
|
|
144
|
+
s: styles.sizeS,
|
|
145
|
+
m: styles.sizeM,
|
|
146
|
+
l: styles.sizeL
|
|
147
|
+
}; // eslint-disable-next-line complexity
|
|
148
|
+
|
|
149
|
+
var NativeSelect = function NativeSelect(_ref7) {
|
|
150
|
+
var _context2;
|
|
151
|
+
|
|
152
|
+
var _ref7$options = _ref7.options,
|
|
153
|
+
options = _ref7$options === void 0 ? [] : _ref7$options,
|
|
154
|
+
_ref7$size = _ref7.size,
|
|
155
|
+
size = _ref7$size === void 0 ? 'm' : _ref7$size,
|
|
156
|
+
themeFromProps = _ref7.theme,
|
|
157
|
+
_ref7$className = _ref7.className,
|
|
158
|
+
className = _ref7$className === void 0 ? null : _ref7$className,
|
|
159
|
+
style = _ref7.style,
|
|
160
|
+
_ref7$defaultValue = _ref7.defaultValue,
|
|
161
|
+
defaultValue = _ref7$defaultValue === void 0 ? {
|
|
162
|
+
label: '',
|
|
163
|
+
value: null
|
|
164
|
+
} : _ref7$defaultValue,
|
|
165
|
+
_ref7$value = _ref7.value,
|
|
166
|
+
value = _ref7$value === void 0 ? undefined : _ref7$value,
|
|
167
|
+
onChange = _ref7.onChange,
|
|
168
|
+
_ref7$placeholder = _ref7.placeholder,
|
|
169
|
+
placeholder = _ref7$placeholder === void 0 ? 'Placeholder' : _ref7$placeholder,
|
|
170
|
+
_ref7$isLoading = _ref7.isLoading,
|
|
171
|
+
isLoading = _ref7$isLoading === void 0 ? false : _ref7$isLoading,
|
|
172
|
+
_ref7$isDisabled = _ref7.isDisabled,
|
|
173
|
+
isDisabled = _ref7$isDisabled === void 0 ? false : _ref7$isDisabled,
|
|
174
|
+
trigger = _ref7.trigger,
|
|
175
|
+
label = _ref7.label,
|
|
176
|
+
note = _ref7.note,
|
|
177
|
+
error = _ref7.error,
|
|
178
|
+
noOptionsMessage = _ref7.noOptionsMessage;
|
|
179
|
+
var theme = useTheme(themeFromProps);
|
|
180
|
+
|
|
181
|
+
var _useState = useState(defaultValue),
|
|
182
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
183
|
+
selected = _useState2[0],
|
|
184
|
+
setSelected = _useState2[1];
|
|
185
|
+
|
|
186
|
+
var displayed = value ? value : selected;
|
|
187
|
+
|
|
188
|
+
var _useState3 = useState(false),
|
|
189
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
190
|
+
hovered = _useState4[0],
|
|
191
|
+
setHovered = _useState4[1];
|
|
192
|
+
|
|
193
|
+
var handleChange = function handleChange(event) {
|
|
194
|
+
var newValue = event.target.value;
|
|
195
|
+
var selectedOption = findSelectedOption(options, newValue);
|
|
196
|
+
setSelected(selectedOption);
|
|
197
|
+
|
|
198
|
+
if (onChange) {
|
|
199
|
+
onChange(selectedOption);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
var optionList = /*#__PURE__*/React.createElement(OptionsWithGroups, {
|
|
204
|
+
optionsWithGroups: placeholder !== null ? _concatInstanceProperty(_context2 = [{
|
|
205
|
+
value: PLACEHOLDER_VALUE,
|
|
206
|
+
label: options.length > 0 ? placeholder : getNoOptionsMessage(noOptionsMessage, isLoading),
|
|
207
|
+
disabled: true
|
|
208
|
+
}]).call(_context2, _toConsumableArray(options)) : options
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
var _trigger;
|
|
212
|
+
|
|
213
|
+
if (trigger) {
|
|
214
|
+
_trigger = trigger({
|
|
215
|
+
selectedLabel: displayed.label,
|
|
216
|
+
selectedValue: displayed.value,
|
|
217
|
+
placeholder: placeholder,
|
|
218
|
+
hovered: hovered
|
|
219
|
+
});
|
|
220
|
+
} else {
|
|
221
|
+
_trigger = /*#__PURE__*/React.createElement(SelectTrigger, {
|
|
222
|
+
theme: theme,
|
|
223
|
+
size: size,
|
|
224
|
+
isOpen: false,
|
|
225
|
+
onKeyDown: function onKeyDown() {
|
|
226
|
+
return false;
|
|
227
|
+
},
|
|
228
|
+
selected: displayed,
|
|
229
|
+
placeholder: placeholder,
|
|
230
|
+
searchInput: "",
|
|
231
|
+
isClearable: false,
|
|
232
|
+
isLoading: isLoading,
|
|
233
|
+
isDisabled: isDisabled,
|
|
234
|
+
error: Boolean(error)
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return /*#__PURE__*/React.createElement(FormTriggerWrapper, {
|
|
239
|
+
className: className,
|
|
240
|
+
theme: theme,
|
|
241
|
+
size: size,
|
|
242
|
+
error: error,
|
|
243
|
+
label: label,
|
|
244
|
+
note: note
|
|
245
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
246
|
+
className: styles.main,
|
|
247
|
+
onMouseEnter: function onMouseEnter() {
|
|
248
|
+
return setHovered(true);
|
|
249
|
+
},
|
|
250
|
+
onMouseLeave: function onMouseLeave() {
|
|
251
|
+
return setHovered(false);
|
|
252
|
+
},
|
|
253
|
+
style: style,
|
|
254
|
+
"data-test": "native-select"
|
|
255
|
+
}, _trigger, /*#__PURE__*/React.createElement("select", {
|
|
256
|
+
"data-test": "native-select_select",
|
|
257
|
+
className: cn(styles.select, SIZE_STYLES[size]),
|
|
258
|
+
disabled: isDisabled ? true : undefined,
|
|
259
|
+
onChange: handleChange,
|
|
260
|
+
value: displayed.label !== '' ? displayed.value : PLACEHOLDER_VALUE
|
|
261
|
+
}, optionList)));
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
NativeSelect.propTypes = {
|
|
265
|
+
className: PropTypes.string,
|
|
266
|
+
options: PropTypes.arrayOf(PropTypes.oneOfType([optionType, optionsGroupType])),
|
|
267
|
+
defaultValue: optionType,
|
|
268
|
+
value: optionType,
|
|
269
|
+
onChange: PropTypes.func,
|
|
270
|
+
size: PropTypes.oneOf(['m', 's', 'l']),
|
|
271
|
+
theme: PropTypes.oneOf(['light', 'dark']),
|
|
272
|
+
placeholder: PropTypes.string,
|
|
273
|
+
isDisabled: PropTypes.bool,
|
|
274
|
+
isLoading: PropTypes.bool,
|
|
275
|
+
trigger: PropTypes.func,
|
|
276
|
+
error: PropTypes.oneOfType([PropTypes.bool, PropTypes.node]),
|
|
277
|
+
label: PropTypes.node,
|
|
278
|
+
note: PropTypes.node,
|
|
279
|
+
noOptionsMessage: noOptionsMessageType,
|
|
280
|
+
style: PropTypes.object
|
|
281
|
+
};
|
|
282
|
+
export default NativeSelect;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
var styles = {
|
|
2
|
+
"main": "_main_1flk5jg_3",
|
|
3
|
+
"select": "_select_1flk5jg_7",
|
|
4
|
+
"sizeL": "_sizeL_1flk5jg_19",
|
|
5
|
+
"rs-reset-letter-spacing": "_rs-reset-letter-spacing_1flk5jg_1",
|
|
6
|
+
"sizeM": "_sizeM_1flk5jg_23",
|
|
7
|
+
"sizeS": "_sizeS_1flk5jg_27"
|
|
8
|
+
};
|
|
9
|
+
export default styles;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React, { HTMLAttributes } from 'react';
|
|
2
|
+
import { Theme } from '@rescui/ui-contexts';
|
|
3
|
+
import type { SelectOption, SelectSize, SelectMode } from '../types';
|
|
4
|
+
declare type SelectTriggerPropsOwn = {
|
|
5
|
+
size: SelectSize;
|
|
6
|
+
mode?: SelectMode;
|
|
7
|
+
theme?: Theme;
|
|
8
|
+
className?: string;
|
|
9
|
+
triggerRef?: React.Ref<HTMLDivElement>;
|
|
10
|
+
isOpen?: boolean;
|
|
11
|
+
selected?: unknown;
|
|
12
|
+
placeholder?: string;
|
|
13
|
+
isClearable?: boolean;
|
|
14
|
+
isLoading?: boolean;
|
|
15
|
+
isDisabled?: boolean;
|
|
16
|
+
isSingleLine?: boolean;
|
|
17
|
+
searchInput?: string;
|
|
18
|
+
isSearchable?: boolean;
|
|
19
|
+
onClearClick?: () => void;
|
|
20
|
+
onItemClearClick?: (s: SelectOption) => void;
|
|
21
|
+
onSearchChange?: (newValue: string) => void;
|
|
22
|
+
error?: boolean;
|
|
23
|
+
};
|
|
24
|
+
export declare type SelectTriggerProps = SelectTriggerPropsOwn & Omit<HTMLAttributes<HTMLDivElement>, keyof SelectTriggerPropsOwn>;
|
|
25
|
+
declare const SelectTrigger: React.FC<SelectTriggerProps>;
|
|
26
|
+
export declare type SelectedOptionProps = {
|
|
27
|
+
selected: SelectOption;
|
|
28
|
+
searchInput?: string;
|
|
29
|
+
placeholder?: string;
|
|
30
|
+
isSearchable?: boolean;
|
|
31
|
+
isDisabled?: boolean;
|
|
32
|
+
inputRef: React.Ref<HTMLInputElement>;
|
|
33
|
+
handleBlur: React.FocusEventHandler<HTMLInputElement>;
|
|
34
|
+
handleFocus: React.FocusEventHandler<HTMLInputElement>;
|
|
35
|
+
handleChange: React.ChangeEventHandler<HTMLInputElement>;
|
|
36
|
+
};
|
|
37
|
+
export default SelectTrigger;
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import "core-js/modules/web.dom-collections.for-each.js";
|
|
2
|
+
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
|
|
3
|
+
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
|
|
4
|
+
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
|
|
5
|
+
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
|
|
6
|
+
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
|
|
7
|
+
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
|
|
8
|
+
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
|
|
9
|
+
import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties";
|
|
10
|
+
var _excluded = ["size", "mode", "theme", "className", "triggerRef", "isOpen", "selected", "placeholder", "isClearable", "isLoading", "isDisabled", "isSingleLine", "searchInput", "isSearchable", "onClearClick", "onItemClearClick", "onSearchChange", "error"];
|
|
11
|
+
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
|
|
12
|
+
|
|
13
|
+
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) { symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
14
|
+
|
|
15
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { Object.defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
16
|
+
|
|
17
|
+
import React, { useRef, useState } from 'react';
|
|
18
|
+
import PropTypes from 'prop-types';
|
|
19
|
+
import cn from 'classnames';
|
|
20
|
+
import { presets, RemovableTag, Tag } from '@rescui/tag';
|
|
21
|
+
import { useTheme } from '@rescui/ui-contexts';
|
|
22
|
+
import { DownIcon, LoadingIcon, CloseIcon } from '@rescui/icons';
|
|
23
|
+
import { optionType } from '../common-prop-types.js';
|
|
24
|
+
import { reverseTheme } from '../utils.js';
|
|
25
|
+
import Foldable from './foldable.js';
|
|
26
|
+
import styles from './trigger.module.pcss.js';
|
|
27
|
+
/* eslint-disable no-nested-ternary */
|
|
28
|
+
|
|
29
|
+
var TAG_PRESET = {
|
|
30
|
+
light: presets.filledLight,
|
|
31
|
+
dark: presets.filledDark
|
|
32
|
+
};
|
|
33
|
+
var THEME_STYLES = {
|
|
34
|
+
light: styles.themeLight,
|
|
35
|
+
dark: styles.themeDark
|
|
36
|
+
};
|
|
37
|
+
var SIZE_STYLES = {
|
|
38
|
+
s: styles.sizeS,
|
|
39
|
+
m: styles.sizeM,
|
|
40
|
+
l: styles.sizeL
|
|
41
|
+
};
|
|
42
|
+
var MODE_STYLES = {
|
|
43
|
+
classic: styles.classic,
|
|
44
|
+
rock: styles.rock
|
|
45
|
+
};
|
|
46
|
+
var TAG_SIZE = {
|
|
47
|
+
s: 'xs',
|
|
48
|
+
m: 's',
|
|
49
|
+
l: 'm'
|
|
50
|
+
}; // eslint-disable-next-line complexity
|
|
51
|
+
|
|
52
|
+
var SelectTrigger = function SelectTrigger(props) {
|
|
53
|
+
var size = props.size,
|
|
54
|
+
mode = props.mode,
|
|
55
|
+
themeFromProps = props.theme,
|
|
56
|
+
className = props.className,
|
|
57
|
+
triggerRef = props.triggerRef,
|
|
58
|
+
isOpen = props.isOpen,
|
|
59
|
+
_props$selected = props.selected,
|
|
60
|
+
selected = _props$selected === void 0 ? null : _props$selected,
|
|
61
|
+
placeholder = props.placeholder,
|
|
62
|
+
isClearable = props.isClearable,
|
|
63
|
+
isLoading = props.isLoading,
|
|
64
|
+
isDisabled = props.isDisabled,
|
|
65
|
+
isSingleLine = props.isSingleLine,
|
|
66
|
+
searchInput = props.searchInput,
|
|
67
|
+
isSearchable = props.isSearchable,
|
|
68
|
+
onClearClick = props.onClearClick,
|
|
69
|
+
onItemClearClick = props.onItemClearClick,
|
|
70
|
+
onSearchChange = props.onSearchChange,
|
|
71
|
+
error = props.error,
|
|
72
|
+
restProps = _objectWithoutProperties(props, _excluded);
|
|
73
|
+
|
|
74
|
+
var theme = useTheme(themeFromProps);
|
|
75
|
+
var inputRef = useRef(null);
|
|
76
|
+
|
|
77
|
+
var _useState = useState(false),
|
|
78
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
79
|
+
isSearchFocused = _useState2[0],
|
|
80
|
+
setIsSearchFocused = _useState2[1];
|
|
81
|
+
|
|
82
|
+
var _useState3 = useState(false),
|
|
83
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
84
|
+
isFocused = _useState4[0],
|
|
85
|
+
setIsFocused = _useState4[1];
|
|
86
|
+
|
|
87
|
+
var _useState5 = useState(false),
|
|
88
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
89
|
+
isSearchKeyFocused = _useState6[0],
|
|
90
|
+
setIsSearchKeyFocused = _useState6[1];
|
|
91
|
+
|
|
92
|
+
var isMulti = selected ? Boolean(selected.length) : false;
|
|
93
|
+
|
|
94
|
+
var handleChange = function handleChange(event) {
|
|
95
|
+
var newValue = event.target.value;
|
|
96
|
+
onSearchChange(newValue);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
var handleBlur = function handleBlur() {
|
|
100
|
+
if (isSearchable && inputRef.current) {
|
|
101
|
+
setIsSearchFocused(false);
|
|
102
|
+
setIsSearchKeyFocused(false);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
var handleFocus = function handleFocus(e) {
|
|
107
|
+
if (isSearchable && inputRef.current) {
|
|
108
|
+
setIsSearchFocused(true);
|
|
109
|
+
var target = e.target;
|
|
110
|
+
setIsSearchKeyFocused(target.getAttribute('data-focus-method') === 'key');
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
var isThemeReversed = mode === 'rock' && (isSearchFocused || isFocused || isOpen);
|
|
115
|
+
return /*#__PURE__*/React.createElement("div", _objectSpread({
|
|
116
|
+
tabIndex: isSearchable || isDisabled ? -1 : 0,
|
|
117
|
+
className: cn(styles.main, SIZE_STYLES[size], MODE_STYLES[mode], THEME_STYLES[theme], isMulti && styles.isMulti, isSearchFocused && styles.searchFocused, isSearchKeyFocused && styles.searchKeyFocused, isOpen && styles.open, isDisabled ? styles.disabled : styles.enabled, Boolean(error) && styles.error, isFocused && styles.focused, className),
|
|
118
|
+
onFocus: function onFocus(e) {
|
|
119
|
+
var target = e.target;
|
|
120
|
+
setIsFocused(target.getAttribute('data-focus-method') === 'key');
|
|
121
|
+
},
|
|
122
|
+
onBlur: function onBlur() {
|
|
123
|
+
return setIsFocused(false);
|
|
124
|
+
},
|
|
125
|
+
ref: triggerRef,
|
|
126
|
+
"data-test": "select"
|
|
127
|
+
}, restProps), isMulti ? /*#__PURE__*/React.createElement(MultiselectOptions, {
|
|
128
|
+
selected: selected,
|
|
129
|
+
size: size,
|
|
130
|
+
isDisabled: isDisabled,
|
|
131
|
+
isSingleLine: isSingleLine,
|
|
132
|
+
onItemClearClick: onItemClearClick,
|
|
133
|
+
theme: theme,
|
|
134
|
+
isThemeReversed: isThemeReversed
|
|
135
|
+
}) : /*#__PURE__*/React.createElement(SelectedOption, {
|
|
136
|
+
selected: selected,
|
|
137
|
+
placeholder: placeholder,
|
|
138
|
+
searchInput: searchInput,
|
|
139
|
+
isSearchable: isSearchable,
|
|
140
|
+
isDisabled: isDisabled,
|
|
141
|
+
inputRef: inputRef,
|
|
142
|
+
handleBlur: handleBlur,
|
|
143
|
+
handleFocus: handleFocus,
|
|
144
|
+
handleChange: handleChange
|
|
145
|
+
}), /*#__PURE__*/React.createElement("div", {
|
|
146
|
+
className: styles.iconsWrapper
|
|
147
|
+
}, /*#__PURE__*/React.createElement(ClearOrLoading, {
|
|
148
|
+
isLoading: isLoading,
|
|
149
|
+
isDisabled: isDisabled,
|
|
150
|
+
isClearable: isClearable,
|
|
151
|
+
onClearClick: onClearClick,
|
|
152
|
+
selected: selected,
|
|
153
|
+
size: size
|
|
154
|
+
}), /*#__PURE__*/React.createElement(DownIcon, {
|
|
155
|
+
size: size,
|
|
156
|
+
className: styles.toggle,
|
|
157
|
+
"data-test": "down-icon"
|
|
158
|
+
})));
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
var MultiselectOptions = function MultiselectOptions(_ref) {
|
|
162
|
+
var isSingleLine = _ref.isSingleLine,
|
|
163
|
+
selected = _ref.selected,
|
|
164
|
+
isThemeReversed = _ref.isThemeReversed,
|
|
165
|
+
size = _ref.size,
|
|
166
|
+
isDisabled = _ref.isDisabled,
|
|
167
|
+
onItemClearClick = _ref.onItemClearClick,
|
|
168
|
+
theme = _ref.theme;
|
|
169
|
+
|
|
170
|
+
var selectedOptions = _mapInstanceProperty(selected).call(selected, function (s) {
|
|
171
|
+
var key = s.value ? s.value : s.label;
|
|
172
|
+
return /*#__PURE__*/React.createElement(RemovableTag, {
|
|
173
|
+
key: key,
|
|
174
|
+
"data-test": "selected-tag-".concat(key),
|
|
175
|
+
className: styles.tag,
|
|
176
|
+
disabled: isDisabled,
|
|
177
|
+
onRemoveClick: function onRemoveClick() {
|
|
178
|
+
return onItemClearClick(s);
|
|
179
|
+
},
|
|
180
|
+
size: TAG_SIZE[size],
|
|
181
|
+
wrap: !isSingleLine,
|
|
182
|
+
theme: isThemeReversed ? reverseTheme(theme) : theme
|
|
183
|
+
}, s.label);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
187
|
+
className: styles.multi,
|
|
188
|
+
"data-test": "multi"
|
|
189
|
+
}, isSingleLine ? /*#__PURE__*/React.createElement(Foldable, {
|
|
190
|
+
className: styles.tagsContainer,
|
|
191
|
+
renderHiddenCounter: function renderHiddenCounter(n, ref) {
|
|
192
|
+
return /*#__PURE__*/React.createElement(Tag, _objectSpread({
|
|
193
|
+
className: styles.tag,
|
|
194
|
+
key: "+",
|
|
195
|
+
ref: ref,
|
|
196
|
+
size: TAG_SIZE[size]
|
|
197
|
+
}, TAG_PRESET[isThemeReversed ? reverseTheme(theme) : theme]), "+".concat(n));
|
|
198
|
+
},
|
|
199
|
+
gap: {
|
|
200
|
+
l: 8,
|
|
201
|
+
m: 4,
|
|
202
|
+
s: 4
|
|
203
|
+
}[size]
|
|
204
|
+
}, selectedOptions) : /*#__PURE__*/React.createElement("div", {
|
|
205
|
+
className: styles.tagsContainer
|
|
206
|
+
}, selectedOptions));
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
function isSelectedEmpty(selected) {
|
|
210
|
+
if (Array.isArray(selected)) {
|
|
211
|
+
return selected.length === 0;
|
|
212
|
+
} else {
|
|
213
|
+
return !selected;
|
|
214
|
+
}
|
|
215
|
+
} // eslint-disable-next-line complexity
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
var SelectedOption = function SelectedOption(_ref2) {
|
|
219
|
+
var selected = _ref2.selected,
|
|
220
|
+
_ref2$searchInput = _ref2.searchInput,
|
|
221
|
+
searchInput = _ref2$searchInput === void 0 ? '' : _ref2$searchInput,
|
|
222
|
+
_ref2$placeholder = _ref2.placeholder,
|
|
223
|
+
placeholder = _ref2$placeholder === void 0 ? '' : _ref2$placeholder,
|
|
224
|
+
isSearchable = _ref2.isSearchable,
|
|
225
|
+
isDisabled = _ref2.isDisabled,
|
|
226
|
+
inputRef = _ref2.inputRef,
|
|
227
|
+
handleBlur = _ref2.handleBlur,
|
|
228
|
+
handleFocus = _ref2.handleFocus,
|
|
229
|
+
handleChange = _ref2.handleChange;
|
|
230
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
231
|
+
className: styles.labelWrapper
|
|
232
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
233
|
+
className: cn(styles.label, isSelectedEmpty(selected) && styles.notSelected)
|
|
234
|
+
}, searchInput === '' ? selected && selected.label ? selected.label : placeholder : ''), isSearchable && !isDisabled && /*#__PURE__*/React.createElement("input", {
|
|
235
|
+
ref: inputRef,
|
|
236
|
+
tabIndex: isSearchable ? 0 : -1,
|
|
237
|
+
onBlur: handleBlur,
|
|
238
|
+
onFocus: handleFocus,
|
|
239
|
+
className: styles.searchInput,
|
|
240
|
+
autoCapitalize: "none",
|
|
241
|
+
autoComplete: "off",
|
|
242
|
+
autoCorrect: "off",
|
|
243
|
+
spellCheck: "false",
|
|
244
|
+
type: "text",
|
|
245
|
+
value: searchInput,
|
|
246
|
+
onChange: handleChange,
|
|
247
|
+
"data-test": "search-input"
|
|
248
|
+
}));
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
var ClearOrLoading = function ClearOrLoading(_ref3) {
|
|
252
|
+
var isClearable = _ref3.isClearable,
|
|
253
|
+
isLoading = _ref3.isLoading,
|
|
254
|
+
isDisabled = _ref3.isDisabled,
|
|
255
|
+
onClearClick = _ref3.onClearClick,
|
|
256
|
+
selected = _ref3.selected,
|
|
257
|
+
size = _ref3.size;
|
|
258
|
+
|
|
259
|
+
if (isLoading) {
|
|
260
|
+
return /*#__PURE__*/React.createElement(LoadingIcon, {
|
|
261
|
+
style: {
|
|
262
|
+
pointerEvents: 'none'
|
|
263
|
+
},
|
|
264
|
+
size: size,
|
|
265
|
+
className: styles.icon,
|
|
266
|
+
"data-test": "loading-icon"
|
|
267
|
+
});
|
|
268
|
+
} else if (isClearable && !isDisabled && !isSelectedEmpty(selected)) {
|
|
269
|
+
return /*#__PURE__*/React.createElement("button", {
|
|
270
|
+
type: "button",
|
|
271
|
+
className: cn(styles.icon, styles.clearIcon),
|
|
272
|
+
"data-test": "clear-icon",
|
|
273
|
+
onClick: function onClick(e) {
|
|
274
|
+
e.stopPropagation();
|
|
275
|
+
onClearClick();
|
|
276
|
+
}
|
|
277
|
+
}, /*#__PURE__*/React.createElement(CloseIcon, {
|
|
278
|
+
size: size
|
|
279
|
+
}));
|
|
280
|
+
} else {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
SelectTrigger.propTypes = {
|
|
286
|
+
size: PropTypes.oneOf(['m', 's', 'l']),
|
|
287
|
+
mode: PropTypes.oneOf(['classic', 'rock']),
|
|
288
|
+
theme: PropTypes.oneOf(['light', 'dark']),
|
|
289
|
+
className: PropTypes.string,
|
|
290
|
+
isOpen: PropTypes.bool,
|
|
291
|
+
selected: PropTypes.oneOfType([optionType, PropTypes.arrayOf(optionType)]),
|
|
292
|
+
placeholder: PropTypes.string,
|
|
293
|
+
isClearable: PropTypes.bool,
|
|
294
|
+
isLoading: PropTypes.bool,
|
|
295
|
+
isDisabled: PropTypes.bool,
|
|
296
|
+
isSearchable: PropTypes.bool,
|
|
297
|
+
isSingleLine: PropTypes.bool,
|
|
298
|
+
onClearClick: PropTypes.func,
|
|
299
|
+
onItemClearClick: PropTypes.func,
|
|
300
|
+
onSearchChange: PropTypes.func,
|
|
301
|
+
error: PropTypes.bool,
|
|
302
|
+
triggerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
|
|
303
|
+
current: PropTypes.any
|
|
304
|
+
})]),
|
|
305
|
+
searchInput: PropTypes.string
|
|
306
|
+
};
|
|
307
|
+
export default SelectTrigger;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
var styles = {
|
|
2
|
+
"main": "_main_gaux6h_24",
|
|
3
|
+
"open": "_open_gaux6h_36",
|
|
4
|
+
"enabled": "_enabled_gaux6h_41",
|
|
5
|
+
"disabled": "_disabled_gaux6h_45",
|
|
6
|
+
"focused": "_focused_gaux6h_49",
|
|
7
|
+
"searchFocused": "_searchFocused_gaux6h_50",
|
|
8
|
+
"sizeL": "_sizeL_gaux6h_56",
|
|
9
|
+
"sizeM": "_sizeM_gaux6h_59",
|
|
10
|
+
"rs-reset-letter-spacing": "_rs-reset-letter-spacing_gaux6h_1",
|
|
11
|
+
"sizeS": "_sizeS_gaux6h_62",
|
|
12
|
+
"isMulti": "_isMulti_gaux6h_136",
|
|
13
|
+
"error": "_error_gaux6h_69",
|
|
14
|
+
"notSelected": "_notSelected_gaux6h_144",
|
|
15
|
+
"themeLight": "_themeLight_gaux6h_148",
|
|
16
|
+
"searchKeyFocused": "_searchKeyFocused_gaux6h_154",
|
|
17
|
+
"rock": "_rock_gaux6h_168",
|
|
18
|
+
"toggle": "_toggle_gaux6h_173",
|
|
19
|
+
"labelWrapper": "_labelWrapper_gaux6h_177",
|
|
20
|
+
"themeDark": "_themeDark_gaux6h_199",
|
|
21
|
+
"label": "_label_gaux6h_177",
|
|
22
|
+
"iconsWrapper": "_iconsWrapper_gaux6h_285",
|
|
23
|
+
"searchInput": "_searchInput_gaux6h_340",
|
|
24
|
+
"multi": "_multi_gaux6h_366",
|
|
25
|
+
"tagsContainer": "_tagsContainer_gaux6h_375",
|
|
26
|
+
"tag": "_tag_gaux6h_375",
|
|
27
|
+
"icon": "_icon_gaux6h_285",
|
|
28
|
+
"clearIcon": "_clearIcon_gaux6h_490"
|
|
29
|
+
};
|
|
30
|
+
export default styles;
|