@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
75 lines (74 loc) • 2.35 kB
JavaScript
import { getFormRuntime as e, isInactive as t, toFormApi as n } from "./formRuntime.js";
import { pickErrors as r } from "./errors.js";
//#region src/runtime/focus.ts
var i = "[tabindex]:not([tabindex=\"-1\"]), input:not([type=\"hidden\"]), button, select, textarea, [role=combobox], a[href]";
function a() {
return new Promise((e) => {
typeof requestAnimationFrame == "function" ? requestAnimationFrame(() => {
e();
}) : setTimeout(e, 16);
});
}
function o(e) {
return e.disabled || e.getAttribute("aria-hidden") === "true";
}
function s(e) {
if (e.matches(i) && !o(e)) return e;
for (let t of e.querySelectorAll(i)) if (!o(t)) return t;
return null;
}
function c() {
return typeof window < "u" && typeof window.matchMedia == "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
}
async function l(e) {
for (let t of e.scopes) t.reveal?.();
e.scopes.some((e) => e.reveal) && await a();
let t = e.element();
if (!t) return !1;
let n = s(t);
if (!n) return !1;
if (n.focus({ preventScroll: !0 }), typeof n.scrollIntoView == "function") {
let e = c();
n.scrollIntoView({
block: e ? "nearest" : "center",
behavior: "auto"
});
}
return n.ownerDocument.activeElement === n;
}
async function u(t, n) {
let r = e(t).fields.get(n);
return r ? l(r) : !1;
}
function d(e) {
return [...e].sort((e, t) => {
let n = e.element(), r = t.element();
return !n || !r ? n ? -1 : +!!r : n === r ? 0 : n.compareDocumentPosition(r) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1;
});
}
function f(i, a) {
let o = n(i), s = e(o), c = a ? new Set(a.names()) : null, l = [];
for (let [e, n] of s.fields) {
if (c && !c.has(e) || t(s, e)) continue;
let i = o.getFieldMeta(e);
i && r(i.errorMap).length !== 0 && l.push({
...n,
name: e
});
}
return d(l).map((e) => [e.name, e]);
}
async function p(e, t) {
await a();
let n = f(e, t)[0];
return n ? l(n[1]) : !1;
}
async function m(t) {
let n = e(t), r = n.options.focusOnInvalid;
if (r === !1) return !1;
let i = n.summary;
return (r === "auto" || r === "summary") && i?.mounted ? (await a(), i.focus(), !0) : r !== "summary" && p(t);
}
//#endregion
export { u as focusField, p as focusFirstInvalid, m as focusOnInvalidSubmit, s as focusTarget, f as invalidFields, a as nextFrame, d as sortByDomOrder };
//# sourceMappingURL=focus.js.map