阻止模态框在外部单击时关闭

32

点击容器外部时如何让 Modal 保持打开状态?

我有一个密码更改屏幕,我需要Modal只有在单击提交按钮时才关闭。而此按钮仅在满足某些条件时才变为活动状态。

<div>
            <Modal show={this.state.show} onHide={this.handleClose}>
                <Modal.Header>
                    <Modal.Title>Change Password</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    <form className="form-horizontal" style={{margin:0}}>
                        <div className='password-heading'>This is the first time you have logged in.</div>
                        <div className='password-heading'>Please set a new password for your account.</div>
                        <br/>

                        <label className="password">Password
                            <input type={this.state.type} className="password__input" onChange={this.passwordStrength}/>
                            <span className="password__show" onClick={this.showHide}>{this.state.type === 'input' ? 'Hide' : 'Show'}</span>
                            <span className="password__strength" data-score={this.state.score} ><div className="strength_string">{this.state.strength}</div></span>
                        </label>
                        <br/>
                        <label className="password">Confirm Password
                            <input type={this.state.type} className="password__input" onChange={this.passwordStrength}/>
                        </label>

                    </form>
                    <br/>
                </Modal.Body>
                <Modal.Footer>
                    <Button onClick={this.submitPassword} disabled={this.state.isDisabled}>Submit</Button>
                </Modal.Footer>
            </Modal>
        </div>

更新
除了添加 backdrop={ 'static' },您很可能仍然可以通过单击Escape键来关闭模态窗口。
为了防止这种情况,请向您的模态窗口添加一个东西:keyboard={ false }

这应该足以保持模态窗口处于打开状态。

5个回答

65

将模态框的背景设置为静态。模态框组件有一个名为backdrop的属性,将其设置为backdrop="static"

<div>
    <Modal show={this.state.show} onHide={this.handleClose} backdrop="static">
        <Modal.Header>
            <Modal.Title>Change Password</Modal.Title>
        </Modal.Header>
        <Modal.Body>
            <form className="form-horizontal" style={{margin:0}}>
                <div className='password-heading'>This is the first time you have logged in.</div>
                <div className='password-heading'>Please set a new password for your account.</div>
                <br/>

                <label className="password">Password
                    <input type={this.state.type} className="password__input" onChange={this.passwordStrength}/>
                    <span className="password__show" onClick={this.showHide}>{this.state.type === 'input' ? 'Hide' : 'Show'}</span>
                    <span className="password__strength" data-score={this.state.score}>
                        <div className="strength_string">{this.state.strength}</div>
                    </span>
                </label>
                <br/>
                <label className="password">Confirm Password
                    <input type={this.state.type} className="password__input" onChange={this.passwordStrength}/>
                </label>

            </form>
            <br/>
        </Modal.Body>
        <Modal.Footer>
            <Button onClick={this.submitPassword} disabled={this.state.isDisabled}>Submit</Button>
        </Modal.Footer>
    </Modal>
</div>

根据文档:

指定“static”背景,点击时不会触发“onHide”。 react-bootstrap


3

移除Modal中的onHide onHide={this.handleClose}


1

不要传递 onHideonClose 属性。


0

我也想实现同样的功能,加上这个<Modal keyboard={false} ...>对我有用。


0
在我的项目中,我使用React和Ant Design的模态框。我发现答案应该是maskClosable={false},请参考Ant Design官方网站的模态框文档enter image description here 希望能帮助到其他人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接