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

77.77% Statements 14/18
57.14% Branches 32/56
60% Functions 3/5
77.77% Lines 14/18

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                                  3x 3x 3x 3x                 3x 3x                                                                       3x           3x 3x 3x   3x                                                     3x                                                                                                       1x   1x                
/**** Libraries ****/
import React, { PureComponent } from 'react';
import { defaultProps } from './props/defaultProps';
import { propTypes } from './props/propTypes';
 
/**** Components ****/
import Label from '@zohodesk/components/lib/Label/Label';
import CheckBox from '@zohodesk/components/lib/CheckBox/CheckBox';
import ValidationMessage from '../ValidationMessage/ValidationMessage';
import { Container, Box } from '@zohodesk/components/lib/Layout';
import FieldContainer from '../FieldContainer/FieldContainer';
 
/**** CSS ****/
import style from '../Fields.module.css';
 
export default class CheckBoxField extends PureComponent {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
    this.handleGetRef = this.handleGetRef.bind(this);
    this.handleLabelClick = this.handleLabelClick.bind(this);
  }
 
  handleChange(value) {
    let { id, onChange } = this.props;
    onChange && onChange(id, value);
  }
 
  handleGetRef(el) {
    let { getRef, id } = this.props;
    getRef && getRef(el, id);
  }
 
  handleLabelClick() {
    let { checked } = this.props;
    this.handleChange(!checked);
  }
 
  render() {
    let {
      labelName,
      id,
      isMandatory,
      validationMessage,
      validationPalette,
      isReadOnly,
      checked,
      errorType,
      isDisabled,
      title,
      dataId,
      dataSelectorId,
      validationRuleMessage,
      validationRulePalette,
      getContainerRef,
      infoText,
      fieldSize,
      labelPalette,
      labelCustomClass,
      direction,
      lineClamp,
      isLocked,
      lockedInfoText,
      customProps,
      renderLabelProps,
      children
    } = this.props;
    const {
      LabelProps = {},
      CheckBoxProps = {},
      ValidationMessageProps1 = {},
      ValidationMessageProps2 = {}
    } = customProps;
    let removeEvent = isDisabled || isReadOnly;
    let isDirectCol = direction === 'column';
    let labelElement = (
      <FieldContainer
        isLocked={isLocked}
        lockedInfoText={lockedInfoText}
        infoText={infoText}
        renderProps={renderLabelProps}
      >
        <Label
          text={labelName}
          id={id}
          size={fieldSize === 'small' ? 'small' : 'medium'}
          palette={isMandatory ? 'mandatory' : isDisabled ? 'primary' : labelPalette}
          infoText={infoText}
          customClass={`${!isDirectCol ? style.checkboxText : ''} ${
            !isDirectCol ? style[`lineClamp_${lineClamp}`] : ''
          } ${isReadOnly || isDisabled ? style.cbTextReadonly : style.cbTextPointer} ${labelCustomClass} ${
            isMandatory ? style.labelMandatory : ''
          }`}
          title={labelName}
          onClick={!removeEvent ? this.handleLabelClick : null}
          variant={isDirectCol ? 'default' : 'primary'}
          dataId={
            isDisabled ? `${dataId}_label_disabled` : isMandatory ? `${dataId}_label_mandatory` : `${dataId}_label`
          }
          {...LabelProps}
        />
      </FieldContainer>
    );
    return (
      <div
        className={`${style.container} ${isDisabled ? style.disabled : isReadOnly ? style.readonly : ''}`}
        data-title={isDisabled ? title : null}
        data-selector-id={dataSelectorId}
      >
        {isDirectCol && labelElement}
        <div
          className={`${isDirectCol ? style.fieldContainer : style.checkboxFieldContainer} ${
            isDirectCol && labelName ? style[`fieldMargin_${fieldSize}`] : ''
          }`}
        >
          <Container alignBox='row' isCover={false} align='top'>
            <Box className={isDirectCol ? style.checkbox : style.checkboxRow}>
              <CheckBox
                id={id}
                onChange={this.handleChange}
                disabled={removeEvent}
                checked={checked}
                getRef={this.handleGetRef}
                dataId={dataId}
                getContainerRef={getContainerRef}
                {...CheckBoxProps}
              />
            </Box>
            {!isDirectCol && <Box flexible>{labelElement}</Box>}
          </Container>
          {children}
          {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>
      </div>
    );
  }
}
 
CheckBoxField.propTypes = propTypes;
 
CheckBoxField.defaultProps = defaultProps;
 
// if (__DOCS__) {
//   CheckBoxField.docs = {
//     componentGroup: 'Form Fields',
//     folderName: 'General'
//   };
// }