import React from 'react' ; import pathToRegexp from 'path-to-regexp' ; import withRouter from 'umi/withRouter' ; import router , { RouteData } from 'umi/router' ; import { MenuDataItem } from '@ant-design/pro-layout/es/typings' ; import find from 'lodash/find' ; import TabsView , { TabView } from './TabsView' ; function searchPathIdAndName ( childrenPathname , originalMenuData ) { function getPathIdAndName ( path , menuData , parent ) { let result ; menuData . forEach ( item => { if ( pathToRegexp ( `${ item . path }(.*)` ) . test ( path ) ) { if ( ! parent && item . name ) { result = [ item . path , item . name ] ; } else if ( parent && ! parent . component && item . name ) { result = [ item . path , item . name ] ; } if ( item . children ) { result = getPathIdAndName ( path , item . children , item ) || result ; } } } ) ; return result ; } return getPathIdAndName ( childrenPathname , originalMenuData , null ) || [ '404' , 'Error' ] ; } export interface TabsViewProps { proRootPath ? : string ; children : React . ReactChildren ; menuData : MenuDataItem [ ] ; location : RouteData ; title : string ; fixedHeader ? : boolean ; } function routeTo ( targetTab ? ) { if ( targetTab ) { router . push ( { ... targetTab . location , } ) ; } } const TabsViewWrapper = ( props ) => { const { children , menuData , location } = props ; if ( location . pathname === '/' ) { return children ; } const [ pathId , pathName ] = searchPathIdAndName ( location . pathname , menuData ) ; const afterRemoveTab = ( removeKey , nextTabKey , activedTabs ) => { const targetTab = find ( activedTabs , { key : nextTabKey } ) ; routeTo ( targetTab ) ; } ; const handleTabChange = ( keyToSwitch , activedTabs ) => { const targetTab = find ( activedTabs , { key : keyToSwitch } ) ; routeTo ( targetTab ) ; } ; return ( < TabsView activeKey = { pathId } activeTitle = { pathName } handleTabChange = { handleTabChange } afterRemoveTab = { afterRemoveTab } extraTabProperties = { { location , } } { ... props } > { children } < /TabsView>); } ; export default withRouter ( TabsViewWrapper as any ) ;