Jiarenxiang | 38dcb05 | 2025-03-13 16:40:09 +0800 | [diff] [blame^] | 1 | import React, { useEffect } from 'react'; |
| 2 | import { |
| 3 | ProForm, |
| 4 | ProFormDigit, |
| 5 | ProFormText, |
| 6 | ProFormTextArea, |
| 7 | ProFormRadio, |
| 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 ConfigFormData = Record<string, unknown> & Partial<API.System.Config>; |
| 14 | |
| 15 | export type ConfigFormProps = { |
| 16 | onCancel: (flag?: boolean, formVals?: ConfigFormData) => void; |
| 17 | onSubmit: (values: ConfigFormData) => Promise<void>; |
| 18 | open: boolean; |
| 19 | values: Partial<API.System.Config>; |
| 20 | configTypeOptions: DictValueEnumObj; |
| 21 | }; |
| 22 | |
| 23 | const ConfigForm: React.FC<ConfigFormProps> = (props) => { |
| 24 | const [form] = Form.useForm(); |
| 25 | |
| 26 | const { configTypeOptions } = props; |
| 27 | |
| 28 | useEffect(() => { |
| 29 | form.resetFields(); |
| 30 | form.setFieldsValue({ |
| 31 | configId: props.values.configId, |
| 32 | configName: props.values.configName, |
| 33 | configKey: props.values.configKey, |
| 34 | configValue: props.values.configValue, |
| 35 | configType: props.values.configType, |
| 36 | createBy: props.values.createBy, |
| 37 | createTime: props.values.createTime, |
| 38 | updateBy: props.values.updateBy, |
| 39 | updateTime: props.values.updateTime, |
| 40 | remark: props.values.remark, |
| 41 | }); |
| 42 | }, [form, props]); |
| 43 | |
| 44 | const intl = useIntl(); |
| 45 | const handleOk = () => { |
| 46 | form.submit(); |
| 47 | }; |
| 48 | const handleCancel = () => { |
| 49 | props.onCancel(); |
| 50 | }; |
| 51 | const handleFinish = async (values: Record<string, any>) => { |
| 52 | props.onSubmit(values as ConfigFormData); |
| 53 | }; |
| 54 | |
| 55 | return ( |
| 56 | <Modal |
| 57 | width={640} |
| 58 | title={intl.formatMessage({ |
| 59 | id: 'system.config.title', |
| 60 | defaultMessage: '编辑参数配置', |
| 61 | })} |
| 62 | open={props.open} |
| 63 | forceRender |
| 64 | destroyOnClose |
| 65 | onOk={handleOk} |
| 66 | onCancel={handleCancel} |
| 67 | > |
| 68 | <ProForm |
| 69 | form={form} |
| 70 | grid={true} |
| 71 | submitter={false} |
| 72 | layout="horizontal" |
| 73 | onFinish={handleFinish}> |
| 74 | <ProFormDigit |
| 75 | name="configId" |
| 76 | label={intl.formatMessage({ |
| 77 | id: 'system.config.config_id', |
| 78 | defaultMessage: '参数主键', |
| 79 | })} |
| 80 | colProps={{ md: 24 }} |
| 81 | placeholder="请输入参数主键" |
| 82 | disabled |
| 83 | hidden={true} |
| 84 | rules={[ |
| 85 | { |
| 86 | required: false, |
| 87 | message: <FormattedMessage id="请输入参数主键!" defaultMessage="请输入参数主键!" />, |
| 88 | }, |
| 89 | ]} |
| 90 | /> |
| 91 | <ProFormText |
| 92 | name="configName" |
| 93 | label={intl.formatMessage({ |
| 94 | id: 'system.config.config_name', |
| 95 | defaultMessage: '参数名称', |
| 96 | })} |
| 97 | colProps={{ md: 24 }} |
| 98 | placeholder="请输入参数名称" |
| 99 | rules={[ |
| 100 | { |
| 101 | required: false, |
| 102 | message: <FormattedMessage id="请输入参数名称!" defaultMessage="请输入参数名称!" />, |
| 103 | }, |
| 104 | ]} |
| 105 | /> |
| 106 | <ProFormText |
| 107 | name="configKey" |
| 108 | label={intl.formatMessage({ |
| 109 | id: 'system.config.config_key', |
| 110 | defaultMessage: '参数键名', |
| 111 | })} |
| 112 | colProps={{ md: 24 }} |
| 113 | placeholder="请输入参数键名" |
| 114 | rules={[ |
| 115 | { |
| 116 | required: false, |
| 117 | message: <FormattedMessage id="请输入参数键名!" defaultMessage="请输入参数键名!" />, |
| 118 | }, |
| 119 | ]} |
| 120 | /> |
| 121 | <ProFormTextArea |
| 122 | name="configValue" |
| 123 | label={intl.formatMessage({ |
| 124 | id: 'system.config.config_value', |
| 125 | defaultMessage: '参数键值', |
| 126 | })} |
| 127 | colProps={{ md: 24 }} |
| 128 | placeholder="请输入参数键值" |
| 129 | rules={[ |
| 130 | { |
| 131 | required: false, |
| 132 | message: <FormattedMessage id="请输入参数键值!" defaultMessage="请输入参数键值!" />, |
| 133 | }, |
| 134 | ]} |
| 135 | /> |
| 136 | <ProFormRadio.Group |
| 137 | valueEnum={configTypeOptions} |
| 138 | name="configType" |
| 139 | label={intl.formatMessage({ |
| 140 | id: 'system.config.config_type', |
| 141 | defaultMessage: '系统内置', |
| 142 | })} |
| 143 | colProps={{ md: 24 }} |
| 144 | placeholder="请输入系统内置" |
| 145 | rules={[ |
| 146 | { |
| 147 | required: false, |
| 148 | message: <FormattedMessage id="请输入系统内置!" defaultMessage="请输入系统内置!" />, |
| 149 | }, |
| 150 | ]} |
| 151 | /> |
| 152 | <ProFormTextArea |
| 153 | name="remark" |
| 154 | label={intl.formatMessage({ |
| 155 | id: 'system.config.remark', |
| 156 | defaultMessage: '备注', |
| 157 | })} |
| 158 | colProps={{ md: 24 }} |
| 159 | placeholder="请输入备注" |
| 160 | rules={[ |
| 161 | { |
| 162 | required: false, |
| 163 | message: <FormattedMessage id="请输入备注!" defaultMessage="请输入备注!" />, |
| 164 | }, |
| 165 | ]} |
| 166 | /> |
| 167 | </ProForm> |
| 168 | </Modal> |
| 169 | ); |
| 170 | }; |
| 171 | |
| 172 | export default ConfigForm; |