Skip to content

Commit 15c288f

Browse files
committed
add setStateSchema top level API
1 parent 3affca8 commit 15c288f

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/Form/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function Form() {
1010
tags: { value: '', error: '' },
1111
};
1212

13-
const delay = () => new Promise(resolve => setTimeout(resolve, 5000));
13+
const delay = () => new Promise(resolve => setTimeout(resolve, 3000));
1414

1515
// Create your own validationStateSchema
1616
// stateSchema property should be the same in validationStateSchema
@@ -58,8 +58,8 @@ function Form() {
5858
useEffect(() => {
5959
delay().then(() => {
6060
setStateSchema({
61-
first_name: { value: 'Denise', error: '' },
62-
last_name: { value: '', error: '' },
61+
first_name: { value: 'Ellie', error: '' },
62+
last_name: { value: 'Eilish', error: '' },
6363
tags: { value: '', error: '' },
6464
});
6565
// setFieldValue({ name: 'first_name', value: 'Hello' });
@@ -81,7 +81,9 @@ function Form() {
8181
onChange={handleOnChange}
8282
/>
8383
</label>
84-
{errors.first_name && <p className="error">{errors.first_name}</p>}
84+
{errors.first_name && dirty.first_name && (
85+
<p className="error">{errors.first_name}</p>
86+
)}
8587
</div>
8688

8789
<div className="form-item">

src/useForm.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function useForm(
1717

1818
const [values, setValues] = useState(get_prop_values(state, VALUE));
1919
const [errors, setErrors] = useState(get_prop_values(state, ERROR));
20-
const [dirty, setDirty] = useState(get_prop_values(state));
20+
const [dirty, setDirty] = useState(get_prop_values(state, false));
2121

2222
const [disable, setDisable] = useState(true);
2323
const [isDirty, setIsDirty] = useState(false);
@@ -29,12 +29,18 @@ function useForm(
2929
setInitialErrorState();
3030
}, []); // eslint-disable-line
3131

32-
// If state schema changes update all fields
32+
// Set a brand new field values and errors
33+
// If stateSchema changes
3334
useEffect(() => {
34-
setValues(get_prop_values(state, VALUE));
35-
setErrors(get_prop_values(state, ERROR));
36-
setDirty(get_prop_values(state, true));
37-
}, [state]);
35+
const values = get_prop_values(state, VALUE);
36+
setValues(values);
37+
setErrors(
38+
Object.keys(values).reduce((accu, curr) => {
39+
accu[curr] = validateField(curr, values[curr]);
40+
return accu;
41+
}, {})
42+
);
43+
}, [state]); // eslint-disable-line
3844

3945
// For every changed in our state this will be fired
4046
// To be able to disable the button

src/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ export const VALUE = 'value';
22
export const ERROR = 'error';
33
export const REQUIRED_FIELD_ERROR = 'This is required field';
44

5+
function is_bool(value) {
6+
return typeof value === 'boolean';
7+
}
8+
59
/**
610
* Determines a value if it's an object
711
*
@@ -23,7 +27,7 @@ export function is_required(value, isRequired) {
2327

2428
export function get_prop_values(stateSchema, prop) {
2529
return Object.keys(stateSchema).reduce((field, key) => {
26-
field[key] = !prop ? false : stateSchema[key][prop];
30+
field[key] = is_bool(prop) ? prop : stateSchema[key][prop];
2731

2832
return field;
2933
}, {});

0 commit comments

Comments
 (0)