import { ModalForm, ProFormText, ProFormList, ProCard, ProFormSelect, ProFormCheckbox, ProFormDependency, } from '@ant-design/pro-components'; import { Col, Form, Row, message } from 'antd'; import React, { PropsWithChildren } from 'react'; import { addApi } from '../services'; import { getButtonSelections } from '../../../services'; const BUTTON_OPTIONS = [ { value: 'add', label: '新增' }, { value: 'update', label: '编辑' }, { value: 'delete', label: '删除' }, { value: 'switch', label: '启停' }, { value: 'import', label: '导入' }, { value: 'export', label: '导出' }, { value: 'view', label: '查看' }, ]; interface EditFormProps { title: string; type: 'default' | 'menu'; initialValues?: { menuName?: string; menuPath?: string; }; onOk?: () => void; } type ButtonFields = Partial<{ buttonName: string; perms: string; status: string; remark: string; prefix: boolean; }>; interface ModalFormProps { menuName: string; menuPath: string; buttonList: ButtonFields[]; buttons: string[]; } const defaultCol = { labelCol: { flex: '120px' }, }; const EditForm: React.FC> = ({ children, title, onOk, type, initialValues, }) => { const [form] = Form.useForm(); return ( width={1200} layout="horizontal" {...defaultCol} title={title} trigger={children as JSX.Element} form={form} autoFocusFirstInput modalProps={{ destroyOnClose: true, }} submitTimeout={2000} onOpenChange={(open) => { if (open) { if (type === 'menu') { form.setFieldsValue({ ...initialValues }); } } }} onFinish={async (values) => { const { buttonList: _buttonList, menuName, menuPath, buttons: _buttons, } = values || {}; const buttons: ButtonFields[] = (_buttons || []).map((_perms) => { const { label: buttonName = '' } = BUTTON_OPTIONS.find((it) => it.value === _perms) || {}; const perms = `${(menuPath || '') .replace(/^\//, '') .replace(/\//g, ':')}:${_perms}`; return { buttonName, perms, status: '1', remark: '', }; }); const buttonList = (_buttonList || []).map((button) => { const { status: _status, prefix, perms: _perms } = button || {}; const status = String(2 - Number(_status)); const perms = prefix ? `${(menuPath || '') .replace(/^\//, '') .replace(/\//g, ':')}:${_perms}` : _perms; return { ...button, status, perms, }; }); const hasMenuButtonList = await getButtonSelections({ menuPath, }); const _hasMenuButtonList = [...buttons, ...buttonList].filter( (item) => { return hasMenuButtonList.some((it) => it.value === item.perms); }, ); if (_hasMenuButtonList.length > 0) { const buttonNames = _hasMenuButtonList .map((item) => item.buttonName) .join('、'); message.error(`按钮${buttonNames}已存在!`); return false; } const buttonApis = [...buttons, ...buttonList].map((button) => { return addApi({ ...button, menuName, menuPath, }); }); if (buttonApis.length === 0) { message.error('按钮不能为空!'); return false; } await Promise.all(buttonApis); message.success('提交成功'); onOk?.(); return true; }} > {({ menuPath }) => { return ( 常规按钮会自动生成权限字符,规则为菜单路由+按钮名称,示例:administration:button:add } fieldProps={{ mode: 'multiple', options: BUTTON_OPTIONS.map((item) => { const value = `${(menuPath || '') .replace(/^\//, '') .replace(/\//g, ':')}:${item.value}`; return { ...item, label: `${item.label} - ${value}`, }; }), }} /> ); }} { return (
{listDom} {action}
); }} > 开启默认前缀,则会自动将页面路由拼接到权限字符前面。 } label="权限字符" rules={[{ required: true }]} >
); }; export default EditForm;