Skip to content

Commit

Permalink
[changed] Move elementToFocus prop to getElementToFocus (#9)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
claydiffrient authored Jul 13, 2016
1 parent 7a316ff commit f3b4731
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/components/Tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default React.createClass({
closeTimeoutMS: React.PropTypes.number,
closeOnBlur: React.PropTypes.bool,
maintainFocus: React.PropTypes.bool,
elementToFocus: React.PropTypes.string,
getElementToFocus: a11yFunction,
getAriaHideElement: a11yFunction
},

Expand Down
17 changes: 3 additions & 14 deletions lib/components/TrayPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default React.createClass({
closeTimeoutMS: PropTypes.number,
children: PropTypes.any,
maintainFocus: PropTypes.bool,
elementToFocus: PropTypes.string,
getElementToFocus: PropTypes.func,
getAriaHideElement: PropTypes.func
},

Expand Down Expand Up @@ -88,8 +88,8 @@ export default React.createClass({

componentDidUpdate() {
if (this.focusAfterRender) {
if (this.props.elementToFocus) {
this.focusSelector(this.props.elementToFocus);
if (this.props.getElementToFocus) {
this.props.getElementToFocus().focus();
} else {
this.focusContent();
}
Expand All @@ -105,17 +105,6 @@ export default React.createClass({
this.refs.content.focus();
},

findSingleElement(querySelectorToUse) {
const el = document.querySelectorAll(querySelectorToUse);
const element = (el.length) ? el[0] : el;
return element;
},

focusSelector(querySelectorToUse) {
const element = this.findSingleElement(querySelectorToUse);
element.focus();
},

toggleAriaHidden(element) {
if (!element.getAttribute('aria-hidden')) {
element.setAttribute('aria-hidden', true);
Expand Down
8 changes: 6 additions & 2 deletions lib/components/__tests__/Tray-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ describe('react-tray', function() {
});
});

describe('elementToFocus prop', function() {
describe('getElementToFocus prop', function() {
const getElementToFocus = () => {
return document.getElementById('two');
};

beforeEach(function(done) {
const props = {isOpen: true, onBlur: function() {}, closeTimeoutMS: 0, maintainFocus: true, elementToFocus: '#two'};
const props = {isOpen: true, onBlur: function() {}, closeTimeoutMS: 0, maintainFocus: true, getElementToFocus: getElementToFocus};
const children = (
<div>
<a href="#" id="one">One</a>
Expand Down

0 comments on commit f3b4731

Please sign in to comment.