-
-
Notifications
You must be signed in to change notification settings - Fork 921
/
MenuWrapper.js
96 lines (88 loc) 路 2.2 KB
/
MenuWrapper.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
// @flow
import * as React from 'react';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import _ from 'lodash';
import { addStyle, getWidth } from 'dom-lib';
import { defaultProps } from '../utils';
import bindElementResize, { unbind as unbindElementResize } from 'element-resize-event';
const omitProps = [
'placement',
'shouldUpdatePosition',
'arrowOffsetLeft',
'arrowOffsetTop',
'positionLeft',
'positionTop',
'getPositionInstance',
'getToggleInstance',
'autoWidth'
];
const resizePlacement = [
'topStart',
'topEnd',
'leftEnd',
'rightEnd',
'auto',
'autoVerticalStart',
'autoVerticalEnd',
'autoHorizontalEnd'
];
type Props = {
classPrefix?: string,
className?: string,
getPositionInstance?: () => any,
getToggleInstance?: () => any,
placement?: string,
autoWidth?: boolean
};
class MenuWrapper extends React.Component<Props> {
menuElement = null;
bindMenuRef = ref => {
this.menuElement = ref;
};
componentDidMount() {
const { autoWidth } = this.props;
if (resizePlacement.includes(this.props.placement)) {
bindElementResize(this.menuElement, this.handleResize);
}
if (autoWidth) {
this.updateMenuStyle();
}
}
componentWillUnmount() {
if (this.menuElement) {
unbindElementResize(this.menuElement);
}
}
updateMenuStyle() {
const { getToggleInstance } = this.props;
if (this.menuElement && getToggleInstance) {
const instance = getToggleInstance();
if (instance && instance.toggle) {
const width = getWidth(findDOMNode(instance.toggle));
addStyle(this.menuElement, 'min-width', `${width}px`);
}
}
}
handleResize = () => {
const { getPositionInstance } = this.props;
const instance = getPositionInstance ? getPositionInstance() : null;
if (instance) {
instance.updatePosition(true);
}
};
render() {
const { className, classPrefix, ...rest } = this.props;
return (
<div
{..._.omit(rest, omitProps)}
ref={this.bindMenuRef}
className={classNames(classPrefix, className)}
/>
);
}
}
const enhance = defaultProps({
classPrefix: 'picker-menu'
});
export default enhance(MenuWrapper);