forked from Semantic-Org/Semantic-UI-React
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuItem.js
138 lines (114 loc) · 3.24 KB
/
MenuItem.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import _ from 'lodash'
import cx from 'classnames'
import React, { Component, PropTypes } from 'react'
import {
createShorthandFactory,
customPropTypes,
getElementType,
getUnhandledProps,
META,
SUI,
useKeyOnly,
useKeyOrValueAndKey,
} from '../../lib'
import Icon from '../../elements/Icon'
const _meta = {
name: 'MenuItem',
type: META.TYPES.COLLECTION,
parent: 'Menu',
props: {
color: SUI.COLORS,
fitted: ['horizontally', 'vertically'],
position: ['right'],
},
}
export default class MenuItem extends Component {
static propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,
/** A menu item can be active. */
active: PropTypes.bool,
/** Primary content. */
children: PropTypes.node,
/** Additional classes. */
className: PropTypes.string,
/** Additional colors can be specified. */
color: PropTypes.oneOf(_meta.props.color),
/** Shorthand for primary content. */
content: customPropTypes.contentShorthand,
/** A menu item or menu can remove element padding, vertically or horizontally. */
fitted: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.oneOf(_meta.props.fitted),
]),
/** A menu item may include a header or may itself be a header. */
header: PropTypes.bool,
/** MenuItem can be only icon. */
icon: PropTypes.oneOfType([
PropTypes.bool,
customPropTypes.itemShorthand,
]),
/** MenuItem index inside Menu. */
index: PropTypes.number,
/** A menu item can be link. */
link: PropTypes.bool,
/** Internal name of the MenuItem. */
name: PropTypes.string,
/**
* Called on click. When passed, the component will render as an `a`
* tag by default instead of a `div`.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onClick: PropTypes.func,
/** A menu item can take right position. */
position: PropTypes.oneOf(_meta.props.position),
}
static _meta = _meta
handleClick = (e) => {
const { onClick } = this.props
if (onClick) onClick(e, this.props)
}
render() {
const {
active,
children,
className,
color,
content,
fitted,
header,
icon,
link,
name,
onClick,
position,
} = this.props
const classes = cx(
color,
position,
useKeyOnly(active, 'active'),
useKeyOnly(icon === true || icon && !(name || content), 'icon'),
useKeyOnly(header, 'header'),
useKeyOnly(link, 'link'),
useKeyOrValueAndKey(fitted, 'fitted'),
'item',
className,
)
const ElementType = getElementType(MenuItem, this.props, () => {
if (onClick) return 'a'
})
const rest = getUnhandledProps(MenuItem, this.props)
if (!_.isNil(children)) {
return <ElementType {...rest} className={classes} onClick={this.handleClick}>{children}</ElementType>
}
return (
<ElementType {...rest} className={classes} onClick={this.handleClick}>
{Icon.create(icon)}
{content || _.startCase(name)}
</ElementType>
)
}
}
MenuItem.create = createShorthandFactory(MenuItem, val => ({ content: val, name: val }), true)