Skip to content

Commit fdf822b

Browse files
marker-daomarker dao ®
andauthored
TextEditorMask: Refactor and improve typing (#32572)
Co-authored-by: marker dao ® <youdontknow@marker-dao.eth>
1 parent 1ecaf60 commit fdf822b

6 files changed

Lines changed: 600 additions & 325 deletions

File tree

packages/devextreme/js/__internal/ui/date_box/date_box.mask.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,10 @@ class DateBoxMask extends DateBoxBase {
291291
const caret = this._caret();
292292
const { text = '' } = this.option();
293293

294-
return caret.end - caret.start === text.length;
294+
const caretStart = caret?.start ?? 0;
295+
const caretEnd = caret?.end ?? 0;
296+
297+
return caretEnd - caretStart === text.length;
295298
}
296299

297300
_getFormatPattern(): string {
@@ -661,7 +664,10 @@ class DateBoxMask extends DateBoxBase {
661664
this._loadMaskValue(this._maskValue);
662665
const { text } = this.option();
663666
if (text) {
664-
this._activePartIndex = getDatePartIndexByPosition(this._dateParts, this._caret().start);
667+
this._activePartIndex = getDatePartIndexByPosition(
668+
this._dateParts,
669+
this._caret()?.start ?? 0,
670+
);
665671

666672
if (!this._isAllSelected()) {
667673
this._clearSearchValue();
@@ -686,6 +692,7 @@ class DateBoxMask extends DateBoxBase {
686692

687693
_maskPasteHandler(e: DxEvent): void {
688694
const { text } = this.option();
695+
// @ts-expect-error text
689696
const newText = this._replaceSelectedText(text, this._caret(), clipboardText(e));
690697
const date = dateLocalization.parse(newText, this._getFormatPattern());
691698

packages/devextreme/js/__internal/ui/number_box/m_number_box.mask.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class NumberBoxMask extends NumberBoxBase<NumberBoxMaskProperties> {
105105
this._caretTimeout = undefined;
106106
const caret = this._caret();
107107

108-
if (caret.start === caret.end && this._useMaskBehavior()) {
108+
if (caret?.start === caret?.end && this._useMaskBehavior()) {
109109
const text = this._getInputVal();
110110
const decimalSeparatorIndex = this._getTextSeparatorIndex(text);
111111

@@ -254,8 +254,8 @@ class NumberBoxMask extends NumberBoxBase<NumberBoxMaskProperties> {
254254
_removeHandler(e) {
255255
const caret = this._caret();
256256
const text = this._getInputVal();
257-
let { start } = caret;
258-
let { end } = caret;
257+
258+
let { start = 0, end = 0 } = caret ?? {};
259259

260260
this._lastKey = getChar(e);
261261
this._lastKeyName = normalizeKeyName(e);
@@ -265,7 +265,11 @@ class NumberBoxMask extends NumberBoxBase<NumberBoxMaskProperties> {
265265

266266
if (start === end) {
267267
const caretPosition = start;
268-
const canDelete = isBackspaceKey && caretPosition > 0 || isDeleteKey && caretPosition < text.length;
268+
269+
const canDelete = isBackspaceKey
270+
&& caretPosition > 0
271+
|| isDeleteKey
272+
&& caretPosition < text.length;
269273

270274
if (canDelete) {
271275
isDeleteKey && end++;
@@ -520,7 +524,7 @@ class NumberBoxMask extends NumberBoxBase<NumberBoxMaskProperties> {
520524
const caret = this._caret();
521525
const point = number.getDecimalSeparator();
522526
const pointIndex = this._getTextSeparatorIndex(text);
523-
const isCaretOnFloat = pointIndex >= 0 && pointIndex < caret.start;
527+
const isCaretOnFloat = pointIndex >= 0 && pointIndex < (caret?.start ?? 0);
524528
const textParts = this._removeStubs(text, true).split(point);
525529

526530
if (!isCaretOnFloat || textParts.length !== 2) {
@@ -702,7 +706,8 @@ class NumberBoxMask extends NumberBoxBase<NumberBoxMaskProperties> {
702706
}
703707

704708
const caret = this._caret();
705-
if (caret.start !== caret.end) {
709+
710+
if (caret?.start !== caret?.end) {
706711
if (normalizeKeyName(e) === MINUS_KEY) {
707712
this._applyRevertedSign(e, caret, true);
708713
return;

0 commit comments

Comments
 (0)