Skip to content

Commit

Permalink
kg: add focus for Select component
Browse files Browse the repository at this point in the history
  • Loading branch information
buronnie committed Oct 12, 2017
1 parent e5dda8e commit f10c9d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
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
4 changes: 3 additions & 1 deletion test/components/DateInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ describe('<DateInput />', () => {
it('should support focus', () => {
const wrapper = mount(<DateInput defaultValue="1/23/1983" />);
const component = wrapper.instance();
assert.equal(typeof component.focus, 'function');
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 f10c9d5

Please sign in to comment.