Jiarenxiang | 38dcb05 | 2025-03-13 16:40:09 +0800 | [diff] [blame^] | 1 | import React, { useEffect, useState } 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 Tree, { DataNode } from 'antd/es/tree'; |
| 12 | import { DictValueEnumObj } from '@/components/DictTag'; |
| 13 | |
| 14 | export type RoleFormData = Record<string, unknown> & Partial<API.System.Role>; |
| 15 | |
| 16 | export type RoleFormProps = { |
| 17 | onCancel: (flag?: boolean, formVals?: RoleFormData) => void; |
| 18 | onSubmit: (values: RoleFormData) => Promise<void>; |
| 19 | open: boolean; |
| 20 | values: Partial<API.System.Role>; |
| 21 | menuTree: DataNode[]; |
| 22 | menuCheckedKeys: string[]; |
| 23 | statusOptions: DictValueEnumObj; |
| 24 | }; |
| 25 | |
| 26 | const RoleForm: React.FC<RoleFormProps> = (props) => { |
| 27 | const [form] = Form.useForm(); |
| 28 | const { menuTree, menuCheckedKeys } = props; |
| 29 | const [menuIds, setMenuIds] = useState<string[]>([]); |
| 30 | const { statusOptions } = props; |
| 31 | |
| 32 | useEffect(() => { |
| 33 | form.resetFields(); |
| 34 | form.setFieldsValue({ |
| 35 | roleId: props.values.roleId, |
| 36 | roleName: props.values.roleName, |
| 37 | roleKey: props.values.roleKey, |
| 38 | roleSort: props.values.roleSort, |
| 39 | dataScope: props.values.dataScope, |
| 40 | menuCheckStrictly: props.values.menuCheckStrictly, |
| 41 | deptCheckStrictly: props.values.deptCheckStrictly, |
| 42 | status: props.values.status, |
| 43 | delFlag: props.values.delFlag, |
| 44 | createBy: props.values.createBy, |
| 45 | createTime: props.values.createTime, |
| 46 | updateBy: props.values.updateBy, |
| 47 | updateTime: props.values.updateTime, |
| 48 | remark: props.values.remark, |
| 49 | }); |
| 50 | }, [form, props]); |
| 51 | |
| 52 | const intl = useIntl(); |
| 53 | const handleOk = () => { |
| 54 | form.submit(); |
| 55 | }; |
| 56 | const handleCancel = () => { |
| 57 | props.onCancel(); |
| 58 | }; |
| 59 | const handleFinish = async (values: Record<string, any>) => { |
| 60 | props.onSubmit({ ...values, menuIds } as RoleFormData); |
| 61 | }; |
| 62 | |
| 63 | return ( |
| 64 | <Modal |
| 65 | width={640} |
| 66 | title={intl.formatMessage({ |
| 67 | id: 'system.role.title', |
| 68 | defaultMessage: '编辑角色信息', |
| 69 | })} |
| 70 | forceRender |
| 71 | open={props.open} |
| 72 | destroyOnClose |
| 73 | onOk={handleOk} |
| 74 | onCancel={handleCancel} |
| 75 | > |
| 76 | <ProForm |
| 77 | form={form} |
| 78 | grid={true} |
| 79 | layout="horizontal" |
| 80 | submitter={false} |
| 81 | onFinish={handleFinish}> |
| 82 | <ProFormDigit |
| 83 | name="roleId" |
| 84 | label={intl.formatMessage({ |
| 85 | id: 'system.role.role_id', |
| 86 | defaultMessage: '角色编号', |
| 87 | })} |
| 88 | placeholder="请输入角色编号" |
| 89 | disabled |
| 90 | hidden={true} |
| 91 | rules={[ |
| 92 | { |
| 93 | required: false, |
| 94 | message: <FormattedMessage id="请输入角色编号!" defaultMessage="请输入角色编号!" />, |
| 95 | }, |
| 96 | ]} |
| 97 | /> |
| 98 | <ProFormText |
| 99 | name="roleName" |
| 100 | label={intl.formatMessage({ |
| 101 | id: 'system.role.role_name', |
| 102 | defaultMessage: '角色名称', |
| 103 | })} |
| 104 | placeholder="请输入角色名称" |
| 105 | rules={[ |
| 106 | { |
| 107 | required: true, |
| 108 | message: <FormattedMessage id="请输入角色名称!" defaultMessage="请输入角色名称!" />, |
| 109 | }, |
| 110 | ]} |
| 111 | /> |
| 112 | <ProFormText |
| 113 | name="roleKey" |
| 114 | label={intl.formatMessage({ |
| 115 | id: 'system.role.role_key', |
| 116 | defaultMessage: '权限字符串', |
| 117 | })} |
| 118 | placeholder="请输入角色权限字符串" |
| 119 | rules={[ |
| 120 | { |
| 121 | required: true, |
| 122 | message: <FormattedMessage id="请输入角色权限字符串!" defaultMessage="请输入角色权限字符串!" />, |
| 123 | }, |
| 124 | ]} |
| 125 | /> |
| 126 | <ProFormDigit |
| 127 | name="roleSort" |
| 128 | label={intl.formatMessage({ |
| 129 | id: 'system.role.role_sort', |
| 130 | defaultMessage: '显示顺序', |
| 131 | })} |
| 132 | placeholder="请输入显示顺序" |
| 133 | rules={[ |
| 134 | { |
| 135 | required: true, |
| 136 | message: <FormattedMessage id="请输入显示顺序!" defaultMessage="请输入显示顺序!" />, |
| 137 | }, |
| 138 | ]} |
| 139 | fieldProps = {{ |
| 140 | defaultValue: 1 |
| 141 | }} |
| 142 | /> |
| 143 | <ProFormRadio.Group |
| 144 | valueEnum={statusOptions} |
| 145 | name="status" |
| 146 | label={intl.formatMessage({ |
| 147 | id: 'system.role.status', |
| 148 | defaultMessage: '角色状态', |
| 149 | })} |
| 150 | placeholder="请输入角色状态" |
| 151 | rules={[ |
| 152 | { |
| 153 | required: true, |
| 154 | message: <FormattedMessage id="请输入角色状态!" defaultMessage="请输入角色状态!" />, |
| 155 | }, |
| 156 | ]} |
| 157 | fieldProps = {{ |
| 158 | defaultValue: "0" |
| 159 | }} |
| 160 | /> |
| 161 | <ProForm.Item |
| 162 | name="menuIds" |
| 163 | label={intl.formatMessage({ |
| 164 | id: 'system.role.auth', |
| 165 | defaultMessage: '菜单权限', |
| 166 | })} |
| 167 | > |
| 168 | <Tree |
| 169 | checkable={true} |
| 170 | multiple={true} |
| 171 | checkStrictly={true} |
| 172 | defaultExpandAll={false} |
| 173 | treeData={menuTree} |
| 174 | defaultCheckedKeys={menuCheckedKeys} |
| 175 | onCheck={(checkedKeys: any) => { |
| 176 | return setMenuIds(checkedKeys.checked); |
| 177 | }} |
| 178 | /> |
| 179 | </ProForm.Item> |
| 180 | <ProFormTextArea |
| 181 | name="remark" |
| 182 | label={intl.formatMessage({ |
| 183 | id: 'system.role.remark', |
| 184 | defaultMessage: '备注', |
| 185 | })} |
| 186 | placeholder="请输入备注" |
| 187 | rules={[ |
| 188 | { |
| 189 | required: false, |
| 190 | message: <FormattedMessage id="请输入备注!" defaultMessage="请输入备注!" />, |
| 191 | }, |
| 192 | ]} |
| 193 | /> |
| 194 | </ProForm> |
| 195 | </Modal> |
| 196 | ); |
| 197 | }; |
| 198 | |
| 199 | export default RoleForm; |