From 9a736facf8753f2f0b7ace191af887f4d011ec65 Mon Sep 17 00:00:00 2001 From: HaixingOoO <974758671@qq.com> Date: Sat, 30 Mar 2024 22:28:07 +0800 Subject: [PATCH 1/3] fix(layout): fix layout shake fix #2088 --- src/layout/Layout.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/layout/Layout.tsx b/src/layout/Layout.tsx index fc3d39e5b7..bc729b09a9 100644 --- a/src/layout/Layout.tsx +++ b/src/layout/Layout.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useMemo } from 'react'; import classNames from 'classnames'; import useConfig from '../hooks/useConfig'; import { StyledProps } from '../common'; @@ -63,23 +63,25 @@ const Layout: React.FC & { Aside: typeof Aside; } = (props) => { const { direction, className, style, children, ...otherLayoutProps } = props; - const [asides, setAsides] = useState([]); - useEffect(() => { - React.Children.forEach(children, (child: React.ReactChild) => { + + const asides = useMemo(() => { + const childs: React.ReactElement[] = []; + React.Children.forEach(children, (child: React.ReactElement) => { if (!child || typeof child !== 'object') { return; } if (child.type === Aside) { - setAsides([child]); + childs.push(child); } }); + return !!childs.length; }, [children]); const { classPrefix } = useConfig(); const layoutClassNames = classNames( `${classPrefix}-layout`, { - [`${classPrefix}-layout--with-sider`]: !!asides.length, + [`${classPrefix}-layout--with-sider`]: asides, [`${classPrefix}-layout__direction-${direction}`]: direction, }, className, From e732e3261c10d64f3d0115289e4de3b772809125 Mon Sep 17 00:00:00 2001 From: HaixingOoO <974758671@qq.com> Date: Sat, 30 Mar 2024 22:36:17 +0800 Subject: [PATCH 2/3] test(layout): fix test error --- src/layout/Layout.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/layout/Layout.tsx b/src/layout/Layout.tsx index bc729b09a9..a7ed36d8da 100644 --- a/src/layout/Layout.tsx +++ b/src/layout/Layout.tsx @@ -64,24 +64,24 @@ const Layout: React.FC & { } = (props) => { const { direction, className, style, children, ...otherLayoutProps } = props; - const asides = useMemo(() => { - const childs: React.ReactElement[] = []; + const shouldAsides = useMemo(() => { + const asides: React.ReactElement[] = []; React.Children.forEach(children, (child: React.ReactElement) => { if (!child || typeof child !== 'object') { return; } if (child.type === Aside) { - childs.push(child); + asides.push(child); } }); - return !!childs.length; + return !!asides.length; }, [children]); const { classPrefix } = useConfig(); const layoutClassNames = classNames( `${classPrefix}-layout`, { - [`${classPrefix}-layout--with-sider`]: asides, + [`${classPrefix}-layout--with-sider`]: shouldAsides, [`${classPrefix}-layout__direction-${direction}`]: direction, }, className, From 060a3f7efab15f87fabfd0a46ec284b4a8f751e4 Mon Sep 17 00:00:00 2001 From: HaixingOoO <974758671@qq.com> Date: Sat, 30 Mar 2024 22:41:30 +0800 Subject: [PATCH 3/3] test(layout): update test snap --- test/snap/__snapshots__/csr.test.jsx.snap | 6 +++--- test/snap/__snapshots__/ssr.test.jsx.snap | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/snap/__snapshots__/csr.test.jsx.snap b/test/snap/__snapshots__/csr.test.jsx.snap index 6d21b14474..3f85c1c610 100644 --- a/test/snap/__snapshots__/csr.test.jsx.snap +++ b/test/snap/__snapshots__/csr.test.jsx.snap @@ -303809,11 +303809,11 @@ exports[`ssr snapshot test > ssr test src/input-number/_example/status.jsx 1`] = exports[`ssr snapshot test > ssr test src/input-number/_example/step.jsx 1`] = `"
"`; -exports[`ssr snapshot test > ssr test src/layout/_example/aside.jsx 1`] = `"

侧边导航布局

Content
Copyright @ 2019-2020 Tencent. All Rights Reserved
"`; +exports[`ssr snapshot test > ssr test src/layout/_example/aside.jsx 1`] = `"

侧边导航布局

Content
Copyright @ 2019-2020 Tencent. All Rights Reserved
"`; -exports[`ssr snapshot test > ssr test src/layout/_example/base.jsx 1`] = `"

顶部导航布局

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

侧边导航布局

Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

组合导航布局

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved
"`; +exports[`ssr snapshot test > ssr test src/layout/_example/base.jsx 1`] = `"

顶部导航布局

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

侧边导航布局

Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

组合导航布局

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved
"`; -exports[`ssr snapshot test > ssr test src/layout/_example/combine.jsx 1`] = `"
\\"logo\\"/
  • 已选内容
  • 菜单内容一
  • 菜单内容二
  • 菜单内容三
Content
Copyright @ 2019-2020 Tencent. All Rights Reserved
"`; +exports[`ssr snapshot test > ssr test src/layout/_example/combine.jsx 1`] = `"
\\"logo\\"/
  • 已选内容
  • 菜单内容一
  • 菜单内容二
  • 菜单内容三
Content
Copyright @ 2019-2020 Tencent. All Rights Reserved
"`; exports[`ssr snapshot test > ssr test src/layout/_example/top.jsx 1`] = `"
\\"logo\\"/
  • 已选内容
  • 菜单内容一
  • 菜单内容二
  • 菜单内容三
Content
Copyright @ 2019-2020 Tencent. All Rights Reserved
"`; diff --git a/test/snap/__snapshots__/ssr.test.jsx.snap b/test/snap/__snapshots__/ssr.test.jsx.snap index 5f22005ef4..142f68a344 100644 --- a/test/snap/__snapshots__/ssr.test.jsx.snap +++ b/test/snap/__snapshots__/ssr.test.jsx.snap @@ -546,11 +546,11 @@ exports[`ssr snapshot test > ssr test src/input-number/_example/status.jsx 1`] = exports[`ssr snapshot test > ssr test src/input-number/_example/step.jsx 1`] = `"
"`; -exports[`ssr snapshot test > ssr test src/layout/_example/aside.jsx 1`] = `"

侧边导航布局

Content
Copyright @ 2019-2020 Tencent. All Rights Reserved
"`; +exports[`ssr snapshot test > ssr test src/layout/_example/aside.jsx 1`] = `"

侧边导航布局

Content
Copyright @ 2019-2020 Tencent. All Rights Reserved
"`; -exports[`ssr snapshot test > ssr test src/layout/_example/base.jsx 1`] = `"

顶部导航布局

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

侧边导航布局

Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

组合导航布局

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved
"`; +exports[`ssr snapshot test > ssr test src/layout/_example/base.jsx 1`] = `"

顶部导航布局

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

侧边导航布局

Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

组合导航布局

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved

Header
Content
Copyright @ 2019-2021 Tencent. All Rights Reserved
"`; -exports[`ssr snapshot test > ssr test src/layout/_example/combine.jsx 1`] = `"
\\"logo\\"/
  • 已选内容
  • 菜单内容一
  • 菜单内容二
  • 菜单内容三
Content
Copyright @ 2019-2020 Tencent. All Rights Reserved
"`; +exports[`ssr snapshot test > ssr test src/layout/_example/combine.jsx 1`] = `"
\\"logo\\"/
  • 已选内容
  • 菜单内容一
  • 菜单内容二
  • 菜单内容三
Content
Copyright @ 2019-2020 Tencent. All Rights Reserved
"`; exports[`ssr snapshot test > ssr test src/layout/_example/top.jsx 1`] = `"
\\"logo\\"/
  • 已选内容
  • 菜单内容一
  • 菜单内容二
  • 菜单内容三
Content
"`;