From 4168df55684299cd4b791c1cd803620974c8d75f Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Mon, 11 Jul 2016 18:55:41 -0400 Subject: [PATCH] [added] autoFocus support to dropdown, calendar, and selectlist --- src/Calendar.jsx | 1 + src/DateTimePicker.jsx | 1 + src/DropdownList.jsx | 5 +---- src/SelectList.jsx | 1 + src/mixins/AutoFocusMixin.js | 15 +++++++++++++++ test/dropdown.browser.jsx | 8 ++++++++ 6 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/mixins/AutoFocusMixin.js diff --git a/src/Calendar.jsx b/src/Calendar.jsx index 907a83c9c..58d8ed397 100644 --- a/src/Calendar.jsx +++ b/src/Calendar.jsx @@ -112,6 +112,7 @@ let Calendar = React.createClass({ mixins: [ require('./mixins/TimeoutMixin'), + require('./mixins/AutoFocusMixin'), require('./mixins/PureRenderMixin'), require('./mixins/RtlParentContextMixin'), require('./mixins/AriaDescendantMixin')(), diff --git a/src/DateTimePicker.jsx b/src/DateTimePicker.jsx index d85a9ac2e..c34764fe5 100644 --- a/src/DateTimePicker.jsx +++ b/src/DateTimePicker.jsx @@ -239,6 +239,7 @@ var DateTimePicker = React.createClass({ id={id} tabIndex='-1' value={value} + autoFocus={false} onChange={this.handleDateSelect} // #75: need to aggressively reclaim focus from the calendar otherwise // disabled header/footer buttons will drop focus completely from the widget diff --git a/src/DropdownList.jsx b/src/DropdownList.jsx index 757b617aa..d6e9749a0 100644 --- a/src/DropdownList.jsx +++ b/src/DropdownList.jsx @@ -44,14 +44,10 @@ var propTypes = { groupBy: CustomPropTypes.accessor, onSelect: React.PropTypes.func, - searchTerm: React.PropTypes.string, onSearch: React.PropTypes.func, - busy: React.PropTypes.bool, - delay: React.PropTypes.number, - dropUp: React.PropTypes.bool, duration: React.PropTypes.number, //popup @@ -72,6 +68,7 @@ var DropdownList = React.createClass({ mixins: [ require('./mixins/TimeoutMixin'), + require('./mixins/AutoFocusMixin'), require('./mixins/PureRenderMixin'), require('./mixins/DataFilterMixin'), require('./mixins/PopupScrollToMixin'), diff --git a/src/SelectList.jsx b/src/SelectList.jsx index 2ee3ee1a9..2127a7450 100644 --- a/src/SelectList.jsx +++ b/src/SelectList.jsx @@ -69,6 +69,7 @@ var SelectList = React.createClass({ mixins: [ require('./mixins/TimeoutMixin'), + require('./mixins/AutoFocusMixin'), require('./mixins/RtlParentContextMixin'), require('./mixins/AriaDescendantMixin')(), require('./mixins/FocusMixin')({ diff --git a/src/mixins/AutoFocusMixin.js b/src/mixins/AutoFocusMixin.js new file mode 100644 index 000000000..0ba5f896e --- /dev/null +++ b/src/mixins/AutoFocusMixin.js @@ -0,0 +1,15 @@ +import React from 'react'; +import { findDOMNode } from 'react-dom'; + +export default { + propTypes: { + autoFocus: React.PropTypes.bool + }, + + componentDidMount() { + let { autoFocus } = this.props; + + if (autoFocus) + this.focus ? this.focus() : findDOMNode(this).focus() + } +}; diff --git a/test/dropdown.browser.jsx b/test/dropdown.browser.jsx index 0f5e14705..812acb780 100644 --- a/test/dropdown.browser.jsx +++ b/test/dropdown.browser.jsx @@ -49,6 +49,14 @@ describe('DROPDOWNS', function(){ expect(openSpy.calledWith(true)).to.be(true); }) + it('should respect autoFocus', () => { + expect( + tsp() + .render(true) + .dom() + ).to.equal(document.activeElement); + }) + it('should not open when clicked while disabled or readOnly', () => { let openSpy = sinon.spy();