javascript - react : Close a modal clicking outside

标签 javascript reactjs

我有一个 模态带有打开模式的按钮的 react 组件。模式也有一个关闭按钮。这是代码和模态的样子:

class Modal extends React.Component {
    static defaultProps = {
        title: "Modal Title",
        float: false,
        color: "pink",
        children: <div>Add children here!</div>
    }

    constructor(props) {
        super(props)
        this.state = {
            show: false
        }
    }

    openModal() {
        this.setState(
            { show: true }
        )
    }

    closeModal() {
        this.setState(
            { show: false }
        );
    }

    render() {
        return (
            <div>
                <button className={"modal-button" + " " + this.props.color} onClick={() => this.openModal()}>{this.props.title}</button>
                {this.state.show && <div className={"modal fade-in" + " " + (this.props.float ? "modal-float" : null)}>
                    <div className="modal-head">
                        <span>{this.props.title}</span>
                        <button id='button' onClick={() => this.closeModal()}>x</button>
                    </div>
                    <div className="modal-body">
                        {this.props.children}
                    </div>
                </div>}
            </div>
        )
    }
}
It has a button that opens the modal. And the modal has a close button too.
我想关闭在外面点击的模式。在另一个问题中,我看到了这样的代码
handleClick = event => {
    event.preventDefault();

    this.setState({ showModal: true }, () => {
      document.addEventListener("click", this.closeMenu);
    });
  };

closeMenu = () => {
    this.setState({ menuOpen: false }, () => {
      document.removeEventListener('click', this.closeMenu);
    });
  }
但是当我在模态框内单击时,它也会关闭模态框。

最佳答案

您需要添加一个条件,当模态打开并且单击源自模态内部时不会关闭模态。要检测点击发生的位置,您可能会使用 refs。
https://reactjs.org/docs/refs-and-the-dom.html
例如,我在 closeMenu 函数中添加了这个条件:

this.modalRef = React.createRef();

handleClick = event => {
    event.preventDefault();

    this.setState({ showModal: true }, () => {
      document.addEventListener("click", this.closeMenu);
    });
  };

closeMenu = () => {
  if(this.modalRef.current && this.modalRef.current.contains(event.target)) {
    return; 
}
    this.setState({ menuOpen: false }, () => {
      document.removeEventListener('click', this.closeMenu);
    });
  }


render() {
    return (
      <div ref={this.modalRef}>
       // modal component 
      </div>
    );
  }
}

这会检测 1. 模态是否打开 (this.modalRef.current) 和 2. 如果单击源自模态内部 (this.modalRef.current.contains(event.target)),在这种情况下它会返回并且不会关闭模态。
这解释了该方法,虽然它使用钩子(Hook)而不是类组件,但概念是相同的:https://javascript.plainenglish.io/detect-a-click-outside-of-a-react-component-with-a-reusable-hook-and-useref-a0c282171c3f

关于javascript - react : Close a modal clicking outside,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67559489/

相关文章:

javascript - 在 React 中点击可点击组件的外部

reactjs - Webpack、React 热重载器和多个条目

java - react excel 文件下载损坏

javascript - 在 Next.js 中导入 react-hook-mousetrap 时出现 "Cannot use import statement outside a module"错误

javascript - 尝试从 NodeJS Express API 中的 Reactjs 文件请求常量数据时出现未定义

javascript - 在 angular5 中使用纯 JavaScript

javascript - 在同一行的复选框选中/未选中时启用 GridViewRow 中的 DropDownList(使用任何 c#、jquery、javascript)

javascript - 如何在等待 ajax 回调时运行 javascript

javascript - 获取并附加来自 url 的前四张图像

javascript - 未捕获( promise ): TypeError: Cannot read property 'router' of undefined