From 1a0053221b58d198bdcca0249fcd76226122c6a2 Mon Sep 17 00:00:00 2001 From: Ryan Cohen Date: Thu, 25 Aug 2022 13:28:44 +0900 Subject: [PATCH] fix: clicking outside modal closes modal again (#2984) --- src/components/Common/Modal/index.tsx | 150 ++++++++++-------- .../RequestModal/TvRequestModal.tsx | 4 +- 2 files changed, 82 insertions(+), 72 deletions(-) diff --git a/src/components/Common/Modal/index.tsx b/src/components/Common/Modal/index.tsx index a6c2aac5..06b4ba5f 100644 --- a/src/components/Common/Modal/index.tsx +++ b/src/components/Common/Modal/index.tsx @@ -7,7 +7,7 @@ import { useLockBodyScroll } from '@app/hooks/useLockBodyScroll'; import globalMessages from '@app/i18n/globalMessages'; import { Transition } from '@headlessui/react'; import type { MouseEvent } from 'react'; -import { Fragment, useRef } from 'react'; +import React, { Fragment, useRef } from 'react'; import ReactDOM from 'react-dom'; import { useIntl } from 'react-intl'; @@ -36,81 +36,89 @@ interface ModalProps { children?: React.ReactNode; } -const Modal = ({ - title, - onCancel, - onOk, - cancelText, - okText, - okDisabled = false, - cancelButtonType = 'default', - okButtonType = 'primary', - children, - disableScrollLock, - backgroundClickable = true, - iconSvg, - secondaryButtonType = 'default', - secondaryDisabled = false, - onSecondary, - secondaryText, - tertiaryButtonType = 'default', - tertiaryDisabled = false, - tertiaryText, - onTertiary, - backdrop, -}: ModalProps) => { - const intl = useIntl(); - const modalRef = useRef(null); - useClickOutside(modalRef, () => { - typeof onCancel === 'function' && backgroundClickable - ? onCancel() - : undefined; - }); - useLockBodyScroll(true, disableScrollLock); +const Modal = React.forwardRef( + ( + { + title, + onCancel, + onOk, + cancelText, + okText, + okDisabled = false, + cancelButtonType = 'default', + okButtonType = 'primary', + children, + disableScrollLock, + backgroundClickable = true, + iconSvg, + secondaryButtonType = 'default', + secondaryDisabled = false, + onSecondary, + secondaryText, + tertiaryButtonType = 'default', + tertiaryDisabled = false, + tertiaryText, + loading = false, + onTertiary, + backdrop, + }, + parentRef + ) => { + const intl = useIntl(); + const modalRef = useRef(null); + useClickOutside(modalRef, () => { + typeof onCancel === 'function' && backgroundClickable + ? onCancel() + : undefined; + }); + useLockBodyScroll(true, disableScrollLock); - return ReactDOM.createPortal( - // eslint-disable-next-line jsx-a11y/no-static-element-interactions -
{ - if (e.key === 'Escape') { - typeof onCancel === 'function' && backgroundClickable - ? onCancel() - : undefined; - } - }} - > + return ReactDOM.createPortal( -
- -
-
- -
+
+ +
+ + {backdrop && (
@@ -205,11 +213,13 @@ const Modal = ({ )}
)} -
-
-
, - document.body - ); -}; + + , + document.body + ); + } +); + +Modal.displayName = 'Modal'; export default Modal; diff --git a/src/components/RequestModal/TvRequestModal.tsx b/src/components/RequestModal/TvRequestModal.tsx index 59e65dfc..6de4253b 100644 --- a/src/components/RequestModal/TvRequestModal.tsx +++ b/src/components/RequestModal/TvRequestModal.tsx @@ -359,12 +359,12 @@ const TvRequestModal = ({ const isOwner = editRequest && editRequest.requestedBy.id === user?.id; - return !data?.externalIds.tvdbId && searchModal.show ? ( + return data && !data.externalIds.tvdbId && searchModal.show ? ( setSearchModal({ show: false })} - loading={!data && !error} + loading={!error} onCancel={onCancel} modalTitle={intl.formatMessage( is4k ? messages.request4ktitle : messages.requesttitle,