@alitajs/layout
Version:
12 lines (9 loc) • 4.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _default = function _default() {
return "import React, { useEffect, useState } from 'react';\nimport { Location, LocationState } from 'history';\n// eslint-disable-next-line\nimport {\n getPageNavBar,\n KeepAliveLayout,\n getTabBarList,\n layoutEmitter,\n request,\n RequestMethodInUmi\n} from '@@/core/umiExports';\n\nimport AlitaLayout, {\n AlitaLayoutProps,\n NavBarProps,\n NavBarListItem,\n TabBarProps,\n} from '@alitajs/alita-layout';\n\ninterface ALitaLayoutProp extends AlitaLayoutProps {\n onPageChange?: (request: RequestMethodInUmi, pathname: string, prevPathName: string) => void;\n}\ninterface BasicLayoutProps {\n layoutConfig: ALitaLayoutProp;\n hasKeepAlive: boolean;\n hideNavBar: boolean;\n location: Location<LocationState>;\n}\nconst changeNavBarConfig = (\n preConfig: NavBarProps | undefined,\n changeData: {},\n) => {\n if (!changeData) return preConfig;\n const { navList, ...other } = preConfig as NavBarProps;\n if (!navList || navList!.length === 0) {\n const config = [] as NavBarListItem[];\n Object.keys(changeData).forEach((i) => {\n config.push({\n pagePath: i,\n navBar: changeData[i],\n });\n });\n return { ...other, navList: config };\n }\n let isChanged = false;\n const newNavList = navList!.map((i) => {\n if (changeData[i.pagePath]) {\n i.navBar = { ...i.navBar, ...changeData[i.pagePath] };\n isChanged = true;\n }\n return i;\n });\n if (isChanged) {\n return { ...other, navList: newNavList };\n }\n Object.keys(changeData).forEach((i) => {\n newNavList.push({\n pagePath: i,\n navBar: changeData[i],\n });\n });\n return { ...other, navList: newNavList };\n};\n\nconst changeTabBarListConfig = (\n preConfig: TabBarProps | undefined,\n changeData: {},\n) => {\n if (!changeData) return preConfig;\n const newChangeData = {...changeData};\n const { list, ...other } = preConfig as TabBarProps;\n if (!list || list!.length === 0) {\n return preConfig;\n }\n\n const newNavList = [] as any[];\n list!.forEach((i) => {\n if (newChangeData[i.pagePath]) {\n const newPagePath = i.pagePath;\n i = { ...i, ...newChangeData[newPagePath] };\n if (newChangeData[newPagePath]?.replace) {\n i.pagePath = newChangeData[newPagePath]?.replace;\n }\n delete newChangeData[newPagePath];\n if(changeData[newPagePath]?.remove) return;\n }\n newNavList.push(i);\n });\n Object.keys(newChangeData).forEach((item: string) => {\n if(newChangeData[item]?.remove) return;\n newNavList.push(newChangeData[item]);\n });\n return { ...other, list: newNavList };\n};\n\nlet prevPathName = '/';\nconst BasicLayout: React.FC<BasicLayoutProps> = (props) => {\n const [pageNavBar, setPageNavBar] = useState({});\n const [tabBarList, setTabBarList] = useState({});\n const { children, layoutConfig, hasKeepAlive, hideNavBar, ...otherProps } = props;\n const { titleList, documentTitle, navBar, tabBar, onPageChange } = layoutConfig;\n useEffect(() => {\n setPageNavBar(getPageNavBar());\n setTabBarList(getTabBarList());\n setTimeout(() => {\n onPageChange && onPageChange(request, props.location.pathname, prevPathName);\n prevPathName = props.location.pathname;\n }, 10);\n }, [props.location.pathname]);\n layoutEmitter?.useSubscription?.((e:T) => {\n setPageNavBar(getPageNavBar());\n setTabBarList(getTabBarList());\n });\n const newNavBar = changeNavBarConfig(navBar, pageNavBar);\n const newTabBarList = changeTabBarListConfig(tabBar, tabBarList);\n const layout = {\n documentTitle,\n navBar: newNavBar,\n tabBar: newTabBarList,\n titleList,\n hideNavBar\n };\n return (\n <AlitaLayout {...layout}>\n {hasKeepAlive && (\n <KeepAliveLayout {...otherProps}>{children}</KeepAliveLayout>\n )}\n {!hasKeepAlive && children}\n </AlitaLayout>\n );\n};\n\nexport default BasicLayout;\n";
};
exports.default = _default;