Skip to content

Commit 357e1f5

Browse files
r-farkhutdinovRuslan Farkhutdinov
andauthored
DropDownEditor: Improve types (DevExpress#33760)
Co-authored-by: Ruslan Farkhutdinov <ruslan.farkhutdinov@devexpress.com>
1 parent fcbfb6c commit 357e1f5

9 files changed

Lines changed: 468 additions & 353 deletions

File tree

packages/devextreme/js/__internal/ui/date_box/m_date_box.strategy.list.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ class ListStrategy extends DateBoxStrategy {
324324
}
325325

326326
_updatePopupHeight(): void {
327-
const dropDownOptionsHeight = getSizeValue(this.dateBox.option('dropDownOptions.height'));
327+
const { dropDownOptions } = this.dateBox.option();
328+
const dropDownOptionsHeight = getSizeValue(dropDownOptions?.height);
328329

329330
if (dropDownOptionsHeight === undefined || dropDownOptionsHeight === 'auto') {
330331
this.dateBox._setPopupOption('height', 'auto');

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

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,34 @@ import messageLocalization from '@js/common/core/localization/message';
33
import type { dxElementWrapper } from '@js/core/renderer';
44
import $ from '@js/core/renderer';
55
import { extend } from '@js/core/utils/extend';
6+
import type { DxEvent } from '@js/events';
67
import type { Properties as ButtonProperties } from '@js/ui/button';
78
import Button from '@js/ui/button';
89

10+
import type TextEditorBase from '../text_box/text_editor.base';
911
import TextEditorButton from '../text_box/texteditor_button_collection/button';
12+
import type DropDownEditor from './m_drop_down_editor';
13+
import type { DropDownEditorProperties } from './m_drop_down_editor';
1014

1115
const DROP_DOWN_EDITOR_BUTTON_CLASS = 'dx-dropdowneditor-button';
1216
const DROP_DOWN_EDITOR_BUTTON_VISIBLE = 'dx-dropdowneditor-button-visible';
17+
const STATE_FOCUSED_CLASS = 'dx-state-focused';
18+
const BUTTON_CLASS = 'dx-button';
19+
const BUTTON_MODE_CONTAINED_CLASS = 'dx-button-mode-contained';
1320

1421
const BUTTON_MESSAGE = 'dxDropDownEditor-selectLabel';
1522

23+
type DropDownButtonOptions = Pick<ButtonProperties, 'focusStateEnabled' | 'hoverStateEnabled' | 'activeStateEnabled' | 'disabled' | 'visible' | 'template'> & { useInkRipple: boolean };
24+
1625
export default class DropDownButton extends TextEditorButton {
17-
currentTemplate: any;
26+
// @ts-expect-error narrow type to enable DropDownEditor-specific options
27+
declare editor: DropDownEditor | null;
28+
29+
declare instance: Button | null;
30+
31+
currentTemplate: DropDownEditorProperties['dropDownButtonTemplate'] | null;
1832

19-
constructor(name, editor, options) {
33+
constructor(name: string, editor: TextEditorBase, options: ButtonProperties) {
2034
super(name, editor, options);
2135

2236
this.currentTemplate = null;
@@ -32,7 +46,6 @@ export default class DropDownButton extends TextEditorButton {
3246
return;
3347
}
3448

35-
// @ts-expect-error openOnFieldClick should be typed
3649
const { openOnFieldClick } = this.editor?.option() ?? {};
3750

3851
if (!openOnFieldClick) {
@@ -41,8 +54,8 @@ export default class DropDownButton extends TextEditorButton {
4154
}
4255
});
4356

44-
eventsEngine.on(instance.$element(), 'mousedown', (e) => {
45-
if (this.editor?.$element().is('.dx-state-focused')) {
57+
eventsEngine.on(instance.$element(), 'mousedown', (e: DxEvent<MouseEvent>) => {
58+
if (this.editor?.$element().is(`.${STATE_FOCUSED_CLASS}`)) {
4659
e.preventDefault();
4760
}
4861
});
@@ -85,55 +98,69 @@ export default class DropDownButton extends TextEditorButton {
8598
};
8699
}
87100

88-
_getOptions() {
101+
_getOptions(): DropDownButtonOptions {
89102
const { editor } = this;
90-
91103
const visible = this._isVisible();
92-
const isReadOnly = editor?.option('readOnly');
104+
const { readOnly } = editor?.option() ?? {};
93105

94106
const options = {
95107
focusStateEnabled: false,
96108
hoverStateEnabled: false,
97109
activeStateEnabled: false,
98110
useInkRipple: false,
99-
disabled: isReadOnly,
111+
disabled: readOnly,
100112
visible,
101113
};
102114

103115
this._addTemplate(options);
116+
104117
return options;
105118
}
106119

107120
_isVisible(): boolean {
108121
const { editor } = this;
109-
// @ts-expect-error
110-
return super._isVisible() && editor?.option('showDropDownButton');
122+
const { showDropDownButton } = editor?.option() ?? {};
123+
124+
return super._isVisible() && !!showDropDownButton;
111125
}
112126

113127
// TODO: get rid of it
114-
_legacyRender($editor, $element, isVisible) {
115-
$editor.toggleClass(DROP_DOWN_EDITOR_BUTTON_VISIBLE, isVisible);
128+
// eslint-disable-next-line class-methods-use-this
129+
_legacyRender(
130+
$editor?: dxElementWrapper,
131+
$element?: dxElementWrapper,
132+
isVisible?: boolean,
133+
): void {
134+
$editor?.toggleClass(DROP_DOWN_EDITOR_BUTTON_VISIBLE, isVisible);
116135

117136
if ($element) {
118137
$element
119-
.removeClass('dx-button')
120-
.removeClass('dx-button-mode-contained')
138+
.removeClass(BUTTON_CLASS)
139+
.removeClass(BUTTON_MODE_CONTAINED_CLASS)
121140
.addClass(DROP_DOWN_EDITOR_BUTTON_CLASS);
122141
}
123142
}
124143

125-
_isSameTemplate() {
126-
return this.editor?.option('dropDownButtonTemplate') === this.currentTemplate;
144+
_isSameTemplate(): boolean {
145+
const { editor } = this;
146+
const { dropDownButtonTemplate } = editor?.option() ?? {};
147+
148+
return dropDownButtonTemplate === this.currentTemplate;
127149
}
128150

129-
_addTemplate(options): void {
130-
if (!this._isSameTemplate()) {
131-
options.template = this.editor?._getTemplateByOption('dropDownButtonTemplate');
132-
this.currentTemplate = this.editor?.option('dropDownButtonTemplate');
151+
_addTemplate(options: DropDownButtonOptions): void {
152+
if (this._isSameTemplate()) {
153+
return;
133154
}
155+
156+
const { editor } = this;
157+
const { dropDownButtonTemplate } = editor?.option() ?? {};
158+
159+
options.template = this.editor?._getTemplateByOption('dropDownButtonTemplate');
160+
this.currentTemplate = dropDownButtonTemplate;
134161
}
135162

136-
// @ts-expect-error
163+
// @ts-expect-error inconsistent return type, fix in TextEditorButton
137164
update(): void {
138165
const shouldUpdate = super.update();
139166

@@ -143,9 +170,8 @@ export default class DropDownButton extends TextEditorButton {
143170
const $editor = editor?.$element();
144171
const options = this._getOptions();
145172

146-
// @ts-expect-error
147173
instance?.option(options);
148-
this._legacyRender($editor, (instance as Button)?.$element(), options.visible);
174+
this._legacyRender($editor, instance?.$element(), options.visible);
149175
}
150176
}
151177
}

0 commit comments

Comments
 (0)