All files TextInput.jsx

8.26% Statements 9/109
4.44% Branches 4/90
8.7% Functions 2/23
5.19% Lines 4/77
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 2281x 1x 1x   2x                                                                                                                                                                                                                                                                                                                                                                                                                                                              
import React from 'react';
import nodeHeight from './nodeHeight';
import {Utils} from 'ship-components-utility';
 
import css from './text-input.css';
 
export default class TextInput extends React.Component {
 
  constructor(props) {
    super(props);
 
    this.state = {
      focus: false,
      height: null,
      minHeight: -Infinity,
      maxHeight: Infinity
    }
 
    this.calculateHeight = this.calculateHeight.bind(this);
    this.handleFocus = this.handleFocus.bind(this);
    this.handleBlur = this.handleBlur.bind(this);
    this.handleChange = this.handleChange.bind(this);
    this.handleKeyDown = this.handleKeyDown.bind(this);
    this.handleEnterKey = this.handleEnterKey.bind(this);
    this.getFontSize = this.getFontSize.bind(this);
  }
 
  componentDidMount() {
    this.calculateHeight();
    window.addEventListener('resize', this.calculateHeight);
  }
 
  componentWillReceiveProps() {
    // Render the content and then update the state/height
    clearTimeout(this.updateId);
    this.updateId = setTimeout(this.calculateHeight, 0);
 
    clearTimeout(this.transitionUpdateId)
    this.transitionUpdateId = setTimeout(this.calculateHeight, 250);
  }
 
  componentWillUnmount() {
    clearTimeout(this.updateId);
    clearTimeout(this.transitionUpdateId)
    window.removeEventListener('resize', this.calculateHeight);
  }
 
  handleFocus(event) {
    if (!this.props.editable) {
      return;
    }
    this.setState({
      focus: true
    });
    if (typeof this.props.onFocus === 'function') {
      this.props.onFocus(event);
    }
  }
 
  handleBlur(event) {
    this.setState({
      focus: false
    });
    if (typeof this.props.onBlur === 'function') {
      this.props.onBlur(event);
    }
  }
 
  handleChange(event) {
    this.calculateHeight();
 
    if (typeof this.props.onChange === 'function') {
      this.props.onChange(event);
    }
  }
 
  handleKeyDown(event) {
    if (event.key === 'Enter' || event.keyCode === 13) {
      this.handleEnterKey(event);
    }
    if (typeof this.props.onKeyDown === 'function') {
      this.props.onKeyDown(event);
    }
  }
 
  handleEnterKey(event) {
    if (!(this.props.multiline && event.shiftKey)) {
      // prevent new line if not Shift + Enter
      event.preventDefault();
    }
    if (typeof this.props.onEnterKeyDown === 'function') {
      this.props.onEnterKeyDown(event);
    }
  }
 
  /**
   * Calculate the height of the node and update the state
   */
  calculateHeight() {
    let state = nodeHeight(
      this.refs.input,
      this.props.minRows,
      this.props.maxRows
    );
    // ONESONY-693, IE11 nodeHeight workaround
    if (Utils.isIEBrowser() && (!this.props.value || this.props.value.length === 0)) {
      state.height = parseInt(this.getFontSize(), 10);
    }
    this.setState(state);
  }
 
  getFontSize() {
    return window.getComputedStyle(this.refs.wrapper).getPropertyValue('font-size');
  }
 
  /**
  * Get css class names for the component for it's different states
  * @return {String}
  */
  classNames() {
    let classes = ['text-input', css.container, this.props.className];
 
    let value = this.props.value || this.props.defaultValue;
    let valueIsNotEmpty = value && value.length > 0;
 
    if (this.state.focus || valueIsNotEmpty) {
     classes.push(css.active);
    }
 
    if (this.state.focus) {
     classes.push(css.focus);
    }
 
    if (this.props.label) {
      classes.push(css.hasLabel);
    }
 
    if (valueIsNotEmpty && typeof this.props.validate === 'function') {
      if (this.validate(this.props.value)) {
        classes.push(css.success);
      } else {
        classes.push(css.error);
      }
    }
 
    return classes
      .filter((cla) => typeof cla === 'string' && cla.length)
      .join(' ')
      .trim();
  }
 
  /**
   * If we have a validate function call it
   * @param  {Mixed} value
   * @return {Boolean}
   */
  validate(value) {
    if (typeof this.props.validate !== 'function') {
      return true;
    }
    return this.props.validate(value);
  }
 
  /**
   * Render
   * @return {React}
   */
  render(){
    let props = this.props;
    let styles = { props };
 
    styles.height = this.state.height;
 
    let maxHeight = Math.max(styles.maxHeight ? styles.maxHeight : -Infinity, this.state.maxHeight);
 
    // Hide scrollbar if we don't need it
    if (maxHeight >= this.state.height) {
      styles.overflow = 'hidden';
    } else {
      styles.overflow = 'auto';
    }
 
    return (
      <div className={this.classNames()}>
        <div className={css.fieldContainer}>
          <textarea
            placeholder={this.props.placeholder}
            tabIndex={this.props.tabIndex}
            onDragStart={this.props.onDragStart}
            onDragEnd={this.props.onDragEnd}
            onDragOver={this.props.onDragOver}
            className={'text-input--field ' + css.field}
            ref='input'
            disabled={this.props.disabled || !this.props.editable}
            style={styles}
            value={this.props.value}
            onClick={this.props.onClick}
            onFocus={this.handleFocus}
            onBlur={this.handleBlur}
            onChange={this.handleChange}
            onKeyDown={this.handleKeyDown}
          />
          {this.props.label ?
            <label className={'text-input--label ' + css.label}>
              {this.props.label}
            </label>
          : null}
        </div>
        {this.props.error ?
          <label className={'text-input--error ' + css.error}>
            {this.props.error}
          </label>
        : null}
      </div>
    );
  }
}
 
/**
 * Defaults
 * @type {Object}
 */
TextInput.defaultProps = {
  editable: true,
  value: '',
  label: null
};