2023-12-05 12:16:58 +01:00
|
|
|
import {
|
|
|
|
type OnChangeFn,
|
|
|
|
type SortingState,
|
|
|
|
type PaginationState,
|
|
|
|
type TableOptions,
|
2023-12-12 14:01:04 +01:00
|
|
|
type VisibilityState,
|
2023-12-05 12:16:58 +01:00
|
|
|
getCoreRowModel,
|
|
|
|
} from '@tanstack/react-table';
|
|
|
|
|
2023-12-13 10:08:16 +01:00
|
|
|
type TableStateColumns = (string | null)[] | null | undefined;
|
2023-12-12 14:01:04 +01:00
|
|
|
|
2023-12-05 12:16:58 +01:00
|
|
|
const createOnSortingChange =
|
|
|
|
(
|
|
|
|
tableState: {
|
|
|
|
sortBy: string;
|
|
|
|
sortOrder: string;
|
|
|
|
},
|
|
|
|
setTableState: (newState: {
|
|
|
|
sortBy?: string;
|
|
|
|
sortOrder?: string;
|
|
|
|
}) => void,
|
|
|
|
): OnChangeFn<SortingState> =>
|
|
|
|
(newSortBy) => {
|
|
|
|
if (typeof newSortBy === 'function') {
|
|
|
|
const computedSortBy = newSortBy([
|
|
|
|
{
|
|
|
|
id: tableState.sortBy,
|
|
|
|
desc: tableState.sortOrder === 'desc',
|
|
|
|
},
|
|
|
|
])[0];
|
|
|
|
setTableState({
|
|
|
|
sortBy: computedSortBy?.id,
|
|
|
|
sortOrder: computedSortBy?.desc ? 'desc' : 'asc',
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const sortBy = newSortBy[0];
|
|
|
|
setTableState({
|
|
|
|
sortBy: sortBy?.id,
|
|
|
|
sortOrder: sortBy?.desc ? 'desc' : 'asc',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createOnPaginationChange =
|
|
|
|
(
|
|
|
|
tableState: {
|
|
|
|
limit: number;
|
|
|
|
offset: number;
|
|
|
|
},
|
|
|
|
setTableState: (newState: {
|
|
|
|
limit?: number;
|
|
|
|
offset?: number;
|
|
|
|
}) => void,
|
|
|
|
): OnChangeFn<PaginationState> =>
|
|
|
|
(newPagination) => {
|
|
|
|
if (typeof newPagination === 'function') {
|
|
|
|
const computedPagination = newPagination({
|
|
|
|
pageSize: tableState.limit,
|
|
|
|
pageIndex: tableState.offset
|
|
|
|
? Math.floor(tableState.offset / tableState.limit)
|
|
|
|
: 0,
|
|
|
|
});
|
|
|
|
setTableState({
|
|
|
|
limit: computedPagination?.pageSize,
|
|
|
|
offset: computedPagination?.pageIndex
|
|
|
|
? computedPagination?.pageIndex *
|
|
|
|
computedPagination?.pageSize
|
|
|
|
: 0,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const { pageSize, pageIndex } = newPagination;
|
|
|
|
setTableState({
|
|
|
|
limit: pageSize,
|
|
|
|
offset: pageIndex ? pageIndex * pageSize : 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-12 14:01:04 +01:00
|
|
|
const createOnColumnVisibilityChange =
|
|
|
|
(
|
|
|
|
tableState: {
|
2023-12-13 10:08:16 +01:00
|
|
|
columns?: TableStateColumns;
|
2023-12-12 14:01:04 +01:00
|
|
|
},
|
|
|
|
setTableState: (newState: {
|
2023-12-13 10:08:16 +01:00
|
|
|
columns?: TableStateColumns;
|
2023-12-12 14:01:04 +01:00
|
|
|
}) => void,
|
|
|
|
): OnChangeFn<VisibilityState> =>
|
|
|
|
(newVisibility) => {
|
|
|
|
const columnsObject = tableState.columns?.reduce(
|
|
|
|
(acc, column) => ({
|
|
|
|
...acc,
|
|
|
|
...(column && { [column]: true }),
|
|
|
|
}),
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (typeof newVisibility === 'function') {
|
|
|
|
const computedVisibility = newVisibility(columnsObject || {});
|
|
|
|
const columns = Object.keys(computedVisibility).filter(
|
|
|
|
(column) => computedVisibility[column],
|
|
|
|
);
|
|
|
|
|
|
|
|
setTableState({ columns });
|
|
|
|
} else {
|
|
|
|
const columns = Object.keys(newVisibility).filter(
|
|
|
|
(column) => newVisibility[column],
|
|
|
|
);
|
|
|
|
setTableState({ columns });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-05 12:16:58 +01:00
|
|
|
const createSortingState = (tableState: {
|
|
|
|
sortBy: string;
|
|
|
|
sortOrder: string;
|
|
|
|
}) => ({
|
|
|
|
sorting: [
|
|
|
|
{
|
|
|
|
id: tableState.sortBy,
|
|
|
|
desc: tableState.sortOrder === 'desc',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const createPaginationState = (tableState: {
|
|
|
|
limit: number;
|
|
|
|
offset: number;
|
|
|
|
}) => ({
|
|
|
|
pagination: {
|
|
|
|
pageIndex: tableState.offset ? tableState.offset / tableState.limit : 0,
|
|
|
|
pageSize: tableState.limit,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-12-12 14:01:04 +01:00
|
|
|
const createColumnVisibilityState = (tableState: {
|
2023-12-13 10:08:16 +01:00
|
|
|
columns?: TableStateColumns;
|
2023-12-12 14:01:04 +01:00
|
|
|
}) =>
|
|
|
|
tableState.columns
|
|
|
|
? {
|
|
|
|
columnVisibility: tableState.columns?.reduce(
|
|
|
|
(acc, column) => ({
|
|
|
|
...acc,
|
|
|
|
...(column && { [column]: true }),
|
|
|
|
}),
|
|
|
|
{},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: {};
|
|
|
|
|
2023-12-05 12:16:58 +01:00
|
|
|
export const withTableState = <T extends Object>(
|
|
|
|
tableState: {
|
|
|
|
sortBy: string;
|
|
|
|
sortOrder: string;
|
|
|
|
limit: number;
|
|
|
|
offset: number;
|
2023-12-13 10:08:16 +01:00
|
|
|
columns?: TableStateColumns;
|
2023-12-05 12:16:58 +01:00
|
|
|
},
|
|
|
|
setTableState: (newState: {
|
|
|
|
sortBy?: string;
|
|
|
|
sortOrder?: string;
|
|
|
|
limit?: number;
|
|
|
|
offset?: number;
|
2023-12-13 10:08:16 +01:00
|
|
|
columns?: TableStateColumns;
|
2023-12-05 12:16:58 +01:00
|
|
|
}) => void,
|
|
|
|
options: Omit<TableOptions<T>, 'getCoreRowModel'>,
|
2023-12-13 10:08:16 +01:00
|
|
|
) => {
|
|
|
|
const hideAllColumns = Object.fromEntries(
|
|
|
|
Object.keys(options.state?.columnVisibility || {}).map((column) => [
|
|
|
|
column,
|
|
|
|
false,
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
const columnVisibility = tableState.columns
|
|
|
|
? {
|
|
|
|
...hideAllColumns,
|
|
|
|
...createColumnVisibilityState(tableState).columnVisibility,
|
|
|
|
}
|
|
|
|
: options.state?.columnVisibility;
|
|
|
|
|
|
|
|
return {
|
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
|
enableSorting: true,
|
|
|
|
enableMultiSort: false,
|
|
|
|
manualPagination: true,
|
|
|
|
manualSorting: true,
|
|
|
|
enableSortingRemoval: false,
|
|
|
|
enableHiding: true,
|
|
|
|
onPaginationChange: createOnPaginationChange(tableState, setTableState),
|
|
|
|
onSortingChange: createOnSortingChange(tableState, setTableState),
|
|
|
|
onColumnVisibilityChange: createOnColumnVisibilityChange(
|
|
|
|
tableState,
|
|
|
|
setTableState,
|
|
|
|
),
|
|
|
|
...options,
|
|
|
|
state: {
|
|
|
|
...createSortingState(tableState),
|
|
|
|
...createPaginationState(tableState),
|
|
|
|
...(options.state || {}),
|
|
|
|
columnVisibility,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|