-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathDropdownButton.js
More file actions
212 lines (196 loc) · 5.84 KB
/
DropdownButton.js
File metadata and controls
212 lines (196 loc) · 5.84 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Button from './Button';
import DropdownMenu from './DropdownMenu';
import { registerStyle, isElInChildren, offset } from './util';
export default class DropdownButton extends Component {
constructor() {
super();
this.state = { opened: false };
registerStyle('no-hover-popup', [
[
'.slds-dropdown-trigger:hover .slds-dropdown--menu.react-slds-no-hover-popup',
'{ visibility: hidden; opacity: 0; }',
],
[
'.slds-dropdown-trigger.react-slds-dropdown-opened .slds-dropdown--menu',
'{ visibility: visible !important; opacity: 1 !important; }',
],
]);
}
onBlur() {
setTimeout(() => {
if (!this.isFocusedInComponent()) {
this.setState({ opened: false });
if (this.props.onBlur) {
this.props.onBlur();
}
}
}, 10);
}
onKeyDown(e) {
if (e.keyCode === 40) { // down
e.preventDefault();
e.stopPropagation();
if (!this.state.opened) {
this.setState({ opened: true });
if (this.props.onClick) {
this.props.onClick(e);
}
setTimeout(() => {
this.focusToTargetItemEl();
}, 20);
} else {
this.focusToTargetItemEl();
}
} else if (e.keyCode === 27) { // ESC
e.preventDefault();
e.stopPropagation();
this.setState({ opened: false });
}
}
onTriggerClick(...args) {
if (!this.props.hoverPopup) {
this.setState({ opened: !this.state.opened }, () => {
if (this.state.opened) {
Object.assign(this.dropdown.style, this.getStyles().dropdownOffset);
}
});
}
if (this.props.onClick) {
this.props.onClick(...args);
}
}
onMenuItemClick(...args) {
if (!this.props.hoverPopup) {
setTimeout(() => {
const triggerElem = this.trigger;
if (triggerElem) triggerElem.focus();
this.setState({ opened: false });
}, 10);
}
if (this.props.onMenuItemClick) {
this.props.onMenuItemClick(...args);
}
}
onMenuClose() {
this.trigger.focus();
this.setState({ opened: false });
}
getStyles() {
const triggerOffset = offset(this.trigger);
const dropdownOffset = offset(this.dropdown);
const triggerPadding = 5;
const nubbinHeight = 8;
const top = -1 *
(dropdownOffset.top - triggerOffset.top - this.trigger.offsetHeight - triggerPadding);
return {
dropdownOffset: {
marginTop: `${top + (this.props.nubbinTop ? nubbinHeight : 0)}px`,
},
};
}
isFocusedInComponent() {
return isElInChildren(this.node, document.activeElement);
}
focusToTargetItemEl() {
const dropdownEl = this.dropdown;
const firstItemEl =
dropdownEl.querySelector('.slds-is-selected > .react-slds-menuitem[tabIndex]') ||
dropdownEl.querySelector('.react-slds-menuitem[tabIndex]');
if (firstItemEl) {
firstItemEl.focus();
}
}
renderButton({ grouped, isFirstInGroup, isLastInGroup, ...props }) {
const pprops = props;
delete pprops.onMenuItemClick;
const button = (
<Button
{ ...pprops }
aria-haspopup
buttonRef={node => (this.trigger = node)}
onClick={ this.onTriggerClick.bind(this) }
onKeyDown={ this.onKeyDown.bind(this) }
onBlur={ this.onBlur.bind(this) }
/>
);
if (grouped) {
const noneStyle = { display: 'none' };
return (
<div className='slds-button-group'>
{ isFirstInGroup ? null : <button className='slds-button' style={ noneStyle } /> }
{ button }
{ isLastInGroup ? null : <button className='slds-button' style={ noneStyle } /> }
</div>
);
}
return button;
}
render() {
const {
className, menuAlign = 'left', menuSize, nubbinTop, hoverPopup, menuHeader, type,
label, children, style, menuStyle, ...props
} = this.props;
let { icon } = this.props;
const dropdownClassNames = classnames(
className,
'slds-dropdown-trigger',
{
'slds-button-space-left': !props.grouped,
'react-slds-dropdown-opened': this.state.opened,
}
);
let iconMore = null;
if (!label && !icon) {
icon = 'down';
}
if (label || type === 'icon-more') {
iconMore = 'down';
}
return (
<div className={ dropdownClassNames } style={style} ref={node => (this.node = node)}>
{ this.renderButton({ type, label, icon, iconMore, ...props }) }
{ this.state.opened || hoverPopup ?
<DropdownMenu
align={ menuAlign }
header={ menuHeader }
size={ menuSize }
nubbinTop={ nubbinTop }
hoverPopup={ hoverPopup }
dropdownMenuRef={node => (this.dropdown = node)}
onMenuItemClick={ this.onMenuItemClick.bind(this) }
onMenuClose={ this.onMenuClose.bind(this) }
onBlur={ this.onBlur.bind(this) }
style={ Object.assign(
{ transition: 'none' },
menuStyle) }
>
{ children }
</DropdownMenu> : null }
</div>
);
}
}
DropdownButton.propTypes = {
className: PropTypes.string,
label: PropTypes.node,
type: PropTypes.string,
icon: PropTypes.string,
menuAlign: PropTypes.oneOf(['left', 'center', 'right']),
menuSize: PropTypes.oneOf(['small', 'medium', 'large']),
menuHeader: PropTypes.string,
nubbinTop: PropTypes.bool,
hoverPopup: PropTypes.bool,
onBlur: PropTypes.func,
onClick: PropTypes.func,
onMenuItemClick: PropTypes.func,
grouped: PropTypes.bool,
isFirstInGroup: PropTypes.bool,
isLastInGroup: PropTypes.bool,
children: PropTypes.node,
/* eslint-disable react/forbid-prop-types */
style: PropTypes.object,
menuStyle: PropTypes.object,
};