-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (71 loc) · 2.04 KB
/
Copy pathindex.js
File metadata and controls
74 lines (71 loc) · 2.04 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
import React from 'react';
import PropTypes from 'prop-types';
import Dropdown from '../dropdown';
import classnames from 'classnames';
export default class Select extends React.Component {
static propTypes = {
value: PropTypes.string,
onChange: PropTypes.func.isRequired,
children: PropTypes.node,
id: PropTypes.string,
style: PropTypes.object,
icon: PropTypes.element,
label: PropTypes.node,
className: PropTypes.string,
}
static defaultProps = {
onChange: () => {}
}
static displayName = 'Select';
state = {}
getLabel() {
if (typeof this.props.value !== 'undefined') {
const children = React.Children.toArray(this.props.children);
const selected = children.find((child) => {
return child.props.value === this.props.value;
});
if (selected) {
return selected.props.children;
}
}
}
render() {
return (
<div
className={ classnames('select-wrapper', this.props.className, {
'select-wrapper--empty': !this.state.label && !this.props.label
}) }
>
{ this.props.label && (
<div className="select-label">{ this.props.label }</div>
) }
<Dropdown
label={ this.getLabel() }
isActive={ this.state.isActive }
className="select"
style={ this.props.style }
icon={ this.props.icon }
>
{
React.Children.map(this.props.children, (c) => {
return React.cloneElement(c, {
onClick: (e) => {
if (c.props.onClick) {
c.props.onClick(e);
}
if (this.props.value !== c.props.value) {
this.props.onChange(c.props.value, this.props.id, c.props.children);
}
this.setState({
label: c.props.children,
isActive: false
});
}
});
})
}
</Dropdown>
</div>
);
}
}