Skip to content

Commit

Permalink
fix: Click out of input should also trigger input focus
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jan 31, 2020
1 parent 1251c70 commit 7a21259
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
}
};

const onInternalMouseUp: React.MouseEventHandler<HTMLDivElement> = (
...args
) => {
if (onMouseUp) {
onMouseUp(...args);
}

if (inputRef.current) {
inputRef.current.focus();
}
};

// ============================= Input =============================
const [inputProps, { focused, typing }] = usePickerInput({
blurToCancel: needConfirmButton,
Expand Down Expand Up @@ -428,7 +440,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
})}
style={style}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
onMouseUp={onInternalMouseUp}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
onContextMenu={onContextMenu}
Expand Down
11 changes: 11 additions & 0 deletions tests/picker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,15 @@ describe('Picker.Basic', () => {

expect(wrapper.find('input').prop('value')).toEqual('2020-1st');
});

it('click outside should also focus', () => {
const wrapper = mount(<MomentPicker />);
const inputElement = (wrapper
.find('input')
.instance() as any) as HTMLInputElement;
inputElement.focus = jest.fn();

wrapper.find('.rc-picker').simulate('mouseUp');
expect(inputElement.focus).toHaveBeenCalled();
});
});

1 comment on commit 7a21259

@vercel
Copy link

@vercel vercel bot commented on 7a21259 Jan 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.