UNPKG

@payfit/unity-components

Version:

257 lines (256 loc) • 9.18 kB
import { Icon as e } from "../icon/Icon.js"; import { Spinner as t } from "../spinner/Spinner.js"; import { CircularIconButton as n } from "../icon-button/CircularIconButton.js"; import { Badge as r } from "../badge/Badge.js"; import { reactAriaValueToSet as ee, setToReactAriaDefaultValue as i, setToReactAriaValue as te } from "./utils/selection-adapter.js"; import { Search as ne } from "../search/Search.js"; import { MultiSelectContext as re } from "./Multiselect.context.js"; import { forwardRef as a, useCallback as o, useMemo as s, useRef as c, useState as l } from "react"; import { uyTv as u } from "@payfit/unity-themes"; import { jsx as d, jsxs as f } from "react/jsx-runtime"; import { useIntl as ie } from "react-intl"; import { Button as ae, ButtonContext as oe } from "react-aria-components/Button"; import { Separator as se } from "react-aria-components/Separator"; import { ListBox as p } from "react-aria-components/ListBox"; import { Popover as ce } from "react-aria-components/Popover"; import { Autocomplete as le, useFilter as ue } from "react-aria-components/Autocomplete"; import { Select as de } from "react-aria-components/Select"; //#region src/core/components/multi-select/MultiSelect.tsx var fe = u({ slots: { root: "uy:relative uy:flex uy:flex-col uy:gap-100 uy:w-full", trigger: "uy:relative uy:h-5.5 uy:sm:h-500 uy:flex uy:items-center uy:justify-between uy:py-125 uy:sm:py-100 uy:px-150 uy:w-full uy:border uy:border-solid uy:rounded-100 uy:sm:rounded-75 uy:focus-visible:outline-none uy:focus-visible:ring-2 uy:focus-visible:ring-utility-focus-ring uy:focus-visible:ring-offset-2 uy:active:not([disabled]):border-border-form-active uy:pointer-events-auto!", inputValue: "uy:flex-1 uy:typography-body uy:text-left uy:line-clamp-1", state: "uy:grow-0 uy:outline-none uy:typography-body uy:placeholder:text-content-neutral-lowest uy:inline-flex uy:gap-50 uy:items-center uy:px-100", suffix: "uy:text-content-neutral-lowest", clearButton: "uy:absolute uy:right-[36px] uy:top-1/2 uy:z-10 uy:-translate-y-1/2", popover: "uy:shadow-300 uy:z-50 uy:w-(--trigger-width) uy:max-h-[min(var(--popover-available-height,300px),300px)] uy:rounded-100 uy:sm:rounded-75 uy:border uy:border-solid uy:border-border-neutral uy:bg-surface-neutral uy:overflow-y-auto uy:overflow-x-hidden uy:outline-none", list: "uy:p-100 uy:overflow-y-auto uy:overflow-x-hidden" }, variants: { isDisabled: { true: { trigger: "uy:bg-surface-form-disabled uy:cursor-not-allowed uy:border-border-form-disabled uy:text-content-form-disabled", inputValue: "uy:text-content-form-disabled!", state: "uy:text-content-form-disabled", suffix: "uy:text-content-form-disabled" } }, isReadOnly: { true: { trigger: "uy:bg-surface-form-disabled uy:cursor-not-allowed uy:border-border-form-disabled", inputValue: "uy:text-content-form-disabled", state: "uy:text-content-form-disabled", suffix: "uy:text-content-form-disabled" } }, isInvalid: { true: { trigger: "uy:bg-surface-form-error uy:border-border-form-error", inputValue: "uy:text-content-form-error", state: "uy:text-content-form-error" } }, isEmpty: { true: { inputValue: "uy:text-content-neutral-lowest" }, false: { inputValue: "uy:text-content-form-enabled" } } }, compoundVariants: [{ isInvalid: !1, isDisabled: !1, isReadOnly: !1, className: { trigger: "uy:border-border-form-enabled uy:bg-surface-form-enabled", inputValue: "uy:text-content-form-enabled uy:bg-surface-form-enabled", state: "uy:text-content-neutral-disabled" } }, { isEmpty: !0, isDisabled: !1, isReadOnly: !1, isInvalid: !1, className: { inputValue: "uy:text-content-neutral-lowest" } }] }); function m(e) { return e instanceof Map; } function pe(e) { return !Object.prototype.hasOwnProperty.call(e, "items"); } function h(e) { return typeof e == "object" && e && "value" in e && (typeof e.value == "string" || typeof e.value == "number") ? e.value : typeof e == "object" && e && "id" in e && (typeof e.id == "string" || typeof e.id == "number") ? e.id : typeof e == "string" || typeof e == "number" ? e : String(e); } var me = (e) => Array.from(e).map(String).join(", "), g = (e) => Array.from(e).map((e) => h(e)).join(", "); function _(a, u) { let _ = ie(), v = c(null), he = c(null), [ge, y] = l(!1), b = pe(a), x = a, S = a, { placeholder: C, isDisabled: w, isReadOnly: T, isLoading: E, isInvalid: D, onClearButtonPress: O, onBlur: _e, onFocus: ve, onChange: k, onOpenChange: A, value: j, defaultValue: M, isSearchable: N, renderEmptyState: P, renderValue: F, maxVisibleItems: I = 7, children: L, searchOptions: ye, ...be } = a, [R, xe] = l(() => new Set(M ?? [])), z = b ? void 0 : x.items, B = b ? void 0 : x.getItemValue, V = j !== void 0, H = s(() => V ? new Set(j) : R, [ V, j, R ]), U = F ?? (b ? me : g), W = o((e) => b || !B ? h(e) : B(e), [B, b]), G = o(() => b || !z ? [] : m(z) ? Array.from(z.values()).flat() : Array.from(z), [z, b]), Se = s(() => { if (b) return new Set(H); let e = G(), t = /* @__PURE__ */ new Set(); return H.forEach((n) => { let r = e.find((e) => W(e) === n); r && t.add(r); }), t; }, [ G, b, W, H ]), K = Array.from(Se), q = Math.max(K.length - I, 0), J = H.size > 0 && !w && !T && !E, { root: Ce, trigger: we, inputValue: Te, state: Ee, suffix: De, clearButton: Oe, popover: ke, list: Y } = fe({ isDisabled: !!w, isReadOnly: !!T, isInvalid: !!D, isEmpty: H.size === 0 }), Ae = s(() => { if (K.length === 0) return C; let e = K.slice(0, I), t = new Set(e); return U(t); }, [ I, C, U, K ]), X = o((e) => { V || xe(e), k?.(e); }, [V, k]), je = o((e) => { X(ee(e)); }, [X]), Me = o(() => { X(/* @__PURE__ */ new Set()), O?.(); }, [O, X]), Z = o((e) => { y(e), A?.(e); }, [y, A]), Q = o(() => { if (b) return S.children; if (!z) return null; if (m(z)) { let e = z; return Array.from(e.entries()).map(([e, t]) => L(e, t)); } return Array.from(z).map((e) => L(e)); }, [ L, z, b, S.children ]), $ = s(() => Q(), [Q]), { contains: Ne } = ue(ye ?? { sensitivity: "base" }); return /* @__PURE__ */ d(de, { ...be, ref: v, className: Ce(), isDisabled: w || T, isInvalid: D, onBlur: _e, onChange: je, onFocus: ve, onOpenChange: Z, placeholder: C, selectionMode: "multiple", shouldCloseOnSelect: !1, value: te(H), defaultValue: i(M), allowsEmptyCollection: !0, children: /* @__PURE__ */ f(re.Provider, { value: { isSearchable: N, maxVisibleItems: I, selectedKeys: H }, children: [ /* @__PURE__ */ f(ae, { ref: u, className: we(), "data-dd-privacy": "mask", isDisabled: w || T, "aria-invalid": D, "aria-readonly": T, children: [ /* @__PURE__ */ d("span", { className: Te(), translate: "no", children: H.size > 0 ? Ae : C }), /* @__PURE__ */ f("span", { className: Ee(), children: [ E && /* @__PURE__ */ d(t, { color: "inherit", size: "small", label: _.formatMessage({ id: "unity:component:form-field:form-input:spinner:label", defaultMessage: "Loading" }) }), D && /* @__PURE__ */ d(e, { src: "WarningCircleOutlined", color: "content.form.invalid", alt: _.formatMessage({ id: "unity:component:form-field:form-input:error:alt", defaultMessage: "Error" }) }), q > 0 && /* @__PURE__ */ f(r, { variant: "numeric", children: ["+", q] }), J && /* @__PURE__ */ d("span", { className: "uy:w-300 uy:shrink-0", "aria-hidden": !0 }) ] }), /* @__PURE__ */ d(e, { className: De(), src: ge ? "CaretUpOutlined" : "CaretDownOutlined", role: "presentation" }) ] }), J && /* @__PURE__ */ d(oe.Provider, { value: null, children: /* @__PURE__ */ d(n, { title: _.formatMessage({ id: "unity:component:common:clear:title", defaultMessage: "Clear" }), className: Oe(), icon: "CloseOutlined", onPress: Me, asElement: "button" }) }), /* @__PURE__ */ d(ce, { ref: he, offset: 1, containerPadding: 8, className: ke(), children: N ? /* @__PURE__ */ f(le, { filter: Ne, children: [ /* @__PURE__ */ d("div", { className: "uy:m-100", children: /* @__PURE__ */ d(ne, { label: _.formatMessage({ id: "unity:component:select:search:label", defaultMessage: "Search options" }), "data-dd-privacy": "mask" }) }), /* @__PURE__ */ d(se, { className: "uy:mx-100 uy:border-t uy:border-border-neutral uy:border-solid" }), /* @__PURE__ */ d(p, { className: Y(), renderEmptyState: P, children: $ }) ] }) : /* @__PURE__ */ d(p, { className: Y(), renderEmptyState: P, children: $ }) }) ] }) }); } var v = a(_); v.displayName = "MultiSelect"; //#endregion export { v as MultiSelect };