Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop modal from closing on outside click

Is there anything that can be done to make keep a Modal open when clicking outside the container?

I have a password change screen and I need the Modal to ONLY close when clicking on the submitbutton. which only becomes active when certain conditions are met.

<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>

UPDATE
Appart from adding backdrop={ 'static' } you will most likely still be able to close the modal by clicking the Escape key.
To prevent this add one more thing to your modal window: keyboard={ false }.

This should suffice in keeping the modal open.

like image 491
morne Avatar asked May 15 '18 15:05

morne


People also ask

How do I stop closing the modal when I click outside?

Data-keyword="false" is to prevent closing modal while clicking Esc button, while data-backdrop="static" , allows you keep modal pop-up opened when clicking on Gray area.

How can you prevent Bootstrap modal from closing when clicking outside the content area?

Static backdropWhen backdrop is set to static, the modal will not close when clicking outside it. Click the button below to try it.

How do you stop a modal closing?

You can prevent closing of modal dialog by setting the beforeClose event argument cancel value to true. In the following sample, the dialog is closed when you enter the username value with minimum 4 characters. Otherwise, it will not be closed.

How do I keep modal open after submitting?

The goal is to submit the data to our db when the submit button is clicked and then hide the form and display a thank you within the same modal. Here's the basics of the code for the modal: <div class="modal fade" id="formModal" tabindex="-1" role="dialog" aria-labelledby="formModalLabel" aria-hidden="true">


3 Answers

Set the modal's backdrop to static. The modal component has a prop of backdrop, set that to 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>

From the documentation:

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

like image 147
Malcor Avatar answered Oct 19 '22 20:10

Malcor


Remove onHide from the Modal onHide={this.handleClose}

like image 2
Hammad Ali Avatar answered Oct 19 '22 19:10

Hammad Ali


Don't pass the onHide or onClose props.

like image 1
user19174705 Avatar answered Oct 19 '22 20:10

user19174705