From 0ce46b91fba58a04d831f9bd341cb43d2beb833a Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Mon, 15 Jun 2015 15:18:34 -0400 Subject: [PATCH] [changed] only autofocus modals when enforceFocus is true (the default) fixes #842, only move focus to the Modal if `enforceFocus` is true --- src/Modal.js | 9 ++++++--- test/ModalSpec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Modal.js b/src/Modal.js index a6a5b20df1..bcd6c2db7e 100644 --- a/src/Modal.js +++ b/src/Modal.js @@ -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 () { diff --git a/test/ModalSpec.js b/test/ModalSpec.js index e81209d9e1..0e516d52dc 100644 --- a/test/ModalSpec.js +++ b/test/ModalSpec.js @@ -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 ( + {}} container={this}> + Message + + ); + } else { + return ; + } + } + }); + + React.render(, focusableContainer); + + setTimeout(function () { + // modal should be focused when opened + document.activeElement.should.equal(focusableContainer); + done(); + }, 0); + }); }); + });