Skip to content

Commit 7d715d6

Browse files
authored
fix: cannot select end time in shadow dom (#860)
1 parent 2fb359e commit 7d715d6

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

src/PickerInput/RangePicker.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,12 @@ function RangePicker<DateType extends object = any>(
428428

429429
// ======================== Click =========================
430430
const onSelectorClick: React.MouseEventHandler<HTMLDivElement> = (event) => {
431-
if (!selectorRef.current.nativeElement.contains(document.activeElement)) {
431+
const rootNode = (event.target as HTMLElement).getRootNode();
432+
if (
433+
!selectorRef.current.nativeElement.contains(
434+
(rootNode as Document | ShadowRoot).activeElement ?? document.activeElement,
435+
)
436+
) {
432437
// Click to focus the enabled input
433438
const enabledIndex = disabled.findIndex((d) => !d);
434439
if (enabledIndex >= 0) {

tests/range.spec.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Note: zombieJ refactoring
22

33
import { act, createEvent, fireEvent, render } from '@testing-library/react';
4+
import { createRoot } from 'react-dom/client';
45
import type { Dayjs } from 'dayjs';
56
import dayjs from 'dayjs';
67
import KeyCode from 'rc-util/lib/KeyCode';
@@ -1973,4 +1974,32 @@ describe('Picker.Range', () => {
19731974
]);
19741975
expect(isOpen()).toBeFalsy();
19751976
});
1977+
1978+
const renderShadow = (props?: any) => {
1979+
const host = document.createElement('div');
1980+
document.body.appendChild(host);
1981+
1982+
const shadowRoot = host.attachShadow({
1983+
mode: 'open',
1984+
delegatesFocus: false,
1985+
});
1986+
const container = document.createElement('div');
1987+
shadowRoot.appendChild(container);
1988+
1989+
act(() => {
1990+
createRoot(container).render(<DayRangePicker {...props} />);
1991+
});
1992+
1993+
return shadowRoot;
1994+
};
1995+
1996+
it('the end date selector can be selected in shadow dom', () => {
1997+
const shadowRoot = renderShadow();
1998+
1999+
openPicker(shadowRoot, 1);
2000+
2001+
expect(shadowRoot.querySelectorAll('.rc-picker-input')[1]).toHaveClass(
2002+
'rc-picker-input-active',
2003+
);
2004+
});
19762005
});

tests/util/commonUtil.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export async function waitFakeTimer() {
112112
});
113113
}
114114

115-
export function openPicker(container: HTMLElement, index = 0) {
115+
export function openPicker(container: HTMLElement | ShadowRoot, index = 0) {
116116
const input = container.querySelectorAll('input')[index];
117117
fireEvent.mouseDown(input);
118118

@@ -123,7 +123,7 @@ export function openPicker(container: HTMLElement, index = 0) {
123123
fireEvent.click(input);
124124
}
125125

126-
export function closePicker(container: HTMLElement, index = 0) {
126+
export function closePicker(container: HTMLElement | ShadowRoot, index = 0) {
127127
const input = container.querySelectorAll('input')[index];
128128
fireEvent.blur(input);
129129

@@ -236,11 +236,9 @@ const dateFnsLocale = {
236236
generateConfig: dateFnsGenerateConfig,
237237
};
238238

239-
type DateFnsSinglePickerProps = Omit<PickerProps<Date>, 'locale' | 'generateConfig'> & React.RefAttributes<PickerRef>;
239+
type DateFnsSinglePickerProps = Omit<PickerProps<Date>, 'locale' | 'generateConfig'> &
240+
React.RefAttributes<PickerRef>;
240241

241242
export const DateFnsSinglePicker = (props: DateFnsSinglePickerProps) => {
242-
return <SinglePicker
243-
{...dateFnsLocale}
244-
{...props}
245-
/>
246-
}
243+
return <SinglePicker {...dateFnsLocale} {...props} />;
244+
};

0 commit comments

Comments
 (0)