forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExplorerControls.tsx
186 lines (174 loc) · 6.23 KB
/
ExplorerControls.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
import React from "react"
import { observer } from "mobx-react"
import { action, computed } from "mobx"
import Select from "react-select"
import { getStylesForTargetHeight } from "../clientUtils/react-select"
import {
ExplorerControlType,
ExplorerChoiceOption,
ExplorerChoice,
} from "./ExplorerConstants"
import { chunk } from "../clientUtils/Util"
import { GridBoolean } from "../gridLang/GridLangConstants"
import classNames from "classnames"
const AVAILABLE_OPTION_CLASS = "AvailableOption"
const UNAVAILABLE_OPTION_CLASS = "UnavailableOption"
const SELECTED_OPTION_CLASS = "SelectedOption"
const EXPLORER_DROPDOWN_CLASS = "ExplorerDropdown"
const HIDDEN_CONTROL_HEADER_CLASS = "HiddenControlHeader"
export class ExplorerControlBar extends React.Component<{
isMobile?: boolean
showControls?: boolean
closeControls?: () => void
}> {
render() {
const { isMobile, showControls, closeControls } = this.props
const mobileCloseButton = isMobile ? (
<a
className="btn btn-primary mobile-button"
onClick={closeControls}
>
Done
</a>
) : undefined
const showMobileControls = isMobile && showControls
return (
<form
className={classNames(
"ExplorerControlBar",
showMobileControls
? `show-controls-popup`
: isMobile
? `hide-controls-popup`
: false
)}
>
{this.props.children}
{mobileCloseButton}
</form>
)
}
}
@observer
export class ExplorerControlPanel extends React.Component<{
choice: ExplorerChoice
explorerSlug?: string
onChange?: (value: string) => void
isMobile: boolean
}> {
private renderCheckboxOrRadio(option: ExplorerChoiceOption, index: number) {
const { explorerSlug, choice } = this.props
const { title, type, value } = choice
const { available, label, checked } = option
const isCheckbox = type === ExplorerControlType.Checkbox
const onChangeValue = isCheckbox
? checked
? GridBoolean.false
: GridBoolean.true
: option.value
const currentValue = isCheckbox
? checked
? GridBoolean.true
: GridBoolean.false
: value
return (
<div key={index} className="ControlOption">
<label
className={classNames(
{
[SELECTED_OPTION_CLASS]: checked,
},
available
? AVAILABLE_OPTION_CLASS
: UNAVAILABLE_OPTION_CLASS
)}
>
<input
onChange={() => this.customOnChange(onChangeValue)}
type={isCheckbox ? "checkbox" : "radio"}
disabled={!available}
name={title}
checked={checked}
value={currentValue}
data-track-note={`${
explorerSlug ?? "explorer"
}-click-${title.toLowerCase()}`}
/>{" "}
{label}
</label>
</div>
)
}
@computed private get options() {
return this.props.choice.options ?? []
}
private renderDropdown() {
const options = this.options
.filter((option) => option.available)
.map((option) => {
return {
label: option.label,
value: option.value,
}
})
const value = options.find(
(option) => option.value === this.props.choice.value
) ?? { label: "-", value: "-" }
const styles = getStylesForTargetHeight(30)
return (
<Select
className={EXPLORER_DROPDOWN_CLASS}
classNamePrefix={EXPLORER_DROPDOWN_CLASS}
isDisabled={options.length < 2}
// menuPlacement="auto" doesn't work perfectly well on mobile, with fixed position
menuPlacement={this.props.isMobile ? "top" : "auto"}
menuPosition={this.props.isMobile ? "fixed" : "absolute"}
options={options}
value={value}
onChange={(option: any) => this.customOnChange(option.value)}
components={{
IndicatorSeparator: null,
}}
styles={styles}
isSearchable={!this.props.isMobile && options.length > 10}
maxMenuHeight={350}
/>
)
}
@action.bound private customOnChange(value: string) {
if (this.props.onChange) this.props.onChange(value)
}
private renderColumn(
key: string,
hideTitle: boolean,
options?: ExplorerChoiceOption[]
) {
const { title, type, displayTitle } = this.props.choice
return (
<div key={key} className="ExplorerControl">
<div
className={classNames("ControlHeader", {
[HIDDEN_CONTROL_HEADER_CLASS]: hideTitle === true,
})}
>
{displayTitle ?? title}
</div>
{type === ExplorerControlType.Dropdown
? this.renderDropdown()
: (options ?? this.options).map((option, index) =>
this.renderCheckboxOrRadio(option, index)
)}
</div>
)
}
render() {
const { choice } = this.props
const { title, type } = choice
const { options } = this
if (type === ExplorerControlType.Radio && options.length > 4)
return chunk(options, 3).map((optionsGroup, column) =>
this.renderColumn(`${title}${column}`, column > 0, optionsGroup)
)
return this.renderColumn(title, type === ExplorerControlType.Checkbox)
}
}