Skip to content

Commit b5cb29a

Browse files
authored
Merge pull request #532 from taro-28/support-row-selection
Support `rowSelection`
2 parents 71f27a3 + 6264e28 commit b5cb29a

File tree

26 files changed

+895
-11
lines changed

26 files changed

+895
-11
lines changed

examples/lib/src/components/UserTable.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { type Table, flexRender } from "@tanstack/react-table";
2-
import { useState } from "react";
2+
import { type JSX, useState } from "react";
33
import { SearchInput } from "./SearchInput";
44
import type { User } from "./userData";
55

6+
const Cell = ({ className, ...props }: JSX.IntrinsicElements["div"]) => (
7+
<div {...props} className={`table-cell py-1 px-2 text-center ${className}`} />
8+
);
9+
610
type Props = {
711
table: Table<User>;
812
};
@@ -39,11 +43,15 @@ export const UserTable = ({ table }: Props) => {
3943
<div className="table border-2 border-gray-200 rounded-md ">
4044
<div className="table-header-group bg-slate-200">
4145
<div className="table-row">
46+
<Cell>
47+
<input
48+
type="checkbox"
49+
checked={table.getIsAllRowsSelected()}
50+
onChange={table.getToggleAllRowsSelectedHandler()}
51+
/>
52+
</Cell>
4253
{table.getFlatHeaders().map((header) => (
43-
<div
44-
key={header.id}
45-
className="table-cell py-2 px-2 text-center space-y-1"
46-
>
54+
<Cell key={header.id} className="space-y-1">
4755
<button
4856
className="font-semibold"
4957
type="button"
@@ -115,13 +123,20 @@ export const UserTable = ({ table }: Props) => {
115123
/>
116124
)}
117125
</div>
118-
</div>
126+
</Cell>
119127
))}
120128
</div>
121129
</div>
122130
<div className="table-row-group">
123131
{table.getRowModel().rows.map((row) => (
124132
<div key={row.id} className="table-row">
133+
<div className="table-cell py-1 px-2 text-center">
134+
<input
135+
type="checkbox"
136+
checked={row.getIsSelected()}
137+
onChange={row.getToggleSelectedHandler()}
138+
/>
139+
</div>
125140
{row.getVisibleCells().map((cell) => (
126141
<div
127142
key={cell.id}
@@ -215,6 +230,7 @@ export const UserTable = ({ table }: Props) => {
215230
pagination: table.getState().pagination,
216231
columnFilters: table.getState().columnFilters,
217232
columnOrder: table.getState().columnOrder,
233+
rowSelection: table.getState().rowSelection,
218234
},
219235
null,
220236
2,

examples/next-app-router/src/app/custom-default-value/table.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const Table = () => {
3232
pagination: { pageIndex: 2, pageSize: 20 },
3333
columnFilters: [{ id: "name", value: "b" }],
3434
columnOrder: userColumns.reverse().map((c) => c.id as string),
35+
rowSelection: { "1": true },
3536
},
3637
},
3738
);

examples/next-app-router/src/app/custom-encoder-decoder/table.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export const Table = () => {
5050
columnOrder: (columnOrder) => ({
5151
columnOrder: JSON.stringify(columnOrder),
5252
}),
53+
rowSelection: (rowSelection) => ({
54+
rowSelection: JSON.stringify(rowSelection),
55+
}),
5356
},
5457
decoders: {
5558
globalFilter: (query) =>
@@ -81,6 +84,10 @@ export const Table = () => {
8184
query["columnOrder"]
8285
? JSON.parse(query["columnOrder"] as string)
8386
: [],
87+
rowSelection: (query) =>
88+
query["rowSelection"]
89+
? JSON.parse(query["rowSelection"] as string)
90+
: {},
8491
},
8592
},
8693
);

examples/next-app-router/src/app/custom-param-name/table.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const Table = () => {
3535
},
3636
columnFilters: (defaultParamName) => `userTable-${defaultParamName}`,
3737
columnOrder: (defaultParamName) => `userTable-${defaultParamName}`,
38+
rowSelection: (defaultParamName) => `userTable-${defaultParamName}`,
3839
},
3940
},
4041
);

examples/next-app-router/src/app/debounce/table.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const Table = () => {
3232
pagination: 1000,
3333
columnFilters: 1000,
3434
columnOrder: 1000,
35+
rowSelection: 1000,
3536
},
3637
},
3738
);

examples/next-pages-router/src/pages/custom-default-value.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default function CustomParamNames() {
2424
pagination: { pageIndex: 2, pageSize: 20 },
2525
columnFilters: [{ id: "name", value: "b" }],
2626
columnOrder: userColumns.reverse().map((c) => c.id as string),
27+
rowSelection: { "1": true },
2728
},
2829
});
2930

examples/next-pages-router/src/pages/custom-encoder-decoder.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export default function CustomEncoderDecoder() {
4242
columnOrder: (columnOrder) => ({
4343
columnOrder: JSON.stringify(columnOrder),
4444
}),
45+
rowSelection: (rowSelection) => ({
46+
rowSelection: JSON.stringify(rowSelection),
47+
}),
4548
},
4649
decoders: {
4750
globalFilter: (query) =>
@@ -71,6 +74,10 @@ export default function CustomEncoderDecoder() {
7174
})),
7275
columnOrder: (query) =>
7376
query["columnOrder"] ? JSON.parse(query["columnOrder"] as string) : [],
77+
rowSelection: (query) =>
78+
query["rowSelection"]
79+
? JSON.parse(query["rowSelection"] as string)
80+
: {},
7481
},
7582
});
7683

examples/next-pages-router/src/pages/custom-param-name.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default function CustomParamNames() {
2727
},
2828
columnFilters: (defaultParamName) => `userTable-${defaultParamName}`,
2929
columnOrder: (defaultParamName) => `userTable-${defaultParamName}`,
30+
rowSelection: (defaultParamName) => `userTable-${defaultParamName}`,
3031
},
3132
});
3233

examples/next-pages-router/src/pages/debounce.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default function Basic() {
2424
pagination: 1000,
2525
columnFilters: 1000,
2626
columnOrder: 1000,
27+
rowSelection: 1000,
2728
},
2829
});
2930

examples/react-router-lib/src/custom-default-value.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default function CustomDefaultValuePage() {
2828
pagination: { pageIndex: 2, pageSize: 20 },
2929
columnFilters: [{ id: "name", value: "b" }],
3030
columnOrder: userColumns.reverse().map((c) => c.id as string),
31+
rowSelection: { "1": true },
3132
},
3233
},
3334
);

0 commit comments

Comments
 (0)