@payfit/unity-components
Version:
280 lines (279 loc) • 10.6 kB
JavaScript
import { Spinner as e } from "../spinner/Spinner.js";
import { CircularIconButton as t } from "../icon-button/CircularIconButton.js";
import { Badge as n } from "../badge/Badge.js";
import { Search as r } from "../search/Search.js";
import { MultiSelectContext as ee } from "./Multiselect.context.js";
import { getSelectionState as te, toggleSelection as ne } from "./utils/select-all.js";
import { MultiSelectSelectAllOption as re } from "./parts/MultiSelectSelectAllOption.js";
import { MultiSelectOptGroup as i } from "./parts/MultiSelectOptGroup.js";
import { MultiSelectOption as a } from "./parts/MultiSelectOption.js";
import { reactAriaValueToSet as ie, setToReactAriaDefaultValue as ae, setToReactAriaValue as oe } from "./utils/selection-adapter.js";
import { Children as o, forwardRef as s, isValidElement as c, useCallback as l, useMemo as u, useRef as d, useState as se } from "react";
import { uyTv as ce } from "@payfit/unity-themes";
import { Fragment as le, jsx as f, jsxs as p } from "react/jsx-runtime";
import { useTranslation as ue } from "react-i18next";
import { Button as de, ButtonContext as fe } from "react-aria-components/Button";
import { Icon as m } from "@payfit/unity-icons";
import { Header as pe } from "react-aria-components/Header";
import { Separator as me } from "react-aria-components/Separator";
import { ListBox as h, ListBoxSection as he } from "react-aria-components/ListBox";
import { Popover as ge } from "react-aria-components/Popover";
import { Autocomplete as _e, useFilter as ve } from "react-aria-components/Autocomplete";
import { ListLayout as g, Virtualizer as _ } from "react-aria-components/Virtualizer";
import { Select as ye } from "react-aria-components/Select";
//#region src/core/components/multi-select/MultiSelect.tsx
var be = ce({
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:isolate uy:shadow-300 uy:z-50 uy:flex uy:min-h-0 uy:flex-col 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-hidden uy:outline-none",
list: "uy:min-h-0 uy:flex-1 uy:py-100 uy:data-[empty=true]:px-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" }
},
showSelectAll: { true: { list: "uy:pt-0" } }
},
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 v(e) {
return e instanceof Map;
}
function xe(e) {
return !Object.prototype.hasOwnProperty.call(e, "items");
}
function y(e) {
return o.toArray(e).some((e) => c(e) ? e.type === a || e.type !== i && y(e.props.children) : !1);
}
function b(e) {
return o.toArray(e).some((e) => c(e) ? e.type === i || b(e.props.children) : !1);
}
function x(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 Se = (e) => Array.from(e).map(String).join(", "), Ce = (e) => Array.from(e).map((e) => x(e)).join(", ");
function S(i, a) {
let { t: o } = ue("unity"), s = d(null), c = d(null), [ce, S] = se(!1), C = xe(i), w = i, T = i, { placeholder: E, isDisabled: D, isReadOnly: O, isLoading: k, isInvalid: A, onClearButtonPress: j, onBlur: we, onFocus: Te, onChange: M, onOpenChange: N, value: P, defaultValue: F, isSearchable: Ee, showSelectAll: I = !1, renderEmptyState: L, renderValue: De, maxVisibleItems: R = 7, children: z, searchOptions: B, ...Oe } = i, [V, ke] = se(() => new Set(F ?? [])), H = C ? void 0 : w.items, U = C ? void 0 : w.getItemValue, Ae = C ? y(T.children) : !!H && !v(H), W = C ? b(T.children) : !!H && v(H), G = I && Ae, je = G && W, K = P !== void 0, q = u(() => K ? new Set(P) : V, [
K,
P,
V
]), J = De ?? (C ? Se : Ce), Y = l((e) => C || !U ? x(e) : U(e), [U, C]), X = l(() => C || !H ? [] : v(H) ? Array.from(H.values()).flat() : Array.from(H), [H, C]), Me = u(() => {
if (C) return new Set(q);
let e = X(), t = /* @__PURE__ */ new Set();
return q.forEach((n) => {
let r = e.find((e) => Y(e) === n);
r && t.add(r);
}), t;
}, [
X,
C,
Y,
q
]), Z = Array.from(Me), Q = Math.max(Z.length - R, 0), Ne = q.size > 0 && !D && !O && !k, { root: Pe, trigger: Fe, inputValue: Ie, state: Le, suffix: Re, clearButton: ze, popover: Be, list: Ve } = be({
isDisabled: !!D,
isReadOnly: !!O,
isInvalid: !!A,
isEmpty: q.size === 0,
showSelectAll: G
}), He = u(() => {
if (Z.length === 0) return E;
let e = Z.slice(0, R), t = new Set(e);
return J(t);
}, [
R,
E,
J,
Z
]), $ = l((e) => {
K || ke(e), M?.(e);
}, [K, M]), Ue = l((e) => {
$(ie(e));
}, [$]), We = l(() => {
$(/* @__PURE__ */ new Set()), j?.();
}, [j, $]), Ge = l((e) => {
S(e), N?.(e);
}, [S, N]), Ke = l(() => {
if (C) return T.children;
if (!H) return null;
if (v(H)) {
let e = H;
return Array.from(e.entries()).map(([e, t]) => z(e, t));
}
return Array.from(H).map((e) => z(e));
}, [
z,
H,
C,
T.children
]), qe = u(() => Ke(), [Ke]), Je = G ? /* @__PURE__ */ p(he, {
id: "select-all",
children: [/* @__PURE__ */ f(pe, {
className: "uy:contents",
children: /* @__PURE__ */ f(re, { withSeparator: je })
}), qe]
}) : qe, { contains: Ye } = ve(B ?? { sensitivity: "base" }), Xe = l((e) => te(q, e), [q]), Ze = l((e) => {
$(ne(q, e));
}, [q, $]);
return /* @__PURE__ */ f(ye, {
...Oe,
ref: s,
className: Pe(),
isDisabled: D || O,
isInvalid: A,
onBlur: we,
onChange: Ue,
onFocus: Te,
onOpenChange: Ge,
placeholder: E,
selectionMode: "multiple",
shouldCloseOnSelect: !1,
value: oe(q),
defaultValue: ae(F),
allowsEmptyCollection: !0,
children: /* @__PURE__ */ p(ee.Provider, {
value: {
showSelectAll: I,
indentStandaloneOptions: G && !W,
isDisabled: D,
isReadOnly: O,
isLoading: k,
searchOptions: B,
getSelectionState: Xe,
toggleSelection: Ze
},
children: [
/* @__PURE__ */ p(de, {
ref: a,
className: Fe(),
"data-dd-privacy": "mask",
isDisabled: D || O,
"aria-invalid": A,
"aria-readonly": O,
children: [
/* @__PURE__ */ f("span", {
className: Ie(),
translate: "no",
children: q.size > 0 ? He : E
}),
/* @__PURE__ */ p("span", {
className: Le(),
children: [
k && /* @__PURE__ */ f(e, {
color: "inherit",
size: "small",
label: o((e) => e.unity.component.formField.formInput.spinner.label)
}),
A && /* @__PURE__ */ f(m, {
src: "WarningCircleOutlined",
color: "content.form.invalid",
alt: o((e) => e.unity.component.formField.formInput.error.alt)
}),
Q > 0 && /* @__PURE__ */ p(n, {
variant: "numeric",
children: ["+", Q]
}),
Ne && /* @__PURE__ */ f("span", {
className: "uy:w-300 uy:shrink-0",
"aria-hidden": !0
})
]
}),
/* @__PURE__ */ f(m, {
className: Re(),
src: ce ? "CaretUpOutlined" : "CaretDownOutlined",
role: "presentation"
})
]
}),
Ne && /* @__PURE__ */ f(fe.Provider, {
value: null,
children: /* @__PURE__ */ f(t, {
title: o((e) => e.unity.component.common.clear.title),
className: ze(),
icon: "CloseOutlined",
onPress: We,
asElement: "button"
})
}),
/* @__PURE__ */ f(ge, {
ref: c,
offset: 1,
containerPadding: 8,
className: Be(),
children: Ee ? /* @__PURE__ */ p(_e, {
filter: Ye,
children: [
/* @__PURE__ */ f("div", {
className: "uy:m-100",
children: /* @__PURE__ */ f(r, {
label: o((e) => e.unity.component.select.search.label),
"data-dd-privacy": "mask"
})
}),
/* @__PURE__ */ f(me, { className: "uy:mx-100 uy:border-t uy:border-border-neutral uy:border-solid" }),
/* @__PURE__ */ f(_, {
layout: g,
layoutOptions: { padding: 8 },
children: /* @__PURE__ */ f(h, {
className: Ve(),
renderEmptyState: L,
children: Je
})
})
]
}) : /* @__PURE__ */ f(le, { children: /* @__PURE__ */ f(_, {
layout: g,
layoutOptions: { padding: 8 },
children: /* @__PURE__ */ f(h, {
className: Ve(),
renderEmptyState: L,
children: Je
})
}) })
})
]
})
});
}
var C = s(S);
C.displayName = "MultiSelect";
//#endregion
export { C as MultiSelect };