Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent picker blur when still in focus at 2.7.x #620

Merged
merged 1 commit into from
Apr 17, 2023
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
5 changes: 3 additions & 2 deletions src/hooks/usePickerInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,9 @@ export default function usePickerInput({
useEffect(() =>
addGlobalMouseDownEvent((e: MouseEvent) => {
const target = getTargetFromEvent(e);
const clickedOutside = isClickOutside(target);

if (open) {
const clickedOutside = isClickOutside(target);

if (!clickedOutside) {
preventBlurRef.current = true;

Expand All @@ -181,6 +180,8 @@ export default function usePickerInput({
} else if (!focused || clickedOutside) {
triggerOpen(false);
}
} else if (focused && !clickedOutside) {
preventBlurRef.current = true;
}
}),
);
Expand Down
25 changes: 25 additions & 0 deletions tests/picker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,31 @@ describe('Picker.Basic', () => {
expect(preventDefault).toHaveBeenCalled();
});

it('not fire blur when clickinside and is in focus ', () => {
const onBlur = jest.fn();
const wrapper = mount(
<MomentPicker onBlur={onBlur} suffixIcon={<div className="suffix-icon">X</div>} />,
);
wrapper.openPicker();
wrapper.find('input').simulate('keyDown', { which: KeyCode.ESC });
// workaround: fire an event that bubbles from suffix div to window
const mouseDownEvent = new MouseEvent('mousedown', {
view: window,
bubbles: true,
});
Reflect.defineProperty(mouseDownEvent, 'target', {
value: wrapper.find('.suffix-icon').getDOMNode(),
enumerable: true,
});
fireEvent(window, mouseDownEvent);
Comment on lines +325 to +333
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems enzyme renders elements independently. I retrieved null by document.querySelector(). Besides, fireEvents(wrapper.find().getDOMNode()) not work. So placed this workaround.


wrapper.find('input').simulate('blur');
expect(onBlur).toHaveBeenCalledTimes(0);

wrapper.find('input').simulate('blur');
expect(onBlur).toHaveBeenCalledTimes(1);
});

describe('full steps', () => {
[
{
Expand Down