| 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189 |
13x
13x
13x
13x
13x
13x
13x
13x
13x
13x
13x
2x
2x
2x
2x
1x
1x
1x
1x
26x
26x
26x
33x
89x
89x
89x
89x
| import React from 'react';
import PropTypes from 'prop-types';
import { omit } from 'lodash';
import {
EventOverlay,
Icon,
ListItem,
} from '@collab-ui/react/';
class SubMenu extends React.Component {
state = {
anchorRef: null,
};
componentDidMount() {
const { children } = this.props;
Iif (!children) {
throw new Error(`[@collab-ui/react] SubMenu: children are required for this component.`);
} else Iif (!this.verifyChildrenElements(['MenuItem', 'SubMenu'])) {
throw new Error(`[@collab-ui/react] SubMenu: children must be SubMenu or MenuItem`);
}
}
verifyChildrenElements = nameArr => {
const { children } = this.props;
let elementCount = 0;
let childrenLength = 0;
React.Children.forEach(children, child => {
childrenLength++;
Eif (child.type && nameArr.includes(child.type.displayName)) {
return elementCount++;
}
});
return elementCount === childrenLength;
};
handleKeyDown = e => {
const { onClick, index } = this.props;
const { handleKeyDown, handleClick } = this.context;
Iif (
e.which === 32
|| e.which === 13
|| e.charCode === 32
|| e.charCode === 13
) {
onClick && onClick(e);
handleClick && handleClick(e, index, this);
e.preventDefault();
} else {
handleKeyDown && handleKeyDown(e, index, this);
}
};
handleClick = e => {
const { handleClick } = this.context;
const { index, onClick } = this.props;
onClick && onClick(e);
handleClick && handleClick(e, index, this);
};
render () {
const {
children,
className,
content,
customNode,
isHeader,
isOpen,
label,
selectedValue,
...props
} = this.props;
const otherProps = omit({...props}, [
'customNode',
'index',
'keepMenuOpen',
'onClick',
]);
return (
<div
className={
'cui-menu-item' +
`${(className && ` ${className}`) || ''}`
}
aria-expanded={isOpen}
aria-haspopup={!!children}
>
<ListItem
active={isOpen}
focusOnLoad
isReadOnly={isHeader}
onClick={this.handleClick}
onKeyDown={this.handleKeyDown}
ref={ref => !this.state.anchorRef && this.setState({anchorRef: ref})}
role='menuitem'
{...otherProps}
>
{
customNode
? customNode
: ([
<div className='cui-menu-item__content' key='content-0'>
{ content || label }
</div>,
<div className='cui-menu-item__selected-value' title={selectedValue} key='content-1'>
{children && selectedValue}
</div>,
<div className='cui-menu-item__arrow' key='content-2'>
{children && <Icon name='arrow-right_16'/>}
</div>
])
}
</ListItem>
{
isOpen &&
<EventOverlay
anchorNode={this.state.anchorRef}
isOpen={isOpen}
direction='right-top'
closeOnClick={false}
isContained
>
<div
aria-label={label}
role='menu'
className='cui-menu-item-container'
>
{children}
</div>
</EventOverlay>
}
</div>
);
}
}
SubMenu.propTypes = {
/** @prop Children nodes to render inside SubMenu | null */
children: PropTypes.node,
/** @prop Optional CSS class names | '' */
className: PropTypes.string,
/** @prop SubMenu content element | null */
content: PropTypes.element,
/** @prop SubMenu custom Node | null */
customNode: PropTypes.node,
/** @prop Index of SubMenu | [] */
index: PropTypes.array,
/** @prop Determines if the SubMenu is the header | false */
isHeader: PropTypes.bool,
/** @prop Determines if the SubMenu is Open | false */
isOpen: PropTypes.bool,
/** @prop Boolean whether to keep the SubMenu open | false */
keepMenuOpen: PropTypes.bool,
/** @prop SubMenu label string | '' */
label: PropTypes.string,
/** @prop Callback function invoked when user clicks the SubMenu | null */
onClick: PropTypes.func,
/** @prop Initial selected value within SubMenu | '' */
selectedValue: PropTypes.string,
};
SubMenu.defaultProps = {
children: null,
className: '',
content: null,
customNode: null,
index: [],
isHeader: false,
isOpen: false,
keepMenuOpen: false,
label: '',
onClick: null,
selectedValue: '',
};
SubMenu.contextTypes = {
handleClick: PropTypes.func,
handleKeyDown: PropTypes.func,
};
SubMenu.displayName = 'SubMenu';
export default SubMenu; |