@payfit/unity-components
Version:
126 lines (125 loc) • 5.14 kB
JavaScript
import { Spinner as e } from "../spinner/Spinner.js";
import { Text as t } from "../text/Text.js";
import { AutocompleteClearButton as n } from "./parts/AutocompleteClearButton.js";
import { AutocompletePanel as r } from "./parts/AutocompletePanel.js";
import { forwardRef as i, useCallback as a, useRef as o, useState as s } from "react";
import { uyTv as c } from "@payfit/unity-themes";
import { jsx as l, jsxs as u } from "react/jsx-runtime";
import { useTranslation as d } from "react-i18next";
import { Icon as f } from "@payfit/unity-icons";
import { useResizeObserver as p } from "usehooks-ts";
import { ComboBox as m, Input as h } from "react-aria-components/ComboBox";
import { useId as g } from "react-aria/useId";
//#region src/core/components/autocomplete/Autocomplete.tsx
var _ = c({
slots: {
base: [
"uy:group uy:transition-colors uy:w-full uy:flex uy:gap-100 uy:py-125 uy:sm:py-100 uy:px-150 uy:rounded-100 uy:sm:rounded-75",
"uy:enabled:bg-surface-form-high-enabled",
"uy:has-data-[focus-visible=\"true\"]:outline-none uy:has-data-[focus-visible=\"true\"]:ring-2 uy:has-data-[focus-visible=\"true\"]:ring-utility-focus-ring uy:has-data-[focus-visible=\"true\"]:ring-offset-2 uy:has-data-[focus-visible=\"true\"]:bg-surface-form-high-focus",
"uy:active:border uy:active:border-solid uy:active:border-border-form-active",
"uy:data-[disabled=true]:border uy:data-[disabled=true]:border-solid uy:data-[disabled=true]:border-border-form-disabled uy:data-[disabled=true]:bg-surface-form-high-disabled",
"uy:data-[invalid=true]:border uy:data-[invalid=true]:border-solid uy:data-[invalid=true]:border-border-form-error uy:data-[invalid=true]:bg-surface-form-high-error"
],
input: [
"uy:h-300 uy:typography-body uy:flex-grow uy:outline-none uy:[&::-webkit-search-cancel-button]:hidden",
"uy:enabled:text-content-form-enabled",
"uy:data-[hovered]:text-content-form-hover",
"uy:data-[focus-visible]:text-content-form-focus",
"uy:active:text-content-form-active",
"uy:group-data-[invalid=true]:bg-surface-form-high-error"
],
icon: ["uy:self-center", "uy:group-data-[disabled=true]:text-content-neutral-lowest-disabled"],
invalidIcon: ["uy:self-center uy:hidden", "uy:group-data-[invalid=true]:block"],
wrapperState: ["uy:flex uy:items-center uy:gap-50"]
},
variants: { isReadOnly: {
true: {
base: "uy:border uy:border-solid uy:border-border-form-disabled uy:bg-surface-form-high-disabled",
icon: "uy:text-content-neutral-lowest-disabled",
input: "uy:bg-surface-form-high-disabled uy:text-content-form-read-only uy:placeholder-content-form-read-only uy:cursor-text"
},
false: {
base: ["uy:bg-surface-form-high-enabled", "uy:has-data-[hovered]:bg-surface-form-high-hover uy:data-[disabled=true]:cursor-not-allowed"],
icon: "uy:text-content-neutral-lowest",
input: "uy:bg-text-content-form-active uy:placeholder-content-neutral-lowest uy:group-data-[disabled=true]:cursor-not-allowed"
}
} }
}), v = ({ t: e, placeholder: t, isDisabled: n }) => n ? "" : t || e((e) => e.unity.component.autocomplete.placeholder), y = (i, c) => {
let { placeholder: y, isDisabled: b, isInvalid: x, isReadOnly: S, items: C, children: w, loadingState: T, name: E, feedbackText: D, ...O } = i, { t: k } = d("unity"), A = o(null), [j, M] = s(null), N = g();
p({
ref: A,
onResize: a(() => {
A.current && M(`${A.current.offsetWidth}px`);
}, [A])
});
let { base: P, input: F, icon: I, invalidIcon: L, wrapperState: R } = _({ isReadOnly: S });
return /* @__PURE__ */ u("search", {
ref: c,
children: [/* @__PURE__ */ u(m, {
className: P(),
name: E,
isInvalid: x,
isDisabled: b || S,
isReadOnly: S,
ref: A,
"aria-label": k((e) => e.unity.component.autocomplete.placeholder),
items: C,
...O,
children: [
/* @__PURE__ */ l(f, {
src: "MagnifyingGlassOutlined",
className: I(),
"aria-hidden": "true"
}),
/* @__PURE__ */ l(h, {
className: F(),
"aria-describedby": N,
placeholder: v({
t: k,
placeholder: y,
isDisabled: b
})
}),
/* @__PURE__ */ u("div", {
className: R(),
children: [
/* @__PURE__ */ l(f, {
className: L(),
src: "WarningCircleOutlined",
color: "content.form.invalid",
alt: k((e) => e.unity.component.formField.formInput.error.alt)
}),
T && T !== "error" && T !== "idle" && /* @__PURE__ */ l("span", {
className: "uy:px-50",
children: /* @__PURE__ */ l(e, {
label: k((e) => e.unity.component.common.loading.label),
color: "primary",
size: "small",
"aria-hidden": "true"
})
}),
/* @__PURE__ */ l(n, {
isReadOnly: S,
isDisabled: b
})
]
}),
/* @__PURE__ */ l(r, {
panelWidth: j,
searchRef: A,
children: w
})
]
}), x && /* @__PURE__ */ l(t, {
id: N,
className: "uy:typography-body-small-strong uy:text-content-form-invalid",
children: D
})]
});
}, b = i(function(e, t) {
return y(e, t);
});
b.displayName = "Autocomplete";
//#endregion
export { b as Autocomplete };