# All files / components SingleStatLayer.tsx

19.23% Statements 5/26
0% Branches 0/21
0% Functions 0/2
20% Lines 5/25

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

    1x

    1x

    1x

     import React, {FunctionComponent} from 'react'
    import {
      SINGLE_STAT_DEFAULT_STYLE,
      SINGLE_STAT_DEFAULT_TEST_ID,
      SINGLE_STAT_RESIZER_DEFAULT_STYLE,
      SINGLE_STAT_SVG_DEFAULT_ATTRIBUTES,
      SINGLE_STAT_SVG_TEXT_DEFAULT_ATTRIBUTES,
      SINGLE_STAT_SVG_TEXT_DEFAULT_STYLE,
    } from '../constants/singleStatStyles'
    import {SingleStatLayerConfig} from '../types'
    import {formatStatValue} from '../utils/formatStatValue'

    interface Props {
      stat: number
      config: SingleStatLayerConfig
    }

    const getDefaultViewBox = (stat: string): string =>
      `0 0 ${stat.length * 55} 100`

    export const SingleStatLayer: FunctionComponent<Props> = props => {
      const {stat, config} = props
      const {
        backgroundColor = null,
        decimalPlaces,
        prefix,
        resizerStyle = {},
        style = {},
        suffix,
        svgAttributes = {viewBox: ''},
        svgStyle = {},
        svgTextAttributes = {},
        svgTextStyle = {},
        testID = SINGLE_STAT_DEFAULT_TEST_ID,
        textColor,
        textOpacity = 1,
      } = config

      const formattedValue = formatStatValue(stat, {decimalPlaces, prefix, suffix})

      let viewBox = getDefaultViewBox(formattedValue)

      Iif (svgAttributes.viewBox) {
        viewBox =
          typeof svgAttributes.viewBox === 'function'
            ? svgAttributes.viewBox(formattedValue)
            : svgAttributes.viewBox
      }

      return (
        <div
          className="giraffe-layer giraffe-layer-single-stat"
          data-testid={testID}
          style={{
            ...SINGLE_STAT_DEFAULT_STYLE,
            ...style,
            backgroundColor: `${backgroundColor}`,
          }}
        >
          <div
            className="giraffe-single-stat--resizer"
            style={{...SINGLE_STAT_RESIZER_DEFAULT_STYLE, ...resizerStyle}}
          >
            <svg
              {...{
                ...SINGLE_STAT_SVG_DEFAULT_ATTRIBUTES,
                ...svgAttributes,
                viewBox,
              }}
              className="giraffe-single-stat--svg"
              style={{...svgStyle}}
            >
              <text
                {...{
                  ...SINGLE_STAT_SVG_TEXT_DEFAULT_ATTRIBUTES,
                  ...svgTextAttributes,
                  opacity: textOpacity,
                }}
                className="giraffe-single-stat--text"
                style={{
                  ...SINGLE_STAT_SVG_TEXT_DEFAULT_STYLE,
                  ...svgTextStyle,
                  fill: textColor,
                }}
              >
                {formattedValue}
              </text>
            </svg>
          </div>
        </div>
      )
    }