# All files / components Tooltip.tsx

19.05% Statements 12/63
0% Branches 0/14
0% Functions 0/8
21.82% Lines 12/55

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
    1841x
    1x
    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
     import * as React from 'react'
    import {FunctionComponent, useMemo} from 'react'
    import {createPortal} from 'react-dom'

    import {TooltipData, Config} from '../types'
    import {useTooltipElement} from '../utils/useTooltipElement'
    import {TOOLTIP_MAXIMUM_OPACITY, TOOLTIP_MINIMUM_OPACITY} from '../constants'

    import {generateTooltipStyles, TooltipDotsStyles} from './TooltipStyles'

    interface Props {
      data: TooltipData
      config: Config
    }

    export const Tooltip: FunctionComponent<Props> = ({data, config}) => {
      const tooltipElement = useTooltipElement('giraffe-tooltip-container')

      const {
        width,
        height,
        legendFont: font,
        legendFontColor: fontColor,
        legendFontBrightColor: fontBrightColor,
        legendBackgroundColor: backgroundColor,
        legendBorder: border,
        legendColumns: columnsWhitelist,
        legendOpacity,
        legendOrientationThreshold: orientationThreshold,
        legendColorizeRows: colorizeRows,
        legendDisable: disableTooltip,
      } = config

      const tooltipOpacity = useMemo(() => {
        if (
          legendOpacity >= TOOLTIP_MINIMUM_OPACITY &&
          legendOpacity <= TOOLTIP_MAXIMUM_OPACITY
        ) {
          return legendOpacity
        }
        return TOOLTIP_MAXIMUM_OPACITY
      }, [legendOpacity])

      let columns = []
      if (disableTooltip !== true) {
        // when legendColumns wasn't defined in the config.
        if (!Array.isArray(columnsWhitelist)) {
          columns = data
        }
        // when legendColumns = [] in the config
        else if (columnsWhitelist.length == 0) {
          return null
        }
        // when legendColumns is an array of requested columns
        else {
          columns = data.filter(column => columnsWhitelist.includes(column.key))
        }
      } else {
        return null
      }

      const switchToVertical = columns.length > orientationThreshold

      // 'switchToVertical': true
      //   each column of data displays vertically, and
      //   additional columns are next to the previous column, therefore,
      //   the limit is the horizontal space (width)
      // 'switchToVertical': false
      //   each column of data displays horizontally, and
      //   additional columns are stacked below the previous column, therefore,
      //   the limit is the vertical space (height)
      const maxLength = switchToVertical ? width : height

      const styles = generateTooltipStyles(
        columns,
        switchToVertical,
        colorizeRows,
        fontColor,
        fontBrightColor
      )

      return createPortal(
        <div
          className="giraffe-tooltip"
          style={{
            opacity: tooltipOpacity,
            border,
            font,
            backgroundColor,
            color: fontBrightColor,
            borderRadius: '3px',
            padding: '10px',
            cursor: 'crosshair',
          }}
          data-testid="giraffe-tooltip"
        >
          <div
            className="giraffe-tooltip-table"
            style={styles.table}
            data-testid="giraffe-tooltip-table"
          >
            {!colorizeRows && <TooltipDotColumn styles={styles.dots} />}
            {columns.map(({name, values}, i) => (
              <TooltipColumn
                key={name}
                name={name}
                maxLength={maxLength}
                values={values}
                columnStyle={styles.columns[i]}
                columnHeaderStyle={styles.headers}
                columnValueStyles={styles.values[i]}
              />
            ))}
          </div>
        </div>,
        tooltipElement
      )
    }

    Tooltip.displayName = 'Tooltip'

    interface TooltipColumnProps {
      name: string
      maxLength: number
      values: string[]
      columnStyle: React.CSSProperties
      columnHeaderStyle: React.CSSProperties
      columnValueStyles: React.CSSProperties[]
    }

    const TooltipColumn: FunctionComponent<TooltipColumnProps> = ({
      name,
      maxLength,
      values,
      columnStyle,
      columnHeaderStyle,
      columnValueStyles,
    }) => {
      const valuesLimitedByPlotDimensions = values.slice(0, maxLength)
      return (
        <div className="giraffe-tooltip-column" style={columnStyle}>
          <div className="giraffe-tooltip-column-header" style={columnHeaderStyle}>
            {name}
          </div>
          {valuesLimitedByPlotDimensions.map((value, i) => (
            <div
              className="giraffe-tooltip-column-value"
              key={i}
              style={columnValueStyles[i]}
            >
              {String(value)}
            </div>
          ))}
        </div>
      )
    }

    TooltipColumn.displayName = 'TooltipColumn'

    interface TooltipDotColumnProps {
      styles: TooltipDotsStyles
    }

    const TooltipDotColumn: FunctionComponent<TooltipDotColumnProps> = ({
      styles,
    }) => {
      const {column, header, value, dots} = styles

      return (
        <div className="giraffe-tooltip-column" style={column}>
          <div className="giraffe-tooltip-column-header" style={header}>
            &nbsp;
          </div>
          {dots.map((dot, i) => (
            <div className="giraffe-tooltip-column-value" key={i} style={value}>
              <div className="giraffe-tooltip-column-dot" style={dot} />
            </div>
          ))}
        </div>
      )
    }

    TooltipDotColumn.displayName = 'TooltipDotColumn'