Skip to content

Commit

Permalink
fix: use the custom injection fn for initial field values
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Nov 28, 2020
1 parent 39cc4fc commit 38cd32b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
14 changes: 2 additions & 12 deletions packages/vee-validate/src/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import {
computed,
onMounted,
watchEffect,
inject,
onBeforeUnmount,
getCurrentInstance,
unref,
InjectionKey,
WatchStopHandle,
} from 'vue';
import { validate as validateValue } from './validate';
Expand All @@ -24,6 +21,7 @@ import {
getFromPath,
setInPath,
keysOf,
injectWithSelf,
} from './utils';
import { isCallable } from '../../shared';
import { FormInitialValues, FormSymbol } from './symbols';
Expand Down Expand Up @@ -272,7 +270,7 @@ function useValidationState({
valueProp: any;
}) {
const errors: Ref<string[]> = ref([]);
const formInitialValues = inject(FormInitialValues, undefined);
const formInitialValues = injectWithSelf(FormInitialValues, undefined);
const initialValue = getFromPath(unref(formInitialValues), unref(name)) ?? initValue;
const { resetMeta, meta } = useMeta(initialValue);
const value = useFieldValue(initialValue, name, form);
Expand Down Expand Up @@ -407,11 +405,3 @@ function useFieldValue(initialValue: any, path: MaybeReactive<string>, form?: Fo

return value;
}

// Uses same component provide as its own injections
// Due to changes in https://github.com/vuejs/vue-next/pull/2424
function injectWithSelf<T>(symbol: InjectionKey<T>, def: T | undefined = undefined): T | undefined {
const vm = getCurrentInstance() as any;

return inject(symbol, vm?.provides[symbol as any] || def);
}
9 changes: 9 additions & 0 deletions packages/vee-validate/src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getCurrentInstance, inject, InjectionKey } from 'vue';
import { isEmptyContainer, isIndex, isNotNestedPath } from './assertions';

function cleanupNonNestedPath(path: string) {
Expand Down Expand Up @@ -119,3 +120,11 @@ export function unsetPath(object: Record<string, any>, path: string): void {
export function keysOf<TRecord extends Record<string, any>>(record: TRecord): (keyof TRecord)[] {
return Object.keys(record);
}

// Uses same component provide as its own injections
// Due to changes in https://github.com/vuejs/vue-next/pull/2424
export function injectWithSelf<T>(symbol: InjectionKey<T>, def: T | undefined = undefined): T | undefined {
const vm = getCurrentInstance() as any;

return inject(symbol, vm?.provides[symbol as any] || def);
}

0 comments on commit 38cd32b

Please sign in to comment.