-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathCDropdownItem.js
107 lines (92 loc) · 2.18 KB
/
CDropdownItem.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React, {useContext} from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import {mapToCssModules, omit, tagPropType} from './Shared/helper.js';
import {Context} from './CDropdownCustom';
//component - CoreUI / CDropdownItem
const CDropdownItem = props=>{
let {
tag: Tag,
className,
cssModule,
//
innerRef,
color,
divider,
header,
active,
...attributes
} = omit(props, ['toggle']);
const context = useContext(Context);
const onClick = e=>{
if (props.disabled || props.header || props.divider) {
e.preventDefault();
return;
}
if (props.onClick) {
props.onClick(e);
}
if (props.toggle) {
context.toggle(e);
}
}
const getTabIndex = ()=>{
if (props.disabled || props.header || props.divider) {
return '-1';
}
return '0';
}
//render
const tabIndex = getTabIndex();
const role = tabIndex > -1 ? 'menuitem' : undefined;
const classes = mapToCssModules(classNames(
className,
header ? 'dropdown-header' : divider ? 'dropdown-divider' : 'dropdown-item',
active ? 'active' : null,
color ? 'bg-'+color : null,
{
disabled: attributes.disabled,
}
), cssModule);
if (Tag === 'button') {
if (header) {
Tag = 'h6';
} else if (divider) {
Tag = 'div';
} else if (props.href) {
Tag = 'a';
}
}
return (
<Tag
type={(Tag === 'button' && (attributes.onClick || props.toggle)) ? 'button' : undefined}
{...attributes}
tabIndex={tabIndex}
role={role}
className={classes}
onClick={onClick}
ref={innerRef}
/>
);
}
CDropdownItem.propTypes = {
tag: tagPropType,
children: PropTypes.node,
className: PropTypes.string,
cssModule: PropTypes.object,
//
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
color: PropTypes.string,
href: PropTypes.string,
divider: PropTypes.bool,
header: PropTypes.bool,
active: PropTypes.bool,
disabled: PropTypes.bool,
onClick: PropTypes.func,
toggle: PropTypes.bool
};
CDropdownItem.defaultProps = {
tag: 'button',
toggle: true
};
export default CDropdownItem;