Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip nulls from the form state type for maximum polaris compatability #699

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/react/spec/useActionFormController.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Form, TextField } from "@shopify/polaris";
import React from "react";
import { Controller, useActionForm } from "../src/useActionForm.js";
import { relatedProductsApi } from "./apis.js";

describe("useActionFormController", () => {
test("null is removed from possible form states to remain compatible with polaris", () => {
const Component = () => {
const { control, submit } = useActionForm(relatedProductsApi.user.create, {
defaultValues: { id: "123" },
});

return (
<Form onSubmit={submit}>
<Controller
name="email"
control={control}
render={({ field: { ref, ...fieldProps } }) => <TextField label="yay" autoComplete="off" {...fieldProps} />}
/>
</Form>
);
};

// this file just does type checks above
expect(true).toBe(true);
});
});
14 changes: 13 additions & 1 deletion packages/react/src/use-action-form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,25 @@ export type FormInput<InputT, Depth extends number = 9, CurrentDepth extends num
? { [K in keyof InputT]: FormInput<InputT[K], Depth, Increment<CurrentDepth>> }
: InputT | null | undefined;

/**
* Type helper to convert `null` to undefined recursively within the form values type
* Many Gadget users use Shopify Polaris as a design system, and many of its input components do not accept `null` values. For maximum compatibility with Polaris, we convert `null` to `undefined` within the form values type, knowing that the Polaris components handle both at runtime just fine.
*/
export type StripNulls<T> = T extends null
? undefined
: T extends (infer U)[]
? StripNulls<U>[]
: T extends object
? { [K in keyof T]: StripNulls<T[K]> }
: T;

pistachiobaby marked this conversation as resolved.
Show resolved Hide resolved
export type UseActionFormResult<
GivenOptions extends OptionsType,
SchemaT,
ActionFunc extends ActionFunction<GivenOptions, any, any, SchemaT, any> | GlobalActionFunction<any>,
FormVariables extends FieldValues,
FormContext = any
> = Omit<UseFormReturn<FormVariables & FormInput<ActionFunc["variablesType"]>, FormContext>, "handleSubmit" | "formState"> & {
> = Omit<UseFormReturn<FormVariables & StripNulls<FormInput<ActionFunc["variablesType"]>>, FormContext>, "handleSubmit" | "formState"> & {
formState: UseActionFormState<ActionFunc, FormVariables, FormContext> & { isReady: boolean };
/**
* Any error that occurred during initial data fetching or action submission
Expand Down