Skip to content

Commit 10f691a

Browse files
Merge pull request #63 from webdevnerdstuff/vuetify-v4
Vuetify v4
2 parents 49bcdf2 + 92c427a commit 10f691a

4 files changed

Lines changed: 67 additions & 47 deletions

File tree

src/plugin/VStepperForm.vue

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,6 @@ function setPageToError(pageIndex: number, page?: Page, source = 'submit'): void
596596
let debounceTimer: ReturnType<typeof setTimeout>;
597597
598598
function onFieldValidate(field: Field, next: () => void): void {
599-
const errors = $useForm.errorBag as ValidateResult['errors'];
600599
const shouldAutoPage = (field.autoPage || settings.value.autoPage ? next : null) as () => void;
601600
602601
// If autoPage //
@@ -606,20 +605,17 @@ function onFieldValidate(field: Field, next: () => void): void {
606605
// First validate the page before proceeding to the next page //
607606
$useForm.validate()
608607
.then((res: ValidateResult) => {
609-
if (res.valid) {
610-
// debounce next //
611-
clearTimeout(debounceTimer);
612-
debounceTimer = setTimeout(() => {
613-
checkForPageErrors(errors, 'field', shouldAutoPage);
614-
}, (field?.autoPageDelay ?? settings.value?.autoPageDelay));
615-
616-
return;
617-
}
618-
619-
const page = computedPages.value[currentPageIdx.value];
620-
const pageIndex = computedPages.value.findIndex((p) => p === page);
621-
622-
setPageToError(pageIndex, page, 'validating');
608+
// `validate()` covers the whole form, so fields on later pages
609+
// are still invalid while the user is on an earlier one. Only
610+
// the current page's errors may block auto paging, which is
611+
// what `checkForPageErrors` filters on (same as the next button).
612+
const errors = res.errors as unknown as ValidateResult['errors'];
613+
614+
// debounce next //
615+
clearTimeout(debounceTimer);
616+
debounceTimer = setTimeout(() => {
617+
checkForPageErrors(errors, 'field', shouldAutoPage);
618+
}, (field?.autoPageDelay ?? settings.value?.autoPageDelay));
623619
})
624620
.catch((error: Error) => {
625621
console.error('Error', error);

src/plugin/__tests__/VStepperForm.cy.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
object as yupObject,
3+
string as yupString,
4+
} from 'yup';
15
import type { Field, Page } from '../../plugin/types';
26
import * as DATA from '@cypress/templates/testData';
37
import VStepperForm from '../VStepperForm.vue';
@@ -637,6 +641,58 @@ describe('Stepper Form', () => {
637641
.should('exist')
638642
.and('be.visible');
639643
});
644+
645+
// ? The validation schema covers the whole form, so fields on later pages
646+
// ? are always invalid while the user is still on an earlier page. Auto
647+
// ? paging must only care about the errors on the current page. //
648+
it('should auto page when a later page still has unfilled required fields', () => {
649+
const schema = yupObject({
650+
address: yupString().required('Address is required'),
651+
animal: yupString().required('Animal is required'),
652+
});
653+
654+
cy.mount(VStepperForm as any, {
655+
props: {
656+
modelValue: { address: null, animal: null },
657+
pages: [
658+
{
659+
fields: [
660+
{
661+
autoPage: true,
662+
label: 'Animal',
663+
name: 'animal',
664+
options: [
665+
{ label: 'Rabbit', value: 'rabbit' },
666+
{ label: 'Duck', value: 'duck' },
667+
],
668+
required: true,
669+
type: 'buttons',
670+
},
671+
],
672+
title: 'Page 1',
673+
},
674+
{
675+
fields: [defaultFields.address],
676+
title: 'Page 2',
677+
},
678+
],
679+
validationSchema: schema,
680+
},
681+
global,
682+
});
683+
684+
cy.getDataCy('vsf-field-address')
685+
.should('not.exist');
686+
687+
cy.getDataCy('vsf-field-animal')
688+
.first()
689+
.click();
690+
691+
// Moved onto the next page //
692+
cy.getDataCy('vsf-field-address')
693+
.should('exist')
694+
.and('be.visible');
695+
});
640696
});
641697

642698
describe('Header Tooltip', () => {

src/plugin/composables/helpers.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { watchDebounced } from '@vueuse/core';
21
import type {
3-
UseAutoPage,
42
UseBuildSettings,
53
UseColumnErrorCheck,
64
UseDeepMerge,
@@ -84,23 +82,6 @@ export const useBuildSettings: UseBuildSettings = (stepperProps: Settings) => {
8482
};
8583

8684

87-
/**
88-
* Automatically pages to the next field.
89-
*/
90-
export const useAutoPage: UseAutoPage = (options) => {
91-
const { emit, field, modelValue, settings } = options;
92-
watchDebounced(modelValue, () => {
93-
if (field?.autoPage == false) {
94-
return;
95-
}
96-
97-
if (field?.autoPage || settings?.autoPage) {
98-
emit('next', field);
99-
}
100-
}, { debounce: (field?.autoPageDelay ?? settings?.autoPageDelay) });
101-
};
102-
103-
10485
/**
10586
* Checks if the column values are between 1 and 12.
10687
*/

src/plugin/types/index.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,19 +268,6 @@ export interface UseDeepMerge {
268268
): Record<string, any>;
269269
}
270270

271-
export interface UseAutoPage {
272-
(
273-
options: {
274-
emit: {
275-
(e: 'next', field: Field): void;
276-
},
277-
field: Field;
278-
modelValue: any;
279-
settings: Settings;
280-
}
281-
): void;
282-
}
283-
284271
export interface UseColumnErrorCheck {
285272
(
286273
options: {

0 commit comments

Comments
 (0)