yapi/client/components/Breadcrumb/Breadcrumb.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-07-28 17:30:56 +08:00
import './Breadcrumb.scss';
import { withRouter, Link } from 'react-router-dom';
import { Breadcrumb } from 'antd';
2017-07-28 21:16:47 +08:00
import axios from 'axios';
2017-07-28 17:30:56 +08:00
import React, { Component } from 'react';
2017-07-28 21:16:47 +08:00
2017-07-28 17:30:56 +08:00
@withRouter
export default class BreadcrumbNavigation extends Component {
constructor(props) {
super(props);
2017-07-28 21:16:47 +08:00
this.state = {
// breadcrumb: [{name:'首页', path: '/'}],
hash: '',
breadcrumb: []
}
}
getBreadcrumb = (pathSnippets) => {
console.log(pathSnippets);
if (/['project'|'group'|'add-interface']/.test(pathSnippets)) {
const type = pathSnippets[0],
id = pathSnippets[1];
const params = {
type,
id
}
axios.get('/user/nav', {params: params}).then( res => {
console.log(res);
});
}
}
componentDidMount() {
this.setState({
hash: location.hash
})
}
shouldComponentUpdate = (nextProps, nextState) => {
// hash改变的时候才render
if (nextState.hash !== this.state.hash) {
return true;
} else {
return false;
}
2017-07-28 17:30:56 +08:00
}
render () {
// 获取接口路径并分割
const pathSnippets = location.hash.split('#')[1].split('/').filter(i => i);
2017-07-28 21:16:47 +08:00
this.getBreadcrumb(pathSnippets);
const extraBreadcrumbItems = this.state.breadcrumb.map((item, index) => {
// const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
console.log(index);
2017-07-28 17:30:56 +08:00
return (
2017-07-28 21:16:47 +08:00
<Breadcrumb.Item key={item.path}>
<Link to={item.path}>
{item.name}
2017-07-28 17:30:56 +08:00
</Link>
</Breadcrumb.Item>
);
});
const breadcrumbItems = [(
<Breadcrumb.Item key="home">
2017-07-28 21:16:47 +08:00
<Link to="/">首页</Link>
2017-07-28 17:30:56 +08:00
</Breadcrumb.Item>
)].concat(extraBreadcrumbItems);
2017-07-28 17:42:19 +08:00
if (pathSnippets.length) {
return (
<Breadcrumb className="breadcrumb-container">
{breadcrumbItems}
</Breadcrumb>
)
} else {
return <span></span>;
}
2017-07-28 17:30:56 +08:00
}
}