Skip to content

Commit 3788753

Browse files
authored
fix: prevent picker blur when still in focus (#612)
1 parent b997954 commit 3788753

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/hooks/usePickerInput.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type * as React from 'react';
2-
import { useState, useEffect, useRef } from 'react';
31
import KeyCode from 'rc-util/lib/KeyCode';
2+
import type * as React from 'react';
3+
import { useEffect, useRef, useState } from 'react';
44
import { addGlobalMouseDownEvent, getTargetFromEvent } from '../utils/uiUtil';
55

66
export default function usePickerInput({
@@ -149,10 +149,9 @@ export default function usePickerInput({
149149
useEffect(() =>
150150
addGlobalMouseDownEvent((e: MouseEvent) => {
151151
const target = getTargetFromEvent(e);
152+
const clickedOutside = isClickOutside(target);
152153

153154
if (open) {
154-
const clickedOutside = isClickOutside(target);
155-
156155
if (!clickedOutside) {
157156
preventBlurRef.current = true;
158157

@@ -163,6 +162,8 @@ export default function usePickerInput({
163162
} else if (!focused || clickedOutside) {
164163
triggerOpen(false);
165164
}
165+
} else if (focused && !clickedOutside) {
166+
preventBlurRef.current = true;
166167
}
167168
}),
168169
);

tests/picker.spec.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,21 @@ describe('Picker.Basic', () => {
341341
expect(mouseDownEvent.defaultPrevented).toBeTruthy();
342342
});
343343

344+
it('not fire blur when clickinside and is in focus ', () => {
345+
const onBlur = jest.fn();
346+
const { container } = render(
347+
<MomentPicker onBlur={onBlur} suffixIcon={<div className="suffix-icon">X</div>} />,
348+
);
349+
openPicker(container);
350+
keyDown(KeyCode.ESC);
351+
fireEvent.mouseDown(container.querySelector('.suffix-icon'));
352+
fireEvent.blur(container.querySelector('input'));
353+
expect(onBlur).toHaveBeenCalledTimes(0);
354+
355+
fireEvent.blur(container.querySelector('input'));
356+
expect(onBlur).toHaveBeenCalledTimes(1);
357+
});
358+
344359
describe('full steps', () => {
345360
[
346361
{

0 commit comments

Comments
 (0)