Fix: Fixed the issue where the debug form Switch component had no default value #3221 (#8662)

### What problem does this PR solve?

Fix: Fixed the issue where the debug form Switch component had no
default value #3221

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
balibabu
2025-07-04 12:21:00 +08:00
committed by GitHub
parent f8a6987f1e
commit 194e088d01

View File

@ -44,8 +44,6 @@ interface IProps {
btnText?: ReactNode; btnText?: ReactNode;
} }
const values = {};
const DebugContent = ({ const DebugContent = ({
parameters, parameters,
ok, ok,
@ -56,15 +54,20 @@ const DebugContent = ({
}: IProps) => { }: IProps) => {
const { t } = useTranslation(); const { t } = useTranslation();
const FormSchema = useMemo(() => { const formSchemaValues = useMemo(() => {
const obj = parameters.reduce<Record<string, z.ZodType>>( const obj = parameters.reduce<{
schema: Record<string, z.ZodType>;
values: Record<string, any>;
}>(
(pre, cur, idx) => { (pre, cur, idx) => {
const type = cur.type; const type = cur.type;
let fieldSchema; let fieldSchema;
let value;
if (StringFields.some((x) => x === type)) { if (StringFields.some((x) => x === type)) {
fieldSchema = z.string(); fieldSchema = z.string().trim().min(1);
} else if (type === BeginQueryType.Boolean) { } else if (type === BeginQueryType.Boolean) {
fieldSchema = z.boolean(); fieldSchema = z.boolean();
value = false;
} else if (type === BeginQueryType.Integer) { } else if (type === BeginQueryType.Integer) {
fieldSchema = z.coerce.number(); fieldSchema = z.coerce.number();
} else { } else {
@ -72,22 +75,25 @@ const DebugContent = ({
} }
if (cur.optional) { if (cur.optional) {
fieldSchema.optional(); fieldSchema = fieldSchema.optional();
} }
pre[idx.toString()] = fieldSchema; const index = idx.toString();
pre.schema[index] = fieldSchema;
pre.values[index] = value;
return pre; return pre;
}, },
{}, { schema: {}, values: {} },
); );
return z.object(obj); return { schema: z.object(obj.schema), values: obj.values };
}, [parameters]); }, [parameters]);
const form = useForm<z.infer<typeof FormSchema>>({ const form = useForm<z.infer<typeof formSchemaValues.schema>>({
defaultValues: values, defaultValues: formSchemaValues.values,
resolver: zodResolver(FormSchema), resolver: zodResolver(formSchemaValues.schema),
}); });
const submittable = true; const submittable = true;
@ -223,8 +229,7 @@ const DebugContent = ({
); );
const onSubmit = useCallback( const onSubmit = useCallback(
(values: z.infer<typeof FormSchema>) => { (values: z.infer<typeof formSchemaValues.schema>) => {
console.log('🚀 ~ values:', values);
const nextValues = Object.entries(values).map(([key, value]) => { const nextValues = Object.entries(values).map(([key, value]) => {
const item = parameters[Number(key)]; const item = parameters[Number(key)];
let nextValue = value; let nextValue = value;
@ -243,7 +248,7 @@ const DebugContent = ({
ok(nextValues); ok(nextValues);
}, },
[ok, parameters], [formSchemaValues, ok, parameters],
); );
return ( return (