All files / src/Switch Switch.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 267 268 269 270                                                                              3x                                   70x   70x   70x             70x             70x             70x               70x 70x   70x                                                                                                 3x   68x 68x                                             3x                                                 18x   18x   18x               18x                                   17x                                                         3x   18x 18x                            
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 LocalSwitchProps = {
  /** Automatically focus on the switch */
  autoFocus?: boolean;
  checked?: boolean;
  inputProps?: React.InputHTMLAttributes<any>;
  /** Is the switch checked by default? */
  defaultChecked?: boolean;
  /** Disables the switch */
  disabled?: boolean;
  /** Makes the switch required and sets aria-invalid to true */
  isRequired?: boolean;
  /** Switch label */
  label?: string;
  palette?: string;
  name?: string;
  /** State of the switch. Can be any color in the palette. */
  state?: string;
  /** Initial value of the switch */
  value?: boolean | string;
  /** Function to invoke when focus is lost */
  onBlur?: React.FocusEventHandler<HTMLInputElement>;
  /** Function to invoke when switch has changed */
  onChange?: React.FormEventHandler<HTMLInputElement>;
  /** Function to invoke when switch is focused */
  onFocus?: React.FocusEventHandler<HTMLInputElement>;
};
export type SwitchProps = BoxProps & LocalSwitchProps;
 
const useProps = createHook<SwitchProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      autoFocus,
      checked,
      inputProps,
      defaultChecked,
      disabled,
      isRequired,
      label,
      name,
      onBlur,
      onChange,
      onFocus,
      overrides,
      state,
      value,
      ...restProps
    } = props;
 
    const boxProps = Box.useProps({ ...restProps, overrides });
 
    const className = useClassName({
      style: styles.Switch,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
    const switchIconClassName = useClassName({
      style: styles.SwitchIcon,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Icon'
    });
    const hiddenSwitchClassName = useClassName({
      style: styles.HiddenSwitch,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'HiddenInput'
    });
    const switchLabelClassName = useClassName({
      style: styles.SwitchLabel,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Label'
    });
 
    const labelId = useUniqueId('label');
    const switchId = useUniqueId('switch');
 
    return {
      ...boxProps,
      'aria-describedby': labelId,
      'aria-invalid': state === 'danger',
      'aria-required': isRequired,
      className,
      children: (
        <React.Fragment>
          {/**
            // @ts-ignore */}
          <Box
            use="input"
            className={hiddenSwitchClassName}
            // @ts-ignore
            autoFocus={autoFocus}
            checked={checked}
            defaultChecked={defaultChecked}
            disabled={disabled}
            id={switchId}
            onBlur={onBlur}
            onChange={onChange}
            onFocus={onFocus}
            overrides={overrides}
            name={name}
            type="checkbox"
            // @ts-ignore
            value={value}
            {...inputProps}
          />
          <Box className={switchIconClassName} overrides={overrides} />
          {label && (
            <Label
              use="span"
              id={labelId}
              className={switchLabelClassName}
              htmlFor={switchId}
              overrides={overrides}
              marginLeft="minor-2"
            >
              {label}
            </Label>
          )}
        </React.Fragment>
      )
    };
  },
  { themeKey: 'Switch' }
);
 
export const Switch = createComponent<SwitchProps>(
  props => {
    const textProps = useProps(props);
    return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: textProps });
  },
  {
    attach: {
      useProps
    },
    defaultProps: {
      use: Label
    },
    themeKey: 'Switch'
  }
);
 
////////////////////////////////////////////////////////////////
 
export type LocalSwitchFieldProps = {
  /** Label for the switch */
  switchLabel?: string;
  /** Additional props for the Switch component */
  switchProps?: SwitchProps;
};
export type SwitchFieldProps = BoxProps & FieldWrapperProps & SwitchProps & LocalSwitchFieldProps;
 
const useSwitchFieldProps = createHook<SwitchFieldProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      autoFocus,
      checked,
      switchLabel,
      switchProps,
      description,
      defaultChecked,
      disabled,
      hint,
      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.SwitchField,
      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 }) => (
            <Switch
              autoFocus={autoFocus}
              checked={checked}
              defaultChecked={defaultChecked}
              disabled={disabled}
              isRequired={isRequired}
              label={switchLabel}
              name={name}
              onBlur={onBlur}
              onChange={onChange}
              onFocus={onFocus}
              overrides={overrides}
              state={state}
              value={value}
              {..._omit(elementProps, 'id')}
              {...switchProps}
              inputProps={{
                id: elementProps.id,
                ...switchProps.inputProps
              }}
            />
          )}
        </FieldWrapper>
      )
    };
  },
  { defaultProps: { switchProps: {} }, themeKey: 'SwitchField' }
);
 
export const SwitchField = createComponent<SwitchFieldProps>(
  props => {
    const switchFieldProps = useSwitchFieldProps(props);
    return createElement({
      children: props.children,
      component: ReakitBox,
      use: props.use,
      htmlProps: switchFieldProps
    });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'SwitchField'
  }
);