All files / button-group button-group.js

95% Statements 19/20
90.48% Branches 19/21
85.71% Functions 6/7
95% Lines 19/20

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                                      109x 25x     84x 78x     6x         37x 37x   37x                 109x       109x         19x 1x     18x 1x     18x 18x                                 24x   24x         3x            
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
 
import * as React from 'react';
 
import {KIND, SIZE, SHAPE} from '../button/index.js';
import {getOverrides} from '../helpers/overrides.js';
import {LocaleContext} from '../locale/index.js';
 
import {StyledRoot} from './styled-components.js';
import type {PropsT} from './types.js';
import type {ButtonGroupLocaleT} from './locale.js';
 
function isSelected(selected, index) {
  if (!Array.isArray(selected) && typeof selected !== 'number') {
    return false;
  }
 
  if (Array.isArray(selected)) {
    return selected.includes(index);
  }
 
  return selected === index;
}
 
type LocaleT = {|locale?: ButtonGroupLocaleT|};
export function ButtonGroupRoot(props: {|...PropsT, ...LocaleT|}) {
  const {overrides = {}} = props;
  const [Root, rootProps] = getOverrides(overrides.Root, StyledRoot);
 
  return (
    <Root
      aria-label={
        props.ariaLabel || (props.locale ? props.locale.ariaLabel : '')
      }
      data-baseweb="button-group"
      {...rootProps}
    >
      {React.Children.map(props.children, (child, index) => {
        Iif (!React.isValidElement(child)) {
          return null;
        }
 
        return React.cloneElement(child, {
          disabled: props.disabled || child.props.disabled,
          isSelected: isSelected(props.selected, index),
          kind: KIND.tertiary,
          onClick: event => {
            if (props.disabled) {
              return;
            }
 
            if (child.props.onClick) {
              child.props.onClick(event);
            }
 
            Eif (props.onClick) {
              props.onClick(event, index);
            }
          },
          shape: props.shape,
          size: props.size,
        });
      })}
    </Root>
  );
}
 
// The wrapper component below was created to continue to support enzyme tests for the ButtonGroup
// component. Enzyme at the moment does not support React context @ 16.3. To get around the limitation
// in enzyme, we create a wrapper around the core ButtonGroup and pass context as a prop. In our tests,
// only ButtonGroupRoot will be tested.
// https://github.com/airbnb/enzyme/issues/1908#issuecomment-439747826
export default function ButtonGroup(props: PropsT) {
  return (
    <LocaleContext.Consumer>
      {locale => <ButtonGroupRoot {...props} locale={locale.buttongroup} />}
    </LocaleContext.Consumer>
  );
}
 
ButtonGroup.defaultProps = {
  disabled: false,
  onClick: () => {},
  shape: SHAPE.default,
  size: SIZE.default,
};