Skip to content
This repository was archived by the owner on Oct 4, 2022. It is now read-only.

Commit 12d974d

Browse files
authored
Add submit function to useFormState (#3)
1 parent db1b3b1 commit 12d974d

7 files changed

Lines changed: 25 additions & 13 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ import connect from '@bluecatend/auto-forms';
1717
import ComponentToConnect from '...';
1818

1919
//Use the connect function to map the state of the component to the state of the auto-wired form:
20-
const AutoWiredTextInput = connect(ComponentToConnect,({value, error, setValue}) => ({
20+
const AutoWiredTextInput = connect(
21+
ComponentToConnect,
22+
({value, error, setValue}) => ({
2123
value,
2224
error,
2325
onChange: useCallback(({target: {value}}) => setValue(value), [setValue]),
2426
})
27+
);
2528

2629
export default AutoWiredTextInput;
27-
2830
```
2931

3032
#### Submit

__tests__/components/__snapshots__/Form-test.jsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ exports[`Form rendering renders expected elements 1`] = `
1616
"test": "foo",
1717
},
1818
},
19+
"submit": [Function],
1920
}
2021
}
2122
>

__tests__/components/useFormState-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ describe('useFormState', () => {
1616
const values = {test: 'test-value'};
1717
const errors = {test: 'test-error'};
1818
const extras = {test: 'test-extra'};
19-
useContext.mockReturnValue({state: {initialValues, values, errors, extras}});
19+
const submit = jest.fn();
20+
useContext.mockReturnValue({state: {initialValues, values, errors, extras}, submit});
2021

2122
const state = useFormState();
2223
expect(state).toEqual({
2324
initialValues,
2425
values,
2526
errors,
2627
extras,
28+
submit,
2729
isChanged: expect.any(Function),
2830
hasErrors: expect.any(Function),
2931
});

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bluecateng/auto-forms",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Auto-wired form hooks",
55
"license": "ISC",
66
"scripts": {

src/components/Form.jsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ const submitForm = ({rules, values, extras, initialValues}, dispatch, getActiveF
4343
true
4444
);
4545

46-
if (passed) {
47-
const setErrors = errors => Object.entries(errors).forEach(([name, payload]) => setError(name, payload));
48-
onSubmit(values, {initialValues, extras, setErrors});
46+
if (!passed) {
47+
return null;
4948
}
49+
50+
const setErrors = errors => Object.entries(errors).forEach(([name, payload]) => setError(name, payload));
51+
return onSubmit(values, {initialValues, extras, setErrors});
5052
});
5153
};
5254

@@ -61,12 +63,15 @@ const Form = ({
6163
...props
6264
}) => {
6365
const [state, dispatch] = useReducer(reducer, {initialValues, initialExtras, rules}, getInitialState);
64-
const handleSubmit = useCallback(
65-
event => (event.preventDefault(), submitForm(state, dispatch, getActiveFields, extraValidation, onSubmit)),
66-
[state]
67-
);
66+
const submit = useCallback(() => submitForm(state, dispatch, getActiveFields, extraValidation, onSubmit), [
67+
state,
68+
getActiveFields,
69+
extraValidation,
70+
onSubmit,
71+
]);
72+
const handleSubmit = useCallback(event => (event.preventDefault(), submit()), [submit]);
6873
return (
69-
<FormContext.Provider value={{state, dispatch}}>
74+
<FormContext.Provider value={{state, dispatch, submit}}>
7075
<form {...props} onSubmit={handleSubmit}>
7176
{children}
7277
</form>

src/components/useFormState.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import FormContext from './FormContext';
88
export default () => {
99
const {
1010
state: {initialValues, values, errors, extras},
11+
submit,
1112
} = useContext(FormContext);
1213
return {
1314
initialValues,
1415
values,
1516
errors,
1617
extras,
18+
submit,
1719
isChanged: () => !isEqual(initialValues, values),
1820
hasErrors: () => Object.values(errors).some(isNotNull),
1921
};

0 commit comments

Comments
 (0)