All files / radio radiogroup.js

80% Statements 8/10
54.55% Branches 6/11
28.57% Functions 2/7
80% Lines 8/10

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                                                                  6x 6x         6x 6x 6x               6x                       18x       18x                                            
/*
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 {getOverrides} from '../helpers/overrides.js';
 
import {RadioGroupRoot as StyledRadioGroupRoot} from './styled-components.js';
import type {PropsT, DefaultPropsT, StatelessStateT} from './types.js';
 
class StatelessRadioGroup extends React.Component<PropsT, StatelessStateT> {
  static defaultProps: DefaultPropsT = {
    name: '',
    value: '',
    disabled: false,
    autoFocus: false,
    labelPlacement: 'right',
    align: 'vertical',
    isError: false,
    required: false,
    onChange: () => {},
    onMouseEnter: () => {},
    onMouseLeave: () => {},
    onFocus: () => {},
    onBlur: () => {},
    overrides: {},
  };
 
  render() {
    const {overrides = {}} = this.props;
    const [RadioGroupRoot, radioGroupRootProps] = getOverrides(
      overrides.RadioGroupRoot,
      StyledRadioGroupRoot,
    );
 
    Eif (__DEV__) {
      const overrideKeys = Object.keys(overrides);
      Iif (overrideKeys.length && !overrideKeys.includes('RadioGroupRoot')) {
        // eslint-disable-next-line no-console
        console.warn(`All overrides beside 'RadioGroupRoot' will be deprecated in the next major version update.
          Pass other overrides to the 'Radio' children instead.
        `);
      }
    }
 
    return (
      <RadioGroupRoot
        role="radiogroup"
        aria-label={this.props['aria-label']}
        aria-labelledby={this.props['aria-labelledby']}
        $align={this.props.align}
        $disabled={this.props.disabled}
        $isError={this.props.isError}
        $required={this.props.required}
        {...radioGroupRootProps}
      >
        {React.Children.map(this.props.children, child => {
          Iif (!React.isValidElement(child)) {
            return null;
          }
 
          return React.cloneElement(child, {
            autoFocus: this.props.autoFocus,
            checked: this.props.value === child.props.value,
            disabled: this.props.disabled || child.props.disabled,
            isError: this.props.isError,
            labelPlacement: this.props.labelPlacement,
            name: this.props.name,
            onBlur: this.props.onBlur,
            onChange: this.props.onChange,
            onFocus: this.props.onFocus,
            onMouseEnter: this.props.onMouseEnter,
            onMouseLeave: this.props.onMouseLeave,
            // will need to remove overrides pass-through on next major version
            overrides: {...this.props.overrides, ...child.props.overrides},
          });
        })}
      </RadioGroupRoot>
    );
  }
}
 
export default StatelessRadioGroup;