Skip to content

Commit

Permalink
fix: updating selection state from outside #144
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuechlin committed Jan 13, 2023
1 parent eaf5b45 commit 170f353
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 49 deletions.
108 changes: 60 additions & 48 deletions docs/pages/examples/RowSelectionExample.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Button } from '@mantine/core';
import { RowSelectionState } from '@tanstack/react-table';
import { useState } from 'react';
import {
Expand All @@ -11,55 +12,66 @@ import {
import { demoData } from '../../demoData';

export default function RowSelectionExample() {
const [data, setData] = useState(demoData);
const [selected, setSelected] = useState<RowSelectionState>({});

const removeSelected = () => {
const next = data.filter((_, i) => !selected[i]);
setSelected({});
setData(next);
};

return (
<DataGrid
data={demoData}
striped
highlightOnHover
withGlobalFilter
withPagination
withColumnFilters
withSorting
withColumnResizing
withRowSelection
state={{
rowSelection: selected,
}}
onRowSelectionChange={setSelected}
columns={[
{
accessorKey: 'text',
header: 'Text that is too long for a Header',
filterFn: stringFilterFn,
size: 300,
cell: highlightFilterValue,
},
{
header: 'Animal',
columns: [
{ accessorKey: 'cat', filterFn: stringFilterFn },
{
accessorKey: 'fish',
filterFn: stringFilterFn,
},
],
},
{
accessorKey: 'city',
filterFn: stringFilterFn,
},
{ accessorKey: 'value', filterFn: numberFilterFn },
{
accessorKey: 'date',
cell: (cell) => cell.getValue<Date>()?.toLocaleDateString(),
filterFn: dateFilterFn,
},
{
accessorKey: 'bool',
filterFn: booleanFilterFn,
},
]}
/>
<>
<Button onClick={removeSelected}>Remove Selected</Button>
<DataGrid
data={data}
striped
highlightOnHover
withGlobalFilter
withPagination
withColumnFilters
withSorting
withColumnResizing
withRowSelection
state={{
rowSelection: selected,
}}
onRowSelectionChange={setSelected}
columns={[
{
accessorKey: 'text',
header: 'Text that is too long for a Header',
filterFn: stringFilterFn,
size: 300,
cell: highlightFilterValue,
},
{
header: 'Animal',
columns: [
{ accessorKey: 'cat', filterFn: stringFilterFn },
{
accessorKey: 'fish',
filterFn: stringFilterFn,
},
],
},
{
accessorKey: 'city',
filterFn: stringFilterFn,
},
{ accessorKey: 'value', filterFn: numberFilterFn },
{
accessorKey: 'date',
cell: (cell) => cell.getValue<Date>()?.toLocaleDateString(),
filterFn: dateFilterFn,
},
{
accessorKey: 'bool',
filterFn: booleanFilterFn,
},
]}
/>
</>
);
}
3 changes: 2 additions & 1 deletion src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ export function DataGrid<TData extends RowData>({

const handleRowSelectionChange: OnChangeFn<RowSelectionState> = useCallback(
(arg0) => {
table.setState((state) => {
table.setState(() => {
const state = table.getState();
const next = functionalUpdate(arg0, state.rowSelection);
onRowSelectionChange && onRowSelectionChange(next);
return {
Expand Down

0 comments on commit 170f353

Please sign in to comment.