Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add GridTableApi.setSort. #672

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/components/Table/GridTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ export function GridTable<R extends Kinded, S = {}, X extends Only<GridTableXss,
// Use this ref to watch for changes in the GridTable's container and resize columns accordingly.
const resizeRef = useRef<HTMLDivElement>(null);

const [sortState, setSortKey, sortOn, caseSensitive] = useSortState<R, S>(columns, props.sorting);
const maybeSorted = useMemo(() => {
if (sortOn === "client" && sortState) {
// If using client-side sort, the sortState use S = number
return sortRows(columns, rows, sortState as any as SortState<number>, caseSensitive);
}
return rows;
}, [columns, rows, sortOn, sortState, caseSensitive]);

const api = useMemo<GridTableApiImpl<R>>(() => {
const api = (props.api as GridTableApiImpl<R>) ?? new GridTableApiImpl();
api.init(persistCollapse, virtuosoRef, rows);
Expand Down Expand Up @@ -283,15 +292,6 @@ export function GridTable<R extends Kinded, S = {}, X extends Only<GridTableXss,
// Make a single copy of our current collapsed state, so we'll have a single observer.
const collapsedIds = useComputed(() => rowState.collapsedIds, [rowState]);

const [sortState, setSortKey, sortOn, caseSensitive] = useSortState<R, S>(columns, props.sorting);
const maybeSorted = useMemo(() => {
if (sortOn === "client" && sortState) {
// If using client-side sort, the sortState use S = number
return sortRows(columns, rows, sortState as any as SortState<number>, caseSensitive);
}
return rows;
}, [columns, rows, sortOn, sortState, caseSensitive]);

const hasTotalsRow = rows.some((row) => row.id === "totals");

// Flatten + component-ize the sorted rows.
Expand Down