breadcrumb.js 5.0 KB
Newer Older
徐立's avatar
徐立 committed
1 2
import React, { PureComponent, createElement } from 'react';
import pathToRegexp from 'path-to-regexp';
3
import { Breadcrumb, Icon } from 'antd';
徐立's avatar
徐立 committed
4 5 6 7 8
import styles from './index.less';
import { urlToList } from '../_utils/pathTools';
import { connect } from 'dva';

export const getBreadcrumb = (breadcrumbNameMap, url) => {
9 10 11 12 13 14 15 16 17
	let breadcrumb = breadcrumbNameMap[url];
	if (!breadcrumb) {
		Object.keys(breadcrumbNameMap).forEach((item) => {
			if (pathToRegexp(item).test(url)) {
				breadcrumb = breadcrumbNameMap[item];
			}
		});
	}
	return breadcrumb || {};
徐立's avatar
徐立 committed
18 19 20
};

@connect(({ global }) => ({
21
	global: global,
徐立's avatar
徐立 committed
22 23
}))
export default class BreadcrumbView extends PureComponent {
24 25 26
	state = {
		breadcrumb: null,
	};
徐立's avatar
徐立 committed
27

28 29 30
	componentDidMount() {
		this.getBreadcrumbDom();
	}
徐立's avatar
徐立 committed
31

32 33 34 35 36 37 38 39 40 41
	componentDidUpdate(preProps) {
		const { location } = this.props;
		if (!location || !preProps.location) {
			return;
		}
		const prePathname = preProps.location.pathname;
		if (prePathname !== location.pathname) {
			this.getBreadcrumbDom();
		}
	}
徐立's avatar
徐立 committed
42

43 44
	getBreadcrumbDom = () => {
		const breadcrumb = this.conversionBreadcrumbList();
徐立's avatar
徐立 committed
45

46 47 48 49
		this.setState({
			breadcrumb,
		});
	};
徐立's avatar
徐立 committed
50

51 52 53 54 55 56 57 58 59
	getBreadcrumbProps = () => {
		const { routes, params, location, breadcrumbNameMap } = this.props;
		return {
			routes,
			params,
			routerLocation: location,
			breadcrumbNameMap,
		};
	};
徐立's avatar
徐立 committed
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	// Generated according to props
	conversionFromProps = () => {
		const { breadcrumbList, breadcrumbSeparator, itemRender, linkElement = 'a' } = this.props;
		return (
			<Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
				{breadcrumbList.map((item) => {
					const title = itemRender ? itemRender(item) : item.title;
					return (
						<Breadcrumb.Item key={item.title}>
							{item.href
								? createElement(
										linkElement,
										{
											[linkElement === 'a' ? 'href' : 'to']: item.href,
										},
										title,
								  )
								: title}
						</Breadcrumb.Item>
					);
				})}
			</Breadcrumb>
		);
	};
徐立's avatar
徐立 committed
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	conversionFromLocation = (routerLocation, breadcrumbNameMap) => {
		const { breadcrumbSeparator, home, itemRender, linkElement = 'a' } = this.props;
		// Convert the url to an array
		const pathSnippets = urlToList(routerLocation.pathname);
		// Loop data mosaic routing
		const extraBreadcrumbItems = pathSnippets.map((url, index) => {
			const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
			if (currentBreadcrumb.inherited) {
				return null;
			}
			const isLinkable = index !== pathSnippets.length - 1 && currentBreadcrumb.component;
			const name = itemRender ? itemRender(currentBreadcrumb) : currentBreadcrumb.name;
			return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
				<Breadcrumb.Item key={url}>
					{createElement(
						isLinkable ? linkElement : 'span',
						{
							[linkElement === 'a' ? 'href' : 'to']: url,
							onClick: () => {
								pathSnippets.length - 1 === index && window.location.reload();
							},
						},
						name,
					)}
				</Breadcrumb.Item>
			) : null;
		});
		// Add home breadcrumbs to your head
		extraBreadcrumbItems.unshift(
			<Breadcrumb.Item key="home">
				{createElement(
					linkElement,
					{
						[linkElement === 'a' ? 'href' : 'to']: this.props.global.system.href || '/',
					},
					home || 'Home',
				)}
			</Breadcrumb.Item>,
		);
徐立's avatar
徐立 committed
125

126 127 128 129 130 131 132 133 134 135 136 137
		return (
			<div style={{ display: 'flex' }}>
				{' '}
				<span style={{ lineHeight: 0.5, marginTop: -2.8, fontSize: 14, color: '#3a3a3a' }}>
					<Icon type="link" /> 位置:
				</span>
				<Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
					{extraBreadcrumbItems}
				</Breadcrumb>
			</div>
		);
	};
徐立's avatar
徐立 committed
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	/**
	 * 将参数转化为面包屑
	 * Convert parameters into breadcrumbs
	 */
	conversionBreadcrumbList = () => {
		const { breadcrumbList, breadcrumbSeparator } = this.props;
		const { routes, params, routerLocation, breadcrumbNameMap } = this.getBreadcrumbProps();
		if (breadcrumbList && breadcrumbList.length) {
			return this.conversionFromProps();
		}
		// 如果传入 routes 和 params 属性
		// If pass routes and params attributes
		if (routes && params) {
			return (
				<Breadcrumb
					className={styles.breadcrumb}
					routes={routes.filter((route) => route.breadcrumbName)}
					params={params}
					itemRender={this.itemRender}
					separator={breadcrumbSeparator}
				/>
			);
		}
		// 根据 location 生成 面包屑
		// Generate breadcrumbs based on location
		if (routerLocation && routerLocation.pathname) {
			return this.conversionFromLocation(routerLocation, breadcrumbNameMap);
		}
		return null;
	};
徐立's avatar
徐立 committed
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	// 渲染Breadcrumb 子节点
	// Render the Breadcrumb child node
	itemRender = (route, params, routes, paths) => {
		const { linkElement = 'a' } = this.props;
		const last = routes.indexOf(route) === routes.length - 1;
		return last || !route.component ? (
			<span>{route.breadcrumbName}</span>
		) : (
			createElement(
				linkElement,
				{
					href: paths.join('/') || '/',
					to: paths.join('/') || '/',
				},
				route.breadcrumbName,
			)
		);
	};

	render() {
		const { breadcrumb } = this.state;
		return breadcrumb;
	}
徐立's avatar
徐立 committed
193
}