All files / src/components/DatatableCore/InputTypes TextFieldWrapper.js

100% Statements 29/29
95.65% Branches 22/23
100% Functions 11/11
100% Lines 29/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                                                  25x 25x               25x 25x 17x       17x 3x         25x 8x       8x 3x       25x 3x 3x 1x       25x 113x 113x 113x           200x               1x                     109x 109x   109x   109x                             1x 1x 6x                     16x                      
import React, { Component, Fragment } from "react";
import equal from "fast-deep-equal";
import {
  Tooltip,
  Zoom,
  withStyles,
  FormControl,
  Input,
  InputLabel
} from "@material-ui/core";
import MaskedInput from "react-text-mask";
import { checkValue, setValue } from "./PickersFunction";
import { customVariant } from "../../MuiTheme";
import {
  valueVerificationPropType,
  cellValPropType,
  classesPropType,
  maskPropType,
  labelPropType,
  typePropType,
  requiredPropType
} from "../../../proptypes";
 
export class TextFieldWrapper extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tooltipOpen: false,
      message: "",
      error: false
    };
  }
 
  componentDidMount() {
    const { valueVerification } = this.props;
    if (valueVerification) {
      const newState = checkValue({
        ...this.props,
        mounting: true
      });
      if (!equal(this.state, newState)) {
        this.setState(newState);
      }
    }
  }
 
  onValueChange = value => {
    const newState = setValue({
      ...this.props,
      value
    });
    if (!equal(this.state, newState)) {
      this.setState(newState);
    }
  };
 
  toggleTooltip = open => {
    const { error } = this.state;
    if (error) {
      this.setState({ tooltipOpen: open });
    }
  };
 
  textMaskCustom = properties => {
    const { inputRef, ...other } = properties;
    const { mask } = this.props;
    return (
      <Fragment>
        {(!mask || mask.length === 0) && (
          <input
            {...other}
            ref={ref => {
              inputRef(ref ? ref.inputElement : null);
            }}
          />
        )}
        {mask && mask.length > 0 && (
          <MaskedInput
            {...other}
            ref={ref => {
              inputRef(ref ? ref.inputElement : null);
            }}
            mask={mask}
            showMask
          />
        )}
      </Fragment>
    );
  };
 
  render() {
    const { type, cellVal, classes, label, required } = this.props;
    const { tooltipOpen, message, error } = this.state;
    const inputValue =
      type === "number" && !cellVal && cellVal !== 0 ? "" : cellVal;
 
    return (
      <Tooltip
        open={tooltipOpen}
        classes={{
          tooltip: classes.errorTooltip
        }}
        title={message}
        TransitionComponent={Zoom}
        interactive
      >
        <FormControl required={required}>
          <InputLabel>{label}</InputLabel>
          <Input
            value={inputValue}
            error={error}
            onFocus={() => this.toggleTooltip(true)}
            onBlur={() => this.setState({ tooltipOpen: false })}
            onChange={e => this.onValueChange(e.target.value)}
            type={type}
            fullWidth
            inputComponent={this.textMaskCustom}
          />
        </FormControl>
      </Tooltip>
    );
  }
}
 
TextFieldWrapper.propTypes = {
  required: requiredPropType,
  label: labelPropType,
  cellVal: cellValPropType,
  classes: classesPropType.isRequired,
  type: typePropType.isRequired,
  mask: maskPropType,
  valueVerification: valueVerificationPropType
};
 
export default withStyles(customVariant)(TextFieldWrapper);