Skip to content

Fix useField's onChange function should accept non-string values as args #1

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ function formikReducer<Values>(
case 'SET_STATUS':
return { ...state, status: msg.payload };
case 'SET_ISSUBMITTING':
return { ...state, isSubmitting: msg.payload };
case 'SET_ISVALIDATING':
return { ...state, isSubmitting: msg.payload };case 'SET_ISVALIDATING':
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch-case formatting is confusing. The SET_ISVALIDATING case is appended to the previous return. Consider placing it on its own line for clarity.

return { ...state, isValidating: msg.payload };
case 'SET_FIELD_VALUE':
return {
Expand Down Expand Up @@ -537,6 +536,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
fieldRegistry.current[name] = {
validate,
};
};
}, []);

const unregisterField = React.useCallback((name: string) => {
Expand Down Expand Up @@ -601,7 +601,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
);

const executeChange = React.useCallback(
(eventOrTextValue: string | React.ChangeEvent<any>, maybePath?: string) => {
(eventOrTextValue: string | React.ChangeEvent<any> | any, maybePath?: string) => {
// By default, assume that the first argument is a string. This allows us to use
// handleChange with React Native and React Native Web's onChangeText prop which
// provides just the value of the input.
Expand All @@ -610,7 +610,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
let parsed;
// If the first argument is not a string though, it has to be a synthetic React Event (or a fake one),
// so we handle like we would a normal HTML change event.
if (!isString(eventOrTextValue)) {
if (!isString(eventOrTextValue) && !Array.isArray(eventOrTextValue) && typeof eventOrTextValue !== 'number' && typeof eventOrTextValue !== 'boolean' && typeof eventOrTextValue !== 'object') {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new if condition in executeChange may not correctly detect synthetic events. Since React events are objects, this check (which excludes objects) will bypass event handling. Consider checking for event-specific properties (e.g. 'target' or 'persist') instead.

Suggested change
if (!isString(eventOrTextValue) && !Array.isArray(eventOrTextValue) && typeof eventOrTextValue !== 'number' && typeof eventOrTextValue !== 'boolean' && typeof eventOrTextValue !== 'object') {
if (eventOrTextValue && (typeof eventOrTextValue === 'object') && (eventOrTextValue.target || eventOrTextValue.persist)) {

// If we can, persist the event
// @see https://reactjs.org/docs/events.html#event-pooling
if ((eventOrTextValue as any).persist) {
Expand Down Expand Up @@ -1216,4 +1216,4 @@ function useEventCallback<T extends (...args: any[]) => any>(fn: T): T {
(...args: any[]) => ref.current.apply(void 0, args),
[]
) as T;
}
}
11 changes: 9 additions & 2 deletions packages/formik/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,18 @@ export interface FieldInputProps<Value> {
/** Is the field checked? */
checked?: boolean;
/** Change event handler */
onChange: FormikHandlers['handleChange'];
onChange: {
/** Classic React change handler, keyed by input name */
(e: React.ChangeEvent<any>): void;
/** Allows direct value setting with the same type as the field value */
(value: Value): void;
/** Support for string values */
(value: string): void;
};
/** Blur event handler */
onBlur: FormikHandlers['handleBlur'];
}

export type FieldValidator = (
value: any
) => string | void | Promise<string | void>;
) => string | void | Promise<string | void>;