UNPKG

@1771technologies/lytenyte-pro

Version:

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

25 lines (24 loc) 1.12 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { forwardRef } from "react"; import { useSortManagerCtx } from "./context.js"; import { itemsWithIdToMap } from "@1771technologies/lytenyte-shared"; import { sortItemsToSortModel } from "./utils/sort-item-to-sort-model.js"; import { useEvent, useSlot } from "@1771technologies/lytenyte-core/yinternal"; export const SortApply = forwardRef(function SortApply({ as, ...props }, forwarded) { const ctx = useSortManagerCtx(); const onApply = useEvent(() => { const lookup = itemsWithIdToMap(ctx.mode ? ctx.grid.state.columnPivotColumns.get() : ctx.grid.state.columns.get()); const model = sortItemsToSortModel(ctx.sortItems, lookup); if (ctx.mode) ctx.grid.state.columnPivotModel.set((prev) => ({ ...prev, sort: model })); else ctx.grid.state.sortModel.set(model); }); const renderer = useSlot({ props: [typeof as !== "function" ? { onClick: onApply } : {}, props], ref: forwarded, slot: as ?? _jsx("button", {}), state: { onApply }, }); return renderer; });