Skip to content

Commit

Permalink
Merge pull request #18 from morrys/refactor-useformstate-update
Browse files Browse the repository at this point in the history
refactor useformstate update
  • Loading branch information
morrys authored Oct 22, 2020
2 parents 47b4672 + 7303462 commit 2a204a9
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/useFormState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,22 @@ export const useFormState = (): FormStateReturn => {
const entryErrors = data.form.entries.filter((value) => !!value.error);
const entryValidated = data.form.entries.filter((value) => value.check === 'DONE');
const errors = entryErrors.length > 0 ? entryErrors : undefined;
let update = false;
const newState = {
...ref.current,
};

const isValid =
data.form.entries.length === entryValidated.length &&
(!errors || Object.keys(errors).length === 0);
if (!areEqual(ref.current.errors, errors)) {
newState.errors = errors;
newState.isValid = !errors || Object.keys(errors).length === 0;
update = true;
}
if (
!areEqual(ref.current.errors, errors) ||
ref.current.isValid !== isValid ||
ref.current.isValidating !== data.form.isValidating ||
ref.current.isSubmitting !== data.form.isSubmitting
) {
newState.isValid = isValid;
newState.isValidating = data.form.isValidating;
newState.isSubmitting = data.form.isSubmitting;
update = true;
}
if (update) {
const newState = {
errors,
isValid,
isValidating: data.form.isValidating,
isSubmitting: data.form.isSubmitting,
};
ref.current = newState;
forceUpdate(ref.current);
}
Expand Down

0 comments on commit 2a204a9

Please sign in to comment.