Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow path meta querying for nested fields closes #4575 #4594

Merged
merged 1 commit into from
Dec 14, 2023
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
5 changes: 5 additions & 0 deletions .changeset/shy-scissors-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vee-validate": patch
---

feat: allow path meta querying for nested fields closes #4575
22 changes: 19 additions & 3 deletions packages/vee-validate/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,15 +719,31 @@ export function useForm<
}

function isFieldTouched(field: Path<TValues>) {
return !!findPathState(field)?.touched;
const pathState = findPathState(field);
if (pathState) {
return pathState.touched;
}

// Find all nested paths and consider their touched state
return pathStates.value.filter(s => s.path.startsWith(field)).some(s => s.touched);
}

function isFieldDirty(field: Path<TValues>) {
return !!findPathState(field)?.dirty;
const pathState = findPathState(field);
if (pathState) {
return pathState.dirty;
}

return pathStates.value.filter(s => s.path.startsWith(field)).some(s => s.dirty);
}

function isFieldValid(field: Path<TValues>) {
return !!findPathState(field)?.valid;
const pathState = findPathState(field);
if (pathState) {
return pathState.valid;
}

return pathStates.value.filter(s => s.path.startsWith(field)).every(s => s.valid);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/vee-validate/tests/useForm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,8 @@ describe('useForm()', () => {
setup() {
form = useForm();
useField('fname');
useField('nested.lname');
useField('nested.fname');

return {};
},
Expand All @@ -1192,10 +1194,13 @@ describe('useForm()', () => {
await flushPromises();
expect(form.meta.value.touched).toBe(false);
expect(form.isFieldTouched('fname')).toBe(false);
expect(form.isFieldTouched('nested')).toBe(false);
form.setFieldTouched('fname', true);
form.setFieldTouched('nested.lname', true);
await flushPromises();
expect(form.meta.value.touched).toBe(true);
expect(form.isFieldTouched('fname')).toBe(true);
expect(form.isFieldTouched('nested')).toBe(true);
});

test('can query field dirty state', async () => {
Expand All @@ -1204,6 +1209,8 @@ describe('useForm()', () => {
setup() {
form = useForm();
useField('fname');
useField('nested.lname');
useField('nested.fname');

return {};
},
Expand All @@ -1213,10 +1220,13 @@ describe('useForm()', () => {
await flushPromises();
expect(form.meta.value.dirty).toBe(false);
expect(form.isFieldDirty('fname')).toBe(false);
expect(form.isFieldDirty('nested')).toBe(false);
form.setFieldValue('fname', 'value');
form.setFieldValue('nested.lname', 'value');
await flushPromises();
expect(form.meta.value.dirty).toBe(true);
expect(form.isFieldDirty('fname')).toBe(true);
expect(form.isFieldDirty('nested')).toBe(true);
});

test('can query field valid state', async () => {
Expand All @@ -1225,6 +1235,8 @@ describe('useForm()', () => {
setup() {
form = useForm();
useField('fname');
useField('nested.lname');
useField('nested.fname');

return {};
},
Expand All @@ -1234,10 +1246,13 @@ describe('useForm()', () => {
await flushPromises();
expect(form.meta.value.valid).toBe(true);
expect(form.isFieldValid('fname')).toBe(true);
expect(form.isFieldValid('nested')).toBe(true);
form.setFieldError('fname', 'ERROR');
form.setFieldError('nested.lname', 'ERROR');
await flushPromises();
expect(form.meta.value.valid).toBe(false);
expect(form.isFieldValid('fname')).toBe(false);
expect(form.isFieldValid('nested')).toBe(false);
});

// #4438
Expand Down