|
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import { Button, Form, Input, Modal, Table } from 'antd'; |
| 3 | +import type { ColumnType } from 'antd/lib/table'; |
| 4 | + |
| 5 | +import useModal from '..'; |
| 6 | + |
| 7 | +interface IDataSource { |
| 8 | + id: string; |
| 9 | + name: string; |
| 10 | + age: number; |
| 11 | + address: string; |
| 12 | +} |
| 13 | + |
| 14 | +const data: IDataSource[] = [ |
| 15 | + { |
| 16 | + id: '1', |
| 17 | + name: 'John Brown', |
| 18 | + age: 32, |
| 19 | + address: 'New York No. 1 Lake Park', |
| 20 | + }, |
| 21 | + { |
| 22 | + id: '2', |
| 23 | + name: 'Jim Green', |
| 24 | + age: 42, |
| 25 | + address: 'London No. 1 Lake Park', |
| 26 | + }, |
| 27 | + { |
| 28 | + id: '3', |
| 29 | + name: 'Joe Black', |
| 30 | + age: 52, |
| 31 | + address: 'Sydney No. 1 Lake Park', |
| 32 | + }, |
| 33 | + { |
| 34 | + id: '4', |
| 35 | + name: 'Jim Red', |
| 36 | + age: 62, |
| 37 | + address: 'London No. 2 Lake Park', |
| 38 | + }, |
| 39 | +]; |
| 40 | + |
| 41 | +export default () => { |
| 42 | + const modal = useModal<IDataSource>(); |
| 43 | + const [form] = Form.useForm<IDataSource>(); |
| 44 | + |
| 45 | + const columns: ColumnType<IDataSource>[] = [ |
| 46 | + { |
| 47 | + key: 'name', |
| 48 | + title: 'name', |
| 49 | + dataIndex: 'name', |
| 50 | + }, |
| 51 | + { |
| 52 | + key: 'age', |
| 53 | + title: 'age', |
| 54 | + dataIndex: 'age', |
| 55 | + }, |
| 56 | + { |
| 57 | + key: 'address', |
| 58 | + title: 'address', |
| 59 | + dataIndex: 'address', |
| 60 | + }, |
| 61 | + { |
| 62 | + key: 'operation', |
| 63 | + title: '操作', |
| 64 | + render: (_, record) => { |
| 65 | + return ( |
| 66 | + <Button |
| 67 | + type="link" |
| 68 | + onClick={() => { |
| 69 | + modal.open(record); |
| 70 | + }} |
| 71 | + > |
| 72 | + 编辑 |
| 73 | + </Button> |
| 74 | + ); |
| 75 | + }, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + |
| 79 | + useEffect(() => { |
| 80 | + if (modal.visible) { |
| 81 | + form.setFieldsValue({ |
| 82 | + name: modal.record?.name, |
| 83 | + age: modal.record?.age, |
| 84 | + address: modal.record?.address, |
| 85 | + }); |
| 86 | + } else { |
| 87 | + form.resetFields(); |
| 88 | + } |
| 89 | + }, [modal.record, modal.visible]); |
| 90 | + |
| 91 | + return ( |
| 92 | + <> |
| 93 | + <Table |
| 94 | + columns={columns} |
| 95 | + size="small" |
| 96 | + scroll={{ y: 200 }} |
| 97 | + dataSource={data} |
| 98 | + rowKey="uuid" |
| 99 | + bordered |
| 100 | + /> |
| 101 | + <Modal |
| 102 | + title="修改信息" |
| 103 | + visible={modal.visible} |
| 104 | + onOk={modal.close} |
| 105 | + onCancel={modal.close} |
| 106 | + > |
| 107 | + <Form form={form}> |
| 108 | + <Form.Item label="姓名" name="name"> |
| 109 | + <Input /> |
| 110 | + </Form.Item> |
| 111 | + <Form.Item label="年龄" name="age"> |
| 112 | + <Input /> |
| 113 | + </Form.Item> |
| 114 | + <Form.Item label="地址" name="address"> |
| 115 | + <Input /> |
| 116 | + </Form.Item> |
| 117 | + </Form> |
| 118 | + </Modal> |
| 119 | + </> |
| 120 | + ); |
| 121 | +}; |
0 commit comments