Skip to content

Commit

Permalink
fix: set falsy initial values
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Oct 7, 2020
1 parent c3ec555 commit 4b29e72
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
genFieldErrorId,
hasCheckedAttr,
getFromPath,
setInPath,
} from './utils';
import { isCallable } from '../../shared';
import { FormInitialValues, FormSymbol } from './symbols';
Expand Down Expand Up @@ -215,7 +216,7 @@ function useValidationState({
}) {
const errors: Ref<string[]> = ref([]);
const { reset: resetFlags, meta } = useMeta();
const initialValue = initValue ?? getFromPath(inject(FormInitialValues, {}), name);
const initialValue = getFromPath(inject(FormInitialValues, {}), name) ?? initValue;
const value = useFieldValue(initialValue, name, form);
if (hasCheckedAttr(type) && initialValue) {
value.value = initialValue;
Expand Down Expand Up @@ -366,6 +367,8 @@ function useFieldValue(initialValue: any, path: string, form?: FormController) {
return ref(initialValue);
}

// set initial value
setInPath(form.values, path, initialValue);
// otherwise use a computed setter that triggers the `setFieldValue`
const value = computed({
get() {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function getFromPath(object: Record<string, any>, path: string): any {
.split(/\.|\[(\d+)\]/)
.filter(Boolean)
.reduce((acc, propKey) => {
if (acc && acc[propKey]) {
if (acc && propKey in acc) {
return acc[propKey];
}

Expand Down

0 comments on commit 4b29e72

Please sign in to comment.