@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
62 lines (61 loc) • 2.13 kB
JavaScript
import { useMemo } from "react";
import { useEvent } from "@1771technologies/lytenyte-core/yinternal";
export function useAggregationBoxItems({ grid, orientation }) {
const model = grid.state.aggModel.useValue();
const gridId = grid.state.gridId.useValue();
const items = useMemo(() => {
const items = Object.entries(model).map(([id, agg], i) => {
const column = grid.api.columnById(id);
const name = column?.name ?? column?.id ?? id;
return {
draggable: false,
id,
index: i,
source: "aggregation",
data: { id, agg },
dragData: {},
label: name,
onAction: () => { },
onDelete: () => {
grid.state.aggModel.set((prev) => {
const next = { ...prev };
delete next[id];
return next;
});
},
onDrop: () => { },
dragPlaceholder: undefined,
};
});
return items;
}, [model, grid.api, grid.state.aggModel]);
const onRootDrop = useEvent((p) => {
const aggId = `${gridId}-agg`;
const c = p.state.siteLocalData?.[aggId];
if (!c)
return;
const current = model[c.id];
if (!current) {
const base = grid.state.columnBase.get();
const agg = c.uiHints?.aggDefault ??
c.uiHints?.aggsAllowed?.[0] ??
base.uiHints?.aggDefault ??
base.uiHints?.aggsAllowed?.[0];
if (!agg)
return;
const next = { fn: agg };
grid.state.aggModel.set((prev) => ({ ...prev, [c.id]: next }));
}
});
return useMemo(() => {
return {
rootProps: {
accepted: [`${gridId}-agg`],
onRootDrop,
orientation,
grid,
},
items,
};
}, [grid, gridId, items, onRootDrop, orientation]);
}