Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/varden/src/composables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function useFieldValue<T>(
}, true);

return computed({
get: () => form.getValue(compiledPath.value),
get: () => form.getValue(compiledPath.value) as Get<T, Paths<T>>,
set(value) {
form.setValue(compiledPath.value, value);
},
Expand Down
16 changes: 10 additions & 6 deletions packages/varden/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
type ComputedRef,
type Ref,
type DeepReadonly,
toRaw,
} from 'vue';

import { getIssuePath, type StandardSchemaV1 } from './standard-schema';
Expand Down Expand Up @@ -40,7 +39,9 @@ export interface FormContext<T> {
path: Path | CompiledPath,
value: Value | undefined,
): void;
getValue<Path extends Paths<T>, Value extends Get<T, Path>>(path: Path | CompiledPath): Value;
getValue<Path extends Paths<T>, Value extends Get<T, Path>>(
path: Path | CompiledPath,
): DeepReadonly<Value> | undefined;
setTouched<Path extends Paths<T>>(path: Path, flag?: boolean): void;
isTouched<Path extends Paths<T>>(path: Path): boolean;
valid: Ref<boolean>;
Expand Down Expand Up @@ -234,17 +235,20 @@ export function useForm<T, O>(props: FormProps<T, O>): FormContext<T> {
applyValidation();
};

const values = readonly(currentValues) as FormContext<T>['values'];

return {
values: readonly(currentValues) as FormContext<T>['values'],
values,
dirty,
// @ts-expect-error private field
__meta: fields,
reset,
resetField,
setValue,
getValue<Path extends Paths<T>, Value extends Get<T, Path>>(path: Path | CompiledPath): Value {
const a = get(currentValues.value, Array.isArray(path) ? path : toCompiledPath(path));
return cloneFn(toRaw(a));
getValue<Path extends Paths<T>, Value extends Get<T, Path>>(
path: Path | CompiledPath,
): DeepReadonly<Value> | undefined {
return get(values.value, Array.isArray(path) ? path : toCompiledPath(path));
},
setTouched<Path extends Paths<T>>(path: Path, flag = true) {
fields.get(path)!.touched = flag;
Expand Down
1 change: 1 addition & 0 deletions packages/varden/tests/field-value.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ describe('form.setValue object', () => {
});
form.setValue('user', { name: 'initial' });
const a = form.getValue('user');
// @ts-expect-error omit readonly for testing purposes
a.name = 'changed';
expect(form.values.value.user?.name).toBe('initial');
});
Expand Down
Loading