Skip to content

Commit

Permalink
feat: add literal union preset
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Jul 26, 2024
1 parent 9689e23 commit 0a769fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
4 changes: 1 addition & 3 deletions packages/filter/src/filter-rule/filter-data-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const FilterDataInput = ({
filterSchema,
onChange,
}: FilterDataInputProps) => {
const refCallback = () => null;
const Placeholder = useView("DataInputPlaceholder");
const requiredArguments = filterSchema
? getParametersExceptFirst(filterSchema)
Expand All @@ -27,12 +26,11 @@ export const FilterDataInput = ({
);

if (!requiredArguments) {
return <Placeholder ref={refCallback} />;
return <Placeholder />;
}

return (
<DataInputView
ref={refCallback}
rule={rule}
inputSchema={requiredArguments}
onChange={onChange}
Expand Down
38 changes: 37 additions & 1 deletion packages/filter/src/specs/preset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,48 @@ export const presetDataInputSpecs: DataInputViewSpec[] = [
...rule,
arguments: [value],
});
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<any>;
console.log(unionSchema, unionSchema.options);
const options = unionSchema.options.map((item: z.ZodLiteral<string>) => ({
label: item.value,
value: item.value,
}));
return (
<SelectView
options={options}
value={rule.arguments?.[0] as string}
onChange={(value) => {
onChange({
...rule,
arguments: [value],
});
}}
/>
);
},
},
] satisfies DataInputViewSpec[];

const ButtonView = forwardRef<
Expand Down

0 comments on commit 0a769fb

Please sign in to comment.