Skip to content

Commit 7a21259

Browse files
committed
fix: Click out of input should also trigger input focus
fix ant-design/ant-design#21149
1 parent 1251c70 commit 7a21259

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Picker.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
272272
}
273273
};
274274

275+
const onInternalMouseUp: React.MouseEventHandler<HTMLDivElement> = (
276+
...args
277+
) => {
278+
if (onMouseUp) {
279+
onMouseUp(...args);
280+
}
281+
282+
if (inputRef.current) {
283+
inputRef.current.focus();
284+
}
285+
};
286+
275287
// ============================= Input =============================
276288
const [inputProps, { focused, typing }] = usePickerInput({
277289
blurToCancel: needConfirmButton,
@@ -428,7 +440,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
428440
})}
429441
style={style}
430442
onMouseDown={onMouseDown}
431-
onMouseUp={onMouseUp}
443+
onMouseUp={onInternalMouseUp}
432444
onMouseEnter={onMouseEnter}
433445
onMouseLeave={onMouseLeave}
434446
onContextMenu={onContextMenu}

tests/picker.spec.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,15 @@ describe('Picker.Basic', () => {
546546

547547
expect(wrapper.find('input').prop('value')).toEqual('2020-1st');
548548
});
549+
550+
it('click outside should also focus', () => {
551+
const wrapper = mount(<MomentPicker />);
552+
const inputElement = (wrapper
553+
.find('input')
554+
.instance() as any) as HTMLInputElement;
555+
inputElement.focus = jest.fn();
556+
557+
wrapper.find('.rc-picker').simulate('mouseUp');
558+
expect(inputElement.focus).toHaveBeenCalled();
559+
});
549560
});

0 commit comments

Comments
 (0)