Skip to content

Commit 24b2ab7

Browse files
marker-daomarker dao ®
andauthored
TextBox Utils: Improve typing (#32550)
Co-authored-by: marker dao ® <youdontknow@marker-dao.eth>
1 parent dde961c commit 24b2ab7

2 files changed

Lines changed: 47 additions & 28 deletions

File tree

packages/devextreme/js/__internal/ui/text_box/m_utils.caret.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ export interface CaretRange {
99
end: number;
1010
}
1111

12-
const {
13-
ios,
14-
// @ts-expect-error Device type doesn't contain mac
15-
mac,
16-
} = devices.real();
12+
// @ts-expect-error mac should be correctly typed in environment.d.ts
13+
const { ios, mac } = devices.real();
1714

1815
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1916
const isFocusingOnCaretChange = ios || mac;
Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,77 @@
11
import { isDxMouseWheelEvent } from '@js/common/core/events/utils/index';
2+
import type { dxElementWrapper } from '@js/core/renderer';
23
import $ from '@js/core/renderer';
4+
import type { PointerInteractionEvent } from '@js/events';
35

4-
// @ts-expect-error
5-
const allowScroll = function (container, delta, shiftKey?: boolean) {
6+
type PointerInteractionEventTarget = Element | null;
7+
8+
interface TextBoxScrollData {
9+
validate: (e: PointerInteractionEvent & {
10+
target?: PointerInteractionEventTarget;
11+
delta?: number;
12+
_needSkipEvent?: boolean;
13+
}) => boolean;
14+
}
15+
16+
export const allowScroll = (
17+
container: dxElementWrapper,
18+
delta: number,
19+
shiftKey?: boolean,
20+
): boolean | undefined => {
621
const $container = $(container);
7-
const scrollTopPos = shiftKey ? $container.scrollLeft() : $container.scrollTop();
22+
// @ts-expect-error scrollLeft, scrollTop should be correctly typed in renderer.d.ts
23+
const scrollTopPos = parseFloat(shiftKey ? $container.scrollLeft() : $container.scrollTop());
824

925
const prop = shiftKey ? 'Width' : 'Height';
10-
// @ts-expect-error
11-
const scrollSize = $container.prop(`scroll${prop}`);
12-
// @ts-expect-error
13-
const clientSize = $container.prop(`client${prop}`);
14-
// @ts-expect-error
26+
27+
// @ts-expect-error prop should be correctly typed in renderer.d.ts
28+
const scrollSize = parseFloat($container.prop(`scroll${prop}`));
29+
// @ts-expect-error prop should be correctly typed in renderer.d.ts
30+
const clientSize = parseFloat($container.prop(`client${prop}`));
31+
1532
// NOTE: round to the nearest integer towards zero
1633
const scrollBottomPos = (scrollSize - clientSize - scrollTopPos) | 0;
17-
// @ts-expect-error
34+
1835
if (scrollTopPos === 0 && scrollBottomPos === 0) {
1936
return false;
2037
}
21-
// @ts-expect-error
38+
2239
const isScrollFromTop = scrollTopPos === 0 && delta >= 0;
2340
const isScrollFromBottom = scrollBottomPos === 0 && delta <= 0;
24-
// @ts-expect-error
2541
const isScrollFromMiddle = scrollTopPos > 0 && scrollBottomPos > 0;
2642

2743
if (isScrollFromTop || isScrollFromBottom || isScrollFromMiddle) {
2844
return true;
2945
}
46+
47+
return undefined;
3048
};
3149

32-
const prepareScrollData = function (container, validateTarget?: any) {
50+
export const prepareScrollData = (
51+
container: dxElementWrapper,
52+
validateTarget?: boolean,
53+
): TextBoxScrollData => {
3354
const $container = $(container);
34-
const isCorrectTarget = function (eventTarget) {
35-
return validateTarget ? $(eventTarget).is(container) : true;
36-
};
3755

38-
return {
39-
// @ts-expect-error
40-
validate(e) {
56+
const isCorrectTarget = (
57+
eventTarget: PointerInteractionEventTarget,
58+
): boolean => (validateTarget ? $(eventTarget).is(container) : true);
59+
60+
const scrollData: TextBoxScrollData = {
61+
validate: (e) => {
4162
if (isDxMouseWheelEvent(e) && isCorrectTarget(e.target)) {
42-
if (allowScroll($container, -e.delta, e.shiftKey)) {
63+
if (allowScroll($container, -(e.delta ?? 0), e.shiftKey)) {
4364
e._needSkipEvent = true;
65+
4466
return true;
4567
}
68+
4669
return false;
4770
}
71+
72+
return false;
4873
},
4974
};
50-
};
5175

52-
export {
53-
allowScroll,
54-
prepareScrollData,
76+
return scrollData;
5577
};

0 commit comments

Comments
 (0)