Skip to content

Commit

Permalink
fix(layout): fix layout shake (#2824)
Browse files Browse the repository at this point in the history
* fix(layout): fix layout shake

fix #2088

* test(layout): fix test error

* test(layout): update test snap
  • Loading branch information
HaixingOoO authored Apr 1, 2024
1 parent f3604ac commit 6a629e3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/layout/Layout.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -63,23 +63,25 @@ const Layout: React.FC<LayoutProps> & {
Aside: typeof Aside;
} = (props) => {
const { direction, className, style, children, ...otherLayoutProps } = props;
const [asides, setAsides] = useState([]);
useEffect(() => {
React.Children.forEach(children, (child: React.ReactChild) => {

const shouldAsides = useMemo(() => {
const asides: React.ReactElement[] = [];
React.Children.forEach(children, (child: React.ReactElement) => {
if (!child || typeof child !== 'object') {
return;
}
if (child.type === Aside) {
setAsides([child]);
asides.push(child);
}
});
return !!asides.length;
}, [children]);

const { classPrefix } = useConfig();
const layoutClassNames = classNames(
`${classPrefix}-layout`,
{
[`${classPrefix}-layout--with-sider`]: !!asides.length,
[`${classPrefix}-layout--with-sider`]: shouldAsides,
[`${classPrefix}-layout__direction-${direction}`]: direction,
},
className,
Expand Down
Loading

0 comments on commit 6a629e3

Please sign in to comment.