All files Layout.tsx

87.5% Statements 14/16
100% Branches 6/6
75% Functions 6/8
84.61% Lines 11/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                      1x                         1x 14x 14x 14x 3x   14x 3x   14x               14x                             1x      
import React, { useState } from 'react';
import { LayoutHeader } from './Header';
import { LayoutSider } from './Sider';
import { LayoutFooter } from './Footer';
import { LayoutContent } from './Content';
 
export interface LayoutContextProps {
  addSider: (id: string) => void;
  removeSider: (id: string) => void;
}
 
export const LayoutContext = React.createContext<LayoutContextProps>({
  addSider: () => null,
  removeSider: () => null,
});
 
export interface LayoutProps extends React.HTMLAttributes<HTMLElement> {
  prefixCls?: string;
  hasSider?: boolean;
}
export interface LayoutState {
  siders: string[];
}
 
const Layout = React.forwardRef<HTMLElement, LayoutProps>((props, ref) => {
  const { prefixCls = 'w-layout', className, hasSider, children, ...other } = props;
  const [siders, setSiders] = useState<string[]>([]);
  const addSider = (id: string) => {
    setSiders((state) => [...state, id]);
  };
  const removeSider = (id: string) => {
    setSiders((state) => [...state.filter((currentId) => currentId !== id)]);
  };
  const cls = [
    prefixCls,
    className,
    (typeof hasSider === 'boolean' && hasSider) || siders.length > 0 ? `${prefixCls}-has-sider` : null,
  ]
    .filter(Boolean)
    .join(' ')
    .trim();
  return (
    <LayoutContext.Provider value={{ addSider, removeSider }}>
      <section ref={ref} className={cls} {...other}>
        {children}
      </section>
    </LayoutContext.Provider>
  );
});
 
type LoginComponent = typeof Layout & {
  Header: typeof LayoutHeader;
  Footer: typeof LayoutFooter;
  Sider: typeof LayoutSider;
  Content: typeof LayoutContent;
};
Layout.displayName = 'Layout';
 
export default Layout as LoginComponent;