@frontify/fondue
Version:
Design system of Frontify
54 lines (53 loc) • 1.41 kB
JavaScript
import { KeyboardCode as o } from "@dnd-kit/core";
import { ROOT_ID as L } from "./constants.es.js";
const y = (r, h, d, I, x, b) => {
const e = document.activeElement;
if (!(e != null && e.parentElement) || e.getAttribute("role") !== "treeitem" || !(e instanceof HTMLLIElement))
return;
const t = Array.from(e.parentElement.children).filter(
(n) => n.nodeName === "LI"
), c = t.indexOf(e), p = d[c], s = p.props.id, f = h.has(s), a = p.props.parentId, i = e.getAttribute("data-has-children") === "true", { code: g } = r, l = () => {
r.preventDefault(), I(s);
}, u = () => {
r.preventDefault(), b(s);
}, k = () => {
r.preventDefault(), x(s);
}, D = () => {
const n = (c + t.length - 1) % t.length;
t[n].focus();
}, m = () => {
const n = (c + 1) % t.length;
t[n].focus();
};
switch (g) {
case o.Enter:
l();
break;
case o.Space:
i ? u() : l();
break;
case o.Right:
if (!i)
break;
f ? m() : u();
break;
case o.Left:
if (i && f)
k();
else if (a && a !== L) {
const n = d.findIndex((E) => E.props.id === a);
t[n].focus();
}
break;
case o.Up:
r.preventDefault(), D();
break;
case o.Down:
r.preventDefault(), m();
break;
}
};
export {
y as handleKeyDownEvent
};
//# sourceMappingURL=treeHandleKeyDown.es.js.map