Skip to content

Commit 61859ed

Browse files
committed
fix: CLink: component refactor
- add NavLink propTypes, - remove 'disabled' prop, - create 'sortAttributes' function for easy component composition - add onClick to prop list and types
1 parent 3d36c5c commit 61859ed

File tree

1 file changed

+51
-45
lines changed

1 file changed

+51
-45
lines changed

src/CLink.js

+51-45
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,54 @@
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 {NavLink} from 'react-router-dom';
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 { NavLink } from 'react-router-dom'
66

77
//component - CoreUI / CLink
8-
9-
const CLink = props=>{
8+
const CLink = props => {
109

1110
const {
1211
className,
1312
cssModule,
1413
//
1514
innerRef,
1615
active,
17-
disabled,
1816
href,
19-
to,
20-
...attributes
21-
} = props;
22-
23-
let href2 = href;
17+
onClick,
18+
...rest
19+
} = props
2420

25-
const onClick = e=>{
26-
if (props.disabled) {
27-
e.preventDefault();
28-
return;
29-
}
30-
if (href2 === '#') {
31-
e.preventDefault();
32-
}
33-
if (props.onClick) {
34-
props.onClick(e);
35-
}
21+
const to = rest ? rest.to : null
22+
const click = e => {
23+
if (!href && !to) {
24+
e.preventDefault()
25+
}
26+
onClick && onClick(e)
3627
}
3728

3829
// render
3930

4031
const classes = mapToCssModules(classNames(
4132
className,
42-
active ? 'active' : null,
43-
disabled ? 'disabled' : null
44-
), cssModule);
45-
46-
//<span {...attributes} className={classes} ref={innerRef} />
47-
48-
to || (href2 || (href2='#'));
49-
50-
return (
51-
to ?
52-
<NavLink to={to} {...attributes} className={classes} ref={innerRef} /> :
53-
<a href={href2} {...attributes} className={classes} onClick={onClick} ref={innerRef} />
54-
);
55-
33+
active ? 'active' : null
34+
), cssModule)
35+
36+
return to ? (
37+
<NavLink
38+
{...rest}
39+
className={classes}
40+
onClick={click}
41+
ref={innerRef}
42+
/>
43+
) : (
44+
<a
45+
href={href || '#'}
46+
className={classes}
47+
{...rest}
48+
onClick={click}
49+
ref={innerRef}
50+
/>
51+
)
5652
}
5753

5854
CLink.propTypes = {
@@ -61,13 +57,23 @@ CLink.propTypes = {
6157
//
6258
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
6359
active: PropTypes.bool,
64-
disabled: PropTypes.bool,
6560
href: PropTypes.string,
66-
to: PropTypes.string,
67-
onClick: PropTypes.func
68-
};
61+
onClick: PropTypes.func,
62+
...NavLink.propTypes,
63+
to: PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.func])
64+
}
6965

70-
CLink.defaultProps = {
71-
};
66+
CLink.sortAttributes = (attributesToSort) => {
67+
const attributes = {}
68+
const linkProps = {}
69+
Object.entries(attributesToSort || {}).forEach(([key, value]) => {
70+
if (Object.keys(CLink.propTypes).includes(key)) {
71+
linkProps[key] = value
72+
} else {
73+
attributes[key] = value
74+
}
75+
})
76+
return { linkProps, attributes }
77+
}
7278

73-
export default CLink;
79+
export default CLink

0 commit comments

Comments
 (0)