Skip to content

Commit 0ce46b9

Browse files
committed
[changed] only autofocus modals when enforceFocus is true (the default)
fixes react-bootstrap#842, only move focus to the Modal if `enforceFocus` is true
1 parent 6c7a5f0 commit 0ce46b9

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/Modal.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,12 @@ const Modal = React.createClass({
269269
},
270270

271271
focusModalContent () {
272-
this.lastFocus = domUtils.activeElement(this);
273-
let modalContent = React.findDOMNode(this.refs.modal);
274-
modalContent.focus();
272+
if (this.props.enforceFocus) {
273+
this.lastFocus = domUtils.activeElement(this);
274+
275+
let modalContent = React.findDOMNode(this.refs.modal);
276+
modalContent.focus();
277+
}
275278
},
276279

277280
restoreLastFocus () {

test/ModalSpec.js

+30
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,36 @@ describe('Modal', function () {
156156
}, 0);
157157
});
158158

159+
it('Should not focus on the Modal when enforceFocus is false', function (done) {
160+
161+
document.activeElement.should.equal(focusableContainer);
162+
163+
let Container = React.createClass({
164+
getInitialState() {
165+
return {modalOpen: true};
166+
},
167+
render() {
168+
if (this.state.modalOpen) {
169+
return (
170+
<Modal enforceFocus={false} onRequestHide={()=>{}} container={this}>
171+
<strong>Message</strong>
172+
</Modal>
173+
);
174+
} else {
175+
return <span/>;
176+
}
177+
}
178+
});
179+
180+
React.render(<Container />, focusableContainer);
181+
182+
setTimeout(function () {
183+
// modal should be focused when opened
184+
document.activeElement.should.equal(focusableContainer);
185+
done();
186+
}, 0);
187+
});
159188
});
160189

190+
161191
});

0 commit comments

Comments
 (0)