|
1 | | -import { CheckboxGroup, Input, FormItem, Form, Button } from '../../src' |
| 1 | +import { DataTable, Form, FormItem, Input } from '../../src' |
2 | 2 |
|
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[] = [ |
4 | 11 | { |
5 | | - label: '豆腐', |
6 | | - value: 'toufu', |
| 12 | + id: 'm5gr84i9', |
| 13 | + amount: 316, |
| 14 | + status: 'success', |
| 15 | + email: 'ken99@example.com', |
7 | 16 | }, |
8 | 17 | { |
9 | | - label: '棉花糖', |
10 | | - value: 'mianhuatang', |
| 18 | + id: '3u1reuv4', |
| 19 | + amount: 242, |
| 20 | + status: 'success', |
| 21 | + email: 'Abe45@example.com', |
11 | 22 | }, |
12 | 23 | { |
13 | | - label: '棉花糖2', |
14 | | - value: 'mianhuatang2', |
| 24 | + id: 'derv1ws0', |
| 25 | + amount: 837, |
| 26 | + status: 'processing', |
| 27 | + email: 'Monserrat44@example.com', |
15 | 28 | }, |
16 | 29 | { |
17 | | - label: '棉花糖3', |
18 | | - value: 'mianhuatang3', |
| 30 | + id: '5kma53ae', |
| 31 | + amount: 874, |
| 32 | + status: 'success', |
| 33 | + email: 'Silas22@example.com', |
19 | 34 | }, |
20 | 35 | { |
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 | + }, |
23 | 73 | }, |
24 | 74 | ] |
25 | 75 |
|
26 | 76 | 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 | + |
27 | 89 | 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 />} /> |
58 | 94 | </Form> |
59 | 95 | </div> |
60 | | - </> |
| 96 | + <div> |
| 97 | + <DataTable.Table table={table} /> |
| 98 | + </div> |
| 99 | + </div> |
61 | 100 | ) |
62 | 101 | } |
0 commit comments