Skip to content

Commit 06f7672

Browse files
committed
feat(dataTable): update usage
1 parent 6000470 commit 06f7672

File tree

3 files changed

+98
-60
lines changed

3 files changed

+98
-60
lines changed

docs/components/data-table/playground.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,27 @@ export const columns: DataTable.ColumnDef<Payment>[] = [
7474
]
7575

7676
function App() {
77-
const handleSubmit = (v: object) => {
78-
// table.getColumn("email")?.setFilterValue(event.target.value)
77+
const table = DataTable.useReactTable({
78+
data,
79+
columns,
80+
getCoreRowModel: DataTable.getCoreRowModel(),
81+
getFilteredRowModel: DataTable.getFilteredRowModel(),
82+
})
83+
84+
const handleSubmit = (v: { email?: string }) => {
85+
console.log(v?.email)
86+
table.getColumn('email')?.setFilterValue(v.email)
7987
}
8088

8189
return (
8290
<div>
8391
<div>
8492
<Form onSubmit={handleSubmit}>
85-
<FormItem name="emial" label="Email" render={<Input />} />
93+
<FormItem name="email" label="Email" render={<Input />} />
8694
</Form>
8795
</div>
8896
<div>
89-
<DataTable.Table
90-
data={data}
91-
columns={columns}
92-
getCoreRowModel={DataTable.getCoreRowModel()}
93-
getFilteredRowModel={DataTable.getFilteredRowModel()}
94-
/>
97+
<DataTable.Table table={table} />
9598
</div>
9699
</div>
97100
)

playground/src/App.tsx

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,101 @@
1-
import { CheckboxGroup, Input, FormItem, Form, Button } from '../../src'
1+
import { DataTable, Form, FormItem, Input } from '../../src'
22

3-
const options = [
3+
export type Payment = {
4+
id: string
5+
amount: number
6+
status: 'pending' | 'processing' | 'success' | 'failed'
7+
email: string
8+
}
9+
10+
const data: Payment[] = [
411
{
5-
label: '豆腐',
6-
value: 'toufu',
12+
id: 'm5gr84i9',
13+
amount: 316,
14+
status: 'success',
15+
email: 'ken99@example.com',
716
},
817
{
9-
label: '棉花糖',
10-
value: 'mianhuatang',
18+
id: '3u1reuv4',
19+
amount: 242,
20+
status: 'success',
21+
email: 'Abe45@example.com',
1122
},
1223
{
13-
label: '棉花糖2',
14-
value: 'mianhuatang2',
24+
id: 'derv1ws0',
25+
amount: 837,
26+
status: 'processing',
27+
email: 'Monserrat44@example.com',
1528
},
1629
{
17-
label: '棉花糖3',
18-
value: 'mianhuatang3',
30+
id: '5kma53ae',
31+
amount: 874,
32+
status: 'success',
33+
email: 'Silas22@example.com',
1934
},
2035
{
21-
label: '棉花糖4',
22-
value: 'mianhuatang4',
36+
id: 'bhqecj4p',
37+
amount: 721,
38+
status: 'failed',
39+
email: 'carmella@example.com',
40+
},
41+
]
42+
43+
export const columns: DataTable.ColumnDef<Payment>[] = [
44+
{
45+
accessorKey: 'id',
46+
header: 'ID',
47+
cell: ({ row }) => row.getValue('id'),
48+
},
49+
{
50+
accessorKey: 'status',
51+
header: 'Status',
52+
cell: ({ row }) => (
53+
<div className="capitalize">{row.getValue('status')}</div>
54+
),
55+
},
56+
{
57+
accessorKey: 'email',
58+
header: 'email',
59+
cell: ({ row }) => <div className="lowercase">{row.getValue('email')}</div>,
60+
},
61+
{
62+
accessorKey: 'amount',
63+
header: () => <div className="text-right">Amount</div>,
64+
cell: ({ row }) => {
65+
const amount = parseFloat(row.getValue('amount'))
66+
// Format the amount as a dollar amount
67+
const formatted = new Intl.NumberFormat('en-US', {
68+
style: 'currency',
69+
currency: 'USD',
70+
}).format(amount)
71+
return <div className="text-right font-medium">{formatted}</div>
72+
},
2373
},
2474
]
2575

2676
export function App() {
77+
const table = DataTable.useReactTable({
78+
data,
79+
columns,
80+
getCoreRowModel: DataTable.getCoreRowModel(),
81+
getFilteredRowModel: DataTable.getFilteredRowModel(),
82+
})
83+
84+
const handleSubmit = (v: { email?: string }) => {
85+
console.log(v?.email)
86+
table.getColumn('email')?.setFilterValue(v.email)
87+
}
88+
2789
return (
28-
<>
29-
<div className="flex min-h-svh items-center justify-center">
30-
<Form onSubmit={(v) => console.log(v)} className="max-w-[300px]">
31-
<FormItem
32-
label="Username"
33-
name="username"
34-
rules={{
35-
required: '内容必填',
36-
}}
37-
render={<Input autoComplete="on" />}
38-
/>
39-
<FormItem
40-
label="Password"
41-
name="password"
42-
rules={{
43-
required: 'message',
44-
}}
45-
render={<Input autoComplete="on" />}
46-
/>
47-
<FormItem
48-
label="like"
49-
name="food"
50-
rules={{
51-
required: 'message',
52-
}}
53-
controlled
54-
className="items-start"
55-
render={<CheckboxGroup options={options} />}
56-
/>
57-
<Button type="submit">Submit</Button>
90+
<div>
91+
<div>
92+
<Form onSubmit={handleSubmit}>
93+
<FormItem name="email" label="Email" render={<Input />} />
5894
</Form>
5995
</div>
60-
</>
96+
<div>
97+
<DataTable.Table table={table} />
98+
</div>
99+
</div>
61100
)
62101
}

src/components/data-table/index.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
flexRender,
3-
useReactTable,
4-
type TableOptions,
5-
} from '@tanstack/react-table'
1+
import { flexRender, type Table as RTable } from '@tanstack/react-table'
62
import {
73
Table as OriginTable,
84
TableBody,
@@ -13,8 +9,8 @@ import {
139
} from '../table'
1410
import './style.css'
1511

16-
export const Table = <T = unknown,>(props: TableOptions<T>) => {
17-
const table = useReactTable(props)
12+
export const Table = <T extends unknown>(props: { table: RTable<T> }) => {
13+
const { table } = props
1814

1915
return (
2016
<OriginTable>
@@ -53,7 +49,7 @@ export const Table = <T = unknown,>(props: TableOptions<T>) => {
5349
) : (
5450
<TableRow>
5551
<TableCell
56-
colSpan={props.columns.length}
52+
colSpan={table.getAllColumns().length}
5753
className="slTableCellEmpty"
5854
>
5955
No results.

0 commit comments

Comments
 (0)