All files / src/Checkbox CheckboxGroup.tsx

66.67% Statements 16/24
83.33% Branches 10/12
50% Functions 3/6
66.67% Lines 16/24

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                                                              1x                           16x   16x   16x               16x 16x 1x     16x 16x       16x             32x                                         1x   15x 15x                                   1x                                                                                                                                                 1x                                        
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
 
import { useClassName, createComponent, createElement, createHook } from '../utils';
import { Box, BoxProps } from '../Box';
import { FieldWrapper, FieldWrapperProps } from '../FieldWrapper';
import { Set, SetProps } from '../Set';
 
import { Checkbox, CheckboxProps } from './Checkbox';
import * as styles from './styles';
 
export type LocalCheckboxGroupProps = {
  /** Default value(s) of the checkbox group */
  defaultValue?: Array<string> | string;
  /** Disables the checkbox group */
  disabled?: boolean;
  name: string;
  /** Checkbox group options */
  options: Array<CheckboxProps>;
  /** Are the checkbox inputs layed out horizontally or vertically? */
  orientation?: 'vertical' | 'horizontal';
  spacing?: SetProps['spacing'];
  /** State of the checkbox group. Can be any color in the palette. */
  state?: string;
  /** Controlled value of the checkbox group */
  value?: Array<string> | string;
  /** Function to invoke when checkbox group has changed */
  onChange?: React.FormEventHandler<HTMLInputElement>;
};
export type CheckboxGroupProps = BoxProps & LocalCheckboxGroupProps;
 
const useProps = createHook<CheckboxGroupProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      defaultValue: initialDefaultValue,
      disabled,
      onChange,
      options,
      orientation,
      overrides,
      name,
      spacing,
      state,
      value: initialValue,
      ...restProps
    } = props;
 
    const boxProps = Box.useProps(restProps);
 
    const className = useClassName({
      style: styles.CheckboxGroup,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
 
    let defaultValue = initialDefaultValue;
    if (typeof initialDefaultValue === 'string') {
      defaultValue = [initialDefaultValue];
    }
 
    let value = initialValue;
    Iif (typeof initialValue === 'string') {
      value = [initialValue];
    }
 
    return {
      role: 'CheckboxGroup',
      ...boxProps,
      className,
      children: (
        <Set orientation={orientation} spacing={spacing}>
          {options.map((option, i) => (
            <Checkbox
              key={i}
              {...option}
              // @ts-ignore
              checked={value ? value.includes(option.value) : undefined}
              // @ts-ignore
              defaultChecked={defaultValue ? defaultValue.includes(option.value) : undefined}
              name={name}
              onChange={onChange}
              overrides={overrides}
              state={state || option.state}
              disabled={disabled || option.disabled}
            />
          ))}
        </Set>
      )
    };
  },
  { defaultProps: { orientation: 'vertical', spacing: 'minor-2' }, themeKey: 'CheckboxGroup' }
);
 
export const CheckboxGroup = createComponent<CheckboxGroupProps>(
  props => {
    const textProps = useProps(props);
    return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: textProps });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'CheckboxGroup'
  }
);
 
////////////////////////////////////////////////////////////////
 
export type LocalCheckboxGroupFieldProps = {
  /** Additional props for the CheckboxGroup component */
  checkboxGroupProps?: CheckboxGroupProps;
};
export type CheckboxGroupFieldProps = BoxProps & FieldWrapperProps & CheckboxGroupProps & LocalCheckboxGroupFieldProps;
 
const useCheckboxGroupFieldProps = createHook<CheckboxGroupFieldProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      defaultChecked,
      description,
      disabled,
      hint,
      isOptional,
      isRequired,
      label,
      name,
      options,
      orientation,
      onChange,
      overrides,
      checkboxGroupProps,
      state,
      tooltip,
      tooltipTriggerComponent,
      value,
      validationText,
      ...restProps
    } = props;
 
    const boxProps = Box.useProps(restProps);
 
    const className = useClassName({
      style: styles.CheckboxGroupField,
      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 }) => (
            <CheckboxGroup
              defaultChecked={defaultChecked}
              disabled={disabled}
              orientation={orientation}
              name={name}
              options={options}
              onChange={onChange}
              overrides={overrides}
              state={state}
              value={value}
              {...elementProps}
              {...checkboxGroupProps}
            />
          )}
        </FieldWrapper>
      )
    };
  },
  { themeKey: 'CheckboxGroupField' }
);
 
export const CheckboxGroupField = createComponent<CheckboxGroupFieldProps>(
  props => {
    const checkboxGroupFieldProps = useCheckboxGroupFieldProps(props);
    return createElement({
      children: props.children,
      component: ReakitBox,
      use: props.use,
      htmlProps: checkboxGroupFieldProps
    });
  },
  {
    attach: {
      useProps
    },
    defaultProps: {
      use: 'fieldset'
    },
    themeKey: 'CheckboxGroupField'
  }
);