Skip to content

Commit ed63549

Browse files
authored
fix onFocus not trigger in IE11 (#513)
* fix onFocus not trigger in IE11 close ant-design/ant-design#25354 * use IE hacks * fix test case
1 parent 042ac70 commit ed63549

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

src/Selector/index.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,21 @@ const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> =
196196
pastedTextRef.current = value;
197197
};
198198

199-
// ====================== Focus ======================
200-
// Should focus input if click the selector
201-
const onClick = ({ target }) => {
202-
if (target !== inputRef.current) {
203-
inputRef.current.focus();
204-
}
205-
};
206-
207199
const onMouseDown: React.MouseEventHandler<HTMLElement> = event => {
208200
const inputMouseDown = getInputMouseDown();
209-
if (event.target !== inputRef.current && !inputMouseDown) {
210-
event.preventDefault();
201+
if (event.target !== inputRef.current) {
202+
if (!inputMouseDown) {
203+
event.preventDefault();
204+
}
205+
// Should focus input if click the selector
206+
const isIE = (document.body.style as any).msTouchAction !== undefined;
207+
if (isIE) {
208+
setTimeout(() => {
209+
inputRef.current.focus();
210+
});
211+
} else {
212+
inputRef.current.focus();
213+
}
211214
}
212215

213216
if ((mode !== 'combobox' && (!showSearch || !inputMouseDown)) || !open) {
@@ -236,12 +239,7 @@ const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> =
236239
);
237240

238241
return (
239-
<div
240-
ref={domRef}
241-
className={`${prefixCls}-selector`}
242-
onClick={onClick}
243-
onMouseDown={onMouseDown}
244-
>
242+
<div ref={domRef} className={`${prefixCls}-selector`} onMouseDown={onMouseDown}>
245243
{selectNode}
246244
</div>
247245
);

tests/Select.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ describe('Select.Basic', () => {
573573

574574
const focusSpy = jest.spyOn(wrapper.find('input').instance(), 'focus');
575575

576+
wrapper.find('.rc-select-selector').simulate('mousedown');
576577
wrapper.find('.rc-select-selector').simulate('click');
577578
expect(focusSpy).toHaveBeenCalled();
578579

@@ -604,6 +605,7 @@ describe('Select.Basic', () => {
604605

605606
const inputSpy = jest.spyOn(wrapper2.find('input').instance(), 'focus');
606607

608+
wrapper2.find('.rc-select-selection-placeholder').simulate('mousedown');
607609
wrapper2.find('.rc-select-selection-placeholder').simulate('click');
608610
expect(inputSpy).toHaveBeenCalled();
609611
});
@@ -770,8 +772,8 @@ describe('Select.Basic', () => {
770772
);
771773

772774
const focusSpy = jest.spyOn(wrapper.find('input').instance(), 'focus');
775+
wrapper.find('.rc-select-selection-placeholder').simulate('mousedown');
773776
wrapper.find('.rc-select-selection-placeholder').simulate('click');
774-
775777
expect(focusSpy).toHaveBeenCalled();
776778
});
777779

tests/shared/blurTest.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { mount } from 'enzyme';
22
import React from 'react';
33
import Option from '../../src/Option';
44
import Select from '../../src/Select';
5-
import { injectRunAllTimers, toggleOpen } from '../utils/common';
5+
import { injectRunAllTimers } from '../utils/common';
66

77
export default function blurTest(mode: any) {
88
describe(`blur of ${mode}`, () => {
@@ -56,7 +56,6 @@ export default function blurTest(mode: any) {
5656
.find('input')
5757
.instance()
5858
.focus();
59-
toggleOpen(wrapper);
6059
expect(handleBlur).not.toHaveBeenCalled();
6160

6261
wrapper.find('input').simulate('blur');
@@ -66,7 +65,7 @@ export default function blurTest(mode: any) {
6665
wrapper
6766
.find('input')
6867
.instance()
69-
.focus();
68+
.blur();
7069
jest.runAllTimers();
7170
handleBlur.mockReset();
7271

0 commit comments

Comments
 (0)