All files / owid-grapher/grapher/controls/entityPicker EntityPicker.tsx

56.6% Statements 296/523
56.52% Branches 26/46
48.89% Functions 22/45
56.6% Lines 296/523

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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 6771x 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 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 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x                         4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x     4x 4x                             4x 4x 4x   4x 4x 4x 4x 4x 4x 4x 4x   4x     4x 4x 4x 4x 4x 4x 4x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x   90x 90x 90x 90x 90x 90x 90x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x           4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 90x 4x 4x 4x 4x 4x       4x 4x         4x 4x 4x 4x 4x 4x                                   4x 4x     4x 4x                                                                   4x 4x     4x 4x         4x 4x                       4x 4x     4x 4x     4x 4x     4x 4x             4x 4x 90x                                                         4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 10x 10x       10x             10x 4x 4x                             4x 4x               4x 4x 4x 4x     4x 4x                                                                               4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x                                                                                                                                                                                                                                                                                                                    
import React from "react"
import { action, computed, observable, runInAction, reaction } from "mobx"
import { observer } from "mobx-react"
import { Flipper, Flipped } from "react-flip-toolkit"
import { bind } from "decko"
import classnames from "classnames"
import { scaleLinear, ScaleLinear } from "d3-scale"
import Select from "react-select"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faSearch } from "@fortawesome/free-solid-svg-icons/faSearch"
import { faTimes } from "@fortawesome/free-solid-svg-icons/faTimes"
import { FuzzySearch } from "../../controls/FuzzySearch"
import {
    partition,
    scrollIntoViewIfNeeded,
    last,
    max,
    isNumber,
    sortByUndefinedLast,
} from "../../../clientUtils/Util"
import { VerticalScrollContainer } from "../../controls/VerticalScrollContainer"
import { SortIcon } from "../../controls/SortIcon"
import { SortOrder } from "../../../coreTable/CoreTableConstants"
import { getStylesForTargetHeight } from "../../../clientUtils/react-select"
import {
    ColumnTypeMap,
    CoreColumn,
    MissingColumn,
} from "../../../coreTable/CoreTableColumns"
import {
    EntityName,
    OwidTableSlugs,
} from "../../../coreTable/OwidTableConstants"
import { EntityPickerManager } from "./EntityPickerConstants"
import { CoreColumnDef } from "../../../coreTable/CoreColumnDef"
import { OwidTable } from "../../../coreTable/OwidTable"
import { SelectionArray } from "../../selection/SelectionArray"
import { ColumnSlug } from "../../../clientUtils/owidTypes"
 
const toggleSort = (order: SortOrder): SortOrder =>
    order === SortOrder.desc ? SortOrder.asc : SortOrder.desc
 
enum FocusDirection {
    first = "first",
    last = "last",
    up = "up",
    down = "down",
}
 
interface EntityOptionWithMetricValue {
    entityName: EntityName
    plotValue: number | string | undefined
    formattedValue: any
}
 
/** Modulo that wraps negative numbers too */
const mod = (n: number, m: number): number => ((n % m) + m) % m
 
@observer
export class EntityPicker extends React.Component<{
    manager: EntityPickerManager
    isDropdownMenu?: boolean
}> {
    @computed private get analyticsNamespace(): string {
        return this.manager.analyticsNamespace ?? ""
    }
 
    @observable private searchInput?: string
    @observable
    private searchInputRef: React.RefObject<HTMLInputElement> =
        React.createRef()
 
    @observable private focusIndex?: number
    @observable
    private focusRef: React.RefObject<HTMLLabelElement> = React.createRef()
    @observable private scrollFocusedIntoViewOnUpdate = false
 
    @observable private blockOptionHover = false
 
    @observable
    private scrollContainerRef: React.RefObject<HTMLDivElement> =
        React.createRef()
 
    @observable private isOpen = false
 
    @computed private get isDropdownMenu(): boolean {
        return !!this.props.isDropdownMenu
    }
 
    @action.bound private selectEntity(
        name: EntityName,
        checked?: boolean
    ): void {
        this.manager.selection.toggleSelection(name)
        // Clear search input
        this.searchInput = ""
        this.manager.analytics?.logEntityPickerEvent(
            this.analyticsNamespace,
            checked ? "select" : "deselect",
            name
        )
    }
 
    @computed private get manager(): EntityPickerManager {
        return this.props.manager
    }
 
    @computed private get metric(): string | undefined {
        return this.manager.entityPickerMetric
    }
 
    @computed private get sortOrder(): SortOrder {
        // On mobile, only allow sorting by entityName (ascending)
        if (this.isDropdownMenu) return SortOrder.asc
        return this.manager.entityPickerSort ?? SortOrder.asc
    }
 
    @computed private get pickerColumnDefs(): CoreColumnDef[] {
        return this.manager.entityPickerColumnDefs ?? []
    }
 
    @computed private get metricOptions(): { label: string; value: string }[] {
        return this.pickerColumnDefs.map(
            (
                col
            ): {
                label: string
                value: string
            } => {
                return {
                    label: col.name || col.slug,
                    value: col.slug,
                }
            }
        )
    }
 
    private getColumn(slug: ColumnSlug | undefined): CoreColumn | undefined {
        if (slug === undefined) return undefined
        return this.manager.entityPickerTable?.get(slug)
    }
 
    @computed private get activePickerMetricColumn(): CoreColumn | undefined {
        return this.getColumn(this.metric)
    }
 
    @computed private get availableEntitiesForCurrentView(): Set<string> {
        if (!this.grapherTable) return this.selection.availableEntityNameSet
        if (!this.manager.requiredColumnSlugs?.length)
            return this.grapherTable.availableEntityNameSet
        return this.grapherTable.entitiesWith(this.manager.requiredColumnSlugs)
    }
 
    @computed
    private get entitiesWithMetricValue(): EntityOptionWithMetricValue[] {
        const { pickerTable, selection } = this
        const col = this.activePickerMetricColumn
        const entityNames = selection.availableEntityNames.slice().sort()
        return entityNames.map((entityName) => {
            const plotValue =
                col && pickerTable
                    ? (pickerTable.getLatestValueForEntity(
                          entityName,
                          col.slug
                      ) as string | number)
                    : undefined
 
            const formattedValue =
                plotValue !== undefined
                    ? col?.formatValueShortWithAbbreviations(plotValue)
                    : undefined
            return {
                entityName,
                plotValue,
                formattedValue,
            }
        })
    }
 
    @computed private get grapherTable(): OwidTable | undefined {
        return this.manager.grapherTable
    }
 
    @computed private get pickerTable(): OwidTable | undefined {
        return this.manager.entityPickerTable
    }
 
    @computed get selection(): SelectionArray {
        return this.manager.selection
    }
 
    @computed get selectionSet(): Set<string> {
        return new Set(this.selection.selectedEntityNames)
    }
 
    @computed private get fuzzy(): FuzzySearch<EntityOptionWithMetricValue> {
        return new FuzzySearch(
            this.entitiesWithMetricValue,
            OwidTableSlugs.entityName
        )
    }
 
    @computed private get searchResults(): EntityOptionWithMetricValue[] {
        if (this.searchInput) return this.fuzzy.search(this.searchInput)
        const { selectionSet } = this
        // Show the selected up top and in order.
        const [selected, unselected] = partition(
            sortByUndefinedLast(
                this.entitiesWithMetricValue,
                (option) => option.plotValue,
                this.sortOrder
            ),
            (option: EntityOptionWithMetricValue): boolean =>
                selectionSet.has(option.entityName)
        )
        return [...selected, ...unselected]
    }
 
    private normalizeFocusIndex(index: number): number | undefined {
        if (this.searchResults.length === 0) return undefined
        return mod(index, this.searchResults.length)
    }
 
    @computed private get focusedOption(): string | undefined {
        return this.focusIndex !== undefined
            ? this.searchResults[this.focusIndex].entityName
            : undefined
    }
 
    @computed private get showDoneButton(): boolean {
        return this.isDropdownMenu && this.isOpen
    }
 
    @action.bound private focusOptionDirection(
        direction: FocusDirection
    ): void {
        if (direction === FocusDirection.first)
            this.focusIndex = this.normalizeFocusIndex(0)
        else if (direction === FocusDirection.last)
            this.focusIndex = this.normalizeFocusIndex(-1)
        else if (direction === FocusDirection.up) {
            const newIndex =
                this.focusIndex === undefined ? -1 : this.focusIndex - 1
            this.focusIndex = this.normalizeFocusIndex(newIndex)
        } else if (direction === FocusDirection.down) {
            const newIndex =
                this.focusIndex === undefined ? 0 : this.focusIndex + 1
            this.focusIndex = this.normalizeFocusIndex(newIndex)
        } else return // Exit without updating scroll
        this.scrollFocusedIntoViewOnUpdate = true
    }
 
    @action.bound private clearSearchInput(): void {
        if (this.searchInput) this.searchInput = ""
    }
 
    @action.bound private onKeyDown(
        event: React.KeyboardEvent<HTMLDivElement>
    ): void {
        // We want to block hover if a key is pressed.
        // The hover will be unblocked iff the user moves the mouse (relative to the menu).
        this.blockHover()
        switch (event.key) {
            case "Enter":
                if (event.keyCode === 229) {
                    // ignore the keydown event from an Input Method Editor(IME)
                    // ref. https://www.w3.org/TR/uievents/#determine-keydown-keyup-keyCode
                    break
                }
                if (!this.focusedOption) return
                const name = this.focusedOption
                this.selectEntity(name)
                this.clearSearchInput()
                this.manager.analytics?.logEntityPickerEvent(
                    this.analyticsNamespace,
                    "enter",
                    name
                )
                break
            case "ArrowUp":
                this.focusOptionDirection(FocusDirection.up)
                break
            case "ArrowDown":
                this.focusOptionDirection(FocusDirection.down)
                break
            default:
                return
        }
        event.preventDefault()
    }
 
    @bind private focusSearch(): void {
        this.searchInputRef.current?.focus()
    }
 
    @action.bound private onSearchFocus(): void {
        this.isOpen = true
        if (this.focusIndex === undefined)
            this.focusOptionDirection(FocusDirection.first)
    }
 
    @action.bound private onSearchBlur(): void {
        // Do not allow focus on elements inside menu; shift focus back to search input.
        if (
            this.scrollContainerRef.current &&
            this.scrollContainerRef.current.contains(document.activeElement)
        ) {
            this.focusSearch()
            return
        }
        this.isOpen = false
        this.focusIndex = undefined
    }
 
    @action.bound private onHover(index: number): void {
        if (!this.blockOptionHover) this.focusIndex = index
    }
 
    @action.bound private blockHover(): void {
        this.blockOptionHover = true
    }
 
    @action.bound private unblockHover(): void {
        this.blockOptionHover = false
    }
 
    @action.bound private onMenuMouseDown(
        event: React.MouseEvent<HTMLDivElement, MouseEvent>
    ): void {
        event.stopPropagation()
        event.preventDefault()
        this.focusSearch()
    }
 
    @bind private highlightLabel(label: string): string | JSX.Element {
        if (!this.searchInput) return label

        const result = this.fuzzy.single(this.searchInput, label)
        if (!result) return label

        const tokens: { match: boolean; text: string }[] = []
        for (let i = 0; i < result.target.length; i++) {
            const currentToken = last(tokens)
            const match = result.indexes.includes(i)
            const char = result.target[i]
            if (!currentToken || currentToken.match !== match) {
                tokens.push({
                    match,
                    text: char,
                })
            } else currentToken.text += char
        }
        return (
            <span translate="no">
                {tokens.map((token, i) =>
                    token.match ? (
                        <mark key={i}>{token.text}</mark>
                    ) : (
                        <span key={i}>{token.text}</span>
                    )
                )}
            </span>
        )
    }
 
    @computed private get barScale(): ScaleLinear<number, number> {
        const maxValue = max(
            this.entitiesWithMetricValue
                .map((option) => option.plotValue)
                .filter(isNumber)
        )
        return scaleLinear()
            .domain([0, maxValue ?? 1])
            .range([0, 1])
    }
 
    componentDidMount(): void {
        // Whenever the search term changes, shift focus to first option in the list
        reaction(
            () => this.searchInput,
            () => this.focusOptionDirection(FocusDirection.first)
        )
    }
 
    componentDidUpdate(): void {
        if (
            this.focusIndex !== undefined &&
            this.scrollFocusedIntoViewOnUpdate &&
            this.scrollContainerRef.current &&
            this.focusRef.current
        ) {
            scrollIntoViewIfNeeded(
                this.scrollContainerRef.current,
                this.focusRef.current
            )
            runInAction(() => (this.scrollFocusedIntoViewOnUpdate = false))
        }
    }
 
    @action private updateMetric(columnSlug: ColumnSlug): void {
        const col = this.getColumn(columnSlug)

        this.manager.setEntityPicker?.({
            metric: columnSlug,
            sort: this.isColumnTypeNumeric(col)
                ? SortOrder.desc
                : SortOrder.asc,
        })
        this.manager.analytics?.logEntityPickerEvent(
            this.analyticsNamespace,
            "sortBy",
            columnSlug
        )
    }
 
    private isColumnTypeNumeric(col: CoreColumn | undefined): boolean {
        // If the column is currently missing (not loaded yet), assume it is numeric.
        return (
            col === undefined ||
            col.isMissing ||
            col instanceof ColumnTypeMap.Numeric
        )
    }
 
    private get pickerMenu(): JSX.Element | null {
        if (
            this.isDropdownMenu ||
            !this.manager.entityPickerColumnDefs ||
            this.manager.entityPickerColumnDefs.length === 0
        )
            return null
        return (
            <div className="MetricSettings">
                <span className="mainLabel">Sort by</span>
                <Select
                    className="metricDropdown"
                    options={this.metricOptions}
                    value={this.metricOptions.find(
                        (option) => option.value === this.metric
                    )}
                    onChange={(option): void => {
                        if (option) this.updateMetric(option.value)
                    }}
                    menuPlacement="bottom"
                    components={{
                        IndicatorSeparator: null,
                    }}
                    styles={getStylesForTargetHeight(26)}
                    isSearchable={false}
                    isLoading={this.manager.entityPickerTableIsLoading}
                />
                <span
                    className="sort"
                    onClick={(): void => {
                        const sortOrder = toggleSort(this.sortOrder)
                        this.manager.setEntityPicker?.({ sort: sortOrder })
                        this.manager.analytics?.logEntityPickerEvent(
                            this.analyticsNamespace,
                            "sortOrder",
                            sortOrder
                        )
                    }}
                >
                    <SortIcon
                        type={
                            this.isColumnTypeNumeric(
                                this.activePickerMetricColumn
                            )
                                ? "numeric"
                                : "text"
                        }
                        order={this.sortOrder}
                    />
                </span>
            </div>
        )
    }
 
    render(): JSX.Element {
        const { selection } = this
        const entities = this.searchResults
        const selectedEntityNames = selection.selectedEntityNames
        const availableEntities = this.availableEntitiesForCurrentView
 
        const selectedDebugMessage = `${selectedEntityNames.length} selected. ${availableEntities.size} available. ${this.entitiesWithMetricValue.length} options total.`
 
        const entityType = selection.entityType
        return (
            <div className="EntityPicker" onKeyDown={this.onKeyDown}>
                <div className="EntityPickerSearchInput">
                    <input
                        className={classnames("input-field", {
                            "with-done-button": this.showDoneButton,
                        })}
                        type="text"
                        placeholder={`Type to add a ${entityType}...`}
                        value={this.searchInput ?? ""}
                        onChange={(e): string =>
                            (this.searchInput = e.currentTarget.value)
                        }
                        onFocus={this.onSearchFocus}
                        onBlur={this.onSearchBlur}
                        ref={this.searchInputRef}
                        data-track-note={`${this.analyticsNamespace}-picker-search-input`}
                    />
                    <div className="search-icon">
                        <FontAwesomeIcon icon={faSearch} />
                    </div>
                    {this.showDoneButton && (
                        <div className="done">
                            <button>Done</button>
                        </div>
                    )}
                </div>
                {this.pickerMenu}
                <div className="EntityListContainer">
                    {(!this.isDropdownMenu || this.isOpen) && (
                        <div
                            className={classnames("EntityList", {
                                isDropdown: this.isDropdownMenu,
                            })}
                            onMouseDown={this.onMenuMouseDown}
                        >
                            <VerticalScrollContainer
                                scrollingShadows={true}
                                scrollLock={true}
                                className="EntitySearchResults"
                                contentsId={entities
                                    .map((c) => c.entityName)
                                    .join(",")}
                                onMouseMove={this.unblockHover}
                                ref={this.scrollContainerRef}
                            >
                                <Flipper
                                    spring={{
                                        stiffness: 300,
                                        damping: 33,
                                    }}
                                    // We only want to animate when the selection changes, but not on changes due to
                                    // searching
                                    flipKey={selectedEntityNames.join(",")}
                                >
                                    {entities.map((option, index) => (
                                        <PickerOption
                                            key={index}
                                            hasDataForActiveMetric={availableEntities.has(
                                                option.entityName
                                            )}
                                            optionWithMetricValue={option}
                                            highlight={this.highlightLabel}
                                            barScale={this.barScale}
                                            onChange={this.selectEntity}
                                            onHover={(): void =>
                                                this.onHover(index)
                                            }
                                            isSelected={this.selectionSet.has(
                                                option.entityName
                                            )}
                                            isFocused={
                                                this.focusIndex === index
                                            }
                                            innerRef={
                                                this.focusIndex === index
                                                    ? this.focusRef
                                                    : undefined
                                            }
                                        />
                                    ))}
                                </Flipper>
                            </VerticalScrollContainer>
                            <div>
                                <div
                                    title={selectedDebugMessage}
                                    className="ClearSelectionButton"
                                    data-track-note={`${this.analyticsNamespace}-clear-selection`}
                                    onClick={(): void =>
                                        selection.clearSelection()
                                    }
                                >
                                    <FontAwesomeIcon icon={faTimes} /> Clear
                                    selection
                                </div>
                            </div>
                        </div>
                    )}
                </div>
            </div>
        )
    }
}
 
interface PickerOptionProps {
    optionWithMetricValue: EntityOptionWithMetricValue
    highlight: (label: string) => JSX.Element | string
    onChange: (name: string, checked: boolean) => void
    onHover?: () => void
    innerRef?: React.RefObject<HTMLLabelElement>
    isFocused?: boolean
    isSelected?: boolean
    barScale?: ScaleLinear<number, number>
    hasDataForActiveMetric: boolean
}
 
class PickerOption extends React.Component<PickerOptionProps> {
    @bind onClick(event: React.MouseEvent<HTMLLabelElement, MouseEvent>): void {
        event.stopPropagation()
        event.preventDefault()
        this.props.onChange(
            this.props.optionWithMetricValue.entityName,
            !this.props.isSelected
        )
    }
 
    render(): JSX.Element {
        const {
            barScale,
            optionWithMetricValue,
            innerRef,
            isSelected,
            isFocused,
            hasDataForActiveMetric,
            highlight,
        } = this.props
        const { entityName, plotValue, formattedValue } = optionWithMetricValue
        const metricValue = formattedValue === entityName ? "" : formattedValue // If the user has this entity selected, don't show the name twice.
 
        return (
            <Flipped flipId={entityName} translate opacity>
                <label
                    className={classnames(
                        "EntityPickerOption",
                        {
                            selected: isSelected,
                            focused: isFocused,
                        },
                        hasDataForActiveMetric ? undefined : "MissingData"
                    )}
                    onMouseMove={this.props.onHover}
                    onMouseOver={this.props.onHover}
                    onClick={this.onClick}
                    ref={innerRef}
                >
                    <div className="input-container">
                        <input
                            type="checkbox"
                            checked={isSelected}
                            value={entityName}
                            tabIndex={-1}
                            readOnly
                        />
                    </div>
                    <div className="info-container">
                        <div className="labels-container">
                            <div className="name">{highlight(entityName)}</div>
                            {plotValue !== undefined && (
                                <div className="metric">{metricValue}</div>
                            )}
                        </div>
                        {barScale && isNumber(plotValue) ? (
                            <div className="plot">
                                <div
                                    className="bar"
                                    style={{
                                        width: `${barScale(plotValue) * 100}%`,
                                    }}
                                />
                            </div>
                        ) : (
                            <div className="plot"></div>
                        )}
                    </div>
                </label>
            </Flipped>
        )
    }
}