All files / src/Checkbox Checkbox.tsx

100% Statements 22/22
100% Branches 2/2
100% Functions 5/5
100% Lines 22/22

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                                                                              3x                                   72x   72x   72x             72x             72x             72x               72x 72x   72x                                                                                     3x   70x 70x                                             3x                                                   19x   19x   19x               19x                                   18x                                                           3x   19x 19x                            
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
import _omit from 'lodash/omit';
 
import { useClassName, createComponent, createElement, createHook, useUniqueId } from '../utils';
import { Box, BoxProps } from '../Box';
import { Label } from '../Label';
import { FieldWrapper, FieldWrapperProps } from '../FieldWrapper';
 
import * as styles from './styles';
 
export type LocalCheckboxProps = {
  /** Automatically focus on the checkbox */
  autoFocus?: boolean;
  checked?: boolean;
  inputProps?: React.InputHTMLAttributes<any>;
  /** Is the checkbox checked by default? */
  defaultChecked?: boolean;
  /** Disables the checkbox */
  disabled?: boolean;
  indeterminate?: boolean;
  /** Makes the checkbox required and sets aria-invalid to true */
  isRequired?: boolean;
  /** Checkbox label */
  label?: string;
  name?: string;
  /** State of the checkbox. Can be any color in the palette. */
  state?: string;
  /** Initial value of the checkbox */
  value?: boolean | string;
  /** Function to invoke when focus is lost */
  onBlur?: React.FocusEventHandler<HTMLInputElement>;
  /** Function to invoke when checkbox has changed */
  onChange?: React.FormEventHandler<HTMLInputElement>;
  /** Function to invoke when checkbox is focused */
  onFocus?: React.FocusEventHandler<HTMLInputElement>;
};
export type CheckboxProps = BoxProps & LocalCheckboxProps;
 
const useProps = createHook<CheckboxProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      autoFocus,
      checked,
      inputProps,
      defaultChecked,
      disabled,
      indeterminate,
      isRequired,
      label,
      name,
      onBlur,
      onChange,
      onFocus,
      state,
      value,
      ...restProps
    } = props;
 
    const boxProps = Box.useProps(restProps);
 
    const className = useClassName({
      style: styles.Checkbox,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
    const checkboxIconClassName = useClassName({
      style: styles.CheckboxIcon,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Icon'
    });
    const hiddenCheckboxClassName = useClassName({
      style: styles.HiddenCheckbox,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'HiddenInput'
    });
    const checkboxLabelClassName = useClassName({
      style: styles.CheckboxLabel,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Label'
    });
 
    const labelId = useUniqueId('label');
    const checkboxId = useUniqueId('checkbox');
 
    return {
      ...boxProps,
      'aria-describedby': labelId,
      'aria-invalid': state === 'danger',
      'aria-required': isRequired,
      className,
      children: (
        <React.Fragment>
          {/**
            // @ts-ignore */}
          <Box
            use="input"
            className={hiddenCheckboxClassName}
            // @ts-ignore
            autoFocus={autoFocus}
            checked={checked}
            defaultChecked={defaultChecked}
            disabled={disabled}
            id={checkboxId}
            // @ts-ignore
            indeterminate={indeterminate}
            onBlur={onBlur}
            onChange={onChange}
            onFocus={onFocus}
            name={name}
            type="checkbox"
            // @ts-ignore
            value={value}
            {...inputProps}
          />
          <Box className={checkboxIconClassName} />
          {label && (
            <Label use="span" id={labelId} className={checkboxLabelClassName} htmlFor={checkboxId} marginLeft="minor-2">
              {label}
            </Label>
          )}
        </React.Fragment>
      )
    };
  },
  { themeKey: 'Checkbox' }
);
 
export const Checkbox = createComponent<CheckboxProps>(
  props => {
    const textProps = useProps(props);
    return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: textProps });
  },
  {
    attach: {
      useProps
    },
    defaultProps: {
      use: Label
    },
    themeKey: 'Checkbox'
  }
);
 
////////////////////////////////////////////////////////////////
 
export type LocalCheckboxFieldProps = {
  /** Label for the checkbox */
  checkboxLabel?: string;
  /** Additional props for the Checkbox component */
  checkboxProps?: CheckboxProps;
};
export type CheckboxFieldProps = BoxProps & FieldWrapperProps & CheckboxProps & LocalCheckboxFieldProps;
 
const useCheckboxFieldProps = createHook<CheckboxFieldProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      autoFocus,
      checked,
      checkboxLabel,
      checkboxProps,
      description,
      defaultChecked,
      disabled,
      hint,
      indeterminate,
      isOptional,
      isRequired,
      label,
      name,
      onBlur,
      onChange,
      onFocus,
      overrides,
      state,
      tooltip,
      tooltipTriggerComponent,
      validationText,
      value,
      ...restProps
    } = props;
 
    const boxProps = Box.useProps(restProps);
 
    const className = useClassName({
      style: styles.CheckboxField,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
 
    return {
      ...boxProps,
      className,
      children: (
        <FieldWrapper
          description={description}
          hint={hint}
          isOptional={isOptional}
          isRequired={isRequired}
          label={label}
          labelType="legend"
          overrides={overrides}
          state={state}
          tooltip={tooltip}
          tooltipTriggerComponent={tooltipTriggerComponent}
          validationText={validationText}
        >
          {({ elementProps }) => (
            <Checkbox
              autoFocus={autoFocus}
              checked={checked}
              defaultChecked={defaultChecked}
              disabled={disabled}
              indeterminate={indeterminate}
              isRequired={isRequired}
              label={checkboxLabel}
              name={name}
              onBlur={onBlur}
              onChange={onChange}
              onFocus={onFocus}
              overrides={overrides}
              state={state}
              value={value}
              {..._omit(elementProps, 'id')}
              {...checkboxProps}
              inputProps={{
                id: elementProps.id,
                ...checkboxProps.inputProps
              }}
            />
          )}
        </FieldWrapper>
      )
    };
  },
  { defaultProps: { checkboxProps: {} }, themeKey: 'CheckboxField' }
);
 
export const CheckboxField = createComponent<CheckboxFieldProps>(
  props => {
    const checkboxFieldProps = useCheckboxFieldProps(props);
    return createElement({
      children: props.children,
      component: ReakitBox,
      use: props.use,
      htmlProps: checkboxFieldProps
    });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'CheckboxField'
  }
);