Skip to content

Commit

Permalink
refactor: add field schema to DataInputViewSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Aug 6, 2024
1 parent 75feec4 commit 98b38de
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-flowers-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fn-sphere/filter": patch
---

Add field schema to the match function in the `DataInputViewSpec`.
13 changes: 7 additions & 6 deletions packages/filter/src/theme/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,25 @@ export const useView = <T extends keyof ThemeSpec>(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<DataInputViewProps> => {
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 () => (
<div>
No view spec found for&nbsp;
{"<" + schema.map((i) => i._def.typeName).join(", ") + ">"}
{"<" + fnParamsSchema.map((i) => i._def.typeName).join(", ") + ">"}
</div>
);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/filter/src/theme/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataInputViewProps>;
};

Expand Down
7 changes: 5 additions & 2 deletions packages/filter/src/views/filter-data-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ export type DataInputProps = {
export const FilterDataInput = forwardRef<HTMLInputElement, DataInputProps>(
({ 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({
Expand Down

0 comments on commit 98b38de

Please sign in to comment.