Fix: Add a no-data filter condition to MetaData (#12189)

### What problem does this PR solve?

Fix: Add a no-data filter condition to MetaData

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
chanx
2025-12-25 10:42:34 +08:00
committed by Jin Hai
parent 02b976ffa4
commit 89ea760e67
8 changed files with 126 additions and 58 deletions

View File

@ -12,7 +12,7 @@ import {
useState,
} from 'react';
import { FieldPath, useForm } from 'react-hook-form';
import { ZodArray, ZodString, z } from 'zod';
import { z } from 'zod';
import { Button } from '@/components/ui/button';
@ -71,34 +71,37 @@ function CheckboxFormMultiple({
}, {});
}, [resolvedFilters]);
// const FormSchema = useMemo(() => {
// if (resolvedFilters.length === 0) {
// return z.object({});
// }
// return z.object(
// resolvedFilters.reduce<
// Record<
// string,
// ZodArray<ZodString, 'many'> | z.ZodObject<any> | z.ZodOptional<any>
// >
// >((pre, cur) => {
// const hasNested = cur.list?.some(
// (item) => item.list && item.list.length > 0,
// );
// if (hasNested) {
// pre[cur.field] = z
// .record(z.string(), z.array(z.string().optional()).optional())
// .optional();
// } else {
// pre[cur.field] = z.array(z.string().optional()).optional();
// }
// return pre;
// }, {}),
// );
// }, [resolvedFilters]);
const FormSchema = useMemo(() => {
if (resolvedFilters.length === 0) {
return z.object({});
}
return z.object(
resolvedFilters.reduce<
Record<
string,
ZodArray<ZodString, 'many'> | z.ZodObject<any> | z.ZodOptional<any>
>
>((pre, cur) => {
const hasNested = cur.list?.some(
(item) => item.list && item.list.length > 0,
);
if (hasNested) {
pre[cur.field] = z
.record(z.string(), z.array(z.string().optional()).optional())
.optional();
} else {
pre[cur.field] = z.array(z.string().optional()).optional();
}
return pre;
}, {}),
);
}, [resolvedFilters]);
return z.object({});
}, []);
const form = useForm<z.infer<typeof FormSchema>>({
resolver: resolvedFilters.length > 0 ? zodResolver(FormSchema) : undefined,