|
| 1 | +import React from 'react'; |
| 2 | +import { Button, Form, Input, Result, Select, Table } from 'antd'; |
| 3 | +import { useList } from 'dt-react-component'; |
| 4 | +import type { Fetcher } from 'dt-react-component/useList'; |
| 5 | + |
| 6 | +import getMockData, { type MockData } from './data'; |
| 7 | + |
| 8 | +const fetcher: Fetcher<MockData, { current: number; pageSize: number; search?: string }> = ( |
| 9 | + params |
| 10 | +) => { |
| 11 | + return new Promise<{ |
| 12 | + data: MockData[]; |
| 13 | + total: number; |
| 14 | + }>((resolve) => { |
| 15 | + setTimeout(() => { |
| 16 | + resolve(getMockData(params)); |
| 17 | + }, 150); |
| 18 | + }); |
| 19 | +}; |
| 20 | + |
| 21 | +export default () => { |
| 22 | + const { error, params, loading, data, mutate } = useList(fetcher, { current: 1, pageSize: 20 }); |
| 23 | + const [form] = Form.useForm(); |
| 24 | + |
| 25 | + if (error) return <Result status={500} />; |
| 26 | + |
| 27 | + const handleSearch = async () => { |
| 28 | + const values = await form.validateFields(); |
| 29 | + mutate({ ...values }); |
| 30 | + }; |
| 31 | + |
| 32 | + const handleReset = async () => { |
| 33 | + form.resetFields(); |
| 34 | + const values = await form.validateFields(); |
| 35 | + // 当传入值有 undefined 的时候,采用 functional 的写法。 |
| 36 | + // 因为底层使用的 lodash 的 merge,采用赋值写法不会对 undefined 做合并 |
| 37 | + mutate((pre) => ({ ...pre, ...values })); |
| 38 | + }; |
| 39 | + |
| 40 | + return ( |
| 41 | + <> |
| 42 | + <Form layout="inline" form={form}> |
| 43 | + <Form.Item name="search"> |
| 44 | + <Input.Search style={{ marginBottom: 12, width: 228 }} /> |
| 45 | + </Form.Item> |
| 46 | + <Form.Item name="filters"> |
| 47 | + <Select style={{ width: 228 }} mode="multiple"> |
| 48 | + <Select.Option key="female" value="female"> |
| 49 | + female |
| 50 | + </Select.Option> |
| 51 | + <Select.Option key="male" value="male"> |
| 52 | + male |
| 53 | + </Select.Option> |
| 54 | + </Select> |
| 55 | + </Form.Item> |
| 56 | + <Button type="ghost" style={{ marginRight: 16 }} onClick={handleReset}> |
| 57 | + 重置 |
| 58 | + </Button> |
| 59 | + <Button type="primary" onClick={handleSearch}> |
| 60 | + 查询 |
| 61 | + </Button> |
| 62 | + </Form> |
| 63 | + <Table |
| 64 | + loading={loading} |
| 65 | + columns={[ |
| 66 | + { |
| 67 | + key: 'name', |
| 68 | + title: 'name', |
| 69 | + dataIndex: 'name', |
| 70 | + }, |
| 71 | + { |
| 72 | + key: 'address', |
| 73 | + title: 'address', |
| 74 | + dataIndex: 'address', |
| 75 | + }, |
| 76 | + { |
| 77 | + key: 'company', |
| 78 | + title: 'company', |
| 79 | + dataIndex: 'company', |
| 80 | + }, |
| 81 | + { |
| 82 | + key: 'gender', |
| 83 | + title: 'gender', |
| 84 | + dataIndex: 'gender', |
| 85 | + }, |
| 86 | + { |
| 87 | + key: 'weight', |
| 88 | + title: 'weight', |
| 89 | + dataIndex: 'weight', |
| 90 | + }, |
| 91 | + ]} |
| 92 | + onChange={(pagination) => |
| 93 | + mutate({ current: pagination.current, pageSize: pagination.pageSize }) |
| 94 | + } |
| 95 | + size="small" |
| 96 | + scroll={{ y: 200 }} |
| 97 | + dataSource={data} |
| 98 | + pagination={{ |
| 99 | + current: params.current, |
| 100 | + pageSize: params.pageSize, |
| 101 | + total: params.total, |
| 102 | + }} |
| 103 | + rowKey="uuid" |
| 104 | + bordered |
| 105 | + /> |
| 106 | + </> |
| 107 | + ); |
| 108 | +}; |
0 commit comments