Skip to content

Commit 056ef35

Browse files
committed
feat: extract the ClassName out of the function; Prevent duplicate execution
1 parent 4591cf5 commit 056ef35

File tree

3 files changed

+33
-39
lines changed

3 files changed

+33
-39
lines changed

src/components/button/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const disableButtonClassName = getBEMModifier(
1818
);
1919

2020
export function Button(props: React.PropsWithChildren<IButton>) {
21-
const { className, children, size = 'normal', onClick, ...custom } = props;
21+
const { className, children, size = 'normal', ...custom } = props;
2222

2323
const disabled = props.disabled ? disableButtonClassName : null;
2424

src/components/dialog/ConfirmDialog.tsx

+5-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ interface ConfirmDialogProps extends IModalFuncProps {
1515
}
1616

1717
export const confirmClassName = prefixClaName('confirm');
18+
const containerClassName = getBEMElement(confirmClassName, 'container');
19+
const indicatorClassName = getBEMElement(confirmClassName, 'indicator');
20+
const contentClassName = getBEMElement(confirmClassName, 'content');
21+
const messageClassName = getBEMElement(confirmClassName, 'message');
22+
const btnsClassName = getBEMElement(confirmClassName, 'btns');
1823

1924
const ConfirmDialog = (props: ConfirmDialogProps) => {
2025
const {
@@ -51,18 +56,11 @@ const ConfirmDialog = (props: ConfirmDialogProps) => {
5156
confirmClassName,
5257
`${props.type}`
5358
);
54-
const containerClassName = getBEMElement(confirmClassName, 'container');
55-
const indicatorClassName = getBEMElement(confirmClassName, 'indicator');
56-
const contentClassName = getBEMElement(confirmClassName, 'content');
57-
const messageClassName = getBEMElement(confirmClassName, 'message');
58-
const btnsClassName = getBEMElement(confirmClassName, 'btns');
59-
6059
const classString = classNames(
6160
confirmClassName,
6261
confirmDescriperClassName,
6362
className
6463
);
65-
6664
const cancelButton = okCancel && (
6765
<ActionButton
6866
actionFn={onCancel}

src/components/dialog/modal.tsx

+27-31
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,6 @@ import {
1010
} from 'mo/common/className';
1111
import { Icon } from 'mo/components/icon';
1212
import { Button, IButton } from 'mo/components/button';
13-
14-
let mousePosition: any;
15-
16-
const getClickPosition = (e: MouseEvent) => {
17-
mousePosition = {
18-
x: e.pageX,
19-
y: e.pageY,
20-
};
21-
setTimeout(() => {
22-
mousePosition = null;
23-
}, 100);
24-
};
25-
26-
if (typeof window !== 'undefined' && window.document?.documentElement) {
27-
document.documentElement.addEventListener('click', getClickPosition, true);
28-
}
29-
30-
export const destroyFns: Array<() => void> = [];
31-
32-
export const modalClassName = prefixClaName('modal');
33-
3413
export interface IModalProps extends IDialogPropTypes {
3514
onOk?: (e: React.MouseEvent<HTMLElement>) => void;
3615
onCancel?: (e: React.SyntheticEvent<Element, Event>) => void;
@@ -41,7 +20,6 @@ export interface IModalProps extends IDialogPropTypes {
4120
cancelButtonProps?: IButton;
4221
okCancel?: boolean;
4322
}
44-
4523
export interface IModalFuncProps extends IDialogPropTypes {
4624
cancelText?: React.ReactNode;
4725
okText?: React.ReactNode;
@@ -56,6 +34,33 @@ export interface IModalFuncProps extends IDialogPropTypes {
5634
type?: string;
5735
}
5836

37+
export const destroyFns: Array<() => void> = [];
38+
export const modalClassName = prefixClaName('modal');
39+
40+
let mousePosition: any;
41+
42+
const getClickPosition = (e: MouseEvent) => {
43+
mousePosition = {
44+
x: e.pageX,
45+
y: e.pageY,
46+
};
47+
setTimeout(() => {
48+
mousePosition = null;
49+
}, 100);
50+
};
51+
52+
if (typeof window !== 'undefined' && window.document?.documentElement) {
53+
document.documentElement.addEventListener('click', getClickPosition, true);
54+
}
55+
56+
const closeClassName = getBEMElement(modalClassName, 'close');
57+
const closeDescriptorClassName = getBEMModifier(`${closeClassName}`, 'x');
58+
const closeIconToRender = (
59+
<span className={closeDescriptorClassName}>
60+
<Icon type="close" />
61+
</span>
62+
);
63+
5964
const Modal: React.FC<IModalProps> = (props) => {
6065
const handleCancel = (e: React.SyntheticEvent<Element, Event>) => {
6166
const { onCancel } = props;
@@ -82,15 +87,6 @@ const Modal: React.FC<IModalProps> = (props) => {
8287
[getBEMModifier(`${modalClassName}`, 'centered')]: !!centered,
8388
});
8489

85-
const closeClassName = getBEMElement(modalClassName, 'close');
86-
const closeDescriptorClassName = getBEMModifier(`${closeClassName}`, 'x');
87-
88-
const closeIconToRender = (
89-
<span className={closeDescriptorClassName}>
90-
<Icon type="close" />
91-
</span>
92-
);
93-
9490
const renderFooter = () => {
9591
const { footer, cancelButtonProps, okButtonProps } = props;
9692
if (footer !== undefined) return footer;

0 commit comments

Comments
 (0)