Skip to content

Commit eeccd0c

Browse files
committed
fix: make initial values partial closes #4195
1 parent 48deea9 commit eeccd0c

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

packages/vee-validate/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface FormMeta<TValues extends Record<string, any>> {
4545
valid: boolean;
4646
validated: boolean;
4747
pending: boolean;
48-
initialValues?: TValues;
48+
initialValues?: Partial<TValues>;
4949
}
5050

5151
export interface FieldState<TValue = unknown> {

packages/vee-validate/src/useForm.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import {
3737
FieldState,
3838
GenericFormValues,
3939
TypedSchema,
40-
YupSchema,
4140
} from './types';
4241
import {
4342
getFromPath,
@@ -69,7 +68,7 @@ export interface FormOptions<
6968
| TypedSchema<TValues, TOutput>
7069
> {
7170
validationSchema?: MaybeRef<TSchema extends TypedSchema ? TypedSchema<TValues, TOutput> : any>;
72-
initialValues?: MaybeRef<TValues>;
71+
initialValues?: MaybeRef<Partial<TValues>>;
7372
initialErrors?: Record<keyof TValues, string | undefined>;
7473
initialTouched?: Record<keyof TValues, boolean>;
7574
validateOnMount?: boolean;
@@ -865,7 +864,7 @@ export function useForm<
865864
function useFormMeta<TValues extends Record<string, unknown>>(
866865
fieldsByPath: Ref<FieldPathLookup<TValues>>,
867866
currentValues: TValues,
868-
initialValues: MaybeRef<TValues>,
867+
initialValues: MaybeRef<Partial<TValues>>,
869868
errors: Ref<FormErrors<TValues>>
870869
) {
871870
const MERGE_STRATEGIES: Record<keyof Pick<FieldMeta<unknown>, 'touched' | 'pending' | 'valid'>, 'every' | 'some'> = {
@@ -899,7 +898,7 @@ function useFormMeta<TValues extends Record<string, unknown>>(
899898

900899
return computed(() => {
901900
return {
902-
initialValues: unref(initialValues) as TValues,
901+
initialValues: unref(initialValues) as Partial<TValues>,
903902
...flags,
904903
valid: flags.valid && !keysOf(errors.value as any).length,
905904
dirty: isDirty.value,
@@ -918,13 +917,13 @@ function useFormInitialValues<TValues extends GenericFormValues>(
918917
const values = resolveInitialValues(opts);
919918
const providedValues = opts?.initialValues;
920919
// these are the mutable initial values as the fields are mounted/unmounted
921-
const initialValues = ref<TValues>(values);
920+
const initialValues = ref<Partial<TValues>>(values);
922921
// these are the original initial value as provided by the user initially, they don't keep track of conditional fields
923922
// this is important because some conditional fields will overwrite the initial values for other fields who had the same name
924923
// like array fields, any push/insert operation will overwrite the initial values because they "create new fields"
925924
// so these are the values that the reset function should use
926925
// these only change when the user explicitly changes the initial values or when the user resets them with new values.
927-
const originalInitialValues = ref<TValues>(deepCopy(values));
926+
const originalInitialValues = ref<Partial<TValues>>(deepCopy(values)) as Ref<Partial<TValues>>;
928927

929928
function setInitialValues(values: Partial<TValues>, updateFields = false) {
930929
initialValues.value = deepCopy(values);

packages/vee-validate/tests/useForm.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,19 +682,19 @@ describe('useForm()', () => {
682682
});
683683

684684
await flushPromises();
685-
expect(formMeta.value.initialValues?.field.name).toBe('1');
685+
expect(formMeta.value.initialValues?.field?.name).toBe('1');
686686
model.value.name = 'test';
687687
await flushPromises();
688688
expect(model.value).toEqual({ name: 'test' });
689-
expect(formMeta.value.initialValues?.field.name).toBe('1');
689+
expect(formMeta.value.initialValues?.field?.name).toBe('1');
690690
reset();
691691
await flushPromises();
692692
expect(model.value).toEqual({ name: '1' });
693-
expect(formMeta.value.initialValues?.field.name).toBe('1');
693+
expect(formMeta.value.initialValues?.field?.name).toBe('1');
694694

695695
model.value.name = 'test';
696696
await flushPromises();
697697
expect(model.value).toEqual({ name: 'test' });
698-
expect(formMeta.value.initialValues?.field.name).toBe('1');
698+
expect(formMeta.value.initialValues?.field?.name).toBe('1');
699699
});
700700
});

0 commit comments

Comments
 (0)