Skip to content

Commit 3300f2c

Browse files
[added] Add elementToFocus prop (#5)
This makes it so if you provide elementAfterFocus then that particular element will be focused after the component renders instead of the content of the tray. This is handy to set focus to a close button or some other arbitrary element within the content.
1 parent 045f1a7 commit 3300f2c

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

lib/components/Tray.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export default React.createClass({
1212
onOpen: React.PropTypes.func,
1313
closeTimeoutMS: React.PropTypes.number,
1414
closeOnBlur: React.PropTypes.bool,
15-
maintainFocus: React.PropTypes.bool
15+
maintainFocus: React.PropTypes.bool,
16+
elementToFocus: React.PropTypes.string
1617
},
1718

1819
getDefaultProps() {

lib/components/TrayPortal.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export default React.createClass({
5858
closeOnBlur: PropTypes.bool,
5959
closeTimeoutMS: PropTypes.number,
6060
children: PropTypes.any,
61-
maintainFocus: PropTypes.bool
61+
maintainFocus: PropTypes.bool,
62+
elementToFocus: PropTypes.string
6263
},
6364

6465
getInitialState() {
@@ -86,7 +87,11 @@ export default React.createClass({
8687

8788
componentDidUpdate() {
8889
if (this.focusAfterRender) {
89-
this.focusContent();
90+
if (this.props.elementToFocus) {
91+
this.focusSelector(this.props.elementToFocus);
92+
} else {
93+
this.focusContent();
94+
}
9095
this.setFocusAfterRender(false);
9196
}
9297
},
@@ -99,6 +104,12 @@ export default React.createClass({
99104
this.refs.content.focus();
100105
},
101106

107+
focusSelector(querySelectorToUse) {
108+
const el = document.querySelectorAll(querySelectorToUse);
109+
const element = (el.length) ? el[0] : el;
110+
element.focus();
111+
},
112+
102113
handleOverlayClick(e) {
103114
if (!isChild(this.refs.content, e.target)) {
104115
this.props.onBlur();

lib/components/__tests__/Tray-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,23 @@ describe('react-tray', function() {
116116
equal(document.activeElement, lastItem);
117117
});
118118
});
119+
120+
describe('focusAfterOpen prop', function() {
121+
beforeEach(function(done) {
122+
const props = {isOpen: true, onBlur: function() {}, closeTimeoutMS: 0, maintainFocus: true, focusAfterOpen: '#two'};
123+
const children = (
124+
<div>
125+
<a href="#" id="one">One</a>
126+
<a href="#" id="two">Two</a>
127+
<a href="#" id="three">Three</a>
128+
</div>
129+
);
130+
renderTray(props, children, () => done());
131+
});
132+
133+
it('sends focus to the DOM node found via the selector passed in the prop', function() {
134+
const secondItem = document.querySelector('#two');
135+
equal(document.activeElement, secondItem);
136+
});
137+
});
119138
});

0 commit comments

Comments
 (0)