-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
73 lines (68 loc) · 2.13 KB
/
index.jsx
File metadata and controls
73 lines (68 loc) · 2.13 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
import React from 'react'
import cls from 'classnames'
function toItemObj (o) {
if (typeof o == 'string')
return { label: o }
return o
}
function toItemValue (o) {
if ('value' in o)
return o.value
return o
}
export class Dropdown extends React.Component {
static propTypes = {
items: React.PropTypes.array.isRequired,
right: React.PropTypes.bool,
hint: React.PropTypes.string,
onSelect: React.PropTypes.func,
onClose: React.PropTypes.func
}
render() {
const onSelect = (item, i) => e => {
this.props.onSelect && this.props.onSelect(toItemValue(item), i)
item.onSelect && item.onSelect()
}
return <ul className={cls('dropdown', { open: this.props.isOpen, closed: !this.props.isOpen, right: this.props.right })} onMouseLeave={this.props.onClose}>
{ this.props.items.map((item,i) => {
let itemObj = toItemObj(item)
const onClick = onSelect(item, i)
if (itemObj.Com)
return <itemObj.Com key={i} obj={itemObj} onClick={onClick} />
return <li key={i} onClick={onClick}>{itemObj.label}</li>
}) }
</ul>
}
}
export default class Btn extends React.Component {
static propTypes = {
items: React.PropTypes.array.isRequired,
className: React.PropTypes.string,
right: React.PropTypes.bool,
hint: React.PropTypes.string,
onSelect: React.PropTypes.func,
onClose: React.PropTypes.func
}
constructor(props) {
super(props)
this.state = { isOpen: false }
}
onOpen() {
this.setState({ isOpen: true })
}
onClose() {
this.setState({ isOpen: false })
}
onSelect(v, index) {
this.setState({ isOpen: false })
this.props.onSelect && this.props.onSelect(v, index)
}
render() {
return <span className="dropdown-btn-container">
<a className={cls('dropdown-btn', this.props.className, { right: this.props.right })} onClick={this.onOpen.bind(this)} data-hint={this.props.hint}>
{this.props.children}
</a>
<Dropdown items={this.props.items} right={this.props.right} isOpen={this.state.isOpen} onClose={this.onClose.bind(this)} onSelect={this.onSelect.bind(this)} />
</span>
}
}