@adyen/adyen-platform-experience-web
Version:

95 lines (94 loc) • 3.57 kB
JavaScript
import { jsx as b } from "../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useRef as x, useState as L, useCallback as U, useEffect as Y, useMemo as E } from "../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import z from "../InputBase.js";
import { CURRENCY_INPUT_BASE_CLASS as V } from "./constants.js";
import { useFetch as j } from "../../../../hooks/useFetch.js";
import { formatAmount as D, getCurrencyExponent as F } from "../../../../utils/currency/main.js";
import k from "../../../../core/Context/preact/useCoreContext.js";
import { useConfigContext as G } from "../../../../core/ConfigContext/preact/context.js";
import { EMPTY_OBJECT as J } from "../../../../utils/value/constants.js";
import w from "../../../../utils/random/uniqueId.js";
const Q = ["EUR", "GBP", "USD"], W = (s) => {
const t = new Map(Q.map((o, a) => [o, a]));
return [...s].sort((o, a) => {
const d = o.id.toUpperCase(), m = a.id.toUpperCase(), i = t.get(d), l = t.get(m);
return i !== void 0 || l !== void 0 ? i === void 0 ? 1 : l === void 0 ? -1 : i - l : d.localeCompare(m);
});
}, ie = ({
amount: s,
currency: t,
currencyItems: o,
hideCurrencySelector: a,
interactionsDisabled: d,
isInvalid: m = !1,
maxValue: i,
name: l,
onAmountChange: A,
onCurrencyChange: v,
readonly: h,
selectedCurrencyCode: P
}) => {
const R = x(t), { i18n: f } = k(), { currencies: I } = G().endpoints, [B, S] = L(s ? `${D(s, t)}` : ""), q = U(
(r) => {
const e = 1.1.toLocaleString(f.locale).match(/\d(.*?)\d/)?.[1] || ".", n = e === "." ? r : r.replace(e, "."), p = F(t);
return Math.trunc(+`${parseFloat(n)}e${p}`) || 0;
},
[t, f.locale]
);
Y(() => {
t !== R.current && (S(s ? `${D(s, t)}` : ""), R.current = t);
}, [s, t]);
const M = (r) => {
let e = r.currentTarget.value.trim();
const n = 1.1.toLocaleString(f.locale).match(/\d(.*?)\d/)?.[1] || ".", p = e.split(n);
if (p.length === 2) {
const g = F(t), C = p[0];
let c = p[1];
c.length >= g && (c = c.substring(0, g), e = C + n + c, r.currentTarget.value = e);
}
if (e.endsWith(n) && (e = e.slice(0, -n.length), r.currentTarget.value = e), typeof i == "number") {
const g = n === "." ? e : e.replace(n, "."), C = parseFloat(g);
if (Number.isFinite(C) && C > i) {
const c = F(t), N = i.toFixed(c);
e = n === "." ? N : N.replace(".", n), r.currentTarget.value = e;
}
}
S(e), A(q(e));
}, u = j({
fetchOptions: { enabled: !!I && !o?.length },
queryFn: U(async () => I?.(J), [I])
}), T = E(() => (u.data?.data ?? []).map((e) => ({
id: e,
name: e
})), [u.data]), _ = E(() => {
const r = o?.length ? o : T;
return W(r);
}, [T, o]), y = x(w()), O = x(w()), $ = E(() => a ? {} : {
onDropdownInput: v,
dropdown: {
items: _,
value: P,
readonly: u.isFetching || h?.currency
}
}, [a, _, P, u.isFetching, v, h?.currency]);
return /* @__PURE__ */ b("div", { className: "adyen-pe-currency-input__container", children: /* @__PURE__ */ b("label", { htmlFor: y.current, "aria-labelledby": O.current, children: /* @__PURE__ */ b(
z,
{
...$,
type: "number",
className: V,
disabled: d || u.isFetching,
isInvalid: m,
lang: f.locale,
min: 0,
name: l,
onInput: M,
uniqueId: y.current,
value: B,
readonly: h?.amount
}
) }) });
};
export {
ie as CurrencyInput
};