All files / owid-grapher/explorer ExplorerControls.tsx

92.21% Statements 142/154
52.63% Branches 10/19
80% Functions 4/5
92.21% Lines 142/154

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 213 214 215 216 217 218 219 220 221 222 223 2241x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x             12x 12x 12x 12x 12x 12x 12x   12x     12x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x     18x 18x 18x 18x 18x 18x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x   18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 69x 69x 69x 69x 69x 60x 60x 60x 60x 69x 60x 60x 60x 60x 69x 69x 69x 69x 69x 69x 69x 69x 69x 4x 4x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x                                                                                                                                            
import React from "react"
import { observer } from "mobx-react"
import { action, computed } from "mobx"
import Select, { components, SingleValueProps } 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>
        )
    }
}
 
interface ExplorerDropdownOption {
    label: string
    value: string
}
 
const ExplorerSingleValue = (props: SingleValueProps) => {
    if (props.selectProps.isSearchable && props.selectProps.menuIsOpen)
        return (
            <components.SingleValue {...props}>
                <span style={{ fontStyle: "italic", opacity: 0.75 }}>
                    Type to search&hellip;
                </span>
            </components.SingleValue>
        )
    else return <components.SingleValue {...props} />
}
 
const ExplorerDropdown = (props: {
    options: ExplorerDropdownOption[]
    value: ExplorerDropdownOption
    isMobile: boolean
    onChange: (option: string) => void
}) => {
    const { options, value, isMobile, onChange } = props
    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={isMobile ? "top" : "auto"}
            menuPosition="absolute"
            options={options}
            value={value}
            onChange={(option: ExplorerDropdownOption) =>
                onChange(option.value)
            }
            components={{
                IndicatorSeparator: null,
                SingleValue: ExplorerSingleValue,
            }}
            styles={styles}
            isSearchable={!isMobile && options.length > 10}
            maxMenuHeight={350}
        />
    )
}
 
@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: "-" }
 
        return (
            <ExplorerDropdown
                options={options}
                value={value}
                isMobile={this.props.isMobile}
                onChange={this.customOnChange}
            />
        )
    }
 
    @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)
    }
}