Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: use watchEffect to compute form meta closes #3580
  • Loading branch information
logaretm committed Nov 17, 2021
1 parent 7c621ab commit e8729dc
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions packages/vee-validate/src/useForm.ts
@@ -1,4 +1,18 @@
import { computed, ref, Ref, provide, reactive, onMounted, isRef, watch, unref, nextTick, warn, markRaw } from 'vue';
import {
computed,
ref,
Ref,
provide,
reactive,
onMounted,
isRef,
watch,
unref,
nextTick,
warn,
markRaw,
watchEffect,
} from 'vue';
import isEqual from 'fast-deep-equal/es6';
import type { SchemaOf } from 'yup';
import { klona as deepCopy } from 'klona/full';
Expand Down Expand Up @@ -722,7 +736,7 @@ function useFormMeta<TValues extends Record<string, unknown>>(
return !isEqual(currentValues, unref(initialValues));
});

const flags = computed(() => {
function calculateFlags() {
const fields = Object.values(fieldsByPath.value).flat(1).filter(Boolean) as PrivateFieldContext[];

return keysOf(MERGE_STRATEGIES).reduce((acc, flag) => {
Expand All @@ -731,13 +745,21 @@ function useFormMeta<TValues extends Record<string, unknown>>(

return acc;
}, {} as Record<keyof Omit<FieldMeta<unknown>, 'initialValue'>, boolean>);
}

const flags = reactive(calculateFlags());
watchEffect(() => {
const value = calculateFlags();
flags.touched = value.touched;
flags.valid = value.valid;
flags.pending = value.pending;
});

return computed(() => {
return {
initialValues: unref(initialValues) as TValues,
...flags.value,
valid: flags.value.valid && !keysOf(errors.value as any).length,
...flags,
valid: flags.valid && !keysOf(errors.value as any).length,
dirty: isDirty.value,
};
});
Expand Down

0 comments on commit e8729dc

Please sign in to comment.