Skip to content

Commit

Permalink
Merge pull request #312 from appfolio/feature/add-focus-to-date-input
Browse files Browse the repository at this point in the history
kg: add focus functionality to DateInput
  • Loading branch information
gthomas-appfolio authored Oct 13, 2017
2 parents f0e84b9 + f10c9d5 commit 0aa6b5b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/components/DateInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ export default class DateInput extends React.Component {
}
}

focus() {
this.inputEl.focus();
}

render() {
const { className, dateVisible, disabled, footer, header, showOnFocus } = this.props;
const { open } = this.state;
Expand Down
7 changes: 7 additions & 0 deletions src/components/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class Select extends React.Component {
this.props.onChange(value);
}

bindInput = el => this.selectEl = el;

focus() {
this.selectEl.focus();
}

render() {
const { value, ...props } = this.props;
delete props.onChange; // don't pass onChange prop to react-select
Expand All @@ -54,6 +60,7 @@ class Select extends React.Component {
inputProps={{ ...props.inputProps, name: props.name || '' }}
onChange={this.onChange}
value={value || this.state.value}
ref={this.bindInput}
{...props}
/>
);
Expand Down
8 changes: 8 additions & 0 deletions test/components/DateInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,12 @@ describe('<DateInput />', () => {
mount(<DateInput parse={callback} defaultValue="1-2-3" dateFormat="MM-DD-YY" />);
assert(callback.calledWith('1-2-3', 'MM-DD-YY'));
});

it('should support focus', () => {
const wrapper = mount(<DateInput defaultValue="1/23/1983" />);
const component = wrapper.instance();
sinon.spy(component.inputEl, 'focus');
component.focus();
assert.equal(component.inputEl.focus.calledOnce, true);
});
});
10 changes: 9 additions & 1 deletion test/components/Select.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import assert from 'assert';
import { shallow } from 'enzyme';
import { shallow, mount } from 'enzyme';
import sinon from 'sinon';

import ReactSelect from 'react-select-plus';
Expand Down Expand Up @@ -79,4 +79,12 @@ describe('<Select />', () => {
const component = shallow(<Select loadOptions={getOptions} />);
assert.equal(component.type(), ReactSelect.Async);
});

it('should support focus', () => {
const wrapper = mount(<Select />);
const component = wrapper.instance();
sinon.spy(component.selectEl, 'focus');
component.focus();
assert.equal(component.selectEl.focus.calledOnce, true);
});
});

0 comments on commit 0aa6b5b

Please sign in to comment.