@rtdui/datatable
Version:
React DataTable component based on Rtdui components
151 lines (148 loc) • 4.47 kB
JavaScript
'use client';
import { makeStateUpdater, memo, getMemoOptions } from '@tanstack/react-table';
const RowActive = {
getInitialState: (state) => {
return {
rowActive: null,
...state
};
},
getDefaultOptions: (table) => {
return {
onRowActiveChange: makeStateUpdater("rowActive", table),
enableRowActive: true
};
},
createTable: (table) => {
table.setRowActive = (updater) => table.options.onRowActiveChange?.(updater);
table.resetRowActive = (defaultState) => table.setRowActive(
defaultState ? null : table.initialState.rowActive ?? null
);
table.getPreActivedRowModel = () => table.getCoreRowModel();
table.getActivedRowModel = memo(
() => [table.getState().rowActive, table.getCoreRowModel()],
(rowActive, rowModel) => {
if (rowActive === null) {
return {
rows: [],
flatRows: [],
rowsById: {}
};
}
return activeRowsFn(table, rowModel);
},
getMemoOptions(table.options, "debugTable", "getActivedRowModel")
);
table.getFilteredActivedRowModel = memo(
() => [table.getState().rowActive, table.getFilteredRowModel()],
(rowActive, rowModel) => {
if (rowActive === null) {
return {
rows: [],
flatRows: [],
rowsById: {}
};
}
return activeRowsFn(table, rowModel);
},
getMemoOptions(table.options, "debugTable", "getFilteredActivedRowModel")
);
table.getGroupedActivedRowModel = memo(
() => [table.getState().rowActive, table.getSortedRowModel()],
(rowActive, rowModel) => {
if (rowActive === null) {
return {
rows: [],
flatRows: [],
rowsById: {}
};
}
return activeRowsFn(table, rowModel);
},
getMemoOptions(table.options, "debugTable", "getGroupedActivedRowModel")
);
table.getIsSomeRowsActived = () => {
const totalActived = table.getState().rowActive !== null ? 1 : 0;
return totalActived > 0 && totalActived < table.getFilteredRowModel().flatRows.length;
};
table.getIsSomePageRowsActived = () => {
const paginationFlatRows = table.getPaginationRowModel().flatRows;
return paginationFlatRows.filter((row) => row.getCanActive()).some((d) => d.getIsActived());
};
},
createRow: (row, table) => {
row.toggleActived = (value) => {
const isActived = row.getIsActived();
if (isActived)
return;
table.setRowActive((old) => {
value = typeof value !== "undefined" ? value : !isActived;
if (row.getCanActive() && isActived === value) {
return old;
}
let activedRowId = old;
if (value) {
if (row.getCanActive()) {
activedRowId = row.id;
}
} else {
activedRowId = null;
}
return activedRowId;
});
};
row.getIsActived = () => {
const { rowActive } = table.getState();
return isRowActived(row, rowActive);
};
row.getCanActive = () => {
if (typeof table.options.enableRowActive === "function") {
return table.options.enableRowActive(row);
}
return table.options.enableRowActive ?? true;
};
row.getToggleActivedHandler = () => {
const canActive = row.getCanActive();
return (e) => {
if (!canActive)
return;
row.toggleActived(
e.target?.checked
);
};
};
}
};
function activeRowsFn(table, rowModel) {
const rowActive = table.getState().rowActive;
const newActivedFlatRows = [];
const newActivedRowsById = {};
const recurseRows = (rows, depth = 0) => {
return rows.map((row) => {
const isActived = isRowActived(row, rowActive);
if (isActived) {
newActivedFlatRows.push(row);
newActivedRowsById[row.id] = row;
}
if (row.subRows?.length) {
row = {
...row,
subRows: recurseRows(row.subRows, depth + 1)
};
}
if (isActived) {
return row;
}
}).filter(Boolean);
};
return {
rows: recurseRows(rowModel.rows),
flatRows: newActivedFlatRows,
rowsById: newActivedRowsById
};
}
function isRowActived(row, active) {
return active === row.id;
}
export { RowActive, activeRowsFn, isRowActived };
//# sourceMappingURL=RowActive.mjs.map