Skip to content

Commit

Permalink
feat: extract the ClassName out of the function; Prevent duplicate ex…
Browse files Browse the repository at this point in the history
…ecution
  • Loading branch information
mumiao committed Dec 12, 2020
1 parent 4591cf5 commit 056ef35
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/components/button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const disableButtonClassName = getBEMModifier(
);

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

const disabled = props.disabled ? disableButtonClassName : null;

Expand Down
12 changes: 5 additions & 7 deletions src/components/dialog/ConfirmDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ interface ConfirmDialogProps extends IModalFuncProps {
}

export const confirmClassName = prefixClaName('confirm');
const containerClassName = getBEMElement(confirmClassName, 'container');
const indicatorClassName = getBEMElement(confirmClassName, 'indicator');
const contentClassName = getBEMElement(confirmClassName, 'content');
const messageClassName = getBEMElement(confirmClassName, 'message');
const btnsClassName = getBEMElement(confirmClassName, 'btns');

const ConfirmDialog = (props: ConfirmDialogProps) => {
const {
Expand Down Expand Up @@ -51,18 +56,11 @@ const ConfirmDialog = (props: ConfirmDialogProps) => {
confirmClassName,
`${props.type}`
);
const containerClassName = getBEMElement(confirmClassName, 'container');
const indicatorClassName = getBEMElement(confirmClassName, 'indicator');
const contentClassName = getBEMElement(confirmClassName, 'content');
const messageClassName = getBEMElement(confirmClassName, 'message');
const btnsClassName = getBEMElement(confirmClassName, 'btns');

const classString = classNames(
confirmClassName,
confirmDescriperClassName,
className
);

const cancelButton = okCancel && (
<ActionButton
actionFn={onCancel}
Expand Down
58 changes: 27 additions & 31 deletions src/components/dialog/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,6 @@ import {
} from 'mo/common/className';
import { Icon } from 'mo/components/icon';
import { Button, IButton } from 'mo/components/button';

let mousePosition: any;

const getClickPosition = (e: MouseEvent) => {
mousePosition = {
x: e.pageX,
y: e.pageY,
};
setTimeout(() => {
mousePosition = null;
}, 100);
};

if (typeof window !== 'undefined' && window.document?.documentElement) {
document.documentElement.addEventListener('click', getClickPosition, true);
}

export const destroyFns: Array<() => void> = [];

export const modalClassName = prefixClaName('modal');

export interface IModalProps extends IDialogPropTypes {
onOk?: (e: React.MouseEvent<HTMLElement>) => void;
onCancel?: (e: React.SyntheticEvent<Element, Event>) => void;
Expand All @@ -41,7 +20,6 @@ export interface IModalProps extends IDialogPropTypes {
cancelButtonProps?: IButton;
okCancel?: boolean;
}

export interface IModalFuncProps extends IDialogPropTypes {
cancelText?: React.ReactNode;
okText?: React.ReactNode;
Expand All @@ -56,6 +34,33 @@ export interface IModalFuncProps extends IDialogPropTypes {
type?: string;
}

export const destroyFns: Array<() => void> = [];
export const modalClassName = prefixClaName('modal');

let mousePosition: any;

const getClickPosition = (e: MouseEvent) => {
mousePosition = {
x: e.pageX,
y: e.pageY,
};
setTimeout(() => {
mousePosition = null;
}, 100);
};

if (typeof window !== 'undefined' && window.document?.documentElement) {
document.documentElement.addEventListener('click', getClickPosition, true);
}

const closeClassName = getBEMElement(modalClassName, 'close');
const closeDescriptorClassName = getBEMModifier(`${closeClassName}`, 'x');
const closeIconToRender = (
<span className={closeDescriptorClassName}>
<Icon type="close" />
</span>
);

const Modal: React.FC<IModalProps> = (props) => {
const handleCancel = (e: React.SyntheticEvent<Element, Event>) => {
const { onCancel } = props;
Expand All @@ -82,15 +87,6 @@ const Modal: React.FC<IModalProps> = (props) => {
[getBEMModifier(`${modalClassName}`, 'centered')]: !!centered,
});

const closeClassName = getBEMElement(modalClassName, 'close');
const closeDescriptorClassName = getBEMModifier(`${closeClassName}`, 'x');

const closeIconToRender = (
<span className={closeDescriptorClassName}>
<Icon type="close" />
</span>
);

const renderFooter = () => {
const { footer, cancelButtonProps, okButtonProps } = props;
if (footer !== undefined) return footer;
Expand Down

0 comments on commit 056ef35

Please sign in to comment.