技术栈:React、Ant design@4.21.0、Ant design pro、umi@^3.5.19、dva
高级表单 & 带弹出层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import { Button, Form, FormInstance, Modal } from 'antd'; import React, { useEffect, useState } from 'react'; import MyForm from "./MyForm";
const AimDetailNewModal: React.FC = (props) => { const [isModalOpen, setIsModalOpen] = useState(false);
const showModal = () => { setIsModalOpen(true); }; const [form] = Form.useForm() const onCancel = () => { setIsModalOpen(false); };
return ( <> <Button type="link" block> 打开表单 </Button>
<Modal visible={isModalOpen} onOk={() => form.submit()} onCancel={onCancel} width="600px"> <MyForm form={form} setIsModalOpen={setIsModalOpen} /> </Modal> </> ); };
export default AimDetailNewModal
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
export default { namespace: 'dvaxxx', state: { xxxList: null, operateRefresh: null }, reducers: { setData(state: any, { payload }: { payload: any }) { return { ...state, ...payload } } } }
|
1 2 3 4 5
| export interface DvaAnalysis { analysisTaskList: AnalysisTask[] operateRefresh: number }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
import { xxx } from '@/api/xxx'; import { Alert, Button, Checkbox, Form, FormInstance, Input, MenuProps, message, Select } from 'antd'; import React, { useEffect, useState } from 'react';
import { connect } from "dva"; import { Dvaxxx } from './types';
const { add } = xxx
interface Iprops { form: FormInstance setIsModalOpen: Function dvaxxx: Dvaxxx dispatch: Function }
const MyForm: React.FC<Iprops> = (props: Iprops) => { const { form, setIsModalOpen, dvaxxx: { operateRefresh }, dispatch } = props
const onFinish = async (values: any) => { try { const res = await add(values)
setIsModalOpen(false)
message.success(`新增xxx ${values.name} 成功`)
dispatch({ type: 'dvaxxx/setData', payload: { operateRefresh: operateRefresh + 1 } })
form.resetFields() } catch (e) { message.success("新增xxx失败") } }
return ( <Form name="basic" labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} initialValues={{ remember: true }} onFinish={onFinish} autoComplete="off" form={form} > <Form.Item label="名称" name="name" rules={[{ required: true, message: '请输入任务名称' }]} > <Input placeholder='请输入任务名称' /> </Form.Item>
<Form.Item label="编号" name="number" rules={[{ required: true, message: '请输入编号' }]} > <Input placeholder='请输入编号' /> </Form.Item> <Form.Item label="其他属性" name="other" rules={[{ required: true, message: '请输入其他' }]} > <Input placeholder='请输入其他' /> </Form.Item> ... </Form> ); };
export default connect(({ dvaxxx }: any) => ({ dvaxxx }))(MyForm)
|