refactor(ui): useAntdForm
This commit is contained in:
@@ -1,27 +1,26 @@
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useDeepCompareEffect } from "ahooks";
|
||||
import { Form, Input, type FormInstance } from "antd";
|
||||
import { createSchemaFieldRule } from "antd-zod";
|
||||
import { z } from "zod";
|
||||
|
||||
import { useAntdForm } from "@/hooks";
|
||||
import { type GoDaddyAccessConfig } from "@/domain/access";
|
||||
|
||||
type AccessEditFormGoDaddyConfigModelType = Partial<GoDaddyAccessConfig>;
|
||||
type AccessEditFormGoDaddyConfigModelValues = Partial<GoDaddyAccessConfig>;
|
||||
|
||||
export type AccessEditFormGoDaddyConfigProps = {
|
||||
form: FormInstance;
|
||||
formName: string;
|
||||
disabled?: boolean;
|
||||
model?: AccessEditFormGoDaddyConfigModelType;
|
||||
onModelChange?: (model: AccessEditFormGoDaddyConfigModelType) => void;
|
||||
model?: AccessEditFormGoDaddyConfigModelValues;
|
||||
onModelChange?: (model: AccessEditFormGoDaddyConfigModelValues) => void;
|
||||
};
|
||||
|
||||
const initModel = () => {
|
||||
const initFormModel = (): AccessEditFormGoDaddyConfigModelValues => {
|
||||
return {
|
||||
apiKey: "",
|
||||
apiSecret: "",
|
||||
} as AccessEditFormGoDaddyConfigModelType;
|
||||
};
|
||||
};
|
||||
|
||||
const AccessEditFormGoDaddyConfig = ({ form, formName, disabled, model, onModelChange }: AccessEditFormGoDaddyConfigProps) => {
|
||||
@@ -40,18 +39,17 @@ const AccessEditFormGoDaddyConfig = ({ form, formName, disabled, model, onModelC
|
||||
.max(64, t("common.errmsg.string_max", { max: 64 })),
|
||||
});
|
||||
const formRule = createSchemaFieldRule(formSchema);
|
||||
const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({
|
||||
form: form,
|
||||
initialValues: model ?? initFormModel(),
|
||||
});
|
||||
|
||||
const [initialValues, setInitialValues] = useState<Partial<z.infer<typeof formSchema>>>(model ?? initModel());
|
||||
useDeepCompareEffect(() => {
|
||||
setInitialValues(model ?? initModel());
|
||||
}, [model]);
|
||||
|
||||
const handleFormChange = (_: unknown, fields: AccessEditFormGoDaddyConfigModelType) => {
|
||||
onModelChange?.(fields);
|
||||
const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => {
|
||||
onModelChange?.(values as AccessEditFormGoDaddyConfigModelValues);
|
||||
};
|
||||
|
||||
return (
|
||||
<Form form={form} disabled={disabled} initialValues={initialValues} layout="vertical" name={formName} onValuesChange={handleFormChange}>
|
||||
<Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}>
|
||||
<Form.Item
|
||||
name="apiKey"
|
||||
label={t("access.form.godaddy_api_key.label")}
|
||||
|
||||
Reference in New Issue
Block a user