86133 | aaa3f5d | 2025-04-20 21:33:29 +0800 | [diff] [blame] | 1 | import React, { useEffect } from 'react'; |
| 2 | import { |
| 3 | ProForm, |
| 4 | ProFormDigit, |
| 5 | ProFormText, |
| 6 | ProFormRadio, |
| 7 | ProFormTextArea, |
| 8 | } from '@ant-design/pro-components'; |
| 9 | import { Form, Modal} from 'antd'; |
| 10 | import { useIntl, FormattedMessage } from '@umijs/max'; |
| 11 | import { DictValueEnumObj } from '@/components/DictTag'; |
| 12 | |
| 13 | export type DictTypeFormData = Record<string, unknown> & Partial<API.System.DictType>; |
| 14 | |
| 15 | export type DictTypeFormProps = { |
| 16 | onCancel: (flag?: boolean, formVals?: DictTypeFormData) => void; |
| 17 | onSubmit: (values: DictTypeFormData) => Promise<void>; |
| 18 | open: boolean; |
| 19 | values: Partial<API.System.DictType>; |
| 20 | statusOptions: DictValueEnumObj; |
| 21 | }; |
| 22 | |
| 23 | const DictTypeForm: React.FC<DictTypeFormProps> = (props) => { |
| 24 | const [form] = Form.useForm(); |
| 25 | |
| 26 | const { statusOptions } = props; |
| 27 | |
| 28 | useEffect(() => { |
| 29 | form.resetFields(); |
| 30 | form.setFieldsValue({ |
| 31 | dictId: props.values.dictId, |
| 32 | dictName: props.values.dictName, |
| 33 | dictType: props.values.dictType, |
| 34 | status: props.values.status, |
| 35 | createBy: props.values.createBy, |
| 36 | createTime: props.values.createTime, |
| 37 | updateBy: props.values.updateBy, |
| 38 | updateTime: props.values.updateTime, |
| 39 | remark: props.values.remark, |
| 40 | }); |
| 41 | }, [form, props]); |
| 42 | |
| 43 | const intl = useIntl(); |
| 44 | const handleOk = () => { |
| 45 | form.submit(); |
| 46 | }; |
| 47 | const handleCancel = () => { |
| 48 | props.onCancel(); |
| 49 | }; |
| 50 | const handleFinish = async (values: Record<string, any>) => { |
| 51 | props.onSubmit(values as DictTypeFormData); |
| 52 | }; |
| 53 | |
| 54 | return ( |
| 55 | <Modal |
| 56 | width={640} |
| 57 | title={intl.formatMessage({ |
| 58 | id: 'system.dict.title', |
| 59 | defaultMessage: '编辑字典类型', |
| 60 | })} |
| 61 | open={props.open} |
| 62 | forceRender |
| 63 | destroyOnClose |
| 64 | onOk={handleOk} |
| 65 | onCancel={handleCancel} |
| 66 | > |
| 67 | <ProForm |
| 68 | form={form} |
| 69 | grid={true} |
| 70 | submitter={false} |
| 71 | layout="horizontal" |
| 72 | onFinish={handleFinish}> |
| 73 | <ProFormDigit |
| 74 | name="dictId" |
| 75 | label={intl.formatMessage({ |
| 76 | id: 'system.dict.dict_id', |
| 77 | defaultMessage: '字典主键', |
| 78 | })} |
| 79 | placeholder="请输入字典主键" |
| 80 | disabled |
| 81 | hidden={true} |
| 82 | rules={[ |
| 83 | { |
| 84 | required: false, |
| 85 | message: <FormattedMessage id="请输入字典主键!" defaultMessage="请输入字典主键!" />, |
| 86 | }, |
| 87 | ]} |
| 88 | /> |
| 89 | <ProFormText |
| 90 | name="dictName" |
| 91 | label={intl.formatMessage({ |
| 92 | id: 'system.dict.dict_name', |
| 93 | defaultMessage: '字典名称', |
| 94 | })} |
| 95 | placeholder="请输入字典名称" |
| 96 | rules={[ |
| 97 | { |
| 98 | required: false, |
| 99 | message: <FormattedMessage id="请输入字典名称!" defaultMessage="请输入字典名称!" />, |
| 100 | }, |
| 101 | ]} |
| 102 | /> |
| 103 | <ProFormText |
| 104 | name="dictType" |
| 105 | label={intl.formatMessage({ |
| 106 | id: 'system.dict.dict_type', |
| 107 | defaultMessage: '字典类型', |
| 108 | })} |
| 109 | placeholder="请输入字典类型" |
| 110 | rules={[ |
| 111 | { |
| 112 | required: false, |
| 113 | message: <FormattedMessage id="请输入字典类型!" defaultMessage="请输入字典类型!" />, |
| 114 | }, |
| 115 | ]} |
| 116 | /> |
| 117 | <ProFormRadio.Group |
| 118 | valueEnum={statusOptions} |
| 119 | name="status" |
| 120 | label={intl.formatMessage({ |
| 121 | id: 'system.dict.status', |
| 122 | defaultMessage: '状态', |
| 123 | })} |
| 124 | initialValue={'0'} |
| 125 | placeholder="请输入状态" |
| 126 | rules={[ |
| 127 | { |
| 128 | required: false, |
| 129 | message: <FormattedMessage id="请输入状态!" defaultMessage="请输入状态!" />, |
| 130 | }, |
| 131 | ]} |
| 132 | /> |
| 133 | <ProFormTextArea |
| 134 | name="remark" |
| 135 | label={intl.formatMessage({ |
| 136 | id: 'system.dict.remark', |
| 137 | defaultMessage: '备注', |
| 138 | })} |
| 139 | placeholder="请输入备注" |
| 140 | rules={[ |
| 141 | { |
| 142 | required: false, |
| 143 | message: <FormattedMessage id="请输入备注!" defaultMessage="请输入备注!" />, |
| 144 | }, |
| 145 | ]} |
| 146 | /> |
| 147 | </ProForm> |
| 148 | </Modal> |
| 149 | ); |
| 150 | }; |
| 151 | |
| 152 | export default DictTypeForm; |