From a7db01a44b10c11f589bbe15d5cd5e23e4c6a121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E5=85=B0=E9=91=AB?= <1192065030@qq.com> Date: Fri, 13 May 2022 15:10:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(Modal):=20=E4=BF=AE=E5=A4=8DModal.show?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E4=B8=8D=E5=AD=98=E5=9C=A8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20#825=20(#827)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/react-modal/src/CallShow.tsx | 4 +- packages/react-modal/src/index.tsx | 211 +++++++++++++------------- 2 files changed, 108 insertions(+), 107 deletions(-) diff --git a/packages/react-modal/src/CallShow.tsx b/packages/react-modal/src/CallShow.tsx index 7608572cb4..ea9be64d1b 100644 --- a/packages/react-modal/src/CallShow.tsx +++ b/packages/react-modal/src/CallShow.tsx @@ -2,8 +2,8 @@ import React from 'react'; import ReactDOM from 'react-dom'; import Modal, { ModalProps } from './'; -export default function CallShow(props: Omit & { children: React.ReactNode }) { - const { title = '提示框', children, ...other } = props; +export default function CallShow(props?: Omit & { children?: React.ReactNode }) { + const { title = '提示框', children, ...other } = props || {}; const dv = document.createElement('div'); dv.id = 'uiw-modal-call-show-element'; document.body.appendChild(dv); diff --git a/packages/react-modal/src/index.tsx b/packages/react-modal/src/index.tsx index 268c5053ed..598b1eacb1 100644 --- a/packages/react-modal/src/index.tsx +++ b/packages/react-modal/src/index.tsx @@ -28,113 +28,114 @@ export interface ModalProps extends IProps, OverlayProps { onConfirm?: (evn: React.MouseEvent & MouseEvent) => void; } -type ShowModalProps = { - show?: (props: Omit & { children: React.ReactNode }) => void; -}; - -const Modal: React.ForwardRefExoticComponent> & ShowModalProps = - React.forwardRef((props, ref) => { - const { - prefixCls = 'w-modal', - className, - children, - useButton = true, - usePortal = true, - autoFocus = false, - isOpen: _ = false, - title, - cancelText, - cancelButtonProps, - confirmButtonProps, - content, - confirmText = '确认', - type = 'light', - icon, - maxWidth = 500, - minWidth = 320, - width, - isCloseButtonShown = true, - onCancel = noop, - onConfirm = noop, - bodyStyle, - ...other - } = props; - const [isOpen, setIsOpen] = useState(props.isOpen); - useEffect(() => { - if (props.isOpen !== isOpen) { - setIsOpen(props.isOpen); - } - }, [props.isOpen]); - - const [loading, setLoading] = useState(false); - const cls = [prefixCls, className, type ? `${type}` : null].filter(Boolean).join(' ').trim(); - function onClose() { - setIsOpen(false); - } - async function handleCancel(e: React.MouseEvent & MouseEvent) { - setLoading(true); - try { - onCancel && (await onCancel(e)); - } catch (e) {} - setIsOpen(false); - setLoading(false); - } - async function handleConfirm(e: React.MouseEvent & MouseEvent) { - setLoading(true); - try { - onConfirm && (await onConfirm(e)); - } catch (e) {} - setIsOpen(false); - setLoading(false); +const Modal: React.ForwardRefExoticComponent> = React.forwardRef< + OverlayProps, + ModalProps +>((props, ref) => { + const { + prefixCls = 'w-modal', + className, + children, + useButton = true, + usePortal = true, + autoFocus = false, + isOpen: _ = false, + title, + cancelText, + cancelButtonProps, + confirmButtonProps, + content, + confirmText = '确认', + type = 'light', + icon, + maxWidth = 500, + minWidth = 320, + width, + isCloseButtonShown = true, + onCancel = noop, + onConfirm = noop, + bodyStyle, + ...other + } = props; + const [isOpen, setIsOpen] = useState(props.isOpen); + useEffect(() => { + if (props.isOpen !== isOpen) { + setIsOpen(props.isOpen); } - return ( - -
-
- {(title || icon) && ( -
- {icon && } - {title &&

{title}

} - {isCloseButtonShown &&
- )} -
- {children || content} + }, [props.isOpen]); + + const [loading, setLoading] = useState(false); + const cls = [prefixCls, className, type ? `${type}` : null].filter(Boolean).join(' ').trim(); + function onClose() { + setIsOpen(false); + } + async function handleCancel(e: React.MouseEvent & MouseEvent) { + setLoading(true); + try { + onCancel && (await onCancel(e)); + } catch (e) {} + setIsOpen(false); + setLoading(false); + } + async function handleConfirm(e: React.MouseEvent & MouseEvent) { + setLoading(true); + try { + onConfirm && (await onConfirm(e)); + } catch (e) {} + setIsOpen(false); + setLoading(false); + } + return ( + +
+
+ {(title || icon) && ( +
+ {icon && } + {title &&

{title}

} + {isCloseButtonShown &&
- {useButton && ( -
- - {cancelText && ( - - )} -
- )} + )} +
+ {children || content}
+ {useButton && ( +
+ + {cancelText && ( + + )} +
+ )}
- - ); - }); +
+
+ ); +}); -Modal.show = CallShow; -export default Modal; +type Modal = typeof Modal & { + show: (props?: Omit & { children?: React.ReactNode }) => void; +}; +(Modal as Modal).show = CallShow; +export default Modal as Modal;