Skip to content

Commit 80943b4

Browse files
authored
Merge pull request #1569 from the-spyke/fix-1437-creatable-no-focus
Fix missing .focus() method in Creatable (#1437)
2 parents 5f7c4c6 + c3e9f46 commit 80943b4

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/AsyncCreatable.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function reduce(obj, props = {}){
1313
const AsyncCreatable = React.createClass({
1414
displayName: 'AsyncCreatableSelect',
1515

16+
focus () {
17+
this.select.focus();
18+
},
19+
1620
render () {
1721
return (
1822
<Select.Async {...this.props}>
@@ -26,6 +30,7 @@ const AsyncCreatable = React.createClass({
2630
return asyncProps.onInputChange(input);
2731
}}
2832
ref={(ref) => {
33+
this.select = ref;
2934
creatableProps.ref(ref);
3035
asyncProps.ref(ref);
3136
}}

src/Creatable.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ const Creatable = React.createClass({
203203
}
204204
},
205205

206+
focus () {
207+
this.select.focus();
208+
},
209+
206210
render () {
207211
const {
208212
newOptionCreator,

test/AsyncCreatable-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,17 @@ describe('AsyncCreatable', () => {
5959
class: ['foo']
6060
});
6161
});
62+
63+
describe('.focus()', () => {
64+
beforeEach(() => {
65+
createControl({});
66+
TestUtils.Simulate.blur(filterInputNode);
67+
});
68+
69+
it('focuses the search input', () => {
70+
expect(filterInputNode, 'not to equal', document.activeElement);
71+
creatableInstance.focus();
72+
expect(filterInputNode, 'to equal', document.activeElement);
73+
});
74+
});
6275
});

test/Creatable-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,17 @@ describe('Creatable', () => {
237237
createControl({ onInputKeyDown: event => done() });
238238
return creatableInstance.onInputKeyDown({ keyCode: 97 });
239239
});
240+
241+
describe('.focus()', () => {
242+
beforeEach(() => {
243+
createControl({});
244+
TestUtils.Simulate.blur(filterInputNode);
245+
});
246+
247+
it('focuses the search input', () => {
248+
expect(filterInputNode, 'not to equal', document.activeElement);
249+
creatableInstance.focus();
250+
expect(filterInputNode, 'to equal', document.activeElement);
251+
});
252+
});
240253
});

0 commit comments

Comments
 (0)