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

Update "form.touched" to suport nested fields #413

Closed
dccarmo opened this issue Feb 8, 2018 · 5 comments
Closed

Update "form.touched" to suport nested fields #413

dccarmo opened this issue Feb 8, 2018 · 5 comments

Comments

@dccarmo
Copy link

dccarmo commented Feb 8, 2018

Related to #368

Bug, Feature, or Question?

Bug

Current Behavior

FormikTouched only allows one level of nesting. TypeScript complains if it's nested further:

[ts] Property 'name' does not exist on type 'boolean'.

Desired Behavior

With the "Deep State" update I'd think that nested fields would also work.

Suggested Solutions

Not sure if this is an implementation issue or a TypeScript definition issue.

Info

  • Formik Version: 0.11.7
  • OS: macOS
  • Node Version: 8.4.0
  • Package Manager and version: npm 5.6.0
@jaredpalmer
Copy link
Owner

This is a typescript issue.

@jaredpalmer
Copy link
Owner

Can you paste in your code that is through the bug?

@dccarmo
Copy link
Author

dccarmo commented Feb 8, 2018

Sure.

Here is the FormFields interface:

interface FormFields {
  business: {
    name: string;
  };
}

And then, in my Field component:

<Field
  name="business.name"
  render={({ field, form }: FieldProps<FormFields>) => (
    <div className="field">
      <div className="control">
        <input
          {...field}
          type="text"
          placeholder="Business Name"
          className={
            form.touched.business.name && form.errors.business.name
              ? 'input is-danger'
              : 'input'
          }
        />
      </div>
    </div>
  )}
/>

The TS error ([ts] Property 'name' does not exist on type 'boolean'.) is given on form.touched.business.name.

Looking at the type definition, it seems that form.errors.business.name won't work as well, because it has a anytype.

I tried searching for a way to get keys of nested objets the same way you're currently doing with keyof but didn't find anything, maybe it's not possible right now.

@jaredpalmer
Copy link
Owner

Correct way to handle this:

<Formik
      initialValues={{ business: { name: '' } }}
      onSubmit={values => console.log(values)}
      render={({
        values,
        handleChange,
        handleBlur,
      }: FormikProps<{ business: { name: string } }>) => (
        <Form>
          <Field
            name="business.name"
            render={({
              field,
              form,
            }: FieldProps<{ business: { name: string } }>) => (
              <div className="field">
                <div className="control">
                  <input
                    {...field}
                    type="text"
                    placeholder="Business Name"
                    className={
                      form.touched.business &&
                      form.touched.business.name &&
                      form.errors.business &&
                      form.errors.business.name
                        ? 'input is-danger'
                        : 'input'
                    }
                  />
                </div>
              </div>
            )}
          />
        </Form>
      )}
    />

@dccarmo
Copy link
Author

dccarmo commented Feb 8, 2018

Nice! Got it working with your PR. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants