Skip to content

Commit 776cc54

Browse files
Stepper Form Demo: fix reset form data in Angular and Vue (#30185)
1 parent 2662d2d commit 776cc54

File tree

11 files changed

+73
-17
lines changed

11 files changed

+73
-17
lines changed

apps/demos/Demos/Stepper/FormIntegration/Angular/app/app.component.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ export class AppComponent {
115115
this.selectedIndex = 0;
116116
this.steps = this.appService.getInitialSteps();
117117
this.formData = this.appService.getInitialFormData();
118-
validationEngine.resetGroup(this.validationGroups[0]);
119-
validationEngine.resetGroup(this.validationGroups[1]);
120118
this.isStepperReadonly = false;
121119
}
122120

apps/demos/Demos/Stepper/FormIntegration/Angular/app/dates-form/dates-form.component.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
that information in Additional Requests. We will do our best to suggest best
44
pricing options, depending on room availability.
55
</p>
6-
<dx-form [(formData)]="formData" [validationGroup]="validationGroup">
6+
<dx-form
7+
#formComponent
8+
[(formData)]="formData"
9+
[validationGroup]="validationGroup"
10+
>
711
<dxi-form-item
812
[isRequired]="true"
913
dataField="dates"

apps/demos/Demos/Stepper/FormIntegration/Angular/app/dates-form/dates-form.component.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Component, Input } from '@angular/core';
2-
import { DxFormTypes } from 'devextreme-angular/ui/form';
1+
import { Component, Input, SimpleChanges, ViewChild } from '@angular/core';
2+
import { DxFormTypes, DxFormComponent } from 'devextreme-angular/ui/form';
33
import { DxDateRangeBoxTypes } from 'devextreme-angular/ui/date-range-box';
44
import type { BookingFormData } from '../app.types';
55

@@ -14,10 +14,20 @@ if (window && window.config?.packageConfigPaths) {
1414
templateUrl: `.${modulePrefix}/dates-form/dates-form.component.html`,
1515
})
1616
export class DatesFormComponent {
17+
@ViewChild('formComponent', { static: false }) form!: DxFormComponent
18+
1719
@Input() formData: BookingFormData;
1820

1921
@Input() validationGroup: string;
2022

23+
ngOnChanges(changes: SimpleChanges) {
24+
if (changes['formData']) {
25+
const value = changes['formData'].currentValue;
26+
27+
this.form?.instance?.reset(value);
28+
}
29+
}
30+
2131
dateRangeBoxOptions: DxDateRangeBoxTypes.Properties = {
2232
startDatePlaceholder: 'Check-in',
2333
endDatePlaceholder: 'Check-out',

apps/demos/Demos/Stepper/FormIntegration/Angular/app/guests-form/guests-form.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
amenities.
55
</p>
66
<dx-form
7+
#formComponent
78
[(formData)]="formData"
89
[validationGroup]="validationGroup"
910
[colCount]="3"

apps/demos/Demos/Stepper/FormIntegration/Angular/app/guests-form/guests-form.component.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Component, Input } from '@angular/core';
2-
import { DxFormTypes } from 'devextreme-angular/ui/form';
1+
import { Component, Input, SimpleChanges, ViewChild } from "@angular/core";
2+
import { DxFormTypes, DxFormComponent } from 'devextreme-angular/ui/form';
33
import { DxNumberBoxTypes } from 'devextreme-angular/ui/number-box';
44
import type { BookingFormData } from '../app.types';
55

@@ -14,10 +14,20 @@ if (window && window.config?.packageConfigPaths) {
1414
templateUrl: `.${modulePrefix}/guests-form/guests-form.component.html`,
1515
})
1616
export class GuestsFormComponent {
17+
@ViewChild('formComponent', { static: false }) form!: DxFormComponent
18+
1719
@Input() formData: BookingFormData;
1820

1921
@Input() validationGroup: string;
2022

23+
ngOnChanges(changes: SimpleChanges) {
24+
if (changes['formData']) {
25+
const value = changes['formData'].currentValue;
26+
27+
this.form?.instance?.reset(value);
28+
}
29+
}
30+
2131
adultsLabelOptions: DxFormTypes.SimpleItem["label"] = {
2232
text: 'Adults',
2333
location: 'top',

apps/demos/Demos/Stepper/FormIntegration/Angular/app/room-meal-plan-form/room-meal-plan-form.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
full board.
55
</p>
66
<dx-form
7+
#formComponent
78
[(formData)]="formData"
89
[validationGroup]="validationGroup"
910
[colCount]="2"

apps/demos/Demos/Stepper/FormIntegration/Angular/app/room-meal-plan-form/room-meal-plan-form.component.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Component, Input } from '@angular/core';
2-
import { DxFormTypes } from 'devextreme-angular/ui/form';
1+
import { Component, Input, SimpleChanges, ViewChild } from "@angular/core";
2+
import { DxFormTypes, DxFormComponent } from 'devextreme-angular/ui/form';
33
import { DxSelectBoxTypes } from 'devextreme-angular/ui/select-box';
44
import type { BookingFormData } from '../app.types';
55

@@ -14,10 +14,20 @@ if (window && window.config?.packageConfigPaths) {
1414
templateUrl: `.${modulePrefix}/room-meal-plan-form/room-meal-plan-form.component.html`,
1515
})
1616
export class RoomMealPlanFormComponent {
17+
@ViewChild('formComponent', { static: false }) form!: DxFormComponent
18+
1719
@Input() formData: BookingFormData;
1820

1921
@Input() validationGroup: string;
2022

23+
ngOnChanges(changes: SimpleChanges) {
24+
if (changes['formData']) {
25+
const value = changes['formData'].currentValue;
26+
27+
this.form?.instance?.reset(value);
28+
}
29+
}
30+
2131
roomLabelOptions: DxFormTypes.SimpleItem["label"] = {
2232
text: 'Room Type',
2333
location: 'top',

apps/demos/Demos/Stepper/FormIntegration/Vue/App.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ const reset = () => {
169169
selectedIndex.value = 0;
170170
steps.value = getInitialSteps();
171171
formData.value = getInitialFormData();
172-
validationEngine.resetGroup(validationGroups[0]);
173-
validationEngine.resetGroup(validationGroups[1]);
174172
isStepperReadonly.value = false;
175173
};
176174

apps/demos/Demos/Stepper/FormIntegration/Vue/DatesTemplate.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<DxForm
66
:form-data="formData"
77
:validation-group="validationGroup"
8+
ref="formRef"
89
>
910
<DxSimpleItem
1011
:is-required="true"
@@ -19,17 +20,24 @@
1920
<script setup lang="ts">
2021
import DxForm, { DxSimpleItem } from 'devextreme-vue/form';
2122
import 'devextreme/ui/date_range_box';
23+
import { ref, watch } from 'vue';
2224
import type { BookingFormData } from './types.ts';
23-
import { initialFormData } from './data.ts';
25+
import { getInitialFormData } from './data.ts';
26+
27+
const formRef = ref(null);
2428
2529
const props = withDefaults(defineProps<{
2630
formData: BookingFormData;
2731
validationGroup?: string;
2832
}>(), {
29-
formData: () => initialFormData,
33+
formData: getInitialFormData,
3034
validationGroup: () => undefined,
3135
});
3236
37+
watch(() => props.formData, (value) => {
38+
formRef.value.instance.reset(value);
39+
});
40+
3341
const labelOptions = {
3442
visible: false,
3543
};

apps/demos/Demos/Stepper/FormIntegration/Vue/GuestsTemplate.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
:form-data="formData"
77
:validation-group="validationGroup"
88
:col-count="3"
9+
ref="formRef"
910
>
1011
<DxSimpleItem
1112
:is-required="true"
@@ -34,17 +35,24 @@
3435
<script setup lang="ts">
3536
import DxForm, { DxRangeRule, DxSimpleItem } from 'devextreme-vue/form';
3637
import 'devextreme/ui/number_box';
38+
import { watch, ref } from 'vue';
3739
import type { BookingFormData } from './types.ts';
38-
import { initialFormData } from './data.ts';
40+
import { getInitialFormData } from './data.ts';
41+
42+
const formRef = ref(null);
3943
4044
const props = withDefaults(defineProps<{
4145
formData: BookingFormData;
4246
validationGroup?: string;
4347
}>(), {
44-
formData: () => initialFormData,
48+
formData: getInitialFormData,
4549
validationGroup: () => undefined,
4650
});
4751
52+
watch(() => props.formData, (value) => {
53+
formRef.value.instance.reset(value);
54+
});
55+
4856
const adultsLabelOptions = {
4957
text: 'Adults',
5058
location: 'top',

0 commit comments

Comments
 (0)