Skip to content

Commit

Permalink
fix: cannot select end time in shadow dom (#860)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyangzhaoa authored Aug 13, 2024
1 parent 2fb359e commit 7d715d6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,12 @@ function RangePicker<DateType extends object = any>(

// ======================== Click =========================
const onSelectorClick: React.MouseEventHandler<HTMLDivElement> = (event) => {
if (!selectorRef.current.nativeElement.contains(document.activeElement)) {
const rootNode = (event.target as HTMLElement).getRootNode();
if (
!selectorRef.current.nativeElement.contains(
(rootNode as Document | ShadowRoot).activeElement ?? document.activeElement,
)
) {
// Click to focus the enabled input
const enabledIndex = disabled.findIndex((d) => !d);
if (enabledIndex >= 0) {
Expand Down
29 changes: 29 additions & 0 deletions tests/range.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Note: zombieJ refactoring

import { act, createEvent, fireEvent, render } from '@testing-library/react';
import { createRoot } from 'react-dom/client';
import type { Dayjs } from 'dayjs';
import dayjs from 'dayjs';
import KeyCode from 'rc-util/lib/KeyCode';
Expand Down Expand Up @@ -1973,4 +1974,32 @@ describe('Picker.Range', () => {
]);
expect(isOpen()).toBeFalsy();
});

const renderShadow = (props?: any) => {
const host = document.createElement('div');
document.body.appendChild(host);

const shadowRoot = host.attachShadow({
mode: 'open',
delegatesFocus: false,
});
const container = document.createElement('div');
shadowRoot.appendChild(container);

act(() => {
createRoot(container).render(<DayRangePicker {...props} />);
});

return shadowRoot;
};

it('the end date selector can be selected in shadow dom', () => {
const shadowRoot = renderShadow();

openPicker(shadowRoot, 1);

expect(shadowRoot.querySelectorAll('.rc-picker-input')[1]).toHaveClass(
'rc-picker-input-active',
);
});
});
14 changes: 6 additions & 8 deletions tests/util/commonUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function waitFakeTimer() {
});
}

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

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

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

Expand Down Expand Up @@ -236,11 +236,9 @@ const dateFnsLocale = {
generateConfig: dateFnsGenerateConfig,
};

type DateFnsSinglePickerProps = Omit<PickerProps<Date>, 'locale' | 'generateConfig'> & React.RefAttributes<PickerRef>;
type DateFnsSinglePickerProps = Omit<PickerProps<Date>, 'locale' | 'generateConfig'> &
React.RefAttributes<PickerRef>;

export const DateFnsSinglePicker = (props: DateFnsSinglePickerProps) => {
return <SinglePicker
{...dateFnsLocale}
{...props}
/>
}
return <SinglePicker {...dateFnsLocale} {...props} />;
};

0 comments on commit 7d715d6

Please sign in to comment.