UNPKG

@1771technologies/lytenyte-pro

Version:

Blazingly fast headless React data grid with 100s of features.

37 lines (36 loc) 1.22 kB
import { useEvent } from "@1771technologies/lytenyte-core/internal"; import { ROOT_LEAF_PREFIX } from "../async-tree/+constants.async-tree.js"; export function useRowAdd(source, onRowAdd, optimistic = false) { const rowAdd = useEvent((rows, placement = "start") => { if (!onRowAdd || !rows.length) return; if (!optimistic) { onRowAdd({ rows, placement }); return; } const toNodes = rows.map((x) => { return { asOf: Date.now(), kind: "leaf", parent: source.tree, path: ROOT_LEAF_PREFIX, relIndex: -1, row: x, optimistic: placement, }; }); if (placement === "start") source.addBefore(toNodes); else source.addAfter(toNodes); source.flatten(); onRowAdd({ rows, placement }).catch(() => { if (placement === "start") source.deleteBefore(toNodes.map((x) => x.row.id)); else source.deleteAfter(toNodes.map((x) => x.row.id)); source.flatten(); }); }); return rowAdd; }