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'
6
6
7
7
//component - CoreUI / CLink
8
-
9
- const CLink = props => {
8
+ const CLink = props => {
10
9
11
10
const {
12
11
className,
13
12
cssModule,
14
13
//
15
14
innerRef,
16
15
active,
17
- disabled,
18
16
href,
19
- to,
20
- ...attributes
21
- } = props ;
22
-
23
- let href2 = href ;
17
+ onClick,
18
+ ...rest
19
+ } = props
24
20
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 )
36
27
}
37
28
38
29
// render
39
30
40
31
const classes = mapToCssModules ( classNames (
41
32
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
+ )
56
52
}
57
53
58
54
CLink . propTypes = {
@@ -61,13 +57,23 @@ CLink.propTypes = {
61
57
//
62
58
innerRef : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . func , PropTypes . string ] ) ,
63
59
active : PropTypes . bool ,
64
- disabled : PropTypes . bool ,
65
60
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
+ }
69
65
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
+ }
72
78
73
- export default CLink ;
79
+ export default CLink
0 commit comments