All files / radio styled-components.js

72.37% Statements 55/76
56.9% Branches 33/58
100% Functions 11/11
74.65% Lines 53/71

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                    5x 5x 5x     17x 16x 15x       17x   17x 16x 16x 16x   1x                   1x 1x   1x                         17x   17x       17x 16x                   16x   14x   1x   1x       1x         9x   9x                       9x 9x   9x 9x 9x           9x 9x 9x     5x 3x 3x               5x 17x 17x 17x                         5x 17x   17x                         5x 17x   17x                                       5x     9x 9x                 5x                 5x 1x                
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import {styled} from '../styles/index.js';
import type {StylePropsT} from './types.js';
 
const DEFAULT = 0;
const HOVERED = 1;
const ACTIVE = 2;
type State = typeof DEFAULT | typeof HOVERED | typeof ACTIVE;
function getState(props): State {
  if (props.$isActive) return ACTIVE;
  if (props.$isHovered) return HOVERED;
  return DEFAULT;
}
 
function getOuterColor(props) {
  const {colors} = props.$theme;
 
  if (!props.$checked) {
    Iif (props.$disabled) return colors.tickMarkFillDisabled;
    Iif (props.$isError) return colors.tickBorderError;
    return colors.tickBorder;
  } else {
    Iif (props.$isError) {
      switch (getState(props)) {
        case DEFAULT:
          return colors.tickFillErrorSelected;
        case HOVERED:
          return colors.tickFillErrorSelectedHover;
        case ACTIVE:
          return colors.tickFillErrorSelectedHoverActive;
      }
    } else {
      Iif (props.$disabled) return colors.tickFillDisabled;
      switch (getState(props)) {
        case DEFAULT:
          return colors.tickFillSelected;
        case HOVERED:
          return colors.tickFillSelectedHover;
        case ACTIVE:
          return colors.tickFillSelectedHoverActive;
      }
    }
  }
 
  return null;
}
 
function getInnerColor(props) {
  const {colors} = props.$theme;
 
  Iif (props.$disabled) {
    return colors.tickMarkFillDisabled;
  }
 
  if (!props.$checked) {
    Iif (props.$isError) {
      switch (getState(props)) {
        case DEFAULT:
          return colors.tickFillError;
        case HOVERED:
          return colors.tickFillErrorHover;
        case ACTIVE:
          return colors.tickFillErrorHoverActive;
      }
    } else {
      switch (getState(props)) {
        case DEFAULT:
          return colors.tickFill;
        case HOVERED:
          return colors.tickFillHover;
        case ACTIVE:
          return colors.tickFillActive;
      }
    }
  } else {
    return colors.tickMarkFill;
  }
}
 
function getLabelPadding(props) {
  const {$labelPlacement = '', $theme} = props;
  let paddingDirection;
  switch ($labelPlacement) {
    case 'top':
      paddingDirection = 'Bottom';
      break;
    case 'bottom':
      paddingDirection = 'Top';
      break;
    case 'left':
      paddingDirection = 'Right';
      break;
    default:
    case 'right':
      paddingDirection = 'Left';
      break;
  }
  const {sizing} = $theme;
  const {scale300} = sizing;
  return {
    [`padding${paddingDirection}`]: scale300,
  };
}
 
function getLabelColor(props) {
  const {$disabled, $theme} = props;
  const {colors} = $theme;
  return $disabled ? colors.foregroundAlt : colors.foreground;
}
 
export const RadioGroupRoot = styled<StylePropsT>('div', props => {
  const {$disabled, $align} = props;
  return {
    flexDirection: $align === 'horizontal' ? 'row' : 'column',
    display: 'flex',
    alignItems: $align === 'horizontal' ? 'center' : 'flex-start',
    cursor: $disabled ? 'not-allowed' : 'pointer',
  };
});
 
export const Root = styled<StylePropsT>('label', props => {
  const {$disabled, $hasDescription, $labelPlacement, $theme} = props;
  const {sizing} = $theme;
  return ({
    flexDirection:
      $labelPlacement === 'top' || $labelPlacement === 'bottom'
        ? 'column'
        : 'row',
    display: 'flex',
    alignItems: 'center',
    cursor: $disabled ? 'not-allowed' : 'pointer',
    marginTop: sizing.scale200,
    marginBottom: $hasDescription ? null : sizing.scale200,
  }: {});
});
 
export const RadioMarkInner = styled<StylePropsT>('div', props => {
  const {animation, sizing} = props.$theme;
 
  return {
    backgroundColor: getInnerColor(props),
    borderTopLeftRadius: '50%',
    borderTopRightRadius: '50%',
    borderBottomRightRadius: '50%',
    borderBottomLeftRadius: '50%',
    height: props.$checked ? sizing.scale200 : sizing.scale600,
    transitionDuration: animation.timing100,
    transitionTimingFunction: animation.easeOutCurve,
    width: props.$checked ? sizing.scale200 : sizing.scale600,
  };
});
 
export const RadioMarkOuter = styled<StylePropsT>('div', props => {
  const {sizing} = props.$theme;
 
  return ({
    alignItems: 'center',
    backgroundColor: getOuterColor(props),
    borderTopLeftRadius: '50%',
    borderTopRightRadius: '50%',
    borderBottomRightRadius: '50%',
    borderBottomLeftRadius: '50%',
    display: 'flex',
    height: sizing.scale700,
    justifyContent: 'center',
    marginTop: sizing.scale0,
    marginRight: sizing.scale0,
    marginBottom: sizing.scale0,
    marginLeft: sizing.scale0,
    verticalAlign: 'middle',
    width: sizing.scale700,
    flexShrink: 0,
  }: {});
});
 
export const Label = styled<StylePropsT>('div', props => {
  const {
    $theme: {typography},
  } = props;
  return {
    verticalAlign: 'middle',
    ...getLabelPadding(props),
    color: getLabelColor(props),
    ...typography.font450,
  };
});
 
// tricky style for focus event cause display: none doesn't work
export const Input = styled('input', {
  opacity: 0,
  width: 0,
  overflow: 'hidden',
  margin: 0,
  padding: 0,
  position: 'absolute',
});
 
export const Description = styled<StylePropsT>('div', props => {
  return {
    ...props.$theme.typography.font300,
    color: props.$theme.colors.colorSecondary,
    cursor: 'auto',
    marginLeft: props.$theme.sizing.scale900,
    maxWidth: '240px',
  };
});