|
| 1 | +import * as React from 'react'; |
| 2 | +import Dialog from 'rc-dialog'; |
| 3 | +import { IDialogPropTypes } from 'rc-dialog/lib/IDialogPropTypes'; |
| 4 | + |
| 5 | +import { classNames, prefixClaName } from 'mo/common/className'; |
| 6 | + |
| 7 | +import { Button, IButton } from 'mo/components/button'; |
| 8 | + |
| 9 | +let mousePosition |
| 10 | + |
| 11 | +const getClickPosition = (e: MouseEvent) => { |
| 12 | + mousePosition = { |
| 13 | + x: e.pageX, |
| 14 | + y: e.pageY, |
| 15 | + }; |
| 16 | + setTimeout(() => { |
| 17 | + mousePosition = null; |
| 18 | + }, 100); |
| 19 | +}; |
| 20 | + |
| 21 | +// 只有点击事件支持从鼠标位置动画展开 |
| 22 | +if (typeof window !== 'undefined' && window.document?.documentElement) { |
| 23 | + document.documentElement.addEventListener('click', getClickPosition, true); |
| 24 | +} |
| 25 | + |
| 26 | +export const destroyFns: Array<() => void> = []; |
| 27 | + |
| 28 | +export interface IModalProps extends IDialogPropTypes { |
| 29 | + /** 点击确定回调 */ |
| 30 | + onOk?: (e: React.MouseEvent<HTMLElement>) => void; |
| 31 | + /** 点击模态框右上角叉、取消按钮、Props.maskClosable 值为 true 时的遮罩层或键盘按下 Esc 时的回调 */ |
| 32 | + onCancel?: (e: React.SyntheticEvent<Element, Event>) => void; |
| 33 | + /** 垂直居中 */ |
| 34 | + centered?: boolean; |
| 35 | + /** 确认按钮文字 */ |
| 36 | + okText?: React.ReactNode; |
| 37 | + /** 取消按钮文字 */ |
| 38 | + cancelText?: React.ReactNode; |
| 39 | + okButtonProps?: IButton; |
| 40 | + cancelButtonProps?: IButton; |
| 41 | +} |
| 42 | + |
| 43 | +export interface IModalFuncProps extends IDialogPropTypes { |
| 44 | + content?: React.ReactNode; |
| 45 | + onOk?: (...args: any[]) => any; |
| 46 | + onCancel?: (...args: any[]) => any; |
| 47 | + okButtonProps?: IButton; |
| 48 | + cancelButtonProps?: IButton; |
| 49 | + centered?: boolean; |
| 50 | + okText?: React.ReactNode; |
| 51 | + cancelText?: React.ReactNode; |
| 52 | + icon?: React.ReactNode; |
| 53 | + okCancel?: boolean; |
| 54 | + type?: string; |
| 55 | + autoFocusButton?: null | 'ok' | 'cancel'; |
| 56 | +} |
| 57 | + |
| 58 | +const Modal: React.FC<IModalProps> = (props) => { |
| 59 | + const handleCancel = (e: React.SyntheticEvent<Element, Event>) => { |
| 60 | + const { onCancel } = props; |
| 61 | + onCancel?.(e); |
| 62 | + }; |
| 63 | + |
| 64 | + const handleOk = (e: React.MouseEvent<HTMLButtonElement>) => { |
| 65 | + const { onOk } = props; |
| 66 | + onOk?.(e); |
| 67 | + }; |
| 68 | + |
| 69 | + const renderFooter = () => { |
| 70 | + const { okText, cancelText } = props; |
| 71 | + return ( |
| 72 | + <> |
| 73 | + <Button onClick={handleCancel} {...props.cancelButtonProps}> |
| 74 | + {cancelText} |
| 75 | + </Button> |
| 76 | + <Button onClick={handleOk} {...props.okButtonProps}> |
| 77 | + {okText} |
| 78 | + </Button> |
| 79 | + </> |
| 80 | + ); |
| 81 | + }; |
| 82 | + |
| 83 | + const { |
| 84 | + footer, |
| 85 | + visible, |
| 86 | + wrapClassName, |
| 87 | + centered, |
| 88 | + getContainer, |
| 89 | + closeIcon, |
| 90 | + focusTriggerAfterClose = true, |
| 91 | + ...restProps |
| 92 | + } = props; |
| 93 | + |
| 94 | + const prefixCls = prefixClaName('modal'); |
| 95 | + const defaultFooter = renderFooter; |
| 96 | + |
| 97 | + const closeIconToRender = ( |
| 98 | + <span className={`${prefixCls}-close-x`}>{closeIcon}</span> |
| 99 | + ); |
| 100 | + |
| 101 | + const wrapClassNameExtended = classNames(wrapClassName, { |
| 102 | + [`${prefixCls}-centered`]: !!centered, |
| 103 | + }); |
| 104 | + return ( |
| 105 | + <Dialog |
| 106 | + {...restProps} |
| 107 | + getContainer={getContainer} |
| 108 | + prefixCls={prefixCls} |
| 109 | + wrapClassName={wrapClassNameExtended} |
| 110 | + footer={footer === undefined ? defaultFooter : footer} |
| 111 | + visible={visible} |
| 112 | + mousePosition={mousePosition} |
| 113 | + onClose={handleCancel} |
| 114 | + closeIcon={closeIconToRender} |
| 115 | + focusTriggerAfterClose={focusTriggerAfterClose} |
| 116 | + /> |
| 117 | + ); |
| 118 | +}; |
| 119 | + |
| 120 | +Modal.defaultProps = { |
| 121 | + width: 520, |
| 122 | + transitionName: 'zoom', |
| 123 | + maskTransitionName: 'fade', |
| 124 | + visible: false, |
| 125 | +}; |
| 126 | + |
| 127 | +export default Modal; |
0 commit comments