import { memo } from "react"; import { useTranslation } from "react-i18next"; import { FormOutlined as FormOutlinedIcon } from "@ant-design/icons"; import { Button, Form, type FormInstance, Input, Select, Space } from "antd"; import { createSchemaFieldRule } from "antd-zod"; import { z } from "zod"; import ModalForm from "@/components/ModalForm"; import MultipleInput from "@/components/MultipleInput"; import Show from "@/components/Show"; import { useAntdForm } from "@/hooks"; type DeployNodeConfigFormBaotaPanelSiteConfigFieldValues = Nullish<{ siteType: string; siteName?: string; siteNames?: string; }>; export type DeployNodeConfigFormBaotaPanelSiteConfigProps = { form: FormInstance; formName: string; disabled?: boolean; initialValues?: DeployNodeConfigFormBaotaPanelSiteConfigFieldValues; onValuesChange?: (values: DeployNodeConfigFormBaotaPanelSiteConfigFieldValues) => void; }; const SITE_TYPE_PHP = "php"; const SITE_TYPE_OTHER = "other"; const MULTIPLE_INPUT_DELIMITER = ";"; const initFormModel = (): DeployNodeConfigFormBaotaPanelSiteConfigFieldValues => { return { siteType: SITE_TYPE_OTHER, siteName: "", siteNames: "", }; }; const DeployNodeConfigFormBaotaPanelSiteConfig = ({ form: formInst, formName, disabled, initialValues, onValuesChange, }: DeployNodeConfigFormBaotaPanelSiteConfigProps) => { const { t } = useTranslation(); const formSchema = z.object({ siteType: z.union([z.literal(SITE_TYPE_PHP), z.literal(SITE_TYPE_OTHER)], { message: t("workflow_node.deploy.form.baotapanel_site_type.placeholder"), }), siteName: z .string() .nullish() .refine((v) => { if (fieldSiteType !== SITE_TYPE_PHP) return true; return !!v?.trim(); }, t("workflow_node.deploy.form.baotapanel_site_name.placeholder")), siteNames: z .string() .nullish() .refine((v) => { if (fieldSiteType !== SITE_TYPE_OTHER) return true; if (!v) return false; return String(v) .split(MULTIPLE_INPUT_DELIMITER) .every((e) => !!e.trim()); }, t("workflow_node.deploy.form.baotapanel_site_names.placeholder")), }); const formRule = createSchemaFieldRule(formSchema); const fieldSiteType = Form.useWatch("siteType", formInst); const fieldSiteNames = Form.useWatch("siteNames", formInst); const handleFormChange = (_: unknown, values: z.infer) => { onValuesChange?.(values); }; return (
} > } > { formInst.setFieldValue("siteNames", e.target.value); }} /> } onChange={(value) => { formInst.setFieldValue("siteNames", value); }} />
); }; const SiteNamesModalInput = memo(({ value, trigger, onChange }: { value?: string; trigger?: React.ReactNode; onChange?: (value: string) => void }) => { const { t } = useTranslation(); const formSchema = z.object({ siteNames: z.array(z.string()).refine((v) => { return v.every((e) => !!e?.trim()); }, t("workflow_node.deploy.form.baotapanel_site_names.errmsg.invalid")), }); const formRule = createSchemaFieldRule(formSchema); const { form: formInst, formProps } = useAntdForm({ name: "workflowNodeDeployConfigFormBaotaPanelSiteNamesModalInput", initialValues: { siteNames: value?.split(MULTIPLE_INPUT_DELIMITER) }, onSubmit: (values) => { onChange?.( values.siteNames .map((e) => e.trim()) .filter((e) => !!e) .join(MULTIPLE_INPUT_DELIMITER) ); }, }); return ( ); }); export default DeployNodeConfigFormBaotaPanelSiteConfig;