Skip to content

Commit 57414a8

Browse files
marker-daomarker dao ®
andauthored
TextEditor ClearButton and Label: Improve typing (#32538)
Co-authored-by: marker dao ® <youdontknow@marker-dao.eth>
1 parent 773dd0f commit 57414a8

3 files changed

Lines changed: 57 additions & 39 deletions

File tree

packages/devextreme/js/__internal/ui/text_box/m_text_editor.base.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,16 +600,16 @@ class TextEditorBase<
600600
onClickHandler: (): void => {
601601
this.focus();
602602
},
603-
onHoverHandler: (e): void => { e.stopPropagation(); },
604-
onActiveHandler: (e): void => { e.stopPropagation(); },
603+
onHoverHandler: (e: MouseEvent | PointerEvent): void => { e.stopPropagation(); },
604+
onActiveHandler: (e: MouseEvent | PointerEvent): void => { e.stopPropagation(); },
605605
$editor: this.$element(),
606606
text: label,
607607
mark: labelMark,
608608
mode: labelMode,
609609
rtlEnabled,
610610
containsButtonsBefore: !!this._$beforeButtonsContainer,
611-
getContainerWidth: () => this._getLabelContainerWidth(),
612-
getBeforeWidth: () => this._getLabelBeforeWidth(),
611+
getContainerWidth: (): number => this._getLabelContainerWidth(),
612+
getBeforeWidth: (): number => this._getLabelBeforeWidth(),
613613
};
614614

615615
this._label = new TextEditorLabelCreator(labelConfig);
@@ -931,14 +931,16 @@ class TextEditorBase<
931931
this._input().attr({ placeholder: this._getPlaceholderAttr() });
932932
break;
933933
case 'label':
934-
this._label.updateText(value);
934+
// @ts-expect-error ts-error
935+
this._label.updateText(value ?? '');
935936
this._setFieldAria(true);
936937
break;
937938
case 'labelMark':
938-
this._label.updateMark(value);
939+
// @ts-expect-error ts-error
940+
this._label.updateMark(value ?? '');
939941
break;
940942
case 'labelMode':
941-
this._label.updateMode(value);
943+
this._label.updateMode(value as LabelMode);
942944
this._setFieldAria();
943945
break;
944946
case 'width':

packages/devextreme/js/__internal/ui/text_box/m_text_editor.clear.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,15 @@ export default class ClearButton extends TextEditorButton {
6767
$editor.toggleClass(TEXTEDITOR_SHOW_CLEAR_BUTTON_CLASS, isVisible);
6868
}
6969

70-
// @ts-expect-error ts-error
71-
update(rendered = false): void {
70+
update(rendered = false): boolean {
7271
if (!rendered) {
7372
super.update();
7473
}
7574

7675
const { editor, instance } = this;
7776

7877
if (!editor) {
79-
return;
78+
return false;
8079
}
8180

8281
const $editor = editor.$element();
@@ -88,5 +87,7 @@ export default class ClearButton extends TextEditorButton {
8887
}
8988

9089
this._legacyRender($editor, isVisible);
90+
91+
return isVisible;
9192
}
9293
}

packages/devextreme/js/__internal/ui/text_box/m_text_editor.label.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { LabelMode } from '@js/common';
12
import { name as click } from '@js/common/core/events/click';
23
import { active } from '@js/common/core/events/core/emitter.feedback';
34
import eventsEngine from '@js/common/core/events/core/events_engine';
@@ -19,10 +20,26 @@ const LABEL_BEFORE_CLASS = 'dx-label-before';
1920
const LABEL_CLASS = 'dx-label';
2021
const LABEL_AFTER_CLASS = 'dx-label-after';
2122

22-
class TextEditorLabel {
23+
export interface TextEditorLabelProperties {
24+
$editor: dxElementWrapper;
25+
text?: string;
26+
mark?: string;
27+
mode?: LabelMode;
28+
rtlEnabled?: boolean;
29+
containsButtonsBefore?: boolean;
30+
beforeWidth?: number;
31+
containerWidth?: number;
32+
getContainerWidth: () => number;
33+
getBeforeWidth: () => number;
34+
onClickHandler: () => void;
35+
onHoverHandler: (e: MouseEvent | PointerEvent) => void;
36+
onActiveHandler: (e: MouseEvent | PointerEvent) => void;
37+
}
38+
39+
export class TextEditorLabel {
2340
public NAME: string;
2441

25-
_props: any;
42+
_props: TextEditorLabelProperties;
2643

2744
_id: string;
2845

@@ -36,7 +53,7 @@ class TextEditorLabel {
3653

3754
_$root!: dxElementWrapper;
3855

39-
constructor(props) {
56+
constructor(props: TextEditorLabelProperties) {
4057
this.NAME = 'dxLabel';
4158
this._props = props;
4259

@@ -97,25 +114,24 @@ class TextEditorLabel {
97114
eventsEngine.off(this._$labelSpan, activeEventName);
98115

99116
if (this._isVisible() && this._isOutsideMode()) {
100-
eventsEngine.on(this._$labelSpan, clickEventName, (e) => {
101-
// @ts-expect-error
102-
const selectedText = getWindow().getSelection().toString();
117+
eventsEngine.on(this._$labelSpan, clickEventName, (e: MouseEvent) => {
118+
const selectedText = getWindow()?.getSelection()?.toString();
103119

104120
if (selectedText === '') {
105121
this._props.onClickHandler();
106122
e.preventDefault();
107123
}
108124
});
109-
eventsEngine.on(this._$labelSpan, hoverStartEventName, (e) => {
125+
eventsEngine.on(this._$labelSpan, hoverStartEventName, (e: MouseEvent) => {
110126
this._props.onHoverHandler(e);
111127
});
112-
eventsEngine.on(this._$labelSpan, activeEventName, (e) => {
128+
eventsEngine.on(this._$labelSpan, activeEventName, (e: MouseEvent) => {
113129
this._props.onActiveHandler(e);
114130
});
115131
}
116132
}
117133

118-
_updateEditorLabelClass(visible): void {
134+
_updateEditorLabelClass(visible: boolean): void {
119135
this._props.$editor
120136
.removeClass(TEXTEDITOR_WITH_FLOATING_LABEL_CLASS)
121137
.removeClass(TEXTEDITOR_LABEL_OUTSIDE_CLASS)
@@ -134,7 +150,7 @@ class TextEditorLabel {
134150
}
135151
}
136152

137-
_isOutsideMode() {
153+
_isOutsideMode(): boolean {
138154
return this._props.mode === 'outside';
139155
}
140156

@@ -150,18 +166,18 @@ class TextEditorLabel {
150166
}
151167

152168
_updateMark(): void {
153-
this._$labelSpan.attr('data-mark', this._props.mark);
169+
this._$labelSpan.attr('data-mark', this._props.mark ?? null);
154170
}
155171

156172
_updateText(): void {
157-
this._$labelSpan.text(this._props.text);
173+
this._$labelSpan.text(this._props.text ?? '');
158174
}
159175

160176
_updateBeforeWidth(): void {
161177
if (this._isVisible()) {
162178
const width = this._props.beforeWidth ?? this._props.getBeforeWidth();
163-
// @ts-expect-error
164-
this._$before.css({ width });
179+
180+
this._$before?.css({ width });
165181

166182
this._updateLabelTransform();
167183
}
@@ -186,55 +202,54 @@ class TextEditorLabel {
186202
}
187203
}
188204

189-
$element() {
205+
$element(): dxElementWrapper {
190206
return this._$root;
191207
}
192208

193-
isVisible() {
209+
isVisible(): boolean {
194210
return this._isVisible();
195211
}
196212

197-
// @ts-expect-error
198-
getId() {
199-
if (this._isVisible()) return this._id;
213+
getId(): string | undefined {
214+
if (this._isVisible()) {
215+
return this._id;
216+
}
217+
218+
return undefined;
200219
}
201220

202-
updateMode(mode): void {
221+
updateMode(mode: LabelMode): void {
203222
this._props.mode = mode;
204223
this._toggleMarkupVisibility();
205224
this._updateBeforeWidth();
206225
this._updateMaxWidth();
207226
}
208227

209-
updateText(text): void {
228+
updateText(text: string): void {
210229
this._props.text = text;
211230
this._updateText();
212231
this._toggleMarkupVisibility();
213232
this._updateBeforeWidth();
214233
this._updateMaxWidth();
215234
}
216235

217-
updateMark(mark): void {
236+
updateMark(mark: string): void {
218237
this._props.mark = mark;
219238
this._updateMark();
220239
}
221240

222-
updateContainsButtonsBefore(containsButtonsBefore): void {
241+
updateContainsButtonsBefore(containsButtonsBefore: boolean): void {
223242
this._props.containsButtonsBefore = containsButtonsBefore;
224243
this._updateEditorBeforeButtonsClass();
225244
}
226245

227-
updateBeforeWidth(beforeWidth): void {
246+
updateBeforeWidth(beforeWidth: number): void {
228247
this._props.beforeWidth = beforeWidth;
229248
this._updateBeforeWidth();
230249
}
231250

232-
updateMaxWidth(containerWidth): void {
251+
updateMaxWidth(containerWidth: number): void {
233252
this._props.containerWidth = containerWidth;
234253
this._updateMaxWidth();
235254
}
236255
}
237-
238-
export {
239-
TextEditorLabel,
240-
};

0 commit comments

Comments
 (0)