formik icon indicating copy to clipboard operation
formik copied to clipboard

allow dot path & bracket path for setFieldValue, setFieldTouched, & setFieldError

Open timc13 opened this issue 8 years ago • 6 comments

Bug

The exposed interface for setFieldValue, setFieldTouched, & setFieldError do not allow dot path names

Current Behavior

only first level field names can be set.

Desired Behavior

allow field names that contain dot path as well as bracket path.

Suggested Solutions

change the keyof Values type for the field argument to string

Info

  • Formik Version: 0.11.0-beta.1
  • OS: MacOS High Sierra 10.13.2
  • Node Version: 8.9.4
  • Package Manager and version: npm 5.6.0

timc13 avatar Jan 23 '18 04:01 timc13

along the same vein, the values in type FormikTouched should not be typed to boolean, as this only allows us to access top level touched fields. (e.g. touched.parent, but not touched.parent.child)

timc13 avatar Jan 23 '18 19:01 timc13

actually, this is resolved and this improvement can be closed :)

screen shot 2018-01-29 at 8 52 30 pm

screen shot 2018-01-29 at 8 48 38 pm

sergii-azizov avatar Jan 29 '18 18:01 sergii-azizov

This has been working in javascript, but not Typescript. In Typescript, we have to cast to any:

setFieldTouched('property.child' as any)

I'm just proposing that we change the signature of FormikActions<Values> from:

setFieldValue: (field: keyof Values, value: any) => void;
setFieldError: (field: keyof Values, message: string) => void;
setFieldTouched: (field: keyof Values, isTouched?: boolean) => void;

to:

setFieldValue: (field: string, value: any) => void;
setFieldError: (field: string, message: string) => void;
setFieldTouched: (field: string, isTouched?: boolean) => void;

timc13 avatar Jan 29 '18 19:01 timc13

@weswigham is there is good way to do this other than just a string?

jaredpalmer avatar Jan 29 '18 21:01 jaredpalmer

I'd just add a second overload that takes a string argument for when you pass something that isn't a direct key. There's no way in the typesystem today to concatenate strings, so there's no way to represent a full object path string as a type.

weswigham avatar Jan 29 '18 23:01 weswigham

please do the same for FormikTouched<Values>. same problem - we can't access anything beyond top level values in touched

timc13 avatar Jan 29 '18 23:01 timc13