From 0a769fbd488e6d4a59e16f4708fd421a44048467 Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Fri, 26 Jul 2024 20:28:18 +0800 Subject: [PATCH] feat: add literal union preset --- .../src/filter-rule/filter-data-input.tsx | 4 +- packages/filter/src/specs/preset.tsx | 38 ++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/packages/filter/src/filter-rule/filter-data-input.tsx b/packages/filter/src/filter-rule/filter-data-input.tsx index 464dfcc..f47a249 100644 --- a/packages/filter/src/filter-rule/filter-data-input.tsx +++ b/packages/filter/src/filter-rule/filter-data-input.tsx @@ -17,7 +17,6 @@ export const FilterDataInput = ({ filterSchema, onChange, }: FilterDataInputProps) => { - const refCallback = () => null; const Placeholder = useView("DataInputPlaceholder"); const requiredArguments = filterSchema ? getParametersExceptFirst(filterSchema) @@ -27,12 +26,11 @@ export const FilterDataInput = ({ ); if (!requiredArguments) { - return ; + return ; } return ( ); }), }, + { + name: "literal union", + match: (schema) => { + if (schema.items.length === 0 || schema.items.length > 1) { + return false; + } + const [item] = schema.items; + const isUnion = item instanceof z.ZodUnion; + if (!isUnion) { + return false; + } + return item.options.every( + (option: unknown) => option instanceof z.ZodLiteral, + ); + }, + view: ({ inputSchema, rule, onChange }) => { + const SelectView = useView("Select"); + const unionSchema = inputSchema.items[0] as z.ZodUnion; + console.log(unionSchema, unionSchema.options); + const options = unionSchema.options.map((item: z.ZodLiteral) => ({ + label: item.value, + value: item.value, + })); + return ( + { + onChange({ + ...rule, + arguments: [value], + }); + }} + /> + ); + }, + }, ] satisfies DataInputViewSpec[]; const ButtonView = forwardRef<