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: fix picker in shadowdom #213

Merged
merged 1 commit into from
Feb 7, 2021
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
23 changes: 12 additions & 11 deletions src/hooks/usePickerInput.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as React from 'react';
import { useState, useEffect, useRef } from 'react';
import KeyCode from 'rc-util/lib/KeyCode';
import { addGlobalMouseDownEvent } from '../utils/uiUtil';
import { addGlobalMouseDownEvent, getTargetFromEvent } from '../utils/uiUtil';

export default function usePickerInput({
open,
Expand All @@ -21,10 +21,7 @@ export default function usePickerInput({
isClickOutside: (clickElement: EventTarget | null) => boolean;
triggerOpen: (open: boolean) => void;
forwardKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => boolean;
onKeyDown: (
e: React.KeyboardEvent<HTMLInputElement>,
preventDefault: () => void,
) => void;
onKeyDown: (e: React.KeyboardEvent<HTMLInputElement>, preventDefault: () => void) => void;
blurToCancel?: boolean;
onSubmit: () => void | boolean;
onCancel: () => void;
Expand All @@ -49,7 +46,7 @@ export default function usePickerInput({
setTyping(true);
triggerOpen(true);
},
onKeyDown: e => {
onKeyDown: (e) => {
const preventDefault = (): void => {
preventDefaultRef.current = true;
};
Expand Down Expand Up @@ -98,7 +95,7 @@ export default function usePickerInput({
}
},

onFocus: e => {
onFocus: (e) => {
setTyping(true);
setFocused(true);

Expand All @@ -107,7 +104,7 @@ export default function usePickerInput({
}
},

onBlur: e => {
onBlur: (e) => {
if (preventBlurRef.current || !isClickOutside(document.activeElement)) {
preventBlurRef.current = false;
return;
Expand Down Expand Up @@ -145,16 +142,20 @@ export default function usePickerInput({

// Global click handler
useEffect(() =>
addGlobalMouseDownEvent(({ target }: MouseEvent) => {
addGlobalMouseDownEvent((e: MouseEvent) => {
const target = getTargetFromEvent(e);

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

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

// Always set back in case `onBlur` prevented by user
requestAnimationFrame(() => {
preventBlurRef.current = false;
});
} else if (!focused) {
} else if (!focused || clickedOutside) {
Copy link
Member

Choose a reason for hiding this comment

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

这个没有意义吧,else if 时 clickedOutside 一定是 true 的。

triggerOpen(false);
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/utils/uiUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export function addGlobalMouseDownEvent(callback: ClickEventHandler) {
if (!globalClickFunc && typeof window !== 'undefined' && window.addEventListener) {
globalClickFunc = (e: MouseEvent) => {
// Clone a new list to avoid repeat trigger events
[...clickCallbacks].forEach(queueFunc => {
[...clickCallbacks].forEach((queueFunc) => {
queueFunc(e);
});
};
Expand All @@ -219,6 +219,17 @@ export function addGlobalMouseDownEvent(callback: ClickEventHandler) {
};
}

export function getTargetFromEvent(e: Event) {
const target = e.target as HTMLElement;

// get target if in shadow dom
if (e.composed && target.shadowRoot) {
return (e.composedPath?.()[0] || target) as HTMLElement;
}

return target;
}

// ====================== Mode ======================
const getYearNextMode = (next: PanelMode): PanelMode => {
if (next === 'month' || next === 'date') {
Expand Down Expand Up @@ -261,5 +272,5 @@ export function elementsContains(
elements: (HTMLElement | undefined | null)[],
target: HTMLElement,
) {
return elements.some(ele => ele && ele.contains(target));
return elements.some((ele) => ele && ele.contains(target));
}