Skip to content

Commit 9aada18

Browse files
r-farkhutdinovRuslan Farkhutdinov
andauthored
DropDownBox, SelectBox, ColorBox, TagBox: Log fieldTemplate deprecation warning (#30846)
Co-authored-by: Ruslan Farkhutdinov <ruslan.farkhutdinov@devexpress.com>
1 parent f797349 commit 9aada18

File tree

8 files changed

+150
-6
lines changed

8 files changed

+150
-6
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
export const dropDownEditorsFieldTemplateWarnings = [
2+
'W0001 - dxSelectBox - \'fieldTemplate\'',
3+
'W0001 - dxColorBox - \'fieldTemplate\'',
4+
'W0001 - dxDropDownBox - \'fieldTemplate\'',
5+
'W0001 - dxTagBox - \'fieldTemplate\'',
6+
];
7+
18
export const knownWarnings = [
29
'W0019 -',
310
'W0022 -',
411
'W2108 -',
12+
...dropDownEditorsFieldTemplateWarnings,
513
];

packages/devextreme/js/__internal/ui/color_box/m_color_box.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
349349
return value;
350350
}
351351

352+
// eslint-disable-next-line class-methods-use-this
353+
_shouldLogFieldTemplateDeprecationWarning(): boolean {
354+
return true;
355+
}
356+
352357
_clean(): void {
353358
super._clean();
354359
delete this._shouldSaveEmptyValue;

packages/devextreme/js/__internal/ui/drop_down_editor/m_drop_down_editor.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ const isIOs = devices.current().platform === 'ios';
5656

5757
type HideOnOutsideClickEvent = DxEvent<MouseEvent | PointerEvent | TouchEvent>;
5858

59+
export const DROP_DOWN_EDITOR_DEPRECATED_OPTIONS = {
60+
fieldTemplate: {
61+
since: '25.2',
62+
message: 'Use the \'fieldAddons\' option instead',
63+
},
64+
};
65+
5966
export interface DropDownEditorProperties extends Omit<
6067
Properties,
6168
| 'onChange'
@@ -77,6 +84,8 @@ export interface DropDownEditorProperties extends Omit<
7784
> {
7885
buttonsLocation?: string;
7986

87+
fieldTemplate?: string | Element | Function | null;
88+
8089
_onMarkupRendered?: () => void;
8190

8291
onPopupInitialized?: (e: { component: DropDownEditor; popup: Popup }) => void;
@@ -373,18 +382,27 @@ class DropDownEditor<
373382

374383
_cleanFocusState(): void {
375384
super._cleanFocusState();
385+
const { fieldTemplate } = this.option();
376386

377-
if (this.option('fieldTemplate')) {
387+
if (fieldTemplate) {
378388
this._detachFocusEvents();
379389
}
380390
}
381391

382392
_getFieldTemplate() {
383-
return this.option('fieldTemplate') && this._getTemplateByOption('fieldTemplate');
393+
const { fieldTemplate } = this.option();
394+
395+
if (!fieldTemplate) {
396+
return;
397+
}
398+
399+
return this._getTemplate(fieldTemplate);
384400
}
385401

386402
_renderMask(): void {
387-
if (this.option('fieldTemplate')) {
403+
const { fieldTemplate } = this.option();
404+
405+
if (fieldTemplate) {
388406
return;
389407
}
390408

@@ -1117,6 +1135,19 @@ class DropDownEditor<
11171135
return super._getSubmitElement();
11181136
}
11191137

1138+
// eslint-disable-next-line class-methods-use-this
1139+
_shouldLogFieldTemplateDeprecationWarning(): boolean {
1140+
return false;
1141+
}
1142+
1143+
_setDeprecatedOptions(): void {
1144+
super._setDeprecatedOptions();
1145+
1146+
if (this._shouldLogFieldTemplateDeprecationWarning()) {
1147+
extend(this._deprecatedOptions, DROP_DOWN_EDITOR_DEPRECATED_OPTIONS);
1148+
}
1149+
}
1150+
11201151
_dispose(): void {
11211152
this._detachFocusOutEvents();
11221153
super._dispose();

packages/devextreme/js/__internal/ui/m_drop_down_box.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ class DropDownBox<
311311
// eslint-disable-next-line class-methods-use-this
312312
_setCollectionWidgetOption(): void {}
313313

314+
// eslint-disable-next-line class-methods-use-this
315+
_shouldLogFieldTemplateDeprecationWarning(): boolean {
316+
return true;
317+
}
318+
314319
_optionChanged(args) {
315320
// @ts-expect-error ts-error
316321
this._dataExpressionOptionChanged(args);

packages/devextreme/js/__internal/ui/m_lookup.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,11 @@ class Lookup extends DropDownList<LookupProperties> {
321321
}
322322

323323
_renderField() {
324+
const { fieldTemplate: fieldTemplateOption } = this.option();
325+
324326
const fieldTemplate = this._getTemplateByOption('fieldTemplate');
325327

326-
if (fieldTemplate && this.option('fieldTemplate')) {
328+
if (fieldTemplate && fieldTemplateOption) {
327329
this._renderFieldTemplate(fieldTemplate);
328330
return;
329331
}

packages/devextreme/js/__internal/ui/m_select_box.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,10 @@ class SelectBox<
444444
}
445445

446446
_updateField(item): void {
447+
const { fieldTemplate: fieldTemplateOption } = this.option();
447448
const fieldTemplate = this._getTemplateByOption('fieldTemplate');
448449

449-
if (!(fieldTemplate && this.option('fieldTemplate'))) {
450+
if (!(fieldTemplate && fieldTemplateOption)) {
450451
// @ts-expect-error ts-error
451452
const text = this._displayGetter(item);
452453

@@ -963,6 +964,11 @@ class SelectBox<
963964
this._caret({ start: valueLength, end: displayValue.length });
964965
}
965966

967+
// eslint-disable-next-line class-methods-use-this
968+
_shouldLogFieldTemplateDeprecationWarning(): boolean {
969+
return true;
970+
}
971+
966972
_dispose(): void {
967973
this._renderInputValueAsync = noop;
968974
delete this._loadItemDeferred;

packages/devextreme/js/__internal/ui/m_tag_box.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,8 @@ class TagBox<
562562
}
563563

564564
_renderField(): void {
565-
const isDefaultFieldTemplate = !isDefined(this.option('fieldTemplate'));
565+
const { fieldTemplate } = this.option();
566+
const isDefaultFieldTemplate = !isDefined(fieldTemplate);
566567

567568
this.$element()
568569
.toggleClass(TAGBOX_DEFAULT_FIELD_TEMPLATE_CLASS, isDefaultFieldTemplate)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import 'ui/select_box';
2+
import 'ui/tag_box';
3+
import 'ui/color_box';
4+
import 'ui/drop_down_box';
5+
import errors from 'core/errors';
6+
7+
const LICENSE_WARNING = 'W0022';
8+
9+
const FIELD_TEMPLATE_WARNING_ARGS = (componentName) => [
10+
'W0001',
11+
componentName,
12+
'fieldTemplate',
13+
'25.2',
14+
'Use the \'fieldAddons\' option instead',
15+
];
16+
17+
const FIELD_TEMPLATE_COMPONENTS = [
18+
{ name: 'dxSelectBox', selector: '#selectBox' },
19+
{ name: 'dxTagBox', selector: '#tagBox' },
20+
{ name: 'dxColorBox', selector: '#colorBox' },
21+
{ name: 'dxDropDownBox', selector: '#dropDownBox' },
22+
];
23+
24+
QUnit.testStart(() => {
25+
const markup =
26+
'<div id="selectBox"></div>\
27+
<div id="tagBox"></div>\
28+
<div id="colorBox"></div>\
29+
<div id="dropDownBox"></div>';
30+
31+
$('#qunit-fixture').html(markup);
32+
});
33+
34+
QUnit.module('Deprecated fieldTemplate', {
35+
beforeEach() {
36+
this.errorsSpy = sinon.spy(errors, 'log');
37+
38+
this.getFilteredWarnings = () =>
39+
this.errorsSpy.getCalls().filter(({ args }) => args[0] !== LICENSE_WARNING);
40+
41+
this.assertWarningIsCorrect = (assert, name) => {
42+
const warnings = this.getFilteredWarnings();
43+
44+
assert.strictEqual(warnings.length, 1, 'only one warning logged');
45+
assert.deepEqual(
46+
warnings[0].args,
47+
FIELD_TEMPLATE_WARNING_ARGS(name),
48+
'warning is raised with correct parameters'
49+
);
50+
};
51+
},
52+
afterEach() {
53+
errors.log.restore();
54+
},
55+
}, () => {
56+
const fieldTemplate = () => $('<div>').dxTextBox();
57+
58+
FIELD_TEMPLATE_COMPONENTS.forEach(({ name, selector }) => {
59+
QUnit.test(
60+
`${name}: fieldTemplate should raise a deprecation warning on initialization`,
61+
function(assert) {
62+
$(selector)[name]({ fieldTemplate })[name]('instance');
63+
this.assertWarningIsCorrect(assert, name);
64+
}
65+
);
66+
67+
QUnit.test(
68+
`${name}: fieldTemplate should raise a deprecation warning on option set`,
69+
function(assert) {
70+
const instance = $(selector)[name]()[name]('instance');
71+
72+
instance.option({ fieldTemplate });
73+
74+
this.assertWarningIsCorrect(assert, name);
75+
}
76+
);
77+
78+
QUnit.test(`${name}: does not log extra warning on value change call`, function(assert) {
79+
const instance = $(selector)[name]({ fieldTemplate })[name]('instance');
80+
81+
instance.option('value', '');
82+
83+
this.assertWarningIsCorrect(assert, name);
84+
});
85+
});
86+
});

0 commit comments

Comments
 (0)