Skip to content

Commit 6f8f5e8

Browse files
Jason Waltonwsmd
Jason Walton
authored andcommitted
Fix validation of raw values onBlur. (#78)
1 parent 3d4c06e commit 6f8f5e8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/useFormState.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export default function useFormState(initialState, options) {
112112

113113
function validate(
114114
e,
115-
value = isRaw ? undefined : e.target.value,
115+
value = isRaw ? formState.current.values[name] : e.target.value,
116116
values = formState.current.values,
117117
) {
118118
let error;

test/useFormState-validation.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ describe('passing a custom input validate function', () => {
109109
expect(formState.current.errors).not.toHaveProperty('name');
110110
});
111111

112+
it('handles validation of raw values on blur', () => {
113+
const validate = jest.fn(val => ((val && val.foo === 'pass') ? true : 'wrong!'));
114+
let onChange;
115+
let onBlur;
116+
const { formState } = renderWithFormState(([, { raw }]) => {
117+
const inputProps = raw({ name: 'name', validate });
118+
({ onChange, onBlur } = inputProps);
119+
return <input {...inputProps} />;
120+
});
121+
122+
onChange({ foo: 'fail' });
123+
onBlur();
124+
expect(formState.current.validity).toHaveProperty('name', false);
125+
expect(formState.current.errors).toHaveProperty('name', 'wrong!');
126+
127+
onChange({ foo: 'pass' });
128+
onBlur();
129+
expect(formState.current.validity).toHaveProperty('name', true);
130+
expect(formState.current.errors).not.toHaveProperty('name');
131+
});
132+
112133
it.each([
113134
['empty array', []],
114135
['empty object', {}],

0 commit comments

Comments
 (0)