@mailstep/design-system 0.8.47 → 0.8.49
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/package.json
CHANGED
|
@@ -5,7 +5,7 @@ import "../Label/index.js";
|
|
|
5
5
|
import SpaceAround_default from "../SpaceAround/index.js";
|
|
6
6
|
import { StyledAsyncSelect, StyledReactSelect, Wrapper } from "./styles.js";
|
|
7
7
|
import { useStylesAndComponents } from "./themes/index.js";
|
|
8
|
-
import { useCallback, useMemo } from "react";
|
|
8
|
+
import { useCallback, useMemo, useRef } from "react";
|
|
9
9
|
import { useTheme } from "@xstyled/styled-components";
|
|
10
10
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
11
11
|
import { i18n } from "@lingui/core";
|
|
@@ -22,9 +22,41 @@ const loadingMessage = () => i18n._({
|
|
|
22
22
|
const Select = ({ label, name, value = "", options = emptyOptions, defaultOptions, loadOptions, onChange, onBlur, error, isInvalid, isLoading, isDarkPlaceholderText, spaceAround = false, disabled, onInputChange, isSearchable, isMulti, className, useSimplifiedOptions = false, showSelectAllButton = false, shortValues = true, shortVariant = "block", style, optionVariant, multiLabelVariant, containerVariant, placeholder, placeholderIcon, innerRef, maxMenuHeight, getOptionValue, onIconClick, title, ...passTroughProps }) => {
|
|
23
23
|
const isAsync = !!loadOptions;
|
|
24
24
|
if (showSelectAllButton && !isMulti) console.error("CheckboxSelect incompatible props");
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
const localGetOptionValue = useMemo(() => getOptionValue || ((option) => option?.value), [getOptionValue]);
|
|
26
|
+
const availableOptionsByValue = useMemo(() => {
|
|
27
|
+
const map = /* @__PURE__ */ new Map();
|
|
28
|
+
for (const option of [...defaultOptions || [], ...options || []]) map.set(localGetOptionValue(option), option);
|
|
29
|
+
return map;
|
|
30
|
+
}, [
|
|
31
|
+
defaultOptions,
|
|
32
|
+
options,
|
|
33
|
+
localGetOptionValue
|
|
34
|
+
]);
|
|
35
|
+
const selectedHistoryRef = useRef(/* @__PURE__ */ new Map());
|
|
36
|
+
const resolveOption = useCallback((optionValue) => availableOptionsByValue.get(optionValue) || selectedHistoryRef.current.get(optionValue), [availableOptionsByValue]);
|
|
37
|
+
let selectedOption = null;
|
|
38
|
+
if (Array.isArray(value)) {
|
|
39
|
+
const resolved = value.map(resolveOption).filter((option) => Boolean(option));
|
|
40
|
+
selectedOption = resolved.length ? resolved : null;
|
|
41
|
+
} else if (value !== void 0) selectedOption = resolveOption(value) || null;
|
|
42
|
+
const handleChange = useCallback((newValue) => {
|
|
43
|
+
(Array.isArray(newValue) ? newValue : newValue ? [newValue] : []).forEach((option) => option && selectedHistoryRef.current.set(localGetOptionValue(option), option));
|
|
44
|
+
onChange?.(newValue);
|
|
45
|
+
}, [onChange, localGetOptionValue]);
|
|
46
|
+
const setAllOptions = useCallback(({ options: optionsToAdd }) => {
|
|
47
|
+
const merged = [...Array.isArray(selectedOption) ? selectedOption : selectedOption ? [selectedOption] : []];
|
|
48
|
+
const visibleOptions = optionsToAdd || [];
|
|
49
|
+
for (const option of visibleOptions) {
|
|
50
|
+
const optionValue = localGetOptionValue(option);
|
|
51
|
+
if (!merged.some((selected) => localGetOptionValue(selected) === optionValue)) merged.push(resolveOption(optionValue) || option);
|
|
52
|
+
}
|
|
53
|
+
handleChange(merged);
|
|
54
|
+
}, [
|
|
55
|
+
handleChange,
|
|
56
|
+
selectedOption,
|
|
57
|
+
localGetOptionValue,
|
|
58
|
+
resolveOption
|
|
59
|
+
]);
|
|
28
60
|
const unsetAllOptions = useCallback(() => {
|
|
29
61
|
if (typeof onChange === "function") onChange?.(emptyOptions);
|
|
30
62
|
}, [onChange]);
|
|
@@ -32,12 +64,6 @@ const Select = ({ label, name, value = "", options = emptyOptions, defaultOption
|
|
|
32
64
|
const modifiedMaxMenuHeight = showSelectAllButton && maxMenuHeight ? maxMenuHeight - 30 : maxMenuHeight;
|
|
33
65
|
const [customComponents, customTheme, customStyles] = useStylesAndComponents(style, optionVariant, multiLabelVariant, containerVariant, useSimplifiedOptions, showSelectAllButton);
|
|
34
66
|
const RenderComponent = isAsync ? StyledAsyncSelect : StyledReactSelect;
|
|
35
|
-
let selectedOption;
|
|
36
|
-
const localGetOptionValue = useMemo(() => getOptionValue || ((option) => option?.value), [getOptionValue]);
|
|
37
|
-
if (Array.isArray(value)) {
|
|
38
|
-
selectedOption = (defaultOptions || options).filter((option) => value.includes(localGetOptionValue(option)));
|
|
39
|
-
selectedOption = selectedOption?.length ? selectedOption : null;
|
|
40
|
-
} else if (value !== void 0) selectedOption = (defaultOptions || options).find((option) => localGetOptionValue(option) === value) || null;
|
|
41
67
|
const theme = useTheme();
|
|
42
68
|
const handleLoadOptions = useMemo(() => {
|
|
43
69
|
return loadOptions ? async (fulltext) => await loadOptions(fulltext) : void 0;
|
|
@@ -56,7 +82,7 @@ const Select = ({ label, name, value = "", options = emptyOptions, defaultOption
|
|
|
56
82
|
loadOptions: handleLoadOptions,
|
|
57
83
|
defaultOptions,
|
|
58
84
|
value: selectedOption,
|
|
59
|
-
onChange,
|
|
85
|
+
onChange: handleChange,
|
|
60
86
|
onBlur,
|
|
61
87
|
isDisabled: disabled || isLoading,
|
|
62
88
|
isLoading,
|