-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathCLink.js
79 lines (70 loc) · 1.58 KB
/
CLink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import { mapToCssModules } from './Shared/helper.js'
import { NavLink } from 'react-router-dom'
//component - CoreUI / CLink
const CLink = props => {
const {
className,
cssModule,
//
innerRef,
active,
href,
onClick,
...rest
} = props
const to = rest ? rest.to : null
const click = e => {
if (!href && !to) {
e.preventDefault()
}
onClick && onClick(e)
}
// render
const classes = mapToCssModules(classNames(
className,
active ? 'active' : null
), cssModule)
return to ? (
<NavLink
{...rest}
className={classes}
onClick={click}
ref={innerRef}
/>
) : (
<a
href={href || '#'}
className={classes}
{...rest}
onClick={click}
ref={innerRef}
/>
)
}
CLink.propTypes = {
className: PropTypes.string,
cssModule: PropTypes.object,
//
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
active: PropTypes.bool,
href: PropTypes.string,
onClick: PropTypes.func,
...NavLink.propTypes,
to: PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.func])
};
CLink.sortAttributes = (attributesToSort) => {
const attributes = {}
const linkProps = {}
Object.entries(attributesToSort || {}).forEach(([key, value]) => {
if (Object.keys(CLink.propTypes).includes(key)) {
linkProps[key] = value
} else {
attributes[key] = value
}
})
return { linkProps, attributes }
}
export default CLink