gpt4 book ai didi

reactjs - 阻止模态在外部点击时关闭

转载 作者:行者123 更新时间:2023-12-03 13:09:33 25 4
gpt4 key购买 nike

有什么办法可以让 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 }

这应该足以保持模式打开。

最佳答案

将模态框的背景设置为静态。模态组件有一个 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>

来自文档:

Specify 'static' for a backdrop that doesn't trigger an "onHide" when clicked. react-bootstrap

关于reactjs - 阻止模态在外部点击时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50354249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com