All files / src/form/fields/RadioField RadioField.js

58.62% Statements 17/29
50.76% Branches 33/65
55.55% Functions 5/9
58.62% Lines 17/29

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                                3x 3x       3x 3x 3x       3x 3x                                         8x                                                                         3x             3x 3x                                         8x 8x 8x 8x                                                                                                                     1x 1x              
/**** Libraries ****/
import React, { PureComponent } from 'react';
import { defaultProps } from './props/defaultProps';
import { propTypes } from './props/propTypes';
 
/**** Components ****/
import Icon from '@zohodesk/icons/es/Icon';
import Label from '@zohodesk/components/lib/Label/Label';
import Radio from '@zohodesk/components/lib/Radio/Radio';
import ValidationMessage from '../ValidationMessage/ValidationMessage';
 
/** Css */
import style from '../Fields.module.css';
 
export default class RadioField extends PureComponent {
  constructor(props) {
    super(props);
    this.data = {
      radios: {},
      focus: this.handleFocus
    }
    this.handleChange = this.handleChange.bind(this);
    this.updateData = this.updateData.bind(this);
    this.handleFocus = this.handleFocus.bind(this);
  }
 
  componentDidMount() {
    const { getRef, id } = this.props;
    getRef && getRef(this.data, id);
  }
 
  componentDidUpdate(prevProps) {
    const { getRef, id } = this.props;
    if (getRef !== prevProps.getRef) {
      getRef && getRef(this.data, id);
    }
  }
 
  componentWillUnmount() {
    const { getRef, id } = this.props;
    getRef && getRef(null, id);
  }
 
  handleChange(value) {
    let { id, onChange } = this.props;
    onChange && onChange(id, value);
  }
 
  updateData(ele, val) {
    this.data.radios[val] = ele;
  }
 
  handleFocus() {
    const { selectedValue, options } = this.props;
    const firstRadioValue = options[0].value;
    if (selectedValue) {
      this.data.radios[selectedValue].focus();
    } else {
      this.data.radios[firstRadioValue].focus();
    }
  }
 
  render() {
    let {
      labelName,
      id,
      isMandatory,
      options,
      validationMessage,
      validationPalette,
      errorType,
      isDisabled,
      title,
      labelPalette,
      labelSize,
      size,
      selectedValue,
      isActive,
      dataId,
      dataSelectorId,
      validationRuleMessage,
      validationRulePalette,
      isReadOnly,
      isBoxStyle,
      variant,
      customProps
    } = this.props;
    const {
      LabelProps = {},
      RadioProps = {},
      InfoIconProps = {},
      ValidationMessageProps1 = {},
      ValidationMessageProps2 = {}
    } = customProps;
    return (
      <div
        className={`${style.container} ${isDisabled ? style.disabled : isReadOnly ? style.readonly : ''}`}
        data-title={isDisabled ? title : null}
        data-selector-id={dataSelectorId}
      >
        {labelName && (
          <Label
            text={labelName}
            size='medium'
            id={id}
            palette={isMandatory ? 'mandatory' : isDisabled ? 'primary' : 'default'}
            customClass={`${style.fieldLabel} ${isMandatory ? style.labelMandatory : ''}`}
            dataId={
              isDisabled ? `${dataId}_label_disabled` : isMandatory ? `${dataId}_label_mandatory` : `${dataId}_label`
            }
            {...LabelProps}
          />
        )}
        <div className={`${style.fieldContainer} ${isBoxStyle ? style.radiosWrapper : ''} ${labelName ? style.fieldMargin_medium : ''} ${style.radioContainer}`}>
          {options.map((option, index) => {
            let { text, value, disabled = false, tooltip, infoTooltip } = option;
            let isDisabledState = disabled || isDisabled;
            let isChecked = selectedValue == value;
            return (
              <span key={index} className={`${!isBoxStyle ? style.radio : ''} ${style.radioWrap}`}>
                <Radio
                  id={index}
                  value={value}
                  name={id}
                  text={text}
                  labelPalette={labelPalette}
                  labelSize={labelSize}
                  active={isActive || (isBoxStyle && isChecked)}
                  disabled={isDisabledState}
                  disabledTitle={tooltip}
                  title={tooltip}
                  onChange={this.handleChange}
                  getRef={this.updateData}
                  size={size}
                  checked={isChecked}
                  dataId={dataId}
                  isReadOnly={isReadOnly}
                  variant={variant}
                  {...RadioProps}
                  a11y={{
                    tabIndex: (!!selectedValue ? (isChecked? '0' : '-1') : (index === 0 ? '0' : '-1')),
                    ...RadioProps.a11y
                  }}
                  customClass={{
                    customRadioWrap: isBoxStyle ? `${style.radioBox} ${!isDisabledState ? style.hoverableRadioBox : '' } ${isChecked ? style.radioBoxActive : ''}` : '',
                    ...RadioProps.customClass
                  }}
                >
                  { !!infoTooltip ? <Icon name='ZD-GN-info' size='16' title={infoTooltip} iconClass={style.infoIcon} {...InfoIconProps} /> : null }
                </Radio>
              </span>
            );
          })}
        </div>
        {validationMessage && (
          <ValidationMessage
            text={validationMessage}
            palette={validationPalette}
            type={errorType}
            dataId={`${dataId}_ValidationMessage`}
            {...ValidationMessageProps1}
          />
        )}
        {validationRuleMessage && (
          <ValidationMessage
            text={validationRuleMessage}
            palette={validationRulePalette}
            type={errorType}
            dataId={`${dataId}_ValidationRuleMessage`}
            {...ValidationMessageProps2}
          />
        )}
      </div>
    );
  }
}
 
RadioField.propTypes = propTypes;
RadioField.defaultProps = defaultProps;
// if (__DOCS__) {
//   RadioField.docs = {
//     componentGroup: 'Form Fields',
//     folderName: 'General'
//   };
// }