|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Button, Form, Input, Result, Select, Switch, 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 }, { revalidate, clearData }); |
| 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 }), { revalidate, clearData }); |
| 38 | + }; |
| 39 | + const [revalidate, setRevalidate] = useState(true); |
| 40 | + const [clearData, setClearData] = useState(true); |
| 41 | + const onChangeRevalidate = () => { |
| 42 | + setRevalidate(!revalidate); |
| 43 | + }; |
| 44 | + const onChangeClearData = () => { |
| 45 | + setClearData(!clearData); |
| 46 | + }; |
| 47 | + |
| 48 | + return ( |
| 49 | + <> |
| 50 | + <Form layout="inline" form={form}> |
| 51 | + <Form.Item name="search"> |
| 52 | + <Input.Search style={{ marginBottom: 12, width: 228 }} /> |
| 53 | + </Form.Item> |
| 54 | + <Form.Item name="filters"> |
| 55 | + <Select style={{ width: 228 }} mode="multiple"> |
| 56 | + <Select.Option key="female" value="female"> |
| 57 | + female |
| 58 | + </Select.Option> |
| 59 | + <Select.Option key="male" value="male"> |
| 60 | + male |
| 61 | + </Select.Option> |
| 62 | + </Select> |
| 63 | + </Form.Item> |
| 64 | + <Button type="ghost" style={{ marginRight: 16 }} onClick={handleReset}> |
| 65 | + 重置 |
| 66 | + </Button> |
| 67 | + <Button type="primary" style={{ marginRight: 32 }} onClick={handleSearch}> |
| 68 | + 查询 |
| 69 | + </Button> |
| 70 | + <Form.Item name="options"> |
| 71 | + <span style={{ marginRight: 4 }}>revalidate</span> |
| 72 | + <Switch |
| 73 | + style={{ marginRight: 16 }} |
| 74 | + defaultChecked |
| 75 | + onChange={onChangeRevalidate} |
| 76 | + /> |
| 77 | + <span style={{ marginRight: 4 }}>clearData</span> |
| 78 | + <Switch defaultChecked onChange={onChangeClearData} /> |
| 79 | + </Form.Item> |
| 80 | + </Form> |
| 81 | + <Table |
| 82 | + loading={loading} |
| 83 | + columns={[ |
| 84 | + { |
| 85 | + key: 'name', |
| 86 | + title: 'name', |
| 87 | + dataIndex: 'name', |
| 88 | + }, |
| 89 | + { |
| 90 | + key: 'address', |
| 91 | + title: 'address', |
| 92 | + dataIndex: 'address', |
| 93 | + }, |
| 94 | + { |
| 95 | + key: 'company', |
| 96 | + title: 'company', |
| 97 | + dataIndex: 'company', |
| 98 | + }, |
| 99 | + { |
| 100 | + key: 'gender', |
| 101 | + title: 'gender', |
| 102 | + dataIndex: 'gender', |
| 103 | + }, |
| 104 | + { |
| 105 | + key: 'weight', |
| 106 | + title: 'weight', |
| 107 | + dataIndex: 'weight', |
| 108 | + }, |
| 109 | + ]} |
| 110 | + onChange={(pagination) => |
| 111 | + mutate( |
| 112 | + { current: pagination.current, pageSize: pagination.pageSize }, |
| 113 | + { revalidate, clearData } |
| 114 | + ) |
| 115 | + } |
| 116 | + size="small" |
| 117 | + scroll={{ y: 200 }} |
| 118 | + dataSource={data} |
| 119 | + pagination={{ |
| 120 | + current: params.current, |
| 121 | + pageSize: params.pageSize, |
| 122 | + total: params.total, |
| 123 | + }} |
| 124 | + rowKey="uuid" |
| 125 | + bordered |
| 126 | + /> |
| 127 | + </> |
| 128 | + ); |
| 129 | +}; |
0 commit comments