Skip to content

Commit

Permalink
fix(layout): Support for multiple PageContainers to be used together
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed Oct 14, 2022
1 parent 224b573 commit 7a9aad7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/layout/src/ProLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,10 @@ const BaseProLayout: React.FC<ProLayoutProps> = (props) => {
}, [location.pathname, location.pathname?.search]);

const [hasFooterToolbar, setHasFooterToolbar] = useState(false);
const [hasPageContainer, setHasPageContainer] = useState(false);
/**
* 使用numberζ˜―ε› δΈΊε€šζ ‡η­Ύι‘΅ηš„ζ—Άε€™ζœ‰ε€šδΈͺ PageContainer,εͺζœ‰ζœ‰δ»»ζ„δΈ€δΈͺε°±εΊ”θ―₯展瀺这δΈͺclassName
*/
const [hasPageContainer, setHasPageContainer] = useState(0);
useDocumentTitle(pageTitleInfo, props.title || false);
const bgImgStyleList = useMemo(() => {
if (bgLayoutImgList && bgLayoutImgList.length > 0) {
Expand Down
6 changes: 3 additions & 3 deletions packages/layout/src/WrapContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { CSSProperties } from 'react';
import React from 'react';

const WrapContent: React.FC<{
hasPageContainer?: boolean;
hasPageContainer?: number;
isChildrenLayout?: boolean;
prefixCls?: string;
style?: CSSProperties;
Expand All @@ -16,11 +16,11 @@ const WrapContent: React.FC<{
hasHeader: boolean;
}> = (props) => {
const { hashId } = useToken();
const { style, prefixCls, children, hasPageContainer } = props;
const { style, prefixCls, children, hasPageContainer = 0 } = props;

const contentClassName = classNames(`${prefixCls}-content`, hashId, {
[`${prefixCls}-has-header`]: props.hasHeader,
[`${prefixCls}-content-has-page-container`]: hasPageContainer,
[`${prefixCls}-content-has-page-container`]: hasPageContainer > 0,
});

const ErrorComponent = props.ErrorBoundary || ErrorBoundary;
Expand Down
4 changes: 2 additions & 2 deletions packages/layout/src/components/PageContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ const PageContainer: React.FC<PageContainerProps> = (props) => {
if (!value || !value?.setHasPageContainer) {
return () => {};
}
value?.setHasPageContainer?.(true);
value?.setHasPageContainer?.((num) => num + 1);
return () => {
value?.setHasPageContainer?.(false);
value?.setHasPageContainer?.((num) => num - 1);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down
4 changes: 2 additions & 2 deletions packages/layout/src/context/RouteContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export type RouteContextType = {
isChildrenLayout?: boolean;
hasFooterToolbar?: boolean;
hasFooter?: boolean;
hasPageContainer?: boolean;
hasPageContainer?: number;
setHasFooterToolbar?: React.Dispatch<React.SetStateAction<boolean>>;
setHasPageContainer?: React.Dispatch<React.SetStateAction<boolean>>;
setHasPageContainer?: React.Dispatch<React.SetStateAction<number>>;
pageTitleInfo?: {
title: string;
id: string;
Expand Down
65 changes: 65 additions & 0 deletions tests/layout/pageContainer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ProLayoutProps } from '@ant-design/pro-components';
import { FooterToolbar, PageContainer, ProLayout } from '@ant-design/pro-components';
import { render as libraryRender } from '@testing-library/react';
import { Button } from 'antd';
import { mount, render } from 'enzyme';
import React, { useEffect, useMemo, useState } from 'react';
import { act } from 'react-dom/test-utils';
Expand All @@ -26,6 +27,70 @@ describe('PageContainer', () => {
expect(html.find('.ant-page-header').exists()).toBeFalsy();
});

it('πŸ’„ has PageContainer className', async () => {
const Demo = () => {
const [state, setState] = useState(0);
return (
<ProLayout>
<Button
onClick={() => {
setState((num) => num + 1);
}}
>
εˆ‡ζ’
</Button>
{state > 0 && state < 3 ? (
<PageContainer title={false} ghost={false} header={undefined} breadcrumbRender={false}>
qixian
</PageContainer>
) : null}
{state > 1 && state < 4 ? (
<PageContainer title={false} ghost={false} header={undefined} breadcrumbRender={false}>
qixian2
</PageContainer>
) : null}
</ProLayout>
);
};
const html = libraryRender(<Demo />);

expect(
!!html.baseElement.querySelector('.ant-pro-layout-content-has-page-container'),
).toBeFalsy();

await act(async () => {
(await html.findByText('εˆ‡ 捒'))?.click?.();
});

expect(
!!html.baseElement.querySelector('.ant-pro-layout-content-has-page-container'),
).toBeTruthy();

await act(async () => {
(await html.findByText('εˆ‡ 捒'))?.click?.();
});

expect(
!!html.baseElement.querySelector('.ant-pro-layout-content-has-page-container'),
).toBeTruthy();

await act(async () => {
(await html.findByText('εˆ‡ 捒'))?.click?.();
});

expect(
!!html.baseElement.querySelector('.ant-pro-layout-content-has-page-container'),
).toBeTruthy();

await act(async () => {
(await html.findByText('εˆ‡ 捒'))?.click?.();
});

expect(
!!html.baseElement.querySelector('ant-pro-layout-content-has-page-container'),
).toBeFalsy();
});

it('πŸ’„ pageContainer support breadcrumbRender', async () => {
const html = mount(
<PageContainer breadcrumbRender={() => <div>θΏ™ι‡Œζ˜―ι’εŒ…ε±‘</div>}>content</PageContainer>,
Expand Down

0 comments on commit 7a9aad7

Please sign in to comment.