-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimization and rewriting PrimaryLayout.
- Loading branch information
Showing
22 changed files
with
500 additions
and
783 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,66 @@ | ||
import React, { PureComponent } from 'react' | ||
import React, { PureComponent, Fragment } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { Breadcrumb, Icon } from 'antd' | ||
import Link from 'umi/navlink' | ||
import pathToRegexp from 'path-to-regexp' | ||
import { queryArray, addLangPrefix, pathMatchRegexp, deLangPrefix } from 'utils' | ||
import withRouter from 'umi/withRouter' | ||
import { withI18n } from '@lingui/react' | ||
import styles from './Layout.less' | ||
import { pathMatchRegexp, queryAncestors } from 'utils' | ||
import styles from './Bread.less' | ||
|
||
@withI18n() | ||
@withRouter | ||
class Bread extends PureComponent { | ||
render() { | ||
const { menuList, location, i18n } = this.props | ||
// 匹配当前路由 | ||
let pathArray = [] | ||
let current | ||
for (let index in menuList) { | ||
if ( | ||
menuList[index].route && | ||
pathMatchRegexp(menuList[index].route, location.pathname) | ||
) { | ||
current = menuList[index] | ||
break | ||
} | ||
} | ||
|
||
const getPathArray = item => { | ||
pathArray.unshift(item) | ||
if (item.breadcrumbParentId) { | ||
getPathArray(queryArray(menuList, 'id', item.breadcrumbParentId)) | ||
} | ||
} | ||
|
||
let paramMap = {} | ||
if (!current) { | ||
pathArray.push( | ||
menuList[0] || { | ||
id: 1, | ||
icon: 'laptop', | ||
name: i18n.t`Dashboard`, | ||
} | ||
) | ||
pathArray.push({ | ||
id: 404, | ||
name: i18n.t`Not Found`, | ||
}) | ||
} else { | ||
getPathArray(current) | ||
|
||
let keys = [] | ||
let values = pathToRegexp(current.route, keys).exec( | ||
deLangPrefix(location.pathname) | ||
) | ||
if (keys.length) { | ||
keys.forEach((currentValue, index) => { | ||
if (typeof currentValue.name !== 'string') { | ||
return | ||
} | ||
paramMap[currentValue.name] = values[index + 1] | ||
}) | ||
} | ||
} | ||
|
||
// 递归查找父级 | ||
const breads = pathArray.map((item, key) => { | ||
generateBreadcrumbs = paths => { | ||
return paths.map((item, key) => { | ||
const content = ( | ||
<span> | ||
<Fragment> | ||
{item.icon ? ( | ||
<Icon type={item.icon} style={{ marginRight: 4 }} /> | ||
) : ( | ||
'' | ||
)} | ||
) : null} | ||
{item.name} | ||
</span> | ||
</Fragment> | ||
) | ||
|
||
return ( | ||
<Breadcrumb.Item key={key}> | ||
{pathArray.length - 1 !== key ? ( | ||
<Link | ||
to={ | ||
addLangPrefix( | ||
pathToRegexp.compile(item.route || '')(paramMap) | ||
) || '#' | ||
} | ||
> | ||
{content} | ||
</Link> | ||
{paths.length - 1 !== key ? ( | ||
<Link to={item.route || '#'}>{content}</Link> | ||
) : ( | ||
content | ||
)} | ||
</Breadcrumb.Item> | ||
) | ||
}) | ||
} | ||
render() { | ||
const { routeList, location, i18n } = this.props | ||
|
||
// Find a route that matches the pathname. | ||
const currentRoute = routeList.find( | ||
_ => _.route && pathMatchRegexp(_.route, location.pathname) | ||
) | ||
|
||
// Find the breadcrumb navigation of the current route match and all its ancestors. | ||
const paths = currentRoute | ||
? queryAncestors(routeList, currentRoute, 'breadcrumbParentId').reverse() | ||
: [ | ||
routeList[0], | ||
{ | ||
id: 404, | ||
name: i18n.t`Not Found`, | ||
}, | ||
] | ||
|
||
return ( | ||
<div className={styles.bread}> | ||
<Breadcrumb>{breads}</Breadcrumb> | ||
</div> | ||
<Breadcrumb className={styles.bread}> | ||
{this.generateBreadcrumbs(paths)} | ||
</Breadcrumb> | ||
) | ||
} | ||
} | ||
|
||
Bread.propTypes = { | ||
menu: PropTypes.array, | ||
location: PropTypes.object, | ||
routeList: PropTypes.array, | ||
} | ||
|
||
export default Bread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.bread { | ||
margin-bottom: 24px; | ||
|
||
:global { | ||
.ant-breadcrumb { | ||
display: flex; | ||
align-items: center; | ||
} | ||
} | ||
} | ||
|
||
@media (max-width: 767px) { | ||
.bread { | ||
margin-bottom: 12px; | ||
} | ||
} |
Oops, something went wrong.