Skip to content

Commit f19100d

Browse files
authored
Pre-release prep for formOptions.validateOnBlur (#115)
1 parent ecdb09d commit f19100d

File tree

5 files changed

+13
-2
lines changed

5 files changed

+13
-2
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- [`formOptions.onTouched`](#formoptionsontouched)
4949
- [`formOptions.onClear`](#formoptionsonclear)
5050
- [`formOptions.onReset`](#formoptionsonreset)
51+
- [`formOptions.validateOnBlur`](#formoptionsvalidateonblur)
5152
- [`formOptions.withIds`](#formoptionswithids)
5253
- [`[formState, inputs]`](#formstate-inputs)
5354
- [Form State](#form-state)
@@ -552,6 +553,10 @@ const [formState, inputs] = useFormState(null, {
552553
formState.reset(); // resetting the form state
553554
```
554555

556+
#### `formOptions.validateOnBlur`
557+
558+
When set to `true`, all form fields will validated when the input loses focus. If not specified, the `validate` function of each input will be called on value change.
559+
555560
#### `formOptions.withIds`
556561

557562
Indicates whether `useFormState` should generate and pass an `id` attribute to its fields. This is helpful when [working with labels](#labels-and-ids).

src/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ interface FormOptions<T> {
4141
onClear(): void;
4242
onReset(): void;
4343
onTouched(event: React.FocusEvent<InputElement>): void;
44+
validateOnBlur: boolean;
4445
withIds: boolean | ((name: string, value?: string) => string);
4546
}
4647

src/parseInputArgs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const defaultInputOptions = {
44
onChange: identity,
55
onBlur: noop,
66
validate: null,
7-
validateOnBlur: false,
7+
validateOnBlur: undefined,
88
touchOnChange: false,
99
};
1010

src/useFormState.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ export default function useFormState(initialState, options) {
251251

252252
formOptions.onChange(e, formState.current.values, newValues);
253253

254-
if (!formOptions.validateOnBlur && !inputOptions.validateOnBlur) {
254+
const validateOnBlur = formOptions.validateOnBlur
255+
? inputOptions.validateOnBlur !== false
256+
: inputOptions.validateOnBlur;
257+
258+
if (!validateOnBlur) {
255259
validate(e, value, newValues);
256260
}
257261

test/types.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const [formState, input] = useFormState<FormFields>(initialState, {
3535
const { name, value } = e.target;
3636
},
3737
withIds: (name, value) => (value ? `${name}.${value.toLowerCase()}` : name),
38+
validateOnBlur: true,
3839
});
3940

4041
let name: string = formState.values.name;

0 commit comments

Comments
 (0)