Skip to content

Commit 2d43933

Browse files
wangmeijianafc163
andauthored
fix: Picker should open when click inside (#490)
* fix: Picker should open when click inside * fix: add picker test * Update src/Picker.tsx Co-authored-by: afc163 <[email protected]> Co-authored-by: 王美建 <> Co-authored-by: afc163 <[email protected]>
1 parent 0b843c9 commit 2d43933

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/Picker.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,8 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
284284
}
285285
};
286286

287-
const onInternalMouseUp: React.MouseEventHandler<HTMLDivElement> = (...args) => {
288-
if (onMouseUp) {
289-
onMouseUp(...args);
290-
}
287+
const onInternalClick: React.MouseEventHandler<HTMLDivElement> = (...args) => {
288+
onClick?.(...args);
291289

292290
if (inputRef.current) {
293291
inputRef.current.focus();
@@ -536,11 +534,11 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
536534
})}
537535
style={style}
538536
onMouseDown={onMouseDown}
539-
onMouseUp={onInternalMouseUp}
537+
onMouseUp={onMouseUp}
540538
onMouseEnter={onMouseEnter}
541539
onMouseLeave={onMouseLeave}
542540
onContextMenu={onContextMenu}
543-
onClick={onClick}
541+
onClick={onInternalClick}
544542
>
545543
<div
546544
className={classNames(`${prefixCls}-input`, {

tests/picker.spec.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,28 +617,38 @@ describe('Picker.Basic', () => {
617617
expect(wrapper.find('input').prop('value')).toEqual('2020-1st');
618618
});
619619

620-
it('click outside should also focus', () => {
621-
const onMouseUp = jest.fn();
622-
const wrapper = mount(<MomentPicker onMouseUp={onMouseUp} />);
620+
it('Picker should open when click inside', () => {
621+
const onClick = jest.fn();
622+
const wrapper = mount(<MomentPicker onClick={onClick} />);
623623
const inputElement = wrapper.find('input').instance() as any as HTMLInputElement;
624624
inputElement.focus = jest.fn();
625625

626-
wrapper.find('.rc-picker').simulate('mouseUp');
626+
wrapper.find('.rc-picker').simulate('click');
627627
expect(inputElement.focus).toHaveBeenCalled();
628628
expect(wrapper.isOpen()).toBeTruthy();
629629

630-
expect(onMouseUp).toHaveBeenCalled();
630+
expect(onClick).toHaveBeenCalled();
631631
});
632632

633633
it('not open when disabled', () => {
634634
const wrapper = mount(<MomentPicker disabled />);
635-
wrapper.find('.rc-picker').simulate('mouseUp');
635+
wrapper.find('.rc-picker').simulate('click');
636636
expect(wrapper.isOpen()).toBeFalsy();
637637

638638
wrapper.setProps({ disabled: false });
639639
expect(wrapper.isOpen()).toBeFalsy();
640640
});
641641

642+
it('not open when mouseup', () => {
643+
const wrapper = mount(<MomentPicker />);
644+
const inputElement = wrapper.find('input').instance() as any as HTMLInputElement;
645+
inputElement.focus = jest.fn();
646+
647+
wrapper.find('.rc-picker').simulate('mouseup');
648+
expect(inputElement.focus).toHaveBeenCalledTimes(0);
649+
expect(wrapper.isOpen()).toBeFalsy();
650+
});
651+
642652
it('defaultOpenValue in timePicker', () => {
643653
resetWarned();
644654
const onChange = jest.fn();

0 commit comments

Comments
 (0)