Allow handling of non-native inputs
See original GitHub issue🚀 Feature request
It should be possible to handle inputs, which are not backed by native input (input, textarea, select).
Current Behavior
Both formik 1.5.x and 2.x define:
// excerpt interface FormikHandlers
interface FormikHandlers {
/** Classic React change handler, keyed by input name */
handleChange(e: React.ChangeEvent<any>): void;
/** Preact-like linkState. Will return a handleChange function. */
handleChange<T = string | React.ChangeEvent<any>>(
field: T
): T extends React.ChangeEvent<any>
? void
: ((e: string | React.ChangeEvent<any>) => void);
}
This requires me to either provide signal in preact linkstate style or as native dom change event. I can not create a pure JS element without linkstate or native dom input, which simply notifies value change passing it to onChange
(aka handleChange
API).
Desired Behavior
It should be possible to notify onChange
with pure updated value.
Suggested Solution
Typing:
AFAIK it can be tricky to make this BC to preact API, because of the T
generic part in :
interface FormikHandlers {
handleChange<T = string | React.ChangeEvent<any>>(
field: T
): T extends React.ChangeEvent<any>
? void
: ((e: string | React.ChangeEvent<any>) => void);
}
- add overload like this:
interface FormikHandlers {
handleChange<T>(value: T, pure: true): void;
}
- or this:
interface FormikHandlers {
handleChange<T>(data: {value: T, field: string}): void;
}
Here, the T generic is not needed, however not sure due to the other overloads if it is valid to omit it.
Runtime:
- Similar, checking the second argument (boolean) to handle the pure value case.
- check for
{value, field}
structure.
Who does this impact? Who is this for?
Evenryone who wants to create a pure JS input, which is not backed by DOM and does not want to use linkstate.
Describe alternatives you’ve considered
There is a solution to this I missed? Checked docs and code for both 1.5.x and 2.x and could not find any…
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:12
Top GitHub Comments
Going to close this, as the feature is being on a PR currently and there is a workaround until its up. Thanks.
@johnrom thanks. It works. Btw. I had to
setFieldTouched
beforesetFieldValue
, otherwise the errors would reset. Not sure if it is a desired behavior.