Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const Select = createClass({
},

componentWillUnmount() {
this.clearFocusTime();
this.clearBlurTime();
this.clearAdjustTimer();
if (this.dropdownContainer) {
Expand Down Expand Up @@ -170,6 +171,12 @@ const Select = createClass({
},

onDropdownVisibleChange(open) {
if (open && !this._focused) {
this.clearBlurTime();
this.timeoutFocus();
this._focused = true;
this.updateFocusClassName();
}
this.setOpenState(open);
},

Expand Down Expand Up @@ -301,13 +308,16 @@ const Select = createClass({
},

onOuterFocus(e) {
this.clearBlurTime();
if (!isMultipleOrTagsOrCombobox(this.props) && e.target === this.getInputDOMNode()) {
return;
}
this.clearBlurTime();
if (this._focused) {
return;
}
this._focused = true;
this.updateFocusClassName();
this.props.onFocus();
this.timeoutFocus();
},

onPopupFocus() {
Expand Down Expand Up @@ -545,6 +555,22 @@ const Select = createClass({
}
}
},

timeoutFocus() {
if (this.focusTimer) {
this.clearFocusTime();
}
this.focusTimer = setTimeout(() => {
this.props.onFocus();
}, 10);
},

clearFocusTime() {
if (this.focusTimer) {
clearTimeout(this.focusTimer);
this.focusTimer = null;
}
},
clearBlurTime() {
if (this.blurTimer) {
clearTimeout(this.blurTimer);
Expand Down Expand Up @@ -574,11 +600,13 @@ const Select = createClass({
if (input && (open || isMultipleOrTagsOrCombobox(this.props))) {
if (activeElement !== input) {
input.focus();
this._focused = true;
}
} else {
const selection = this.refs.selection;
if (activeElement !== selection) {
selection.focus();
this._focused = true;
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,39 @@ describe('Select', () => {
<Option value="2">2</Option>
</Select>
);
jest.useFakeTimers();
wrapper.find('.rc-select').simulate('focus');
jest.runAllTimers();
});

it('set _focused to true', () => {
expect(wrapper.instance()._focused).toBe(true);
});

it('fires focus event', () => {
expect(handleFocus).toBeCalled();
expect(handleFocus.mock.calls.length).toBe(1);
});

it('set className', () => {
expect(wrapper.find('.rc-select').node.className).toContain('-focus');
});
});

describe('click input will trigger focus', () => {
let handleFocus;
let wrapper;
beforeEach(() => {
handleFocus = jest.fn();
wrapper = mount(
<Select onFocus={handleFocus}>
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>
);
jest.useFakeTimers();
wrapper.find('.rc-select input').simulate('click');
jest.runAllTimers();
});

it('set _focused to true', () => {
Expand All @@ -265,6 +297,7 @@ describe('Select', () => {

it('fires focus event', () => {
expect(handleFocus).toBeCalled();
expect(handleFocus.mock.calls.length).toBe(1);
});

it('set className', () => {
Expand Down