Skip to content

Commit

Permalink
feat(core): #85 supports view components of the react lazy type
Browse files Browse the repository at this point in the history
  • Loading branch information
BroKun committed Dec 10, 2024
1 parent e153f5f commit 5e9b194
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 25 deletions.
18 changes: 18 additions & 0 deletions apps/docs/src/application-react/antd-menu/antd-menu-component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { MAIN_MENU_BAR } from '@difizen/mana-app';
import * as React from 'react';
import { forwardRef } from 'react';

import { MenuRender } from '../workbench/menu/render.js';

export const ManaMenubarComponent = forwardRef(function ManaMenubarComponent(
props,
ref: React.ForwardedRef<HTMLDivElement>,
) {
return (
<div ref={ref}>
<MenuRender path={MAIN_MENU_BAR} />
</div>
);
});

export default ManaMenubarComponent;
17 changes: 2 additions & 15 deletions apps/docs/src/application-react/antd-menu/antd-menu-view.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
import { MacCommandOutlined } from '@ant-design/icons';
import { MAIN_MENU_BAR } from '@difizen/mana-app';
import { BaseView, view } from '@difizen/mana-app';
import { singleton } from '@difizen/mana-app';
import { prop } from '@difizen/mana-app';
import * as React from 'react';
import { forwardRef } from 'react';
import { lazy } from 'react';

import { MenuRender } from '../workbench/menu/render.js';

export const ManaMenubarComponent = forwardRef(function ManaMenubarComponent(
props,
ref: React.ForwardedRef<HTMLDivElement>,
) {
return (
<div ref={ref}>
<MenuRender path={MAIN_MENU_BAR} />
</div>
);
});
export const ManaMenubarComponent = lazy(() => import('./antd-menu-component.js'));

@singleton()
@view({ id: 'AntdMenuView', component: ManaMenubarComponent })
Expand Down
5 changes: 5 additions & 0 deletions packages/mana-core/src/view/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export const isForwardRefComponent = (
component.render !== undefined
);
};

// 判断是否是懒加载组件的函数
export const isLazyComponent = (component: any) => {
return component && component.$$typeof === Symbol.for('react.lazy');
};

Check warning on line 21 in packages/mana-core/src/view/utils.tsx

View check run for this annotation

Codecov / codecov/patch

packages/mana-core/src/view/utils.tsx#L20-L21

Added lines #L20 - L21 were not covered by tests
/**
* hack
* @param component react component
Expand Down
57 changes: 47 additions & 10 deletions packages/mana-core/src/view/view-container.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ObservableContext, useInject } from '@difizen/mana-observable';
import type { Syringe } from '@difizen/mana-syringe';
import * as React from 'react';
import { Suspense } from 'react';

import { useMount, useUnmount } from '../utils/hooks';

import { useViewSize } from './hooks';
import { isForwardRefComponent } from './utils';
import { isForwardRefComponent, isLazyComponent } from './utils';
import type { View } from './view-protocol';
import type { ViewComponent } from './view-protocol';
import { OriginViewComponent } from './view-protocol';
Expand Down Expand Up @@ -53,8 +54,50 @@ export const ViewContainer = React.forwardRef<HTMLDivElement, ViewContainerProps
);
},
);

export const LazyWrapper = (
ViewComponent:
| React.FC
| React.ForwardRefExoticComponent<any>
| React.LazyExoticComponent<React.FC | React.ForwardRefExoticComponent<any>>,
) => {
const LazyWrapperRender: WrapperViewComponent = ({
children,
...props
}: {
children: React.ReactNode;
}) => {
const containerRef = React.useRef<HTMLDivElement>(null);
if (isLazyComponent(ViewComponent)) {
return (
<Suspense fallback={<></>}>
<ViewContainer
ref={containerRef}
component={ViewComponent}
viewComponentProps={props}
>
{children}
</ViewContainer>
</Suspense>
);
}
return (
<ViewContainer
ref={containerRef}
component={ViewComponent}
viewComponentProps={props}
>
{children}
</ViewContainer>
);
};
return LazyWrapperRender;
};

Check warning on line 95 in packages/mana-core/src/view/view-container.tsx

View check run for this annotation

Codecov / codecov/patch

packages/mana-core/src/view/view-container.tsx#L59-L95

Added lines #L59 - L95 were not covered by tests
export const ViewWrapper = (
ViewComponent: React.FC | React.ForwardRefExoticComponent<any>,
ViewComponent:
| React.FC
| React.ForwardRefExoticComponent<any>
| React.LazyExoticComponent<React.FC | React.ForwardRefExoticComponent<any>>,

Check warning on line 100 in packages/mana-core/src/view/view-container.tsx

View check run for this annotation

Codecov / codecov/patch

packages/mana-core/src/view/view-container.tsx#L97-L100

Added lines #L97 - L100 were not covered by tests
container: Syringe.Container,
) => {
const ViewWrapperRender: WrapperViewComponent = ({
Expand All @@ -63,16 +106,10 @@ export const ViewWrapper = (
}: {
children: React.ReactNode;
}) => {
const containerRef = React.useRef<HTMLDivElement>(null);
const ChildComponent = LazyWrapper(ViewComponent);

Check warning on line 109 in packages/mana-core/src/view/view-container.tsx

View check run for this annotation

Codecov / codecov/patch

packages/mana-core/src/view/view-container.tsx#L109

Added line #L109 was not covered by tests
return (
<ObservableContext.Provider value={{ getContainer: () => container }}>
<ViewContainer
ref={containerRef}
component={ViewComponent}
viewComponentProps={props}
>
{children}
</ViewContainer>
<ChildComponent {...props}>{children}</ChildComponent>

Check warning on line 112 in packages/mana-core/src/view/view-container.tsx

View check run for this annotation

Codecov / codecov/patch

packages/mana-core/src/view/view-container.tsx#L112

Added line #L112 was not covered by tests
</ObservableContext.Provider>
);
};
Expand Down

0 comments on commit 5e9b194

Please sign in to comment.