All files / elements/forms/CheckboxField styled.js

100% Statements 38/38
100% Branches 17/17
80% Functions 8/10
100% Lines 35/35

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                          8x                     28x 28x 15x   13x 13x         13x 13x                 13x             8x                             8x                   33x   8x 28x                           28x 28x 2x     2x   26x 78x 26x 78x 78x 33x   78x                               26x                                   8x                             28x 28x       28x                     28x 28x                           8x                                                                                         8x                                       8x        
import React from 'react';
import PropTypes from 'prop-types';
import {
  ControlGroup,
  ControlLabel,
  getAssistiveText,
  PlainText,
} from 'src/elements/forms/utils';
import { Flex } from 'src/elements/grid';
import { removeSomeProps } from 'src/utils/componentHelpers';
import { defaultControlStyles, refType } from 'src/utils/styledHelpers';
import { CheckboxInput } from './CheckboxInput';
 
const renderControlGroupLabel = propsFromControlGroup => {
  const {
    activeColor,
    bg,
    controlId,
    controlLabelProps,
    disabled,
    labelText,
    plainText,
    isRequired,
    validationError,
  } = propsFromControlGroup;
  if (!labelText) {
    return null;
  }
  const labelCaption = !isRequired ? labelText : `${labelText}*`;
  const defaultPositionProps = {
    height: 'auto',
    position: 'relative',
    top: 'auto',
  };
  const positionProps = !plainText ? defaultPositionProps : '';
  const labelProps = {
    activeColor,
    bg,
    ...controlLabelProps,
    disabled,
    htmlFor: controlId,
    validationError,
    ...positionProps,
  };
  return (
    <ControlLabel {...labelProps}>
      {labelCaption}
    </ControlLabel>
  );
};
 
const propsToTrim = [
  'activeColor',
  'assistiveText',
  'assistiveTextProps',
  'bg',
  'controlGroupProps',
  'controlId',
  'controlLabelProps',
  'isHidden',
  'isRequired',
  'labelText',
  'plainText',
  'validationError',
];
 
const plainTextPropsToTrim = [
  'flexDirection',
  'isHidden',
  'name',
  'onChange',
  'options',
  'wrapperProps',
  ...propsToTrim,
];
 
const isChecked = values => values && values.filter(val => !val).length === 0;
 
const renderValueOrComponent = propsFromComponent => {
  const { name, options } = propsFromComponent;
 
  const {
    controlId,
    disabled,
    flexDirection,
    hasSelectAll,
    inputRef,
    enableAutoChecking,
    onChange = () => {},
    onChangeSelectAll = () => {},
    plainText,
    optionSelectAll = { label: 'Select All', value: true },
    values,
  } = propsFromComponent;
  if (plainText) {
    const plainTextProps = {
      ...removeSomeProps(propsFromComponent, plainTextPropsToTrim),
    };
    return (<PlainText {...plainTextProps} />);
  }
  const childProps = { id: controlId, ...removeSomeProps(propsFromComponent, propsToTrim) };
  const optionValues = options.map(option => option.value);
  const checkboxFields = options.map((option, idx) => {
    const checkedProp = {};
    if (enableAutoChecking) {
      checkedProp.checked = values[idx];
    }
    return (
      <CheckboxInput
        {...childProps}
        {...checkedProp}
        // eslint-disable-next-line react/no-array-index-key
        key={`${name}[${idx}]`}
        disabled={disabled}
        flexDirection={flexDirection}
        inputRef={inputRef}
        name={`${name}[${idx}]`}
        onChange={onChange}
        option={option}
        value={optionValues[idx]}
      />
    );
  });
  return (
    <Flex flexDirection="column">
      {hasSelectAll
        && (
          <CheckboxInput
            checked={isChecked(values)}
            inputRef={inputRef}
            name={`${name}_selectAll`}
            onChange={onChangeSelectAll}
            option={optionSelectAll}
            value={optionSelectAll.value}
          />
        )}
      {checkboxFields}
    </Flex>
  );
};
 
const CheckboxField = props => {
  const {
    activeColor,
    assistiveText,
    assistiveTextProps,
    bg,
    controlGroupProps,
    controlId,
    controlLabelProps,
    disabled,
    isHidden,
    isRequired,
    labelText,
    plainText,
    validationError,
  } = props;
  const additionalControlGroupProps = {
    ...controlGroupProps,
    controlId,
  };
  const labelProps = {
    activeColor,
    bg,
    controlId,
    controlLabelProps,
    disabled,
    isRequired,
    labelText,
    plainText,
    validationError,
  };
  const assistiveProps = { assistiveText, isRequired };
  return (
    <ControlGroup
      {...additionalControlGroupProps}
      assistiveText={getAssistiveText(assistiveProps)}
      assistiveTextProps={assistiveTextProps}
      isHidden={isHidden}
      validationError={validationError}
    >
      {renderControlGroupLabel(labelProps)}
      {renderValueOrComponent(props)}
    </ControlGroup>
  );
};
 
CheckboxField.propTypes = {
  /** Defines the color for the label and border color when the focus is in the control. */
  activeColor: PropTypes.string,
  /** Provides helper text for the control.  When provided with no `assistiveText`, `isRequired` will add a default '*Required` helper text.  When provided with `validationError`, `assistiveText`'s value will not be displayed on the UI. */
  assistiveText: PropTypes.oneOfType([
    PropTypes.object,
    PropTypes.string,
  ]),
  /** Allows for custom props to be passed down to the `AsssistiveText` component. */
  assistiveTextProps: PropTypes.object,
  bg: PropTypes.string,
  /** Allows for custom props to be passed down to the `ControlGroup` component. */
  controlGroupProps: PropTypes.object,
  /** Will pass its value to the control's `id` as well as the label's `htmlFor`.
   * @deprecated Do not use! Use `name` instead!
  */
  controlId: PropTypes.string,
  /** Allows for custom props to be passed down to the `ControlLabel` component. */
  controlLabelProps: PropTypes.object,
  /** Basic HTML attribute, needed for styling. */
  disabled: PropTypes.bool,
  flexDirection: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.array,
  ]),
  fontFamily: PropTypes.string,
  /** Allows for a ref to be defined to the DOM input. */
  inputRef: refType,
  /** Hides component */
  isHidden: PropTypes.bool,
  /** This will add an asterisk (*) to the `labelText` and provided `assistiveText` if none is provided. */
  isRequired: PropTypes.bool,
  /** The string value displayed on top of the control in the `ControlLabel` component. */
  labelText: PropTypes.string,
  /** Used to render a dropdown control as a `PlainText` element. */
  plainText: PropTypes.bool,
  /** Error text that will appear below the control when validation fires. */
  validationError: PropTypes.oneOfType([
    PropTypes.bool,
    PropTypes.string,
  ]),
  /** Allows for custom props to be passed down to the `CheckboxFieldWrapper` component. */
  wrapperProps: PropTypes.object,
};
 
CheckboxField.defaultProps = {
  activeColor: defaultControlStyles.activeColor,
  assistiveText: '',
  assistiveTextProps: {},
  bg: defaultControlStyles.bg,
  controlGroupProps: {},
  controlId: '',
  controlLabelProps: {},
  disabled: false,
  flexDirection: 'column',
  fontFamily: 'base',
  inputRef: () => {},
  isHidden: false,
  isRequired: false,
  labelText: '',
  plainText: false,
  validationError: '',
  wrapperProps: {},
};
 
CheckboxField.Input = CheckboxInput;
 
/** @component */
export { CheckboxField };