Skip to content

Commit b180770

Browse files
MayGowsmd
authored andcommitted
Add possibility to Add validateOnBlur once on formOptions. (#110)
1 parent c196327 commit b180770

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/useFormState.js

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

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

254-
if (!inputOptions.validateOnBlur) {
254+
if (!formOptions.validateOnBlur && !inputOptions.validateOnBlur) {
255255
validate(e, value, newValues);
256256
}
257257

test/useFormState-validation.test.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('passing a custom input validate function', () => {
1111
<input {...text({ name: 'name', validate })} />
1212
));
1313

14-
expect(validate).not.toHaveBeenCalledWith();
14+
expect(validate).not.toHaveBeenCalled();
1515
change({ value: 'test' });
1616
expect(validate).toHaveBeenCalledWith(
1717
'test',
@@ -31,7 +31,25 @@ describe('passing a custom input validate function', () => {
3131
));
3232

3333
change({ value: 'test' });
34-
expect(validate).not.toHaveBeenCalledWith();
34+
expect(validate).not.toHaveBeenCalled();
35+
blur();
36+
expect(validate).toHaveBeenCalledWith(
37+
'test',
38+
{ name: 'test' },
39+
INPUT_CHANGE_EVENT,
40+
);
41+
});
42+
43+
it('calls input validate function on blur with validateOnBlur on formState', () => {
44+
const validate = jest.fn(() => false);
45+
const { change, blur } = renderWithFormState(
46+
([, { text }]) => <input {...text({ name: 'name', validate })} />,
47+
{},
48+
{ validateOnBlur: true },
49+
);
50+
51+
change({ value: 'test' });
52+
expect(validate).not.toHaveBeenCalled();
3553
blur();
3654
expect(validate).toHaveBeenCalledWith(
3755
'test',

0 commit comments

Comments
 (0)