-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathMenuItem.js
136 lines (113 loc) · 3.22 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
import cx from 'clsx'
import _ from 'lodash'
import PropTypes from 'prop-types'
import * as React from 'react'
import {
childrenUtils,
createShorthandFactory,
customPropTypes,
getComponentType,
getUnhandledProps,
SUI,
useKeyOnly,
useKeyOrValueAndKey,
useEventCallback,
} from '../../lib'
import Icon from '../../elements/Icon'
/**
* A menu can contain an item.
*/
const MenuItem = React.forwardRef(function (props, ref) {
const {
active,
children,
className,
color,
content,
disabled,
fitted,
header,
icon,
link,
name,
onClick,
position,
} = props
const classes = cx(
color,
position,
useKeyOnly(active, 'active'),
useKeyOnly(disabled, 'disabled'),
useKeyOnly(icon === true || (icon && !(name || content)), 'icon'),
useKeyOnly(header, 'header'),
useKeyOnly(link, 'link'),
useKeyOrValueAndKey(fitted, 'fitted'),
'item',
className,
)
const ElementType = getComponentType(props, {
getDefault: () => {
if (onClick) return 'a'
},
})
const rest = getUnhandledProps(MenuItem, props)
const handleClick = useEventCallback((e) => {
if (!disabled) {
props.onClick?.(e, props)
}
})
if (!childrenUtils.isNil(children)) {
return (
<ElementType {...rest} className={classes} onClick={handleClick} ref={ref}>
{children}
</ElementType>
)
}
return (
<ElementType {...rest} className={classes} onClick={handleClick} ref={ref}>
{Icon.create(icon, { autoGenerateKey: false })}
{childrenUtils.isNil(content) ? _.startCase(name) : content}
</ElementType>
)
})
MenuItem.displayName = 'MenuItem'
MenuItem.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
/** 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(SUI.COLORS),
/** Shorthand for primary content. */
content: customPropTypes.contentShorthand,
/** A menu item can be disabled. */
disabled: PropTypes.bool,
/** A menu item or menu can remove element padding, vertically or horizontally. */
fitted: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['horizontally', 'vertically'])]),
/** 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 left or right position. */
position: PropTypes.oneOf(['left', 'right']),
}
MenuItem.create = createShorthandFactory(MenuItem, (val) => ({ content: val, name: val }))
export default MenuItem