Преглед на файлове

fix: [layout] Fix layout mount sider class delay (#1384)

* fix: fix layout delay mout .has-sider class

* fix: fix type define

* chore: format

* chore: change var name
代强 преди 2 години
родител
ревизия
d52979c0c4
променени са 3 файла, в които са добавени 6 реда и са изтрити 3 реда
  1. 2 2
      packages/semi-foundation/base/foundation.ts
  2. 1 0
      packages/semi-ui/layout/Sider.tsx
  3. 3 1
      packages/semi-ui/layout/index.tsx

+ 2 - 2
packages/semi-foundation/base/foundation.ts

@@ -16,7 +16,7 @@ export interface DefaultAdapter<P = Record<string, any>, S = Record<string, any>
     getProps(): P;
     getState(key: string): any;
     getStates(): S;
-    setState(s: Partial<S>, callback?: any): void;
+    setState<K extends keyof S>(s: Pick<S, K>, callback?: any): void;
     getCache(c: string): any;
     getCaches(): any;
     setCache(key: any, value: any): void;
@@ -88,7 +88,7 @@ class BaseFoundation<T extends Partial<DefaultAdapter<P, S>>, P = Record<string,
         return this._adapter.getStates();
     }
 
-    setState(states: Partial<S>, cb?: (...args: any) => void) {
+    setState<K extends keyof S>(states: Pick<S, K>, cb?: (...args: any) => void) {
         return this._adapter.setState({ ...states }, cb);
     }
 

+ 1 - 0
packages/semi-ui/layout/Sider.tsx

@@ -61,6 +61,7 @@ class Sider extends React.PureComponent<SiderProps> {
     };
 
     static contextType = LayoutContext;
+    static elementType = "Layout.Sider"
 
     unRegisters: Array<() => void> = [];
     context: ContextType;

+ 3 - 1
packages/semi-ui/layout/index.tsx

@@ -111,7 +111,9 @@ class Layout extends React.Component<BasicLayoutProps, BasicLayoutState> {
         const { prefixCls, className, children, hasSider, tagName, ...others } = this.props;
         const { siders } = this.state;
         const classString = cls(className, prefixCls, {
-            [`${prefixCls}-has-sider`]: typeof hasSider === 'boolean' ? hasSider : siders.length > 0,
+            [`${prefixCls}-has-sider`]: typeof hasSider === 'boolean' && hasSider || siders.length > 0 || React.Children.toArray(children).some((child: React.ReactNode) => {
+                return React.isValidElement(child) && child.type && (child.type as any).elementType === "Layout.Sider";
+            }),
         });
         const Tag: any = tagName;
         return (