Skip to content
Source
Sign inSign up
TanStack/form

[WIP] Shared Component Library Docs #825

Closedcrutchcorn(GitHub profile, opens in a new tab)wants to merge 8 commits intomain fromdemo-internal-componentsopened

This PR:

  • Adds an example of moving TanStack Form into your own shared component library
  • Adds the docs to explain what the example is doing
    1. ☁️ Nx Cloud Report

      We didn't find any information for the current pull request with the commit 2adadbf80b3c82802a83a53bba6fec2d09f54e97. Please verify you are running the latest version of the NxCloud runner.

      Check the Nx Cloud Source Control Integration documentation for more information.

      Alternatively, you can contact us at cloud-support@nrwl.io.


      Sent with 💌 from NxCloud.

    2. Review PR in StackBlitz Codeflow _Run & review this pull request in [StackBlitz Codeflow](https:///pr.new/TanStack/form/pull/825)._

      commit: bb26093

      @tanstack/angular-form

      npm i https://pkg.pr.new/@tanstack/angular-form@825

      @tanstack/form-core

      npm i https://pkg.pr.new/@tanstack/form-core@825

      @tanstack/lit-form

      npm i https://pkg.pr.new/@tanstack/lit-form@825

      @tanstack/react-form

      npm i https://pkg.pr.new/@tanstack/react-form@825

      @tanstack/solid-form

      npm i https://pkg.pr.new/@tanstack/solid-form@825

      @tanstack/valibot-form-adapter

      npm i https://pkg.pr.new/@tanstack/valibot-form-adapter@825

      @tanstack/vue-form

      npm i https://pkg.pr.new/@tanstack/vue-form@825

      @tanstack/yup-form-adapter

      npm i https://pkg.pr.new/@tanstack/yup-form-adapter@825

      @tanstack/zod-form-adapter

      npm i https://pkg.pr.new/@tanstack/zod-form-adapter@825


      templates

    3. Balastrong(GitHub profile, opens in a new tab) approved these changes

    4. One thing I am running into with this branch, field.state.value seems to always be string. If you have something else as the value you run into a world of pain. What is the best course of action for field values that are not string?

    5. One thing I am running into with this branch, field.state.value seems to always be string. If you have something else as the value you run into a world of pain. What is the best course of action for field values that are not string?

      How are you defining your component?

      function TextInputField<
        TFormData extends unknown,
        TName extends DeepKeyValueName<TFormData, string>,
      >

      Here you can change string to your data type

    6. Ended up doing this, which is stupid, but works.

      type ExpirationSelectFieldProps<
        Key extends string,
        TFormData extends { [K in Key]: ExpirationItem | null },
        TName extends DeepKeyValueName<TFormData, ExpirationItem>,
      > = Omit<FieldOptions<TFormData, TName>, 'name'> & {
        name: Key
        form: ReactFormApi<TFormData, any> & {
          state: FormState<TFormData>
        }
        selectProps?: Omit<ComponentProps<typeof Select>, 'onChange' | 'value'> & {
          label?: string
          placeholder?: string
          inputWrapperClassName?: string
        }
        symbol: string
      }
      
      export function ExpirationSelectField<
        Key extends string,
        TFormData extends { [K in Key]: ExpirationItem | null },
        TName extends DeepKeyValueName<TFormData, ExpirationItem>,
      >(props: ExpirationSelectFieldProps<Key, TFormData, TName>) { ... } 
    7. I was also working on integrating it more smoothly, i.e. abstracting the boiler plate and allowing to make custom components of it. This is my first POC, works for my side project now. Any feedback / suggestions / improvements are always welcome.

      Usage:

      // app/SomeForm.tsx
      import { FieldInput, Form } from "../lib/form/Form"
      export const SomeForm = () => {
      
          const form = useForm({
              defaultValues: {
                  name: '',
                  id: '',
              }
          })
      
          return (
              <Form onSubmit={form.handleSubmit}>
                  <div className="flex flex-col space-y-2">
                      <FieldInput formT={form} name="name" placeholder="Name" />
                      <FieldInput formT={form} name="id" placeholder="ID" />
                      <button type="submit">Add</button>
                  </div>
              </Form>
          )
      }

      Abstract form components created to reduce boiler plate and allow for custom custom form components with @tanstack/form:

      // lib/form/Form.tsx
      import { DeepKeys, FieldApi, useForm } from '@tanstack/react-form'
      
      export const Form: React.FC<React.FormHTMLAttributes<HTMLFormElement>> = ({
          children,
          ...props
      }) => {
          return (
              <form
                  {...props}
                  onSubmit={(e) => {
                      e.preventDefault()
                      e.stopPropagation()
                      if (props.onSubmit) {
                          props.onSubmit(e)
                      }
                  }}
              >
                  {children}
              </form>
          )
      }
      
      export const FieldInfo = ({ field }: { field: FieldApi<any, any, any, any> }) => {
          return (
              <>
                  {field.state.meta.isTouched && field.state.meta.errors.length ? (
                      <em className=" text-red-500 text-xs">{field.state.meta.errors.join(',')}</em>
                  ) : null}
                  {field.state.meta.isValidating ? 'Validating...' : null}
              </>
          )
      }
      
      export const FieldInput = <TFormData, TNames extends DeepKeys<TFormData>>({
          formT,
          name,
          ...props
      }: {
          formT: ReturnType<typeof useForm<TFormData, any>>
          name: TNames
      } & React.InputHTMLAttributes<HTMLInputElement>) => (
          <formT.Field<TNames, any, any> name={name}>
              {(field) => (
                  <>
                      <input
                          value={field.state.value}
                          onChange={(e) => field.handleChange(e.target.value)}
                          onBlur={field.handleBlur}
                          className="w-full rounded-md border border-gray-300 px-4 py-2 transition duration-200 ease-in-out focus:border-transparent focus:outline-none focus:ring-2 focus:ring-blue-500"
                          {...props}
                      />
                      <FieldInfo field={field} />
                  </>
              )}
          </formT.Field>
      )

      This works so far, but I might want to look into your solution @crutchcorn, with DeepKeyValueName, what's still going wrong (as it seems it's not merged)?

    8. examples/react/custom-component-wrapper/src/index.tsx line 23
      @@ -0,0 +3,21 @@
      3+import { useForm } from '@tanstack/react-form'
      4+import type {
      5+ DeepKeyValueName,
      6+ FieldOptions,
      7+ ReactFormApi,
      8+} from '@tanstack/react-form'
      9+
      10+/**
      11+ * Export these components to your design system or a dedicated component location
      12+ */
      13+interface TextInputFieldProps<
      14+ TFormData extends unknown,
      15+ TName extends DeepKeyValueName<TFormData, string>,
      16+> extends FieldOptions<TFormData, TName> {
      17+ form: ReactFormApi<TFormData, any>
      18+ // Your custom props
      19+ label: string
      20+}
      21+
      22+function TextInputField<
      23+ TFormData extends unknown,

      Does anyone know why extends unknown constrains field.name to string instead of string | number?

      @typescript-eslint/no-unnecessary-type-constraint and other sources I've found claim that an unconstrained generic type parameter already defaults to unknown, but this must not be true. Maybe some TS wizard will say "oh that's the extends unknown trick it fixes ..." 😅

      Before I found this issue I had some of the implementation done myself but couldn't work out why DeepKeys doesn't exclude number, among other TS issues.

      It's also a shame that form.Field<TName, any, string> doesn't work, because the types of field.state.value and field.handleChange are exactly where it would be useful to have TypeScript help.

      The issue comes from the way object keys are treated in distributive types.

      See this issue: https://github.com/microsoft/TypeScript/issues/61242

    9. There's an issue with DeepKeyValueName when a nested object is nullable:

        interface Fields {
          nested: {
            name: string
            labels: string[]
          } | null
        }
      
        const form = useForm<Fields>({
          defaultValues: {
            nested: { name: '', labels: [] },
          },
        })
      
        return <TextInputField form={form} name="nested.name" />
      Type '"nested.name"' is not assignable to type '`nested.labels[${number}]`'.
    10. @RazerM can you open a different GH issue for the failing type error you're running into there?

    11. @crutchcorn it's unique to DeepKeyValueName added in this PR, <form.Field name={...} /> doesn't have the issue.

    12. i've been adapting this branch into my project, and it's been working really well so far. one thing i've noticed: adding an array field to my form triggers a typescript error:

      Type instantiation is excessively deep and possibly infinite
    13. i've been adapting this branch into my project, and it's been working really well so far. one thing i've noticed: adding an array field to my form triggers a typescript error:

      Type instantiation is excessively deep and possibly infinite

      Did you use vs code? If so, I think vs code fails to infer the related types. Just pass a generic 'any' type to resolve that issue. image

    14. @epenflow yeah, i'm on cursor. had the same thought that my ide could be causing this, so i need to test with a different one to verify. i meant to delete my comment here since i'm using this branch, which i originally thought was related, but it's not. i don't think your fix will work in my case.

    15. Hey all! From the experimentation with this PR from many community contributors, it's become clear that this particular implantation was not the right direction for us.

      To remedy this, we've introduced an alternative API via a new PR here: https://github.com/TanStack/form/pull/1179

      This new API should have either substantially fewer or none of the same problems of typings as this one; all while allowing for more flexibility.

      Please review #1179 and let me know your thoughts there - thanks!

Sign in to join this conversation.