Skip to content

Commit 53d01f2

Browse files
authored
fix: prevent picker blur when still in focus (#620)
1 parent 3cc178f commit 53d01f2

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/hooks/usePickerInput.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,9 @@ export default function usePickerInput({
167167
useEffect(() =>
168168
addGlobalMouseDownEvent((e: MouseEvent) => {
169169
const target = getTargetFromEvent(e);
170+
const clickedOutside = isClickOutside(target);
170171

171172
if (open) {
172-
const clickedOutside = isClickOutside(target);
173-
174173
if (!clickedOutside) {
175174
preventBlurRef.current = true;
176175

@@ -181,6 +180,8 @@ export default function usePickerInput({
181180
} else if (!focused || clickedOutside) {
182181
triggerOpen(false);
183182
}
183+
} else if (focused && !clickedOutside) {
184+
preventBlurRef.current = true;
184185
}
185186
}),
186187
);

tests/picker.spec.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,31 @@ describe('Picker.Basic', () => {
314314
expect(preventDefault).toHaveBeenCalled();
315315
});
316316

317+
it('not fire blur when clickinside and is in focus ', () => {
318+
const onBlur = jest.fn();
319+
const wrapper = mount(
320+
<MomentPicker onBlur={onBlur} suffixIcon={<div className="suffix-icon">X</div>} />,
321+
);
322+
wrapper.openPicker();
323+
wrapper.find('input').simulate('keyDown', { which: KeyCode.ESC });
324+
// workaround: fire an event that bubbles from suffix div to window
325+
const mouseDownEvent = new MouseEvent('mousedown', {
326+
view: window,
327+
bubbles: true,
328+
});
329+
Reflect.defineProperty(mouseDownEvent, 'target', {
330+
value: wrapper.find('.suffix-icon').getDOMNode(),
331+
enumerable: true,
332+
});
333+
fireEvent(window, mouseDownEvent);
334+
335+
wrapper.find('input').simulate('blur');
336+
expect(onBlur).toHaveBeenCalledTimes(0);
337+
338+
wrapper.find('input').simulate('blur');
339+
expect(onBlur).toHaveBeenCalledTimes(1);
340+
});
341+
317342
describe('full steps', () => {
318343
[
319344
{

0 commit comments

Comments
 (0)