Dropdown.js
2.8 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
import React,{PropTypes} from 'react';
import cx from 'classnames';
import s from './Dropdown.scss';
class Dropdown extends React.Component {
constructor(props) {
super(props);
this.mouseOnFlag=this.mouseOnFlag.bind(this);
this.mouseOutFlag=this.mouseOutFlag.bind(this);
this.triggerCloseMenu=this.triggerCloseMenu.bind(this);
this.showMenu=this.showMenu.bind(this);
this.state={
showMenu:false,
showMenuItem:false,
closeFlag:false,
showItemKey:''
}
}
static propTypes = {
resource: PropTypes.object.isRequired,
children: PropTypes.element,
showMenu:PropTypes.bool.isRequired,
options:PropTypes.array.isRequired,
menuWidth:PropTypes.number.isRequired,
detailWidth:PropTypes.number.isRequired,
detailHeight:PropTypes.number.isRequired,
onChange:PropTypes.func.isRequired
}
static defaultProps={
showMenu:false,
menuWidth:160,
detailWidth:300,
detailHeight:320
}
mouseOutFlag(){
this.setState({
closeFlag:true
})
}
mouseOnFlag(){
this.setState({
closeFlag:false
})
}
overMenu(fixType,e){
this.setState({
showMenuItem:true,
showItemKey:fixType
});
this.props.onChange(fixType);
}
triggerCloseMenu(){
if(this.state.closeFlag){
this.setState({
showMenu:false,
showMenuItem:false
});
}
}
showMenu(){
this.setState({
showMenu:true
});
}
componentDidMount(){
window.addEventListener('mousedown', this.triggerCloseMenu);
}
componentWillUnmount() {
window.removeEventListener('mousedown', this.triggerCloseMenu);
}
menu(){
const {showMenuItem}=this.state;
const {menuWidth,detailWidth,detailHeight}=this.props;
const menuItemDetailStyle={
right:`${menuWidth}px`,
minWidth:`${detailWidth}px`,
minHeight:`${detailHeight}px`,
top:`-${detailHeight/2}px`
};
return (
<div style={menuItemDetailStyle} className={cx(s.menu_item_body_wrap,showMenuItem?s.show:'')}>
{this.props.children}
</div>
)
}
render(){
const self=this;
const {showMenu,showMenuItem,showItemKey}=this.state;
const {resource,options,menuWidth}=this.props;
const menuItemStyle={minWidth:`${menuWidth}px`};
return (
<div className={cx(s.dropdown_wrap)}>
<div className={cx(s.show_btn_wrap)}>
<a href="javascript:;" onClick={this.showMenu}>查看</a>
</div>
<ul style={menuItemStyle} className={cx(s.dropdown_menu,showMenu?s.show:'')} onMouseOver={this.mouseOnFlag} onMouseOut={this.mouseOutFlag}>
{options.map((fixType,_index)=>{
return(
<li key = {_index}>
<a className={cx(s.menu_item_wrap)} href="javascript:;" onMouseOver={self.overMenu.bind(self,fixType)}>{resource[fixType]}</a>
{showMenuItem&&showItemKey==fixType?this.menu():''}
</li>
)
})}
</ul>
</div>
)
}
}
export default Dropdown;