refactor(ui): useAntdForm
This commit is contained in:
@@ -7,25 +7,26 @@ import { createSchemaFieldRule } from "antd-zod";
|
||||
import { z } from "zod";
|
||||
import { Upload as UploadIcon } from "lucide-react";
|
||||
|
||||
import { useAntdForm } from "@/hooks";
|
||||
import { type SSHAccessConfig } from "@/domain/access";
|
||||
import { readFileContent } from "@/utils/file";
|
||||
|
||||
type AccessEditFormSSHConfigModelType = Partial<SSHAccessConfig>;
|
||||
type AccessEditFormSSHConfigModelValues = Partial<SSHAccessConfig>;
|
||||
|
||||
export type AccessEditFormSSHConfigProps = {
|
||||
form: FormInstance;
|
||||
formName: string;
|
||||
disabled?: boolean;
|
||||
model?: AccessEditFormSSHConfigModelType;
|
||||
onModelChange?: (model: AccessEditFormSSHConfigModelType) => void;
|
||||
model?: AccessEditFormSSHConfigModelValues;
|
||||
onModelChange?: (model: AccessEditFormSSHConfigModelValues) => void;
|
||||
};
|
||||
|
||||
const initModel = () => {
|
||||
const initFormModel = (): AccessEditFormSSHConfigModelValues => {
|
||||
return {
|
||||
host: "127.0.0.1",
|
||||
port: 22,
|
||||
username: "root",
|
||||
} as AccessEditFormSSHConfigModelType;
|
||||
};
|
||||
};
|
||||
|
||||
const AccessEditFormSSHConfig = ({ form, formName, disabled, model, onModelChange }: AccessEditFormSSHConfigProps) => {
|
||||
@@ -71,21 +72,21 @@ const AccessEditFormSSHConfig = ({ form, formName, disabled, model, onModelChang
|
||||
.and(z.string().refine((v) => !v || form.getFieldValue("key"), { message: t("access.form.ssh_key.placeholder") })),
|
||||
});
|
||||
const formRule = createSchemaFieldRule(formSchema);
|
||||
const formInst = form as FormInstance<z.infer<typeof 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());
|
||||
const [keyFileList, setKeyFileList] = useState<UploadFile[]>([]);
|
||||
useDeepCompareEffect(() => {
|
||||
setInitialValues(model ?? initModel());
|
||||
setKeyFileList(model?.key?.trim() ? [{ uid: "-1", name: "sshkey", status: "done" }] : []);
|
||||
}, [model]);
|
||||
|
||||
const [keyFileList, setKeyFileList] = useState<UploadFile[]>([]);
|
||||
|
||||
const handleFormChange = (_: unknown, fields: AccessEditFormSSHConfigModelType) => {
|
||||
onModelChange?.(fields);
|
||||
const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => {
|
||||
onModelChange?.(values as AccessEditFormSSHConfigModelValues);
|
||||
};
|
||||
|
||||
const handleUploadChange: UploadProps["onChange"] = async ({ file }) => {
|
||||
const handleKeyFileChange: UploadProps["onChange"] = async ({ file }) => {
|
||||
if (file && file.status !== "removed") {
|
||||
formInst.setFieldValue("key", (await readFileContent(file.originFileObj ?? (file as unknown as File))).trim());
|
||||
setKeyFileList([file]);
|
||||
@@ -98,7 +99,7 @@ const AccessEditFormSSHConfig = ({ form, formName, disabled, model, onModelChang
|
||||
};
|
||||
|
||||
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}>
|
||||
<div className="flex space-x-2">
|
||||
<div className="w-2/3">
|
||||
<Form.Item name="host" label={t("access.form.ssh_host.label")} rules={[formRule]}>
|
||||
@@ -138,7 +139,7 @@ const AccessEditFormSSHConfig = ({ form, formName, disabled, model, onModelChang
|
||||
<Input.TextArea autoComplete="new-password" hidden placeholder={t("access.form.ssh_key.placeholder")} value={formInst.getFieldValue("key")} />
|
||||
</Form.Item>
|
||||
<Form.Item label={t("access.form.ssh_key.label")} tooltip={<span dangerouslySetInnerHTML={{ __html: t("access.form.ssh_key.tooltip") }}></span>}>
|
||||
<Upload beforeUpload={() => false} fileList={keyFileList} maxCount={1} onChange={handleUploadChange}>
|
||||
<Upload beforeUpload={() => false} fileList={keyFileList} maxCount={1} onChange={handleKeyFileChange}>
|
||||
<Button icon={<UploadIcon size={16} />}>{t("access.form.ssh_key.upload")}</Button>
|
||||
</Upload>
|
||||
</Form.Item>
|
||||
|
||||
Reference in New Issue
Block a user