-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathChromeDropdown.react.js
122 lines (111 loc) · 3.48 KB
/
ChromeDropdown.react.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
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
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import Popover from 'components/Popover/Popover.react';
import PropTypes from 'lib/PropTypes';
import Position from 'lib/Position';
import React from 'react';
import styles from 'components/ChromeDropdown/ChromeDropdown.scss';
export default class ChromeDropdown extends React.Component {
constructor() {
super();
this.state = {
open: false,
selected: false,
};
this.dropdownRef = React.createRef();
}
componentWillReceiveProps(nextProps) {
this.keyValueMap = {};
nextProps.options.forEach((value) => {
if (value instanceof Object) {
this.keyValueMap[value.key] = value.value;
}
});
if (Object.keys(this.keyValueMap).length === 0) {
this.keyValueMap = null;
}
}
componentWillMount() {
this.styles = this.props.styles || styles;
}
select(value, e) {
e.stopPropagation();
this.setState({
open: false,
selected: true,
}, () => {
this.props.onChange(value);
});
}
render() {
let widthStyle = { width: parseFloat(this.props.width || 140) };
let styles = this.styles;
let color = this.props.color || 'purple';
let label = this.props.value;
if (this.keyValueMap) {
label = this.keyValueMap[label];
}
if (!this.state.selected && this.props.placeholder) {
// If it's time to show placeholder, show placeholder.
label = this.props.placeholder;
}
let content = (
<div className={[styles.current, styles[color]].join(' ')} onClick={() => this.setState({ open: true })}>
<div>{label}</div>
</div>
);
if (this.state.open) {
let position = Position.inWindow(this.dropdownRef.current);
let measuredWidth = parseFloat(this.dropdownRef.current.offsetWidth);
widthStyle = { width: measuredWidth };
content = (
<Popover fixed={true} position={position} onExternalClick={() => this.setState({ open: false })}>
<div style={widthStyle} className={[styles.menu, styles[color], 'chromeDropdown'].join(' ')}>
{this.props.options.map((o) => {
let key = o;
let value = o;
if (o instanceof Object) {
key = o.key;
value = o.value;
}
return <div key={key} onClick={this.select.bind(this, key)}>{value}</div>
})}
</div>
</Popover>
);
}
return (
<div style={widthStyle} className={styles.dropdown} ref={this.dropdownRef} tabIndex={0}>
{content}
</div>
);
}
}
ChromeDropdown.propTypes = {
color: PropTypes.oneOf(['blue', 'purple']).describe(
'Determines the color of the dropdown.'
),
value: PropTypes.string.isRequired.describe(
'The current value of the dropdown.'
),
options: PropTypes.array.isRequired.describe(
'An array of options available in the dropdown. Can be an array of string or array of { key, value }'
),
onChange: PropTypes.func.isRequired.describe(
'A function called when the dropdown is changed.'
),
width: PropTypes.string.describe(
'An optional width override.'
),
placeholder: PropTypes.string.describe(
'Placeholder text used in place of default selection.'
),
styles: PropTypes.object.describe(
'Styles override used to provide dropdown with differnt skin.'
),
};