Skip to content
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
9 changes: 5 additions & 4 deletions src/hooks/usePickerInput.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as React from 'react';
import { useState, useEffect, useRef } from 'react';
import KeyCode from 'rc-util/lib/KeyCode';
import type * as React from 'react';
import { useEffect, useRef, useState } from 'react';
import { addGlobalMouseDownEvent, getTargetFromEvent } from '../utils/uiUtil';

export default function usePickerInput({
Expand Down Expand Up @@ -149,10 +149,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 @@ -163,6 +162,8 @@ export default function usePickerInput({
} else if (!focused || clickedOutside) {
triggerOpen(false);
}
} else if (focused && !clickedOutside) {
preventBlurRef.current = true;
}
}),
);
Expand Down
15 changes: 15 additions & 0 deletions tests/picker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ describe('Picker.Basic', () => {
expect(mouseDownEvent.defaultPrevented).toBeTruthy();
});

it('not fire blur when clickinside and is in focus ', () => {
const onBlur = jest.fn();
const { container } = render(
<MomentPicker onBlur={onBlur} suffixIcon={<div className="suffix-icon">X</div>} />,
);
openPicker(container);
keyDown(KeyCode.ESC);
fireEvent.mouseDown(container.querySelector('.suffix-icon'));
fireEvent.blur(container.querySelector('input'));
expect(onBlur).toHaveBeenCalledTimes(0);

fireEvent.blur(container.querySelector('input'));
expect(onBlur).toHaveBeenCalledTimes(1);
});

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