Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent modal from closing when modal is partial target of click #30

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default class ReactModal2 extends React.Component {
closeOnBackdropClick: true
};

preventModalClose = false

componentDidMount() {
if (ExecutionEnvironment.canUseDOM) {
setFocusOn(ReactModal2.getApplicationElement(), this.modal);
Expand All @@ -60,13 +62,15 @@ export default class ReactModal2 extends React.Component {
}

handleBackdropClick = () => {
if (this.props.closeOnBackdropClick) {
if (this.props.closeOnBackdropClick && !this.preventModalClose) {
this.props.onClose();
}

this.preventModalClose = false
}

handleModalClick = event => {
event.stopPropagation();
this.preventModalClose = true
}

render() {
Expand All @@ -78,7 +82,8 @@ export default class ReactModal2 extends React.Component {
<div ref={i => this.modal = i}
className={this.props.modalClassName}
style={this.props.modalStyles}
onClick={this.handleModalClick}
onMouseDown={this.handleModalClick}
onMouseUp={this.handleModalClick}
tabIndex="-1">
{this.props.children}
</div>
Expand Down
20 changes: 18 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,34 @@ describe('ReactModal2', function() {
expect(called).to.be.false;
});

it('should not call `onClose` when the modal is clicked', function() {
it('should not call `onClose` when the modal is the start target of a click', function() {
var called = false;
var onClose = function() { called = true; };

var dom = <ReactModal2 onClose={onClose}/>;
var instance = ReactDOM.render(dom, this.root);

TestUtils.Simulate.click(instance.modal);
TestUtils.Simulate.mouseDown(instance.modal);
TestUtils.Simulate.mouseUp(instance.backdrop);
TestUtils.Simulate.click(instance.backdrop);

expect(called).to.be.false;
});

it('should not call `onClose` when the modal is the end target of a click', function() {
var called = false;
var onClose = function() { called = true; };

var dom = <ReactModal2 onClose={onClose}/>;
var instance = ReactDOM.render(dom, this.root);

TestUtils.Simulate.mouseDown(instance.backdrop);
TestUtils.Simulate.mouseUp(instance.modal);
TestUtils.Simulate.click(instance.backdrop);

expect(called).to.be.false;
});

it('should scope the focus on the modal when mounted', function() {
var input = document.createElement('input');
document.body.appendChild(input);
Expand Down