Skip to content

Commit 780a217

Browse files
authored
refactor: remove javascript , use typescript rewrite (#329)
* refactor: remove javascript , use typescript rewrite * fix: revert js => ts * fix: revert js => ts * fix: format
1 parent 99e3753 commit 780a217

File tree

8 files changed

+41
-34
lines changed

8 files changed

+41
-34
lines changed

src/Children/toArray.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function toArray(
1111
): React.ReactElement[] {
1212
let ret: React.ReactElement[] = [];
1313

14-
React.Children.forEach(children, (child: any) => {
14+
React.Children.forEach(children, (child: any | any[]) => {
1515
if ((child === undefined || child === null) && !option.keepEmpty) {
1616
return;
1717
}

src/Dom/dynamicCSS.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import canUseDom from './canUseDom';
22

3-
const MARK_KEY = `rc-util-key` as any;
4-
5-
function getMark({ mark }: Options = {}) {
6-
if (mark) {
7-
return mark.startsWith('data-') ? mark : `data-${mark}`;
8-
}
9-
return MARK_KEY;
10-
}
3+
const MARK_KEY = `rc-util-key`;
114

125
interface Options {
136
attachTo?: Element;
@@ -16,6 +9,13 @@ interface Options {
169
mark?: string;
1710
}
1811

12+
function getMark({ mark }: Options = {}) {
13+
if (mark) {
14+
return mark.startsWith('data-') ? mark : `data-${mark}`;
15+
}
16+
return MARK_KEY;
17+
}
18+
1919
function getContainer(option: Options) {
2020
if (option.attachTo) {
2121
return option.attachTo;
@@ -65,7 +65,6 @@ function findExistNode(key: string, option: Options = {}) {
6565

6666
export function removeCSS(key: string, option: Options = {}) {
6767
const existNode = findExistNode(key, option);
68-
6968
existNode?.parentNode?.removeChild(existNode);
7069
}
7170

src/Dom/focus.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import isVisible from './isVisible';
22

3+
type DisabledElement =
4+
| HTMLLinkElement
5+
| HTMLInputElement
6+
| HTMLFieldSetElement
7+
| HTMLButtonElement
8+
| HTMLOptGroupElement
9+
| HTMLOptionElement
10+
| HTMLSelectElement
11+
| HTMLTextAreaElement;
12+
313
function focusable(node: HTMLElement, includePositive = false): boolean {
414
if (isVisible(node)) {
515
const nodeName = node.nodeName.toLowerCase();
@@ -24,7 +34,7 @@ function focusable(node: HTMLElement, includePositive = false): boolean {
2434
}
2535

2636
// Block focusable if disabled
27-
if (isFocusableElement && (node as any).disabled) {
37+
if (isFocusableElement && (node as DisabledElement).disabled) {
2838
tabIndex = null;
2939
}
3040

@@ -37,13 +47,13 @@ function focusable(node: HTMLElement, includePositive = false): boolean {
3747
}
3848

3949
export function getFocusNodeList(node: HTMLElement, includePositive = false) {
40-
const res = [...node.querySelectorAll('*')].filter(child => {
41-
return focusable(child as HTMLElement, includePositive);
50+
const res = [...node.querySelectorAll<HTMLElement>('*')].filter(child => {
51+
return focusable(child, includePositive);
4252
});
4353
if (focusable(node, includePositive)) {
4454
res.unshift(node);
4555
}
46-
return res as HTMLElement[];
56+
return res;
4757
}
4858

4959
let lastFocusElement = null;

src/Dom/isVisible.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ export default (element: HTMLElement | SVGGraphicsElement): boolean => {
33
return false;
44
}
55

6-
if ((element as HTMLElement).offsetParent) {
6+
if (element instanceof HTMLElement && element.offsetParent) {
77
return true;
88
}
99

10-
if ((element as SVGGraphicsElement).getBBox) {
11-
const box = (element as SVGGraphicsElement).getBBox();
12-
if (box.width || box.height) {
10+
if (element instanceof SVGGraphicsElement && element.getBBox) {
11+
const { width, height } = element.getBBox();
12+
if (width || height) {
1313
return true;
1414
}
1515
}
1616

17-
if ((element as HTMLElement).getBoundingClientRect) {
18-
const box = (element as HTMLElement).getBoundingClientRect();
19-
if (box.width || box.height) {
17+
if (element instanceof HTMLElement && element.getBoundingClientRect) {
18+
const { width, height } = element.getBoundingClientRect();
19+
if (width || height) {
2020
return true;
2121
}
2222
}

src/Dom/scrollLocker.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface scrollLockOptions {
55
container: HTMLElement;
66
}
77

8+
let uuid = 0;
9+
810
interface Ilocks {
911
target: typeof uuid;
1012
options: scrollLockOptions;
@@ -17,8 +19,6 @@ const scrollingEffectClassNameReg = new RegExp(
1719
'g',
1820
);
1921

20-
let uuid = 0;
21-
2222
// https://github.com/ant-design/ant-design/issues/19340
2323
// https://github.com/ant-design/ant-design/issues/19332
2424
const cacheStyle = new Map<Element, React.CSSProperties>();
@@ -92,14 +92,15 @@ export default class ScrollLocker {
9292
container,
9393
setStyle(
9494
{
95-
width: scrollBarSize !== 0 ? `calc(100% - ${scrollBarSize}px)` : undefined,
95+
width:
96+
scrollBarSize !== 0
97+
? `calc(100% - ${scrollBarSize}px)`
98+
: undefined,
9699
overflow: 'hidden',
97100
overflowX: 'hidden',
98101
overflowY: 'hidden',
99102
},
100-
{
101-
element: container,
102-
},
103+
{ element: container },
103104
),
104105
);
105106
}

src/composeProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function composeProps<T extends Record<string, any>>(
1111
Object.keys(patchProps).forEach(key => {
1212
const func = patchProps[key];
1313
if (typeof func === 'function') {
14-
composedProps[key] = (...args) => {
14+
composedProps[key] = (...args: any[]) => {
1515
func(...args);
1616
return originProps[key]?.(...args);
1717
};

src/deprecated.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export default function deprecated(props, instead, component) {
22
if (typeof window !== 'undefined' && window.console && window.console.error) {
33
window.console.error(
44
`Warning: ${props} is deprecated at [ ${component} ], ` +
5-
`use [ ${instead} ] instead of it.`
5+
`use [ ${instead} ] instead of it.`,
66
);
77
}
88
}

src/isMobile.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ export default () => {
55

66
const agent =
77
navigator.userAgent || navigator.vendor || (window as any).opera;
8-
if (
8+
return (
99
/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(
1010
agent,
1111
) ||
1212
/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55\/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk\/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(
1313
agent?.substr(0, 4),
1414
)
15-
) {
16-
return true;
17-
}
18-
return false;
15+
);
1916
};

0 commit comments

Comments
 (0)