Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add micro-app-link component #915

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/plugin-qiankun/src/master/MicroApp.tsx.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ export const MicroApp = forwardRef(
...propsFromConfig,
...stateForSlave,
...propsFromParams,
globalRoutesInfo: {
masterHistoryType,
routes: globalSettings.routes
},
setLoading,
},
},
Expand Down Expand Up @@ -223,6 +227,10 @@ export const MicroApp = forwardRef(
...propsFromConfig,
...stateForSlave,
...propsFromParams,
globalRoutesInfo: {
masterHistoryType,
routes: globalSettings.routes
},
setLoading,
};

Expand Down
81 changes: 81 additions & 0 deletions packages/plugin-qiankun/src/slave/MicroAppLink.tsx.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useModel } from '@@/plugin-model';
import React, { forwardRef, FC, useImperativeHandle, useRef, MouseEvent, HTMLAttributes, useCallback } from 'react';
import noop from "{{{ lodashPath }}}/noop";
import find from "{{{ lodashPath }}}/find";

const COMPONENT_NAME = 'qiankun-microapp-link';

interface MicroAppLinkProps {
// 搴旂敤鍚嶇О
appName?: string;

// 鐩稿璺敱锛屼笉鍖呭惈搴旂敤鍓嶇紑
appRoute: string;

// 鏄惁涓诲簲鐢ㄤ笅鐨勮矾鐢憋紝榛樿false
isMaster?: boolean;
}

const urlFactory = (routes: Record<string, any>[]) => ({ appName, appRoute, isMaster }: MicroAppLinkProps) => {
if (isMaster) {
return appRoute;
}

const app = find(routes, ({ microApp }) => microApp === appName);
if (!app) {
console.error(`[${COMPONENT_NAME}] microapp "${app}" is not found`);
return appRoute;
}

const prefix = app.path.replace('/*', '');
return `${prefix}/${appRoute.startsWith('/') ? appRoute.slice(1) : appRoute}`;
};

export const MicroAppLink: FC<MicroAppLinkProps & HTMLAttributes<HTMLAnchorElement>> = forwardRef((props, ref) => {
const {
children,
appName,
appRoute,
isMaster = false,
...anchorProps
} = props;
const stateFromMaster = (useModel || noop)('@@qiankunStateFromMaster');
const linkRef = useRef<HTMLAnchorElement>();

const { historyType, routes } = stateFromMaster?.globalRoutesInfo || {};
const linkProps = { appName, appRoute, isMaster };
const createHerf = urlFactory(routes);
const linkUrl = historyType === 'hash' ? `#${createHerf(linkProps)}` : createHerf(linkProps);

useImperativeHandle(ref, () => linkRef.current);

const handleClick = useCallback((e: MouseEvent<HTMLAnchorElement>) => {
if (anchorProps.onClick) {
return anchorProps.onClick(e);
}

// hash璺敱 浣跨敤 a鏍囩 榛樿琛屼负
if (historyType === 'hash') {
return;
}

if (historyType === 'browser') {
e?.stopPropagation();
e?.preventDefault();
return window.history.pushState({}, '', linkUrl);
}

console.error(`[${COMPONENT_NAME}] not support "masterHistoryType = '${historyType}'"`)
return;
}, [anchorProps.onClick, historyType]);

return (
<a
{...anchorProps}
ref={linkRef}
className={`${COMPONENT_NAME} ${anchorProps.className}`}
href={linkUrl}
onClick={handleClick}
>{children}</a>
);
});
5 changes: 5 additions & 0 deletions packages/plugin-qiankun/src/slave/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ export default function (api: IApi) {
path: 'plugin-qiankun/lifecycles.ts',
content: readFileSync(join(__dirname, 'lifecycles.ts.tpl'), 'utf-8'),
});

api.writeTmpFile({
path: 'plugin-qiankun/MicroAppLink.tsx',
content: readFileSync(join(__dirname, 'MicroAppLink.tsx.tpl'), 'utf-8'),
});
});

api.addMiddlewares(async () => {
Expand Down