@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
100 lines (99 loc) • 2.4 kB
JavaScript
import { useCallback as e, useId as t, useMemo as n } from "react";
import { Fragment as r, jsx as i } from "react/jsx-runtime";
import { Dnd as a, DragHandle as o, SortableItem as s, useDroppable as c } from "@mitcsutt/kiln-dnd";
//#region src/dnd/dragAndDrop.tsx
var l = "repeater", u = {
instructions: "To pick up, press Space or Enter. Then use the arrow keys to move, Space or Enter to drop, and Escape to cancel.",
handle: (e) => `Drag ${e}`,
lifted: (e, t, n) => `Picked up ${e}, position ${String(t)} of ${String(n)}.`,
moved: (e, t, n) => `${e} moved to position ${String(t)} of ${String(n)}.`,
dropped: (e, t, n) => `Dropped ${e} at position ${String(t)} of ${String(n)}.`,
canceled: (e, t, n) => `Move cancelled. ${e} is back at position ${String(t)} of ${String(n)}.`
};
function d(e) {
let { instructions: t, handle: r, lifted: i, moved: a, dropped: o, canceled: s } = {
...u,
...e
};
return n(() => {
let e = (e) => ({ item: t, index: n, count: r }) => e(t, n + 1, r);
return {
instructions: t,
handle: r,
listLifted: e(i),
listMoved: e(a),
listDropped: e(o),
listCanceled: e(s)
};
}, [
t,
r,
i,
a,
o,
s
]);
}
function f({ label: t, messages: n, onMove: r, children: o }) {
let s = d(n), c = e(({ item: e, target: t, input: n }) => {
t?.kind === "list" && e.index !== void 0 && r(e.index, t.index, n);
}, [r]);
return /* @__PURE__ */ i(a, {
announcements: s,
onDragEnd: c,
children: /* @__PURE__ */ i(p, {
label: t,
children: o
})
});
}
function p({ label: e, children: n }) {
let { ref: r } = c({
id: t(),
label: e,
list: l
});
return /* @__PURE__ */ i("div", {
ref: r,
children: n
});
}
function m({ id: e, index: t, label: n, setItemElement: r, children: a }) {
return /* @__PURE__ */ i(s, {
id: e,
index: t,
label: n,
list: l,
handle: !0,
children: ({ ref: e, props: n }) => /* @__PURE__ */ i(h, {
source: e,
index: t,
setItemElement: r,
children: (e) => a({
ref: e,
props: n
})
})
});
}
function h({ source: t, index: n, setItemElement: a, children: o }) {
let s = e((e) => {
t(e), a(n)(e);
}, [
t,
n,
a
]);
return /* @__PURE__ */ i(r, { children: o(s) });
}
function g(e) {
return /* @__PURE__ */ i(o, { ...e });
}
var _ = {
List: f,
Item: m,
Handle: g
};
//#endregion
export { _ as dragAndDrop };
//# sourceMappingURL=dragAndDrop.js.map