chentsulin/sweetalert-react

onOutsideClick get fired when body of popup get clicked (when lazy created)

Opened this issue · 2 comments

Hi,
found an issue with the onOutsideClick handler. It get fired even if I click input field inside the popup.
(Note that this is not duplicate with previous reported issue)

If I render SweetAlert but don't show it yet and later on change the show:true, then this invalid behavior happens. If I have show:true on initial render time, then everything works.

Looks like that if it's show:false, when registerOutsideClickHandler get executed, it document.getElementsByClassName('sweet-alert') returns null (of course because the popup is not created.

Found out that if I move the registerOutsideClickHandler from componentDidMount after the place in setupWithProps where it creates the popup then it starts to work.

Sorry for late reply. I think we can avoid handler be executed when targetNode is null or undefined.

export default function outsideTargetHandlerFactory(targetNode, eventHandler) {
  return (evt) => {
    evt.stopPropagation();
    if (!targetNode) return; // <------- Added this line should fix this
    let current = evt.target;
    let found = false;
    while (current.parentNode) {
      found = isDOMEquals(current, targetNode);
      if (found) return;
      current = current.parentNode;
    }
    eventHandler(evt);
  };
}