Skip to content

Commit 1752de6

Browse files
committed
fix(3942): added support for NativeSyntheticEvent
1 parent f87f884 commit 1752de6

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

docs/api/formik.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,24 @@ keys and shape will match your schema exactly. Internally, Formik transforms raw
7474
on your behalf. If you are using `validate`, then that function will determine
7575
the `errors` objects shape.
7676

77-
#### `handleBlur: (e: any) => void`
77+
#### `handleBlur: (e: React.FocusEvent<any> | NativeSyntheticEvent<any> | any) => void`
7878

7979
`onBlur` event handler. Useful for when you need to track whether an input has
8080
been `touched` or not. This should be passed to `<input onBlur={handleBlur} ... />`
8181

82-
#### `handleChange: (e: React.ChangeEvent<any>) => void`
82+
#### `handleChange: (e: React.ChangeEvent<any> | NativeSyntheticEvent<any>) => void`
8383

8484
General input change event handler. This will update the `values[key]` where
8585
`key` is the event-emitting input's `name` attribute. If the `name` attribute is
8686
not present, `handleChange` will look for an input's `id` attribute. Note:
8787
"input" here means all HTML inputs.
8888

89-
#### `handleReset: () => void`
89+
#### `handleReset: (e?: React.SyntheticEvent<any> | NativeSyntheticEvent<any>) => void`
9090

9191
Reset handler. Will reset the form to its initial state. This should be passed
9292
to `<button onClick={handleReset}>...</button>`
9393

94-
#### `handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void`
94+
#### `handleSubmit: (e?: React.FormEvent<HTMLFormElement> | NativeSyntheticEvent<HTMLFormElement>) => void`
9595

9696
Submit handler. This should be passed to `<form onSubmit={props.handleSubmit}>...</form>`. To learn more about the submission process, see [Form Submission](../guides/form-submission.md).
9797

docs/api/useField.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ An object that contains:
139139

140140
- `name: string` - The name of the field
141141
- `checked?: boolean` - Whether or not the input is checked, this is _only_ defined if `useField` is passed an object with a `name`, `type: 'checkbox'` or `type: 'radio'`.
142-
- `onBlur: () => void` - A blur event handler
143-
- `onChange: (e: React.ChangeEvent<any>) => void` - A change event handler
142+
- `onBlur: (e: React.FocusEvent<any> | NativeSyntheticEvent<any>) => void` - A blur event handler
143+
- `onChange: (e: NativeSyntheticEvent<any> | React.ChangeEvent<any>) => void` - A change event handler
144144
- `value: Value` - The field's value (plucked out of `values`) or, if it is a checkbox or radio input, then potentially the `value` passed into `useField`.
145145
- `multiple?: boolean` - Whether or not the multiple values can be selected. This is only ever defined when `useField` is passed an object with `multiple: true`
146146

@@ -160,9 +160,9 @@ An object that contains relevant computed metadata about a field. More specifica
160160
An object that contains helper functions which you can use to imperatively change the value, error value or touched status for the field in question. This is useful for components which need to change a field's status directly, without triggering change or blur events.
161161

162162
- `setValue(value: any, shouldValidate?: boolean): Promise<void | FormikErrors>` - A function to change the field's value. Calling this will trigger validation to run if `validateOnChange` is set to `true` (which it is by default). You can also explicitly prevent/skip validation by passing a second argument as `false`.
163-
If `validateOnChange` is set to `true` and there are errors, they will be resolved in the returned `Promise`.
163+
If `validateOnChange` is set to `true` and there are errors, they will be resolved in the returned `Promise`.
164164

165165
- `setTouched(value: boolean, shouldValidate?: boolean): void` - A function to change the field's touched status. Calling this will trigger validation to run if `validateOnBlur` is set to `true` (which it is by default). You can also explicitly prevent/skip validation by passing a second argument as `false`.
166-
If `validateOnBlur` is set to `true` and there are errors, they will be resolved in the returned `Promise`.
166+
If `validateOnBlur` is set to `true` and there are errors, they will be resolved in the returned `Promise`.
167167

168168
- `setError(value: any): void` - A function to change the field's error value

packages/formik/src/Formik.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import deepmerge from 'deepmerge';
22
import isPlainObject from 'lodash/isPlainObject';
33
import cloneDeep from 'lodash/cloneDeep';
44
import * as React from 'react';
5+
import { NativeSyntheticEvent } from 'react-native'
56
import isEqual from 'react-fast-compare';
67
import invariant from 'tiny-warning';
78
import { FieldConfig } from './Field';
@@ -604,7 +605,13 @@ export function useFormik<Values extends FormikValues = FormikValues>({
604605
);
605606

606607
const executeChange = React.useCallback(
607-
(eventOrTextValue: string | React.ChangeEvent<any>, maybePath?: string) => {
608+
(
609+
eventOrTextValue:
610+
| string
611+
| React.ChangeEvent<any>
612+
| NativeSyntheticEvent<any>,
613+
maybePath?: string
614+
) => {
608615
// By default, assume that the first argument is a string. This allows us to use
609616
// handleChange with React Native and React Native Web's onChangeText prop which
610617
// provides just the value of the input.
@@ -620,8 +627,8 @@ export function useFormik<Values extends FormikValues = FormikValues>({
620627
(eventOrTextValue as React.ChangeEvent<any>).persist();
621628
}
622629
const target = eventOrTextValue.target
623-
? (eventOrTextValue as React.ChangeEvent<any>).target
624-
: (eventOrTextValue as React.ChangeEvent<any>).currentTarget;
630+
? (eventOrTextValue as React.ChangeEvent<any> | NativeSyntheticEvent<any>).target
631+
: (eventOrTextValue as React.ChangeEvent<any> | NativeSyntheticEvent<any>).currentTarget;
625632

626633
const {
627634
type,
@@ -661,8 +668,8 @@ export function useFormik<Values extends FormikValues = FormikValues>({
661668

662669
const handleChange = useEventCallback<FormikHandlers['handleChange']>(
663670
(
664-
eventOrPath: string | React.ChangeEvent<any>
665-
): void | ((eventOrTextValue: string | React.ChangeEvent<any>) => void) => {
671+
eventOrPath: string | React.ChangeEvent<any> | NativeSyntheticEvent<any>
672+
): void | ((eventOrTextValue: string | React.ChangeEvent<any> | NativeSyntheticEvent<any>) => void) => {
666673
if (isString(eventOrPath)) {
667674
return event => executeChange(event, eventOrPath);
668675
} else {
@@ -808,7 +815,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
808815
});
809816

810817
const handleSubmit = useEventCallback(
811-
(e?: React.FormEvent<HTMLFormElement>) => {
818+
(e?: React.FormEvent<HTMLFormElement> | NativeSyntheticEvent<HTMLFormElement>) => {
812819
if (e && e.preventDefault && isFunction(e.preventDefault)) {
813820
e.preventDefault();
814821
}

packages/formik/src/types.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import { NativeSyntheticEvent } from 'react-native';
23
import { FieldConfig } from './Field';
34
/**
45
* Values of fields in the form
@@ -125,29 +126,30 @@ export interface FormikHelpers<Values> {
125126

126127
/**
127128
* Formik form event handlers
129+
* import { NativeSyntheticEvent } from 'react-native';
128130
*/
129131
export interface FormikHandlers {
130132
/** Form submit handler */
131-
handleSubmit: (e?: React.FormEvent<HTMLFormElement>) => void;
133+
handleSubmit: (e?: React.FormEvent<HTMLFormElement> | NativeSyntheticEvent<HTMLFormElement>) => void;
132134
/** Reset form event handler */
133-
handleReset: (e?: React.SyntheticEvent<any>) => void;
135+
handleReset: (e?: React.SyntheticEvent<any> | NativeSyntheticEvent<any>) => void;
134136
handleBlur: {
135137
/** Classic React blur handler, keyed by input name */
136-
(e: React.FocusEvent<any>): void;
138+
(e: React.FocusEvent<any> | NativeSyntheticEvent<any>): void;
137139
/** Preact-like linkState. Will return a handleBlur function. */
138-
<T = string | any>(fieldOrEvent: T): T extends string
139-
? (e: any) => void
140+
<T = string | NativeSyntheticEvent<any> | any>(fieldOrEvent: T): T extends string
141+
? (e: NativeSyntheticEvent<any> | any) => void
140142
: void;
141143
};
142144
handleChange: {
143145
/** Classic React change handler, keyed by input name */
144-
(e: React.ChangeEvent<any>): void;
146+
(e: React.ChangeEvent<any> | NativeSyntheticEvent<any>): void;
145147
/** Preact-like linkState. Will return a handleChange function. */
146-
<T = string | React.ChangeEvent<any>>(
148+
<T = string | NativeSyntheticEvent<any> | React.ChangeEvent<any>>(
147149
field: T
148-
): T extends React.ChangeEvent<any>
150+
): T extends React.ChangeEvent<any> | NativeSyntheticEvent<any>
149151
? void
150-
: (e: string | React.ChangeEvent<any>) => void;
152+
: (e: string | React.ChangeEvent<any> | NativeSyntheticEvent<any>) => void;
151153
};
152154

153155
getFieldProps: <Value = any>(

0 commit comments

Comments
 (0)