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

67 lines (66 loc) • 1.9 kB
JavaScript
import { useState as x, useRef as b, useMemo as O, useCallback as R, useEffect as A } from "../external/preact/hooks/dist/hooks.module.js";
import { InteractionKeyCode as l } from "../components/types.js";
import { uniqueId as D } from "../utils/random/uniqueId.js";
const T = (f, o) => {
if (!o) return 0;
const s = f.findIndex((i) => i.id === o);
return s === -1 ? 0 : s;
}, h = (f, o) => {
const [s, i] = x(!1), [I, c] = x(T(f, o)), u = b([]), m = b(D().replace(/.*?(?=\d+$)/, "")).current, r = f.length, p = O(() => {
const e = [];
for (let n = 0; n < r; n++)
e[n] = (t) => u.current[n] = t;
return e;
}, [r]), d = R(
(e, n) => {
var a;
let t = e;
do
if (t < 0 && (t += r), t >= r && (t %= r), ((a = u.current[t]) == null ? void 0 : a.disabled) === !1) break;
while ((t += n) !== e);
return t;
},
[r]
), g = R((e) => {
var t;
const n = u.current.findIndex((a) => a === e.currentTarget);
((t = u.current[n]) == null ? void 0 : t.disabled) === !1 && (e.preventDefault(), c(n));
}, []), k = O(() => {
const e = {
[l.ARROW_LEFT]: () => c((n) => d(
n - 1,
-1
/* BACKWARD */
)),
[l.ARROW_RIGHT]: () => c((n) => d(
n + 1,
1
/* FORWARD */
)),
[l.HOME]: () => c(d(
0,
1
/* FORWARD */
)),
[l.END]: () => c(d(
r - 1,
-1
/* BACKWARD */
))
};
return (n) => {
var t;
e[n.key] && (n.preventDefault(), (t = e[n.key]) == null || t.call(e), i(!0));
};
}, [r]);
return A(() => {
if (s) {
const e = u.current[I];
(e == null ? void 0 : e.disabled) === !1 && (e == null || e.focus()), i(!1);
}
}, [I, s]), { activeIndex: I, onClick: g, onKeyDown: k, refs: p, uniqueId: m };
};
export {
h as default,
h as useTabbedControl
};