Skip to content

Commit f3b4731

Browse files
[changed] Move elementToFocus prop to getElementToFocus (#9)
This moves in the direction of allowing the caller to specify the method of finding the element needed rather than handling selection within the library itself.
1 parent 7a316ff commit f3b4731

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

lib/components/Tray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default React.createClass({
1414
closeTimeoutMS: React.PropTypes.number,
1515
closeOnBlur: React.PropTypes.bool,
1616
maintainFocus: React.PropTypes.bool,
17-
elementToFocus: React.PropTypes.string,
17+
getElementToFocus: a11yFunction,
1818
getAriaHideElement: a11yFunction
1919
},
2020

lib/components/TrayPortal.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default React.createClass({
5959
closeTimeoutMS: PropTypes.number,
6060
children: PropTypes.any,
6161
maintainFocus: PropTypes.bool,
62-
elementToFocus: PropTypes.string,
62+
getElementToFocus: PropTypes.func,
6363
getAriaHideElement: PropTypes.func
6464
},
6565

@@ -88,8 +88,8 @@ export default React.createClass({
8888

8989
componentDidUpdate() {
9090
if (this.focusAfterRender) {
91-
if (this.props.elementToFocus) {
92-
this.focusSelector(this.props.elementToFocus);
91+
if (this.props.getElementToFocus) {
92+
this.props.getElementToFocus().focus();
9393
} else {
9494
this.focusContent();
9595
}
@@ -105,17 +105,6 @@ export default React.createClass({
105105
this.refs.content.focus();
106106
},
107107

108-
findSingleElement(querySelectorToUse) {
109-
const el = document.querySelectorAll(querySelectorToUse);
110-
const element = (el.length) ? el[0] : el;
111-
return element;
112-
},
113-
114-
focusSelector(querySelectorToUse) {
115-
const element = this.findSingleElement(querySelectorToUse);
116-
element.focus();
117-
},
118-
119108
toggleAriaHidden(element) {
120109
if (!element.getAttribute('aria-hidden')) {
121110
element.setAttribute('aria-hidden', true);

lib/components/__tests__/Tray-test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,13 @@ describe('react-tray', function() {
117117
});
118118
});
119119

120-
describe('elementToFocus prop', function() {
120+
describe('getElementToFocus prop', function() {
121+
const getElementToFocus = () => {
122+
return document.getElementById('two');
123+
};
124+
121125
beforeEach(function(done) {
122-
const props = {isOpen: true, onBlur: function() {}, closeTimeoutMS: 0, maintainFocus: true, elementToFocus: '#two'};
126+
const props = {isOpen: true, onBlur: function() {}, closeTimeoutMS: 0, maintainFocus: true, getElementToFocus: getElementToFocus};
123127
const children = (
124128
<div>
125129
<a href="#" id="one">One</a>

0 commit comments

Comments
 (0)