提交 8d5f01e8 作者: 郁骅焌

角色管理修改

上级 ebf75992
...@@ -11,9 +11,7 @@ import UpdateForm from './components/UpdateForm' ...@@ -11,9 +11,7 @@ import UpdateForm from './components/UpdateForm'
// 详情数据 // 详情数据
const initDetail = { const initDetail = {
id: null, id: null
field1: '',
field2: '',
} }
const CrudList = () => { const CrudList = () => {
......
import React, { useState, useRef, useEffect, useImperativeHandle } from 'react'
import { Form, Input, InputNumber, Select, AutoComplete } from 'antd'
import { SageForm } from '@/components/Common'
const CreateForm = (props, ref) => {
const formRef = useRef()
const onFinish = (values) => {
if (props.onFinish) {
props.onFinish(values)
}
}
const onFinishFailed = ({ values }) => {
console.log(values)
}
// 表单字段设置
const formFields = [
{
name: 'roleName',
label: '角色名',
type: 'input',
rules: [{ required: true }]
},
{
name: 'remarks',
label: '备注',
type: 'textarea'
}
]
// 暴露外部方法
useImperativeHandle(ref, () => ({
submit: () => formRef.current.submit(),
validateFields: (nameList) => formRef.current.validateFields(nameList),
getFieldsValue: (nameList) => formRef.current.getFieldsValue(nameList),
setFieldsValue: (values) => formRef.current.setFieldsValue(values),
resetFields: (fields) => formRef.current.resetFields(fields)
}))
return (
<SageForm
ref={formRef}
formFields={formFields}
onFinish={onFinish}
onFinishFailed={onFinishFailed}
/>
)
}
export default React.forwardRef(CreateForm)
import React, { useState, useRef, useEffect, useImperativeHandle } from 'react'
import { Form, Input, InputNumber, Select, AutoComplete } from 'antd'
import { SageForm } from '@/components/Common'
const UpdateForm = (props, ref) => {
const formRef = useRef()
const onFinish = (values) => {
if (props.onFinish) {
props.onFinish(values)
}
}
const onFinishFailed = ({ values }) => {
console.log(values)
}
// 表单字段设置
const formFields = [
{
name: 'roleName',
label: '角色名',
type: 'input',
rules: [{ required: true }]
},
{
name: 'remarks',
label: '备注',
type: 'textarea'
}
]
// 暴露外部方法
useImperativeHandle(ref, () => ({
submit: () => formRef.current.submit(),
validateFields: (nameList) => formRef.current.validateFields(nameList),
getFieldsValue: (nameList) => formRef.current.getFieldsValue(nameList),
setFieldsValue: (values) => formRef.current.setFieldsValue(values),
resetFields: (fields) => formRef.current.resetFields(fields)
}))
return (
<SageForm
ref={formRef}
formFields={formFields}
onFinish={onFinish}
onFinishFailed={onFinishFailed}
/>
)
}
export default React.forwardRef(UpdateForm)
...@@ -5,6 +5,9 @@ import { SageLayoutLR, SageTable, SageModal, SageForm, SageButton, sageMessage, ...@@ -5,6 +5,9 @@ import { SageLayoutLR, SageTable, SageModal, SageForm, SageButton, sageMessage,
import { MenuTree } from '@/components/Business' import { MenuTree } from '@/components/Business'
import { PlusOutlined, CheckOutlined } from '@ant-design/icons'; import { PlusOutlined, CheckOutlined } from '@ant-design/icons';
import { queryRule, updateRule, addRule, removeRule, getRuleDetail } from './service'; import { queryRule, updateRule, addRule, removeRule, getRuleDetail } from './service';
import CreateForm from './components/CreateForm'
import UpdateForm from './components/UpdateForm'
const initDetail = { const initDetail = {
id: null, id: null,
...@@ -17,12 +20,14 @@ const TableList = () => { ...@@ -17,12 +20,14 @@ const TableList = () => {
// 状态 // 状态
const [detail, setDetail] = useState(initDetail) // 详情 const [detail, setDetail] = useState(initDetail) // 详情
const [status, setStatus] = useState('') // 状态 1、add 2、update 3、detail const [status, setStatus] = useState('') // 状态 1、add 2、update 3、detail
const [modalLoading, setModalLoading] = useState(false) // 窗口loading
const [role, setRole] = useState({}) // 菜单分配对象 const [role, setRole] = useState({}) // 菜单分配对象
// ref对象 // ref对象
const tableRef = useRef(); const tableRef = useRef();
const modalRef = useRef(); const modalRef = useRef();
const formRef = useRef(); const createFormRef = useRef();
const updateFormRef = useRef();
const menutreeRef = useRef(); const menutreeRef = useRef();
// 查询条件 // 查询条件
...@@ -69,7 +74,7 @@ const TableList = () => { ...@@ -69,7 +74,7 @@ const TableList = () => {
modalRef.current.setTitle('编辑角色') modalRef.current.setTitle('编辑角色')
modalRef.current.setVisible(true) modalRef.current.setVisible(true)
formRef.current.setFieldsValue({ updateFormRef.current.setFieldsValue({
roleName: roleData.roleName, roleName: roleData.roleName,
remarks: roleData.remarks remarks: roleData.remarks
}) })
...@@ -149,8 +154,8 @@ const TableList = () => { ...@@ -149,8 +154,8 @@ const TableList = () => {
setDetail(initDetail) setDetail(initDetail)
modalRef.current.setTitle('新增角色') modalRef.current.setTitle('新增角色')
modalRef.current.setVisible(true) modalRef.current.setVisible(true)
if (formRef.current) { if (createFormRef.current) {
formRef.current.setFieldsValue(initDetail) createFormRef.current.resetFields()
} }
} }
} }
...@@ -169,15 +174,20 @@ const TableList = () => { ...@@ -169,15 +174,20 @@ const TableList = () => {
// 窗口确认按钮 // 窗口确认按钮
const onOk = async () => { const onOk = async () => {
if (formRef.current) { if (status === 'add' && createFormRef.current) {
formRef.current.submit(); createFormRef.current.submit();
}
if (status === 'update' && updateFormRef.current) {
updateFormRef.current.submit();
} }
} }
// 表单提交成功 // 表单提交成功
const onFinish = async (values) => { const onFinish = async (values) => {
const formData = Object.assign({}, values) const formData = Object.assign({}, values)
let res = null let res = null
setModalLoading(true)
switch (status) { switch (status) {
case 'add': case 'add':
res = await addRule(formData) res = await addRule(formData)
...@@ -189,6 +199,7 @@ const TableList = () => { ...@@ -189,6 +199,7 @@ const TableList = () => {
default: default:
break; break;
} }
setModalLoading(false)
if (res.isSuccess) { if (res.isSuccess) {
sageMessage.success('保存成功') sageMessage.success('保存成功')
...@@ -242,12 +253,23 @@ const TableList = () => { ...@@ -242,12 +253,23 @@ const TableList = () => {
<SageModal <SageModal
ref={modalRef} ref={modalRef}
onOk={onOk} onOk={onOk}
confirmLoading={modalLoading}
> >
<SageForm {
ref={formRef} status === 'add' ?
initialValues={detail} <CreateForm
ref={createFormRef}
onFinish={onFinish} onFinish={onFinish}
/> /> : null
}
{
status === 'update' ?
<UpdateForm
ref={updateFormRef}
detail={detail}
onFinish={onFinish}
/> : null
}
</SageModal> </SageModal>
</PageHeaderWrapper> </PageHeaderWrapper>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论