-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathSelect.js
143 lines (117 loc) · 3.24 KB
/
Select.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React from 'react';
import styled from 'styled-components';
import enhanceWithClickOutside from 'react-click-outside';
import find from 'lodash/find';
import get from 'lodash/get';
import Icon from 'ui/components/common/Icon';
import { Div } from 'ui/components/common/base';
const OptionsContainer = styled(Div)`
position: absolute;
left: 0;
width: 100%;
z-index: 2147483646;
background: #fff;
max-height: 600px;
overflow-y: auto;
${(props) => `${props.anchorPoint}: 100%;`}
box-shadow: 0 1px 2px 0px rgba(0, 0, 0, 0.33);
`;
const Option = styled(Div)`
padding: ${(props) => props.withPadding ? '5px 5px 5px 32px' : '5px'};
user-select: none;
&:hover {
cursor: pointer;
background-color: #c9dcf4;
}
`;
const Container = styled(Div)`
position: relative;
cursor: pointer;
height: 100%;
padding: 0 6px 0 8px;
background-color: ${(props) => props.isOpen && props.quickEdit ? '#f0f0f0' : 'white'};
`;
const ValueContainer = styled(Div)`
display: flex;
align-items: center;
height: 100%;
user-select: none;
`;
const customIconStyle = {
cursor: 'pointer',
marginRight: 8
};
const dropdownIconStyle = {
cursor: 'pointer',
marginLeft: 4
};
export class Select extends React.Component {
state = {
isOpen: false
};
toggle = () => {
this.setState({ isOpen: !this.state.isOpen });
};
handleClickOutside = () => {
this.setState({ isOpen: false });
};
selectValue = (option) => {
const { onChange } = this.props;
return () => {
onChange && onChange(option.value);
}
};
autoScrollToEnd = (ref) => {
if (ref && this.props.autoScrollToEnd) {
ref.scrollTop = ref.scrollHeight;
}
};
renderOptions = () => {
if (!this.state.isOpen) {
return false;
}
const options = this.props.options.map((option) => {
if (this.props.optionComponent) {
const OptionComponent = this.props.optionComponent;
return <OptionComponent key={ option.value } onClick={ this.selectValue(option) } { ...option }/>;
}
return (
<Option key={ option.value } onClick={ this.selectValue(option) } withPadding={ this.props.valueIcon }>
{ option.label }
</Option>
);
});
return (
<OptionsContainer innerRef={ this.autoScrollToEnd } anchorPoint={ this.props.anchorPoint }>
{ options }
</OptionsContainer>
)
};
renderValue = () => {
const selectedOption = find(this.props.options, { value: this.props.value });
return (
<ValueContainer>
{ this.props.valueIcon && <Icon src={ this.props.valueIcon } style={ customIconStyle }/> }
{
this.props.valueComponent
? <this.props.valueComponent { ...selectedOption }/>
: get(selectedOption, 'label', null)
}
{ this.props.showDropdownIcon && <Icon src="select" style={ dropdownIconStyle }/> }
</ValueContainer>
);
};
render() {
return (
<Container isOpen={ this.state.isOpen } quickEdit={ this.props.quickEdit } onClick={ this.toggle }>
{ this.renderOptions() }
{ this.renderValue() }
</Container>
);
}
}
Select.defaultProps = {
anchorPoint: 'top',
showDropdownIcon: true
};
export default enhanceWithClickOutside(Select);