Skip to content

Commit

Permalink
[changed] only autofocus modals when enforceFocus is true (the default)
Browse files Browse the repository at this point in the history
fixes react-bootstrap#842, only move focus to the Modal if `enforceFocus` is true
  • Loading branch information
jquense committed Jun 15, 2015
1 parent 6c7a5f0 commit 0ce46b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,12 @@ const Modal = React.createClass({
},

focusModalContent () {
this.lastFocus = domUtils.activeElement(this);
let modalContent = React.findDOMNode(this.refs.modal);
modalContent.focus();
if (this.props.enforceFocus) {
this.lastFocus = domUtils.activeElement(this);

let modalContent = React.findDOMNode(this.refs.modal);
modalContent.focus();
}
},

restoreLastFocus () {
Expand Down
30 changes: 30 additions & 0 deletions test/ModalSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,36 @@ describe('Modal', function () {
}, 0);
});

it('Should not focus on the Modal when enforceFocus is false', function (done) {

document.activeElement.should.equal(focusableContainer);

let Container = React.createClass({
getInitialState() {
return {modalOpen: true};
},
render() {
if (this.state.modalOpen) {
return (
<Modal enforceFocus={false} onRequestHide={()=>{}} container={this}>
<strong>Message</strong>
</Modal>
);
} else {
return <span/>;
}
}
});

React.render(<Container />, focusableContainer);

setTimeout(function () {
// modal should be focused when opened
document.activeElement.should.equal(focusableContainer);
done();
}, 0);
});
});


});

0 comments on commit 0ce46b9

Please sign in to comment.