From 98b38de2456a7da1d370f538ec312ff812599684 Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Tue, 6 Aug 2024 19:59:16 +0800 Subject: [PATCH] refactor: add field schema to DataInputViewSpec --- .changeset/eleven-flowers-bow.md | 5 +++++ packages/filter/src/theme/hooks.tsx | 13 +++++++------ packages/filter/src/theme/types.ts | 5 ++++- packages/filter/src/views/filter-data-input.tsx | 7 +++++-- 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 .changeset/eleven-flowers-bow.md diff --git a/.changeset/eleven-flowers-bow.md b/.changeset/eleven-flowers-bow.md new file mode 100644 index 0000000..5168a06 --- /dev/null +++ b/.changeset/eleven-flowers-bow.md @@ -0,0 +1,5 @@ +--- +"@fn-sphere/filter": patch +--- + +Add field schema to the match function in the `DataInputViewSpec`. diff --git a/packages/filter/src/theme/hooks.tsx b/packages/filter/src/theme/hooks.tsx index 5a9f1b7..99f6bba 100644 --- a/packages/filter/src/theme/hooks.tsx +++ b/packages/filter/src/theme/hooks.tsx @@ -27,24 +27,25 @@ export const useView = (type: T) => { * Must be used within a `FilterThemeProvider` component. */ export const useDataInputView = ( - schema?: [] | [z.ZodTypeAny, ...z.ZodTypeAny[]], + fnParamsSchema?: [] | [z.ZodTypeAny, ...z.ZodTypeAny[]], + fieldSchema?: z.ZodTypeAny, ): ComponentType => { const dataInputViews = useView("dataInputViews"); - if (!schema) { + if (!fnParamsSchema) { return () => null; } const targetSpec = dataInputViews.find((spec) => { if (typeof spec.match === "function") { - return spec.match(schema); + return spec.match(fnParamsSchema, fieldSchema); } - return isSameType(z.tuple(spec.match), z.tuple(schema)); + return isSameType(z.tuple(spec.match), z.tuple(fnParamsSchema)); }); if (!targetSpec) { - console.error("no view spec found for", schema, dataInputViews); + console.error("no view spec found for", fnParamsSchema, dataInputViews); return () => (
No view spec found for  - {"<" + schema.map((i) => i._def.typeName).join(", ") + ">"} + {"<" + fnParamsSchema.map((i) => i._def.typeName).join(", ") + ">"}
); } diff --git a/packages/filter/src/theme/types.ts b/packages/filter/src/theme/types.ts index 0772bbb..c6b6354 100644 --- a/packages/filter/src/theme/types.ts +++ b/packages/filter/src/theme/types.ts @@ -28,7 +28,10 @@ export type DataInputViewSpec = { match: | [] | [z.ZodTypeAny, ...z.ZodTypeAny[]] - | ((parameterSchemas: [] | [z.ZodTypeAny, ...z.ZodTypeAny[]]) => boolean); + | (( + parameterSchemas: [] | [z.ZodTypeAny, ...z.ZodTypeAny[]], + fieldSchema?: z.ZodTypeAny, + ) => boolean); view: ComponentType; }; diff --git a/packages/filter/src/views/filter-data-input.tsx b/packages/filter/src/views/filter-data-input.tsx index 13eb548..3eca601 100644 --- a/packages/filter/src/views/filter-data-input.tsx +++ b/packages/filter/src/views/filter-data-input.tsx @@ -10,11 +10,14 @@ export type DataInputProps = { export const FilterDataInput = forwardRef( ({ rule }, ref) => { const { Input: InputView } = useView("components"); - const { selectedFilter, updateRule } = useFilterRule(rule); + const { selectedField, selectedFilter, updateRule } = useFilterRule(rule); const requiredArguments = selectedFilter ? getParametersExceptFirst(selectedFilter) : undefined; - const DataInputView = useDataInputView(requiredArguments); + const DataInputView = useDataInputView( + requiredArguments, + selectedField?.fieldSchema, + ); const updateInput = (value: unknown[]) => { updateRule({