Skip to content

Commit 6d0e7fa

Browse files
committed
fix: Breadcrumb components rewrite
1 parent 543dffd commit 6d0e7fa

File tree

5 files changed

+106
-199
lines changed

5 files changed

+106
-199
lines changed

src/CBreadcrumb.js

+13-118
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,33 @@
1-
import React from 'react';
2-
import PropTypes from 'prop-types';
3-
import classNames from 'classnames';
4-
import {Route, Link, matchPath} from 'react-router-dom';
5-
import CBreadcrumbCustom from './CBreadcrumbCustom';
6-
import CBreadcrumbItem from './CBreadcrumbItem';
7-
8-
let routes;
9-
10-
const getPaths = pathname=>{
11-
const paths = ['/'];
12-
if (pathname === '/') return paths;
13-
pathname.split('/').reduce((prev, curr) => {
14-
const currPath = `${prev}/${curr}`;
15-
paths.push(currPath);
16-
return currPath;
17-
});
18-
return paths;
19-
};
20-
21-
const findRouteName = url=>{
22-
const aroute = routes.find(route => matchPath(url, {path: route.path, exact: route.exact}));
23-
return (aroute && aroute.name) ? aroute.name : null
24-
};
25-
26-
//component - CoreUI / CBreadcrumbRouteItem
27-
28-
const CBreadcrumbRouteItem = ({ match }) => {
29-
const routeName = findRouteName(match.url);
30-
if (routeName) {
31-
return (
32-
match.isExact ?
33-
<CBreadcrumbItem active>{routeName}</CBreadcrumbItem>
34-
:
35-
<CBreadcrumbItem>
36-
<Link to={match.url || ''}>
37-
{routeName}
38-
</Link>
39-
</CBreadcrumbItem>
40-
);
41-
}
42-
return null;
43-
};
44-
45-
CBreadcrumbRouteItem.propTypes = {
46-
match: PropTypes.shape({
47-
url: PropTypes.string,
48-
isExact: PropTypes.bool
49-
})
50-
};
51-
52-
//component - CoreUI / CBreadcrumbRouter
53-
54-
let postItems;
55-
56-
const CBreadcrumbRouter = args=>{
57-
58-
const paths = getPaths(args.location.pathname);
59-
const items = paths.map(
60-
(path, i) => <Route key={i.toString()} path={path} component={CBreadcrumbRouteItem} />
61-
);
62-
if (postItems)
63-
items.push(postItems);
64-
65-
// render
66-
67-
return items;
68-
/*
69-
return (
70-
<CBreadcrumbCustom>
71-
{items}
72-
</CBreadcrumbCustom>
73-
);
74-
*/
75-
76-
};
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
4+
import { mapToCssModules } from './Shared/helper.js'
775

786
//component - CoreUI / CBreadcrumb
797

80-
const CBreadcrumb = props=>{
8+
const CBreadcrumb = props => {
819

8210
const {
83-
children,
8411
className,
85-
custom,
12+
innerRef,
13+
cssModule,
8614
//
87-
routesProps,
8815
...attributes
89-
} = props;
16+
} = props
9017

91-
if (children)
92-
postItems = children;
93-
else {
94-
postItems = null;
95-
}
96-
97-
routes = props.appRoutes;
18+
const classes = mapToCssModules(classNames(className, 'breadcrumb'))
9819

9920
//render
100-
101-
delete attributes.appRoutes;
102-
const classes = classNames(className);
103-
104-
if (!custom){
105-
return (
106-
<CBreadcrumbCustom {...attributes} listClassName={classes}>
107-
<Route path="/:path" component={CBreadcrumbRouter} {...routesProps} />
108-
</CBreadcrumbCustom>
109-
);
110-
}
111-
11221
return (
113-
<CBreadcrumbCustom {...attributes} listClassName={classes}>
114-
{children}
115-
</CBreadcrumbCustom>
116-
);
117-
22+
<ol {...attributes} className={classes} ref={innerRef}/>
23+
)
11824
}
11925

12026
CBreadcrumb.propTypes = {
121-
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
12227
children: PropTypes.node,
12328
className: PropTypes.string,
124-
custom: PropTypes.bool,
12529
//
12630
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
127-
appRoutes: PropTypes.any,
128-
match: PropTypes.object,
129-
routesProps: PropTypes.object
130-
};
131-
132-
CBreadcrumb.defaultProps = {
133-
tag: 'div',
134-
className: '',
135-
appRoutes: [{ path: '/', exact: true, name: 'Home', component: null }]
136-
};
31+
}
13732

138-
export default CBreadcrumb;
33+
export default CBreadcrumb

src/CBreadcrumbCustom.js

-64
This file was deleted.

src/CBreadcrumbItem.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
import React from 'react';
2-
import PropTypes from 'prop-types';
3-
import classNames from 'classnames';
4-
import {tagPropType, mapToCssModules} from './Shared/helper.js';
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
4+
import { mapToCssModules } from './Shared/helper.js'
55

66
//component - CoreUI / CBreadcrumbItem
77

8-
const CBreadcrumbItem = props=>{
8+
const CBreadcrumbItem = props => {
99

1010
const {
11-
tag: Tag,
1211
className,
1312
cssModule,
1413
//
1514
innerRef,
1615
active,
1716
...attributes
18-
} = props;
17+
} = props
1918

2019
//render
2120

2221
const classes = mapToCssModules(classNames(
2322
className,
2423
active ? 'active' : false,
2524
'breadcrumb-item'
26-
), cssModule);
25+
), cssModule)
2726

2827
return (
29-
<Tag {...attributes} className={classes} aria-current={active ? 'page' : undefined} ref={innerRef} />
30-
);
28+
<li
29+
className={classes}
30+
role="presentation"
31+
aria-current={active ? 'page' : undefined}
32+
{...attributes}
33+
ref={innerRef}
34+
/>
35+
)
3136

3237
}
3338

3439
CBreadcrumbItem.propTypes = {
35-
tag: tagPropType,
3640
className: PropTypes.string,
3741
cssModule: PropTypes.object,
3842
//
3943
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
4044
active: PropTypes.bool
41-
};
42-
43-
CBreadcrumbItem.defaultProps = {
44-
tag: 'li'
45-
};
45+
}
4646

47-
export default CBreadcrumbItem;
47+
export default CBreadcrumbItem

src/CBreadcrumbRouter.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
4+
import { mapToCssModules } from './Shared/helper.js'
5+
import { CBreadcrumb, CBreadcrumbItem } from './index'
6+
import { Link, useLocation } from 'react-router-dom';
7+
8+
//component - CoreUI / CBreadcrumbRouter
9+
10+
const getPaths = pathname => {
11+
const paths = ['/']
12+
if (pathname === '/') return paths;
13+
pathname.split('/').reduce((prev, curr) => {
14+
const currPath = `${prev}/${curr}`
15+
paths.push(currPath)
16+
return currPath
17+
})
18+
return paths
19+
}
20+
21+
const CBreadcrumbRouteItem = ({name, path}, currPath) => {
22+
if (path === currPath) {
23+
return <CBreadcrumbItem active>{name}</CBreadcrumbItem>
24+
} else {
25+
return <CBreadcrumbItem>
26+
<Link to={path}>
27+
{name}
28+
</Link>
29+
</CBreadcrumbItem>
30+
}
31+
}
32+
33+
const CBreadcrumbRouter = props => {
34+
35+
const {
36+
className,
37+
cssModule,
38+
//
39+
innerRef,
40+
routes,
41+
...attributes
42+
} = props;
43+
44+
const currPath = useLocation().pathname
45+
const paths = getPaths(currPath)
46+
const currRoutes = routes.filter(route => paths.includes(route.path))
47+
48+
//render
49+
50+
const classes = mapToCssModules(classNames(className), cssModule)
51+
52+
return (
53+
<CBreadcrumb
54+
className={classes}
55+
{...attributes}
56+
ref={innerRef}
57+
>
58+
{
59+
currRoutes.map(route => {
60+
return CBreadcrumbRouteItem(route, currPath)
61+
})
62+
}
63+
</CBreadcrumb>
64+
)
65+
}
66+
67+
CBreadcrumbRouter.propTypes = {
68+
className: PropTypes.string,
69+
cssModule: PropTypes.object,
70+
//
71+
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
72+
routes: PropTypes.array
73+
}
74+
75+
export default CBreadcrumbRouter

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export {default as CCollapse} from './CCollapse';
7676
export {default as CTooltip} from './CTooltip';
7777
//export {default as CTooltipPopoverWrapper} from './CTooltipPopoverWrapper';
7878
export {default as CBreadcrumb} from './CBreadcrumb';
79+
export {default as CBreadcrumbRouter} from './CBreadcrumbRouter';
7980
export {default as CBreadcrumbItem} from './CBreadcrumbItem';
8081
export {default as CFooter} from './CFooter';
8182
export {default as CHeader} from './CHeader';

0 commit comments

Comments
 (0)