All files / src/Radio Radio.tsx

100% Statements 13/13
100% Branches 2/2
100% Functions 2/2
100% Lines 13/13

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                                                                        3x                                   76x   76x   76x             76x             76x             76x               76x 76x   76x                                                                                             3x   75x 75x                        
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
 
import { useClassName, createComponent, createElement, createHook, useUniqueId } from '../utils';
import { Box, BoxProps } from '../Box';
import { Label } from '../Label';
 
import * as styles from './styles';
 
export type LocalRadioProps = {
  /** Automatically focus on the radio */
  autoFocus?: boolean;
  checked?: boolean;
  radioProps?: React.InputHTMLAttributes<any>;
  /** Is the radio checked by default? */
  defaultChecked?: boolean;
  /** Disables the radio */
  disabled?: boolean;
  /** Makes the radio required and sets aria-invalid to true */
  isRequired?: boolean;
  /** Radio label */
  label?: string;
  name?: string;
  /** State of the radio. Can be any color in the palette. */
  state?: string;
  /** Initial value of the radio */
  value?: boolean | string;
  /** Function to invoke when focus is lost */
  onBlur?: React.FocusEventHandler<HTMLInputElement>;
  /** Function to invoke when radio has changed */
  onChange?: React.FormEventHandler<HTMLInputElement>;
  /** Function to invoke when radio is focused */
  onFocus?: React.FocusEventHandler<HTMLInputElement>;
};
export type RadioProps = BoxProps & LocalRadioProps;
 
const useProps = createHook<RadioProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      autoFocus,
      checked,
      defaultChecked,
      disabled,
      isRequired,
      label,
      name,
      onBlur,
      onChange,
      onFocus,
      overrides,
      radioProps,
      state,
      value,
      ...restProps
    } = props;
 
    const boxProps = Box.useProps({ ...restProps, overrides });
 
    const className = useClassName({
      style: styles.Radio,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
    const radioIconClassName = useClassName({
      style: styles.RadioIcon,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Icon'
    });
    const hiddenRadioClassName = useClassName({
      style: styles.HiddenRadio,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'HiddenInput'
    });
    const radioLabelClassName = useClassName({
      style: styles.RadioLabel,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Label'
    });
 
    const labelId = useUniqueId('label');
    const radioId = useUniqueId('radio');
 
    return {
      ...boxProps,
      'aria-describedby': labelId,
      'aria-invalid': state === 'danger',
      'aria-required': isRequired,
      className,
      children: (
        <React.Fragment>
          <Box
            use="input"
            className={hiddenRadioClassName}
            // @ts-ignore
            autoFocus={autoFocus}
            checked={checked}
            defaultChecked={defaultChecked}
            disabled={disabled}
            id={radioId}
            onBlur={onBlur}
            onChange={onChange}
            onFocus={onFocus}
            name={name}
            type="radio"
            // @ts-ignore
            value={value}
            overrides={overrides}
            {...radioProps}
          />
          <Box className={radioIconClassName} overrides={overrides} />
          {label && (
            <Label
              use="span"
              id={labelId}
              className={radioLabelClassName}
              htmlFor={radioId}
              overrides={overrides}
              marginLeft="minor-2"
            >
              {label}
            </Label>
          )}
        </React.Fragment>
      )
    };
  },
  { themeKey: 'Radio' }
);
 
export const Radio = createComponent<RadioProps>(
  props => {
    const textProps = useProps(props);
    return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: textProps });
  },
  {
    attach: {
      useProps
    },
    defaultProps: {
      use: Label
    },
    themeKey: 'Radio'
  }
);