fix: 修复编辑表单打开后展示数据错误的问题
This commit is contained in:
@@ -10,6 +10,7 @@ export default function AddRoleDialogue({ form }: AddRoleDialogueProps) {
|
|||||||
<RoleDisplayForm
|
<RoleDisplayForm
|
||||||
form={form}
|
form={form}
|
||||||
initialValues={{
|
initialValues={{
|
||||||
|
id: null,
|
||||||
name: "",
|
name: "",
|
||||||
code: "",
|
code: "",
|
||||||
sort: 0,
|
sort: 0,
|
||||||
@@ -17,6 +18,7 @@ export default function AddRoleDialogue({ form }: AddRoleDialogueProps) {
|
|||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
status: "ACTIVE",
|
status: "ACTIVE",
|
||||||
}}
|
}}
|
||||||
|
mode="add"
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import type { FormInstance } from "antd"
|
import type { FormInstance } from "antd"
|
||||||
import RoleDisplayForm, { type RoleFormValues } from "@/components/role-display-form"
|
import RoleDisplayForm, { type RoleFormValues } from "@/components/role-display-form"
|
||||||
|
import type { Role } from "@/types/entity"
|
||||||
|
|
||||||
export interface EditRoleDialogueProps {
|
export interface EditRoleDialogueProps {
|
||||||
form: FormInstance<RoleFormValues>
|
form: FormInstance<RoleFormValues>
|
||||||
@@ -7,5 +8,5 @@ export interface EditRoleDialogueProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function EditRoleDialogue({ form, initialValues }: EditRoleDialogueProps) {
|
export default function EditRoleDialogue({ form, initialValues }: EditRoleDialogueProps) {
|
||||||
return <RoleDisplayForm form={form} initialValues={initialValues} />
|
return <RoleDisplayForm form={form} initialValues={initialValues} mode="edit" />
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { App, Form, type FormInstance, Input, InputNumber, Select, Switch } from "antd"
|
import { App, Form, type FormInstance, Input, InputNumber, Select, Switch } from "antd"
|
||||||
import { type Status, StatusOptions } from "@/types/constant"
|
import { type Status, StatusOptions } from "@/types/constant"
|
||||||
|
import type { FormMode } from "@/types/form"
|
||||||
|
import { useEffect, useMemo } from "react"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Role form values.
|
* Role form values.
|
||||||
*/
|
*/
|
||||||
export interface RoleFormValues {
|
export interface RoleFormValues {
|
||||||
|
id: number | string | null
|
||||||
name: string
|
name: string
|
||||||
code: string
|
code: string
|
||||||
sort: number
|
sort: number
|
||||||
@@ -18,12 +21,22 @@ export interface RoleFormValues {
|
|||||||
*/
|
*/
|
||||||
export interface RoleDisplayFormProps {
|
export interface RoleDisplayFormProps {
|
||||||
initialValues?: RoleFormValues
|
initialValues?: RoleFormValues
|
||||||
isEditing?: boolean
|
|
||||||
form: FormInstance<RoleFormValues>
|
form: FormInstance<RoleFormValues>
|
||||||
isAdding?: boolean
|
mode: FormMode
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function RoleDisplayForm({ initialValues, form }: RoleDisplayFormProps) {
|
export default function RoleDisplayForm({ initialValues, form, mode }: RoleDisplayFormProps) {
|
||||||
|
const isEditing = useMemo<boolean>(() => mode == "edit", [mode])
|
||||||
|
|
||||||
|
// Initialise form values
|
||||||
|
useEffect(() => {
|
||||||
|
if (initialValues) {
|
||||||
|
form.setFieldsValue(initialValues)
|
||||||
|
} else {
|
||||||
|
form.resetFields()
|
||||||
|
}
|
||||||
|
}, [initialValues, form])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form<RoleFormValues>
|
<Form<RoleFormValues>
|
||||||
form={form}
|
form={form}
|
||||||
@@ -31,6 +44,9 @@ export default function RoleDisplayForm({ initialValues, form }: RoleDisplayForm
|
|||||||
layout="vertical"
|
layout="vertical"
|
||||||
labelAlign="right"
|
labelAlign="right"
|
||||||
validateTrigger="onBlur">
|
validateTrigger="onBlur">
|
||||||
|
<Form.Item<RoleFormValues> label="角色编号" hidden={!isEditing} name="id">
|
||||||
|
<Input disabled />
|
||||||
|
</Form.Item>
|
||||||
<Form.Item<RoleFormValues>
|
<Form.Item<RoleFormValues>
|
||||||
label="角色名称"
|
label="角色名称"
|
||||||
name="name"
|
name="name"
|
||||||
@@ -56,7 +72,7 @@ export default function RoleDisplayForm({ initialValues, form }: RoleDisplayForm
|
|||||||
<InputNumber />
|
<InputNumber />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item<RoleFormValues> label="是否为默认角色" name="defaultValue">
|
<Form.Item<RoleFormValues> label="是否为默认角色" name="defaultValue">
|
||||||
<Switch />
|
<Switch disabled={isEditing}/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item<RoleFormValues> label="角色状态" name="status">
|
<Form.Item<RoleFormValues> label="角色状态" name="status">
|
||||||
<Select<Status> options={StatusOptions} />
|
<Select<Status> options={StatusOptions} />
|
||||||
|
|||||||
+45
-1
@@ -17,6 +17,8 @@ import type { QueryRoleForm } from "@/types/form"
|
|||||||
import type { Status } from "@/types/constant"
|
import type { Status } from "@/types/constant"
|
||||||
import AddRoleDialogue from "@/components/add-role-dialogue"
|
import AddRoleDialogue from "@/components/add-role-dialogue"
|
||||||
import type { RoleFormValues } from "@/components/role-display-form"
|
import type { RoleFormValues } from "@/components/role-display-form"
|
||||||
|
import EditRoleDialogue from "@/components/edit-role-dialogue"
|
||||||
|
import { addRole } from "@/api/role"
|
||||||
|
|
||||||
export default function RolePage() {
|
export default function RolePage() {
|
||||||
const { message, modal } = App.useApp()
|
const { message, modal } = App.useApp()
|
||||||
@@ -94,6 +96,48 @@ export default function RolePage() {
|
|||||||
console.error("用户取消添加角色")
|
console.error("用户取消添加角色")
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
.finally(() => {
|
||||||
|
addRoleForm.resetFields()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const onEditRoleFinish = async () => {
|
||||||
|
try {
|
||||||
|
const values = await editRoleForm.validateFields()
|
||||||
|
console.log(values)
|
||||||
|
// await RoleApi.addRole(values)
|
||||||
|
void message.success(`角色 ${values.name} 修改成功`)
|
||||||
|
return true
|
||||||
|
} catch (error: unknown) {
|
||||||
|
if (error instanceof Error && error.message.includes("Validation Failed")) {
|
||||||
|
return false
|
||||||
|
} else if (axios.isAxiosError<GeneralErrorResponse>(error)) {
|
||||||
|
void message.error(error.response?.data.message ?? "创建失败,请稍后再试")
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleEditRole = (role: Role) => {
|
||||||
|
modal
|
||||||
|
.confirm({
|
||||||
|
title: "修改用户",
|
||||||
|
content: <EditRoleDialogue form={editRoleForm} initialValues={role} />,
|
||||||
|
width: 600,
|
||||||
|
onOk: onEditRoleFinish,
|
||||||
|
})
|
||||||
|
.then(
|
||||||
|
() => {
|
||||||
|
const formValues = queryForm.getFieldsValue()
|
||||||
|
queryRoles(pageNum, pageSize, formValues)
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
console.error("用户取消添加角色")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.finally(() => {
|
||||||
|
editRoleForm.resetFields()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -194,7 +238,7 @@ export default function RolePage() {
|
|||||||
render: (role: Role) => (
|
render: (role: Role) => (
|
||||||
<>
|
<>
|
||||||
<Space.Compact>
|
<Space.Compact>
|
||||||
<Button variant="solid" onClick={() => {}}>
|
<Button variant="solid" onClick={() => handleEditRole(role)}>
|
||||||
修改
|
修改
|
||||||
</Button>
|
</Button>
|
||||||
<Button variant="solid" danger>
|
<Button variant="solid" danger>
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import type { CountryCode as RegionAbbreviation } from "libphonenumber-js"
|
|||||||
import type { Status } from "@/types/constant"
|
import type { Status } from "@/types/constant"
|
||||||
import type { Dayjs } from "dayjs"
|
import type { Dayjs } from "dayjs"
|
||||||
|
|
||||||
|
export type FormMode = "add" | "edit"
|
||||||
|
|
||||||
export interface UserFormValues extends Omit<
|
export interface UserFormValues extends Omit<
|
||||||
User,
|
User,
|
||||||
"id" | "password" | "regionAbbreviation" | "departmentId" | "positionId" | "createdAt" | "updatedAt"
|
"id" | "password" | "regionAbbreviation" | "departmentId" | "positionId" | "createdAt" | "updatedAt"
|
||||||
|
|||||||
Reference in New Issue
Block a user