All files / src/components/NumberInput index.jsx

0% Statements 0/104
0% Branches 0/65
0% Functions 0/25
0% Lines 0/61
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                                                                                                                                                                                                                                                                                                                                                                   
import styles from './style.postcss';
 
import React, { PureComponent } from 'react';
import is from 'is_js';
import classnames from 'classnames';
import Icon from 'components/Icon';
import PropTypes from 'prop-types';
 
const REG_EXP_ACCEPTED_CHARS = /^[0-9]+$/;
 
export default class NumberInput extends PureComponent {
  constructor(props) {
    super(props);
    this.state = { focused: false };
    this._parseProps(props);
    this._onUpArrowClicked = this._onUpArrowClicked.bind(this);
    this._onDownArrowClicked = this._onDownArrowClicked.bind(this);
    this._onValueChanged = this._onValueChanged.bind(this);
    this._onFocus = this._onFocus.bind(this);
    this._onBlur = this._onBlur.bind(this);
    this._refToInput = this._refToInput.bind(this);
  }
 
  componentWillUpdate(nextProps) {
    this._parseProps(nextProps);
  }
 
  _parseProps(props) {
    this._value = this._parseNumber(props.value);
    this._defaultValue = this._parseNumber(props.defaultValue);
    this._min = this._parseNumber(props.min);
    this._max = this._parseNumber(props.max);
    this._step = this._parseNumber(props.step, 1);
  }
 
  _parseNumber(target, defaultValue) {
    if (is.number(target)) {
      return Number(target.toFixed(0));
    }
 
    if (REG_EXP_ACCEPTED_CHARS.test(target)) {
      return Number(target, 10);
    }
 
    return defaultValue;
  }
 
  _onUpArrowClicked() {
    this._setNewValue(this._getValidNumber(this._value, this._defaultValue) + 1);
    this._focusOnInput();
  }
 
  _onDownArrowClicked() {
    this._setNewValue(this._getValidNumber(this._value, this._defaultValue) - 1);
    this._focusOnInput();
  }
 
  _setNewValue(value) {
    this._onValueChanged({ target: { value } });
  }
 
  _focusOnInput() {
    this._input.focus();
  }
 
  _onValueChanged({ target: { value: newValue } }) {
    if (is.empty(newValue)) {
      return this._sendCallbackWithNewValue(null);
    }
 
    if (! REG_EXP_ACCEPTED_CHARS.test(newValue)) {
      return;
    }
 
    const numberValue = Number(newValue, 10);
    if ((is.undefined(this._min) || numberValue >= this._min) &&
        (is.undefined(this._max) || numberValue <= this._max)) {
      return this._sendCallbackWithNewValue(numberValue);
    }
 
    if (numberValue < this._min) {
      return this._sendCallbackWithNewValue(this._min);
    }
 
    if (numberValue > this._max) {
      return this._sendCallbackWithNewValue(this._max);
    }
  }
 
  _sendCallbackWithNewValue(newValue) {
    if (newValue !== this._value && is.function(this.props.onChange)) {
      this.props.onChange(newValue);
    }
  }
 
  _onFocus() {
    this.setState({ focused: true });
  }
 
  _onBlur() {
    this.setState({ focused: false });
  }
 
  _refToInput(c) {
    this._input = c;
  }
 
  _getValidNumber(...values) {
    const validNumber = values.find((value) => is.number(value));
    if (is.not.number(validNumber)) return '';
    return validNumber;
  }
 
  render() {
    const inputValue = this._getValidNumber(this._value, this._defaultValue);
    const classes = classnames(styles.NumberInput_inputContainer,
        this.props.className,
        { [styles.__focused]: this.state.focused });
    return <div className={styles.NumberInput}>
      {
        this.props.labelName &&
        <span className={styles.NumberInput_label}>
          {this.props.labelName}
        </span>
      }
      <div className={classes}>
        <input className={styles.NumberInput_inputContainer_input}
            type="text"
            value={inputValue}
            disabled={this.props.readonly || this.props.disabled}
            onChange={this._onValueChanged}
            onFocus={this._onFocus}
            onBlur={this._onBlur}
            ref={this._refToInput} />
        {
          ! this.props.disabled &&
          <div className={styles.NumberInput_arrowsContainer}>
            <div className={styles.NumberInput_arrow} onClick={this._onUpArrowClicked}>
              <Icon id="chevron-small-up" />
            </div>
            <div className={styles.NumberInput_arrow} onClick={this._onDownArrowClicked}>
              <Icon id="chevron-small-down" />
            </div>
          </div>
        }
      </div>
    </div>;
  }
}
 
NumberInput.propTypes = {
  value: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
  ]),
  defaultValue: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
  ]),
  min: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
  ]),
  max: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
  ]),
  step: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
  ]),
  onChange: PropTypes.func.isRequired,
  disabled: PropTypes.bool,
  readonly: PropTypes.bool,
  className: PropTypes.string,
  labelName: PropTypes.string,
};