Skip to content

Commit 1128d44

Browse files
Scheduler: Implement legacy form option (#31080)
1 parent 369a2dc commit 1128d44

File tree

8 files changed

+974
-58
lines changed

8 files changed

+974
-58
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
beforeEach, describe, expect, it,
3+
} from '@jest/globals';
4+
5+
import { createScheduler } from './__mock__/create_scheduler';
6+
import { setupSchedulerTestEnvironment } from './__mock__/m_mock_scheduler';
7+
8+
describe('LegacyForm', () => {
9+
beforeEach(() => {
10+
setupSchedulerTestEnvironment();
11+
});
12+
13+
it('should be false by default', async () => {
14+
const { scheduler } = await createScheduler({ });
15+
16+
// @ts-expect-error private option
17+
expect(scheduler.option('editing').legacyForm).toBe(false);
18+
});
19+
20+
it('should be true when explicitly set', async () => {
21+
const { scheduler } = await createScheduler({ editing: { legacyForm: true } });
22+
23+
// @ts-expect-error private option
24+
expect(scheduler.option('editing').legacyForm).toBe(true);
25+
});
26+
27+
it('should be changed by option()', async () => {
28+
const { scheduler } = await createScheduler({});
29+
30+
scheduler.option('editing', { legacyForm: true });
31+
32+
// @ts-expect-error private option
33+
expect(scheduler.option('editing').legacyForm).toBe(true);
34+
});
35+
});

packages/devextreme/js/__internal/scheduler/appointment_popup/m_form.ts

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import $ from '@js/core/renderer';
1111
import dateUtils from '@js/core/utils/date';
1212
import dateSerialization from '@js/core/utils/date_serialization';
1313
import { extend } from '@js/core/utils/extend';
14-
import Form from '@js/ui/form';
14+
import Form, { type Item } from '@js/ui/form';
1515
import { current, isFluent } from '@js/ui/themes';
1616

1717
import timeZoneUtils from '../m_utils_time_zone';
@@ -61,17 +61,37 @@ const updateRecurrenceItemVisibility = (recurrenceRuleExpr, value, form) => {
6161
form.getEditor(recurrenceRuleExpr)?.changeValueByVisibility(value);
6262
};
6363

64+
const defaultFormOptions = {
65+
showValidationSummary: true,
66+
scrollingEnabled: true,
67+
colCount: 'auto',
68+
colCountByScreen: {
69+
lg: 2,
70+
xs: 1,
71+
},
72+
showColonAfterLabel: false,
73+
labelLocation: 'top',
74+
screenByWidth: (width) => (width < SCREEN_SIZE_OF_SINGLE_COLUMN || devices.current().deviceType !== 'desktop' ? 'xs' : 'lg'),
75+
elementAttr: {
76+
class: E2E_TEST_CLASSES.form,
77+
},
78+
};
79+
6480
export class AppointmentForm {
6581
scheduler: any;
6682

67-
form: any;
83+
form: Form;
6884

6985
// NOTE: flag to prevent double value set during form updating
7086
private isFormUpdating = false;
7187

7288
constructor(scheduler) {
7389
this.scheduler = scheduler;
74-
this.form = null;
90+
const element = $('<div>');
91+
92+
this.form = this.scheduler.createComponent(element, Form, {
93+
...defaultFormOptions,
94+
});
7595
}
7696

7797
get dxForm() {
@@ -103,13 +123,13 @@ export class AppointmentForm {
103123
const colSpan = isRecurrence ? 1 : 2;
104124

105125
const mainItems = [
106-
...this._createMainItems(expr, triggerResize, changeSize, allowTimeZoneEditing),
126+
...this.createMainItems(expr, triggerResize, changeSize, allowTimeZoneEditing),
107127
...this.scheduler.createResourceEditorModel(),
108128
];
109129

110130
changeSize(isRecurrence);
111131

112-
const items = [
132+
const items: Item[] = [
113133
{
114134
itemType: 'group',
115135
name: APPOINTMENT_FORM_GROUP_NAMES.Main,
@@ -124,49 +144,33 @@ export class AppointmentForm {
124144
name: APPOINTMENT_FORM_GROUP_NAMES.Recurrence,
125145
visible: isRecurrence,
126146
colSpan,
127-
items: this._createRecurrenceEditor(expr),
147+
items: this.createRecurrenceEditor(expr),
128148
},
129149
];
130150

131-
const element = $('<div>');
132-
133-
this.scheduler.createComponent(element, Form, {
151+
this.form.option({
134152
items,
135-
showValidationSummary: true,
136-
scrollingEnabled: true,
137-
colCount: 'auto',
138-
colCountByScreen: {
139-
lg: 2,
140-
xs: 1,
141-
},
142153
formData,
143-
showColonAfterLabel: false,
144-
labelLocation: 'top',
145-
onInitialized: (e) => {
146-
this.form = e.component;
147-
},
148-
screenByWidth: (width) => (width < SCREEN_SIZE_OF_SINGLE_COLUMN || devices.current().deviceType !== 'desktop' ? 'xs' : 'lg'),
149-
elementAttr: {
150-
class: E2E_TEST_CLASSES.form,
151-
},
152154
});
153155
}
154156

155-
_dateBoxValueChanged(args, dateExpr, isNeedCorrect) {
157+
private dateBoxValueChanged(args, dateExpr, isNeedCorrect) {
156158
validateAppointmentFormDate(args.component, args.value, args.previousValue);
157159

158160
const value = dateSerialization.deserializeDate(args.value);
159161
const previousValue = dateSerialization.deserializeDate(args.previousValue);
160162
const dateEditor = this.form.getEditor(dateExpr);
163+
// @ts-expect-error should be fixed in the future
161164
const dateValue = dateSerialization.deserializeDate(dateEditor.option('value'));
162165

163166
if (!this.isFormUpdating && dateValue && value && isNeedCorrect(dateValue, value)) {
164167
const duration = previousValue ? dateValue.getTime() - previousValue.getTime() : 0;
168+
// @ts-expect-error should be fixed in the future
165169
dateEditor.option('value', new Date(value.getTime() + duration));
166170
}
167171
}
168172

169-
_createTimezoneEditor(timeZoneExpr, secondTimeZoneExpr, visibleIndex, colSpan, isMainTimeZone, cssClass, visible = false) {
173+
private createTimezoneEditor(timeZoneExpr, secondTimeZoneExpr, visibleIndex, colSpan, isMainTimeZone, cssClass, visible = false) {
170174
const noTzTitle = messageLocalization.format('dxScheduler-noTimezoneTitle');
171175

172176
return {
@@ -189,6 +193,7 @@ export class AppointmentForm {
189193
const { form } = this;
190194
const secondTimezoneEditor = form.getEditor(secondTimeZoneExpr);
191195
if (isMainTimeZone) {
196+
// @ts-expect-error should be fixed in the future
192197
secondTimezoneEditor.option('value', args.value);
193198
}
194199
},
@@ -197,7 +202,7 @@ export class AppointmentForm {
197202
};
198203
}
199204

200-
_createDateBoxItems(dataExprs, allowTimeZoneEditing) {
205+
private createDateBoxItems(dataExprs, allowTimeZoneEditing) {
201206
const colSpan = allowTimeZoneEditing ? 2 : 1;
202207
const firstDayOfWeek = this.scheduler.getFirstDayOfWeek();
203208

@@ -209,11 +214,11 @@ export class AppointmentForm {
209214
'dxScheduler-editorLabelStartDate',
210215
E2E_TEST_CLASSES.startDateEditor,
211216
(args) => {
212-
this._dateBoxValueChanged(args, dataExprs.endDateExpr, (endValue, startValue) => endValue < startValue);
217+
this.dateBoxValueChanged(args, dataExprs.endDateExpr, (endValue, startValue) => endValue < startValue);
213218
},
214219
),
215220

216-
this._createTimezoneEditor(
221+
this.createTimezoneEditor(
217222
dataExprs.startDateTimeZoneExpr,
218223
dataExprs.endDateTimeZoneExpr,
219224
1,
@@ -230,11 +235,11 @@ export class AppointmentForm {
230235
'dxScheduler-editorLabelEndDate',
231236
E2E_TEST_CLASSES.endDateEditor,
232237
(args) => {
233-
this._dateBoxValueChanged(args, dataExprs.startDateExpr, (startValue, endValue) => endValue < startValue);
238+
this.dateBoxValueChanged(args, dataExprs.startDateExpr, (startValue, endValue) => endValue < startValue);
234239
},
235240
),
236241

237-
this._createTimezoneEditor(
242+
this.createTimezoneEditor(
238243
dataExprs.endDateTimeZoneExpr,
239244
dataExprs.startDateTimeZoneExpr,
240245
3,
@@ -246,7 +251,7 @@ export class AppointmentForm {
246251
];
247252
}
248253

249-
_changeFormItemDateType(name: string, groupName: string, isAllDay: boolean): void {
254+
private changeFormItemDateType(name: string, groupName: string, isAllDay: boolean): void {
250255
const editorPath = this.getEditorPath(name, groupName);
251256
const itemEditorOptions = this.form.itemOption(editorPath).editorOptions;
252257

@@ -257,7 +262,7 @@ export class AppointmentForm {
257262
this.form.itemOption(editorPath, 'editorOptions', newEditorOption);
258263
}
259264

260-
_createMainItems(dataExprs, triggerResize, changeSize, allowTimeZoneEditing) {
265+
private createMainItems(dataExprs, triggerResize, changeSize, allowTimeZoneEditing) {
261266
return [
262267
{
263268
name: this.normalizeEditorName(dataExprs.textExpr),
@@ -279,7 +284,7 @@ export class AppointmentForm {
279284
lg: 2,
280285
xs: 1,
281286
},
282-
items: this._createDateBoxItems(dataExprs, allowTimeZoneEditing),
287+
items: this.createDateBoxItems(dataExprs, allowTimeZoneEditing),
283288
},
284289
{
285290
itemType: 'group',
@@ -302,23 +307,28 @@ export class AppointmentForm {
302307
const { value } = args;
303308
const startDateEditor = this.form.getEditor(dataExprs.startDateExpr);
304309
const endDateEditor = this.form.getEditor(dataExprs.endDateExpr);
310+
// @ts-expect-error should be fixed in the future
305311
const startDate = dateSerialization.deserializeDate(startDateEditor.option('value'));
306312

307313
if (!this.isFormUpdating && startDate) {
308314
if (value) {
309315
const allDayStartDate = dateUtils.trimTime(startDate);
316+
// @ts-expect-error should be fixed in the future
310317
startDateEditor.option('value', new Date(allDayStartDate));
318+
// @ts-expect-error should be fixed in the future
311319
endDateEditor.option('value', new Date(allDayStartDate));
312320
} else {
313321
const startDateWithStartHour = getStartDateWithStartHour(startDate, this.scheduler.getStartDayHour());
314322
const endDate = this.scheduler.getCalculatedEndDate(startDateWithStartHour);
323+
// @ts-expect-error should be fixed in the future
315324
startDateEditor.option('value', startDateWithStartHour);
325+
// @ts-expect-error should be fixed in the future
316326
endDateEditor.option('value', endDate);
317327
}
318328
}
319329

320-
this._changeFormItemDateType(dataExprs.startDateExpr, 'Main', value);
321-
this._changeFormItemDateType(dataExprs.endDateExpr, 'Main', value);
330+
this.changeFormItemDateType(dataExprs.startDateExpr, 'Main', value);
331+
this.changeFormItemDateType(dataExprs.endDateExpr, 'Main', value);
322332
},
323333
},
324334
}, {
@@ -370,7 +380,7 @@ export class AppointmentForm {
370380
];
371381
}
372382

373-
_createRecurrenceEditor(dataExprs) {
383+
private createRecurrenceEditor(dataExprs) {
374384
return [{
375385
name: this.normalizeEditorName(dataExprs.recurrenceRuleExpr),
376386
dataField: dataExprs.recurrenceRuleExpr,
@@ -387,7 +397,7 @@ export class AppointmentForm {
387397
}];
388398
}
389399

390-
setEditorsType(allDay) {
400+
private setEditorsType(allDay) {
391401
const { startDateExpr, endDateExpr } = this.scheduler.getDataAccessors().expr;
392402

393403
const startDateItemPath = this.getEditorPath(startDateExpr, 'Main');
@@ -407,13 +417,13 @@ export class AppointmentForm {
407417
}
408418
}
409419

410-
updateRecurrenceEditorStartDate(date, expression) {
420+
private updateRecurrenceEditorStartDate(date, expression) {
411421
const options = { startDate: date };
412422

413423
this.setEditorOptions(expression, 'Recurrence', options);
414424
}
415425

416-
setEditorOptions(name, groupName: 'Main' | 'Recurrence', options) {
426+
private setEditorOptions(name, groupName: 'Main' | 'Recurrence', options) {
417427
const editorPath = this.getEditorPath(name, groupName);
418428
const editor = this.form.itemOption(editorPath);
419429

0 commit comments

Comments
 (0)