-
Notifications
You must be signed in to change notification settings - Fork 256
/
Dropdown.tsx
212 lines (181 loc) · 5.26 KB
/
Dropdown.tsx
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Libraries
import React, {Component, CSSProperties} from 'react'
import classnames from 'classnames'
// Components
import {ClickOutside} from 'src/shared/components/ClickOutside'
import DropdownDivider from 'src/reusable_ui/components/dropdowns/DropdownDivider'
import DropdownItem from 'src/reusable_ui/components/dropdowns/DropdownItem'
import DropdownButton from 'src/reusable_ui/components/dropdowns/DropdownButton'
import FancyScrollbar from 'src/shared/components/FancyScrollbar'
// Types
import {
DropdownMenuColors,
ComponentStatus,
ComponentColor,
ComponentSize,
IconFont,
} from 'src/reusable_ui/types'
import {ErrorHandling} from 'src/shared/decorators/errors'
interface Props {
children: JSX.Element[]
onChange: (value: any) => void
selectedID: string
buttonColor?: ComponentColor
buttonSize?: ComponentSize
menuColor?: DropdownMenuColors
status?: ComponentStatus
widthPixels?: number
icon?: IconFont
wrapText?: boolean
customClass?: string
maxMenuHeight?: number
}
interface State {
expanded: boolean
}
@ErrorHandling
class Dropdown extends Component<Props, State> {
public static defaultProps: Partial<Props> = {
buttonColor: ComponentColor.Default,
buttonSize: ComponentSize.Small,
status: ComponentStatus.Default,
widthPixels: 120,
wrapText: false,
maxMenuHeight: 250,
menuColor: DropdownMenuColors.Sapphire,
}
public static Button = DropdownButton
public static Item = DropdownItem
public static Divider = DropdownDivider
constructor(props: Props) {
super(props)
this.state = {
expanded: false,
}
}
public render() {
const width = `${this.props.widthPixels}px`
this.validateChildCount()
return (
<ClickOutside onClickOutside={this.collapseMenu}>
<div className={this.containerClassName} style={{width}}>
{this.button}
{this.menuItems}
</div>
</ClickOutside>
)
}
private toggleMenu = (): void => {
this.setState({expanded: !this.state.expanded})
}
private collapseMenu = (): void => {
this.setState({expanded: false})
}
private get containerClassName(): string {
const {buttonColor, buttonSize, status, wrapText, customClass} = this.props
return classnames(
`dropdown dropdown-${buttonSize} dropdown-${buttonColor}`,
{
disabled: status === ComponentStatus.Disabled,
'dropdown-wrap': wrapText,
[customClass]: customClass,
}
)
}
private get button(): JSX.Element {
const {
selectedID,
status,
buttonColor,
buttonSize,
icon,
children,
} = this.props
const {expanded} = this.state
const selectedChild = children.find(child => child.props.id === selectedID)
return (
<DropdownButton
active={expanded}
color={buttonColor}
size={buttonSize}
icon={icon}
onClick={this.toggleMenu}
status={status}
>
{selectedChild.props.children}
</DropdownButton>
)
}
private get menuItems(): JSX.Element {
const {selectedID, maxMenuHeight, menuColor, children} = this.props
const {expanded} = this.state
if (expanded) {
return (
<div
className={`dropdown--menu-container dropdown--${menuColor}`}
style={this.menuStyle}
>
<FancyScrollbar
autoHide={false}
autoHeight={true}
maxHeight={maxMenuHeight}
>
<div className="dropdown--menu">
{React.Children.map(children, (child: JSX.Element) => {
if (this.childTypeIsValid(child)) {
if (child.type === DropdownItem) {
return (
<DropdownItem
{...child.props}
key={child.props.id}
selected={child.props.id === selectedID}
onClick={this.handleItemClick}
>
{child.props.children}
</DropdownItem>
)
}
return (
<DropdownDivider {...child.props} key={child.props.id} />
)
} else {
throw new Error(
'Expected children of type <Dropdown.Item /> or <Dropdown.Divider />'
)
}
})}
</div>
</FancyScrollbar>
</div>
)
}
return null
}
private get menuStyle(): CSSProperties {
const {wrapText, widthPixels} = this.props
if (wrapText) {
return {
width: `${widthPixels}px`,
}
}
return {
minWidth: `${widthPixels}px`,
}
}
private handleItemClick = (value: any): void => {
const {onChange} = this.props
onChange(value)
this.collapseMenu()
}
private validateChildCount = (): void => {
const {children} = this.props
if (React.Children.count(children) === 0) {
throw new Error(
'Dropdowns require at least 1 child element. We recommend using Dropdown.Item and/or Dropdown.Divider.'
)
}
}
private childTypeIsValid = (child: JSX.Element): boolean =>
child.type === DropdownItem || child.type === DropdownDivider
}
export default Dropdown