2023-10-19 15:03:20 +08:00
|
|
|
import type { PropsWithChildren } from 'react';
|
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { render } from '@testing-library/react';
|
2024-09-12 14:11:43 +08:00
|
|
|
|
|
|
|
import { waitFakeTimer } from '../../../tests/utils';
|
|
|
|
import type { ImageProps, MenuProps } from '../../index';
|
2023-10-23 17:16:41 +08:00
|
|
|
import {
|
2024-09-12 14:11:43 +08:00
|
|
|
App,
|
2023-10-23 17:16:41 +08:00
|
|
|
AutoComplete,
|
|
|
|
Cascader,
|
|
|
|
ColorPicker,
|
|
|
|
DatePicker,
|
|
|
|
Drawer,
|
|
|
|
Dropdown,
|
2024-08-09 22:22:45 +08:00
|
|
|
FloatButton,
|
2023-11-21 15:53:39 +08:00
|
|
|
Image,
|
2023-10-23 17:16:41 +08:00
|
|
|
Menu,
|
|
|
|
Modal,
|
|
|
|
Popconfirm,
|
|
|
|
Popover,
|
|
|
|
Select,
|
|
|
|
Tooltip,
|
|
|
|
Tour,
|
|
|
|
TreeSelect,
|
2024-09-12 14:11:43 +08:00
|
|
|
} from '../../index';
|
2023-10-19 15:03:20 +08:00
|
|
|
import type { ZIndexConsumer, ZIndexContainer } from '../hooks/useZIndex';
|
|
|
|
import { consumerBaseZIndexOffset, containerBaseZIndexOffset, useZIndex } from '../hooks/useZIndex';
|
2024-09-12 14:11:43 +08:00
|
|
|
import { resetWarned } from '../warning';
|
2023-10-23 17:16:41 +08:00
|
|
|
import zIndexContext from '../zindexContext';
|
2023-10-19 15:03:20 +08:00
|
|
|
|
2024-08-09 22:22:45 +08:00
|
|
|
const WrapWithProvider: React.FC<PropsWithChildren<{ container: ZIndexContainer }>> = ({
|
2023-10-19 15:03:20 +08:00
|
|
|
children,
|
2024-08-09 22:22:45 +08:00
|
|
|
container,
|
2023-10-19 15:03:20 +08:00
|
|
|
}) => {
|
2024-08-09 22:22:45 +08:00
|
|
|
const [, contextZIndex] = useZIndex(container);
|
2023-10-19 15:03:20 +08:00
|
|
|
return <zIndexContext.Provider value={contextZIndex}>{children}</zIndexContext.Provider>;
|
|
|
|
};
|
|
|
|
|
2024-08-09 22:22:45 +08:00
|
|
|
const containerComponent: Partial<
|
|
|
|
Record<ZIndexContainer, React.FC<Readonly<PropsWithChildren<{ rootClassName?: string }>>>>
|
2023-10-23 17:16:41 +08:00
|
|
|
> = {
|
|
|
|
Modal: ({ children, ...restProps }) => (
|
|
|
|
<Modal {...restProps} open>
|
|
|
|
{children}
|
|
|
|
</Modal>
|
|
|
|
),
|
|
|
|
Drawer: ({ children, ...restProps }) => (
|
|
|
|
<Drawer {...restProps} open>
|
|
|
|
{children}
|
|
|
|
</Drawer>
|
|
|
|
),
|
|
|
|
Popover: ({ children, ...restProps }) => (
|
|
|
|
<Popover {...restProps} open content="test">
|
|
|
|
{children}
|
|
|
|
</Popover>
|
|
|
|
),
|
|
|
|
Popconfirm: ({ children, ...restProps }) => (
|
|
|
|
<Popconfirm {...restProps} open title="test">
|
|
|
|
{children}
|
|
|
|
</Popconfirm>
|
|
|
|
),
|
|
|
|
Tooltip: ({ children, ...restProps }) => (
|
|
|
|
<Tooltip {...restProps} open title="test">
|
|
|
|
{children}
|
|
|
|
</Tooltip>
|
|
|
|
),
|
|
|
|
Tour: ({ children, ...restProps }) => (
|
2024-08-09 22:22:45 +08:00
|
|
|
<Tour {...restProps} open steps={[{ title: 'cover title', description: children }]} />
|
2023-10-23 17:16:41 +08:00
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = [
|
|
|
|
{
|
|
|
|
label: 'Option 1',
|
|
|
|
value: '1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Option 2',
|
|
|
|
value: '2',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const items: MenuProps['items'] = [
|
|
|
|
{
|
|
|
|
label: 'Test',
|
|
|
|
key: 'SubMenu',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
type: 'group',
|
|
|
|
label: 'Item 1',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
label: 'Option 1',
|
|
|
|
key: 'setting:1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Option 2',
|
|
|
|
key: 'setting:2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'group',
|
|
|
|
label: 'Item 2',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
label: 'Option 3',
|
|
|
|
key: 'setting:3',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Option 4',
|
|
|
|
key: 'setting:4',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2024-08-09 22:22:45 +08:00
|
|
|
const consumerComponent: Partial<
|
|
|
|
Record<ZIndexConsumer, React.FC<Readonly<{ rootClassName: string }>>>
|
|
|
|
> = {
|
2023-10-23 17:16:41 +08:00
|
|
|
SelectLike: ({ rootClassName, ...props }) => (
|
|
|
|
<>
|
|
|
|
<Select
|
|
|
|
{...props}
|
|
|
|
rootClassName={`${rootClassName} comp-item comp-Select`}
|
|
|
|
options={options}
|
|
|
|
open
|
|
|
|
/>
|
|
|
|
<Cascader
|
|
|
|
{...props}
|
|
|
|
rootClassName={`${rootClassName} comp-item comp-Cascader`}
|
|
|
|
options={options}
|
|
|
|
open
|
|
|
|
/>
|
|
|
|
<TreeSelect
|
|
|
|
{...props}
|
|
|
|
rootClassName={`${rootClassName} comp-item comp-TreeSelect`}
|
|
|
|
treeData={options}
|
|
|
|
open
|
|
|
|
/>
|
|
|
|
<AutoComplete
|
|
|
|
{...props}
|
|
|
|
rootClassName={`${rootClassName} comp-item comp-AutoComplete`}
|
|
|
|
options={options}
|
|
|
|
open
|
|
|
|
/>
|
2023-11-14 18:01:23 +08:00
|
|
|
<ColorPicker {...props} open rootClassName={`${rootClassName} comp-item comp-ColorPicker`} />
|
2023-10-23 17:16:41 +08:00
|
|
|
</>
|
|
|
|
),
|
|
|
|
Dropdown: (props) => (
|
|
|
|
<Dropdown
|
|
|
|
{...props}
|
|
|
|
menu={{
|
|
|
|
items: options.map((item) => ({
|
|
|
|
key: item.value,
|
|
|
|
label: item.label,
|
|
|
|
})),
|
|
|
|
}}
|
|
|
|
open
|
|
|
|
>
|
|
|
|
<button type="button">test</button>
|
|
|
|
</Dropdown>
|
|
|
|
),
|
|
|
|
DatePicker: ({ rootClassName, ...props }) => (
|
|
|
|
<>
|
|
|
|
<DatePicker {...props} rootClassName={`${rootClassName} comp-item comp-DatePicker`} open />
|
|
|
|
<DatePicker.TimePicker
|
|
|
|
{...props}
|
|
|
|
rootClassName={`${rootClassName} comp-item comp-TimePicker`}
|
|
|
|
open
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
Menu: (props) => <Menu {...props} items={items} defaultOpenKeys={['SubMenu']} />,
|
2023-11-21 15:53:39 +08:00
|
|
|
ImagePreview: ({ rootClassName }: ImageProps) => (
|
2023-11-24 16:17:52 +08:00
|
|
|
<>
|
|
|
|
<Image
|
|
|
|
src="xxx"
|
|
|
|
preview={{
|
|
|
|
visible: true,
|
|
|
|
rootClassName: `${rootClassName} comp-item comp-ImagePreview`,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Image.PreviewGroup
|
|
|
|
preview={{
|
|
|
|
visible: true,
|
|
|
|
rootClassName: `${rootClassName} comp-item comp-ImagePreviewGroup`,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Image src="xxx" />
|
|
|
|
</Image.PreviewGroup>
|
|
|
|
</>
|
2023-11-21 15:53:39 +08:00
|
|
|
),
|
2023-10-23 17:16:41 +08:00
|
|
|
};
|
|
|
|
|
2024-08-09 22:22:45 +08:00
|
|
|
const getConsumerSelector = (baseSelector: string, consumer: ZIndexConsumer): string => {
|
2023-10-23 17:16:41 +08:00
|
|
|
let selector = baseSelector;
|
|
|
|
if (consumer === 'SelectLike') {
|
2023-11-14 18:01:23 +08:00
|
|
|
selector = ['Select', 'Cascader', 'TreeSelect', 'AutoComplete', 'ColorPicker']
|
|
|
|
.map((item) =>
|
|
|
|
item === 'ColorPicker'
|
|
|
|
? `${baseSelector}.ant-popover-placement-bottomLeft`
|
|
|
|
: `${baseSelector}.comp-${item}.ant-slide-up`,
|
|
|
|
)
|
2023-10-23 17:16:41 +08:00
|
|
|
.join(',');
|
|
|
|
} else if (consumer === 'DatePicker') {
|
|
|
|
selector = ['DatePicker', 'TimePicker']
|
|
|
|
.map((item) => `${baseSelector}.comp-${item}.ant-picker-dropdown`)
|
|
|
|
.join(',');
|
|
|
|
} else if (['Menu'].includes(consumer)) {
|
|
|
|
selector = `${baseSelector}.ant-menu-submenu-placement-rightTop`;
|
2023-11-21 15:53:39 +08:00
|
|
|
} else if (consumer === 'ImagePreview') {
|
2023-11-24 16:17:52 +08:00
|
|
|
selector = ['ImagePreview', 'ImagePreviewGroup']
|
|
|
|
.map(
|
|
|
|
(item) =>
|
|
|
|
`${baseSelector}.comp-${item} .ant-image-preview-wrap, ${baseSelector}.comp-${item}.ant-image-preview-operations-wrapper`,
|
|
|
|
)
|
|
|
|
.join(',');
|
2023-10-23 17:16:41 +08:00
|
|
|
}
|
|
|
|
return selector;
|
2024-08-09 22:22:45 +08:00
|
|
|
};
|
2023-10-23 17:16:41 +08:00
|
|
|
|
2023-10-19 15:03:20 +08:00
|
|
|
describe('Test useZIndex hooks', () => {
|
2023-10-23 17:16:41 +08:00
|
|
|
beforeEach(() => {
|
2024-09-12 14:11:43 +08:00
|
|
|
resetWarned();
|
2023-10-23 17:16:41 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2024-08-09 22:22:45 +08:00
|
|
|
Object.keys(containerComponent).forEach((containerKey) => {
|
|
|
|
Object.keys(consumerComponent).forEach((key) => {
|
|
|
|
const containerZIndexValue = containerBaseZIndexOffset[containerKey as ZIndexContainer];
|
|
|
|
const consumerZIndexValue = consumerBaseZIndexOffset[key as ZIndexConsumer];
|
2023-10-19 15:03:20 +08:00
|
|
|
describe(`Test ${key} zIndex in ${containerKey}`, () => {
|
2023-10-23 17:16:41 +08:00
|
|
|
it('Test hooks', () => {
|
2023-10-19 15:03:20 +08:00
|
|
|
const fn = jest.fn();
|
2024-08-09 22:22:45 +08:00
|
|
|
const Child: React.FC = () => {
|
2023-10-19 15:03:20 +08:00
|
|
|
const [zIndex] = useZIndex(key as ZIndexConsumer);
|
|
|
|
useEffect(() => {
|
|
|
|
fn(zIndex);
|
|
|
|
}, [zIndex]);
|
|
|
|
return <div>Child</div>;
|
|
|
|
};
|
|
|
|
|
2024-09-14 11:57:40 +08:00
|
|
|
const Demo: React.FC = () => (
|
2024-08-09 22:22:45 +08:00
|
|
|
<WrapWithProvider container={containerKey as ZIndexContainer}>
|
|
|
|
<WrapWithProvider container={containerKey as ZIndexContainer}>
|
|
|
|
<WrapWithProvider container={containerKey as ZIndexContainer}>
|
2023-10-19 15:03:20 +08:00
|
|
|
<Child />
|
|
|
|
</WrapWithProvider>
|
|
|
|
</WrapWithProvider>
|
|
|
|
</WrapWithProvider>
|
|
|
|
);
|
2024-09-12 14:11:43 +08:00
|
|
|
render(<Demo />);
|
2024-09-14 11:57:40 +08:00
|
|
|
|
2023-10-19 15:03:20 +08:00
|
|
|
expect(fn).toHaveBeenLastCalledWith(
|
2024-08-09 22:22:45 +08:00
|
|
|
1000 + containerZIndexValue * 3 + consumerZIndexValue,
|
2023-10-19 15:03:20 +08:00
|
|
|
);
|
|
|
|
});
|
2023-10-24 21:54:36 +08:00
|
|
|
|
2023-10-23 19:46:08 +08:00
|
|
|
it('Test Component', async () => {
|
2024-08-09 22:22:45 +08:00
|
|
|
const Container = containerComponent[containerKey as ZIndexContainer]!;
|
|
|
|
const Consumer = consumerComponent[key as ZIndexConsumer]!;
|
2023-10-23 17:16:41 +08:00
|
|
|
|
2024-09-14 11:57:40 +08:00
|
|
|
const Demo: React.FC = () => (
|
2023-10-23 17:16:41 +08:00
|
|
|
<>
|
|
|
|
<Consumer rootClassName="consumer1" />
|
|
|
|
<Container rootClassName="container1">
|
|
|
|
<Consumer rootClassName="consumer2" />
|
|
|
|
<Container rootClassName="container2">
|
|
|
|
<Consumer rootClassName="consumer3" />
|
|
|
|
</Container>
|
|
|
|
</Container>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
2024-09-12 14:11:43 +08:00
|
|
|
const { unmount } = render(<Demo />);
|
2023-10-23 17:16:41 +08:00
|
|
|
|
|
|
|
await waitFakeTimer(1000);
|
|
|
|
|
2023-10-24 22:25:32 +08:00
|
|
|
const selector1 = getConsumerSelector('.consumer1', key as ZIndexConsumer);
|
2023-10-23 17:16:41 +08:00
|
|
|
const selector2 = getConsumerSelector('.consumer2', key as ZIndexConsumer);
|
|
|
|
const selector3 = getConsumerSelector('.consumer3', key as ZIndexConsumer);
|
|
|
|
|
2023-11-24 16:17:52 +08:00
|
|
|
if (['SelectLike', 'DatePicker', 'ImagePreview'].includes(key)) {
|
2024-08-09 22:22:45 +08:00
|
|
|
let comps = document.querySelectorAll<HTMLElement>(selector1);
|
2023-10-24 22:25:32 +08:00
|
|
|
comps.forEach((comp) => {
|
2024-08-09 22:22:45 +08:00
|
|
|
expect(comp?.style.zIndex).toBeFalsy();
|
2023-10-24 22:25:32 +08:00
|
|
|
});
|
2024-08-09 22:22:45 +08:00
|
|
|
comps = document.querySelectorAll<HTMLElement>(selector2);
|
2023-10-23 17:16:41 +08:00
|
|
|
comps.forEach((comp) => {
|
2024-08-09 22:22:45 +08:00
|
|
|
const isColorPicker = comp?.className.includes('comp-ColorPicker');
|
2023-11-14 18:01:23 +08:00
|
|
|
const consumerOffset = isColorPicker
|
2023-11-17 10:54:19 +08:00
|
|
|
? containerBaseZIndexOffset.Popover
|
2024-08-09 22:22:45 +08:00
|
|
|
: consumerZIndexValue;
|
2023-11-24 16:17:52 +08:00
|
|
|
const operOffset = comp.classList.contains('ant-image-preview-operations-wrapper')
|
|
|
|
? 1
|
|
|
|
: 0;
|
2024-08-09 22:22:45 +08:00
|
|
|
expect(comp?.style.zIndex).toBe(
|
|
|
|
String(1000 + containerZIndexValue + consumerOffset + operOffset),
|
2023-10-23 17:16:41 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-08-09 22:22:45 +08:00
|
|
|
comps = document.querySelectorAll<HTMLElement>(selector3);
|
2023-10-23 17:16:41 +08:00
|
|
|
comps.forEach((comp) => {
|
2024-08-09 22:22:45 +08:00
|
|
|
const isColorPicker = comp?.className.includes('comp-ColorPicker');
|
2023-11-14 18:01:23 +08:00
|
|
|
const consumerOffset = isColorPicker
|
2023-11-17 10:54:19 +08:00
|
|
|
? containerBaseZIndexOffset.Popover
|
2024-08-09 22:22:45 +08:00
|
|
|
: consumerZIndexValue;
|
2023-11-24 16:17:52 +08:00
|
|
|
const operOffset = comp.classList.contains('ant-image-preview-operations-wrapper')
|
|
|
|
? 1
|
|
|
|
: 0;
|
2024-08-09 22:22:45 +08:00
|
|
|
expect(comp?.style.zIndex).toBe(
|
|
|
|
String(1000 + containerZIndexValue * 2 + consumerOffset + operOffset),
|
2023-10-23 17:16:41 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
} else {
|
2024-08-09 22:22:45 +08:00
|
|
|
const element1 = document.querySelector<HTMLElement>(selector1);
|
|
|
|
const element2 = document.querySelector<HTMLElement>(selector2);
|
|
|
|
const element3 = document.querySelector<HTMLElement>(selector3);
|
|
|
|
expect(element1?.style.zIndex).toBe(key === 'Tour' ? '1001' : '');
|
|
|
|
expect(element2?.style.zIndex).toBe(
|
|
|
|
String(1000 + containerZIndexValue + consumerZIndexValue),
|
2023-10-23 17:16:41 +08:00
|
|
|
);
|
2024-08-09 22:22:45 +08:00
|
|
|
expect(element3?.style.zIndex).toBe(
|
|
|
|
String(1000 + containerZIndexValue * 2 + consumerZIndexValue),
|
2023-10-23 17:16:41 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
unmount();
|
2023-11-24 21:22:16 +08:00
|
|
|
}, 20000);
|
2023-10-19 15:03:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-11-17 10:54:19 +08:00
|
|
|
|
|
|
|
it('Modal static func should always use max zIndex', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
const instance = Modal.confirm({
|
|
|
|
title: 'bamboo',
|
2023-11-22 18:02:32 +08:00
|
|
|
content: <Select open />,
|
2023-11-17 10:54:19 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
expect(document.querySelector('.ant-modal-wrap')).toHaveStyle({
|
|
|
|
zIndex: '2000',
|
|
|
|
});
|
|
|
|
|
2023-11-22 18:02:32 +08:00
|
|
|
expect(document.querySelector('.ant-select-dropdown')).toHaveStyle({
|
|
|
|
zIndex: '2050',
|
|
|
|
});
|
|
|
|
|
2023-11-17 10:54:19 +08:00
|
|
|
instance.destroy();
|
|
|
|
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
// Clean up for static method
|
|
|
|
document.body.innerHTML = '';
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2024-07-04 15:44:32 +08:00
|
|
|
|
|
|
|
it('warning for too large zIndex auto offset', () => {
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
render(
|
|
|
|
<Drawer open zIndex={999999999}>
|
|
|
|
<Tooltip open title="test">
|
|
|
|
<div>test</div>
|
|
|
|
</Tooltip>
|
|
|
|
</Drawer>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Tooltip] `zIndex` is over design token `zIndexPopupBase` too much. It may cause unexpected override.',
|
|
|
|
);
|
2024-08-09 22:22:45 +08:00
|
|
|
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('FloatButton support zIndex', () => {
|
|
|
|
const { container, rerender } = render(
|
|
|
|
<WrapWithProvider container="FloatButton">
|
|
|
|
<FloatButton />
|
|
|
|
</WrapWithProvider>,
|
|
|
|
);
|
|
|
|
expect(container.querySelector<HTMLElement>('.ant-float-btn')?.style.zIndex).toBe(
|
|
|
|
// parentZIndex + containerBaseZIndexOffset["FloatButton"]
|
|
|
|
String(1100 + containerBaseZIndexOffset.FloatButton),
|
|
|
|
);
|
|
|
|
rerender(
|
|
|
|
<WrapWithProvider container="FloatButton">
|
|
|
|
<FloatButton style={{ zIndex: 666 }} />
|
|
|
|
</WrapWithProvider>,
|
|
|
|
);
|
|
|
|
expect(container.querySelector<HTMLElement>('.ant-float-btn')?.style.zIndex).toBe(String(666));
|
2024-09-14 11:57:40 +08:00
|
|
|
});
|
|
|
|
|
2024-09-12 14:11:43 +08:00
|
|
|
it('not warning for static func', () => {
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
const Demo = () => {
|
|
|
|
const { modal } = App.useApp();
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
modal.confirm({ content: <Select open /> });
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
render(
|
|
|
|
<App>
|
|
|
|
<Demo />
|
|
|
|
</App>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
errorSpy.mockRestore();
|
2024-07-04 15:44:32 +08:00
|
|
|
});
|
2023-10-19 15:03:20 +08:00
|
|
|
});
|