All files / src TimeInput.js

92.03% Statements 127/138
80.52% Branches 62/77
100% Functions 3/3
94.87% Lines 111/117
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                        5x     9x   5x   76x     5x                   150x 150x 1x   150x         224x                       212x 212x     76x           74x 74x     2x     1x     11x 11x 11x 11x 11x 4x 3x   7x 5x   8x 8x 8x 8x     46x 46x 46x 46x 46x 10x 10x   46x 46x     54x     3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x   1x 6x 4x   6x     3x 3x     3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x   1x 6x 6x   6x     3x 3x     64x   11x   3x   3x   1x     46x           12x 12x 12x 12x   12x 12x 12x 7x 7x 8x 8x 8x 1x       1x 1x 1x     7x 7x 7x     7x   5x   5x 5x 6x   5x   12x 12x 12x             64x 64x 64x          
import React from 'react';
import CreateReactClass from 'create-react-class';
import PropTypes from 'prop-types';
 
import isTwelveHourTime from './lib/is-twelve-hour-time';
import replaceCharAt from './lib/replace-char-at';
import getGroupId from './lib/get-group-id';
import getGroups from './lib/get-groups';
import adder from './lib/time-string-adder';
import caret from './lib/caret';
import validate from './lib/validate';
 
const SILHOUETTE = '00:00:00:000 AM';
 
// isSeparator :: Char -> Bool
const isSeparator = char => /[:\s]/.test(char);
 
var TimeInput = CreateReactClass({
  getInitialState() {
    return {};
  },
  getDefaultProps() {
    return {
      value: '12:00 AM'
    };
  },
  propTypes: {
    className: PropTypes.string,
    value: PropTypes.string,
    onChange: PropTypes.func
  },
  render() {
    var className = 'TimeInput';
    if (this.props.className) {
      className += ' ' + this.props.className;
    }
    return (
      <div className={className}>
        <input
          className="TimeInput-input"
          ref={input => {
            this.input = input;
          }}
          type="text"
          value={this.format(this.props.value)}
          onChange={this.handleChange}
          onBlur={this.handleBlur}
          onKeyDown={this.handleKeyDown}
        />
      </div>
    );
  },
  format(val) {
    if (isTwelveHourTime(val)) val = val.replace(/^00/, '12');
    return val.toUpperCase();
  },
  componentDidMount() {
    this.mounted = true;
  },
  componentWillUnmount() {
    this.mounted = false;
  },
  componentDidUpdate() {
    var index = this.state.caretIndex;
    if (index || index === 0) caret.set(this.input, index);
  },
  handleBlur() {
    Eif (this.mounted) this.setState({ caretIndex: null });
  },
  handleEscape() {
    Eif (this.mounted) this.input.blur();
  },
  handleTab(event) {
    var start = caret.start(this.input);
    var value = this.props.value;
    var groups = getGroups(value);
    var groupId = getGroupId(start);
    if (event.shiftKey) {
      if (!groupId) return;
      groupId--;
    } else {
      if (groupId >= groups.length - 1) return;
      groupId++;
    }
    event.preventDefault();
    var index = groupId * 3;
    if (this.props.value.charAt(index) === ' ') index++;
    Eif (this.mounted) this.setState({ caretIndex: index });
  },
  handleArrows(event) {
    event.preventDefault();
    var start = caret.start(this.input);
    var value = this.props.value;
    var amount = event.which === 38 ? 1 : -1;
    if (event.shiftKey) {
      amount *= 2;
      if (event.metaKey) amount *= 2;
    }
    value = adder(value, getGroupId(start), amount);
    this.onChange(value, start);
  },
  silhouette() {
    return this.props.value.replace(/\d/g, (val, i) => SILHOUETTE.charAt(i));
  },
  handleBackspace(event) {
    event.preventDefault();
    var start = caret.start(this.input);
    var value = this.props.value;
    var end = caret.end(this.input);
    Iif (!start && !end) return;
    var diff = end - start;
    var silhouette = this.silhouette();
    if (!diff) {
      if (value[start - 1] === ':') start--;
      value = replaceCharAt(value, start - 1, silhouette.charAt(start - 1));
      start--;
    } else {
      while (diff--) {
        if (value[end - 1] !== ':') {
          value = replaceCharAt(value, end - 1, silhouette.charAt(end - 1));
        }
        end--;
      }
    }
    Iif (value.charAt(start - 1) === ':') start--;
    this.onChange(value, start);
  },
  handleForwardSpace(event) {
    event.preventDefault();
    var start = caret.start(this.input);
    var value = this.props.value;
    var end = caret.end(this.input);
    Iif ((start === end) === value.length - 1) return;
    var diff = end - start;
    var silhouette = this.silhouette();
    if (!diff) {
      if (value[start] === ':') start++;
      value = replaceCharAt(value, start, silhouette.charAt(start));
      start++;
    } else {
      while (diff--) {
        Eif (value[end - 1] !== ':') {
          value = replaceCharAt(value, start, silhouette.charAt(start));
        }
        start++;
      }
    }
    Iif (value.charAt(start) === ':') start++;
    this.onChange(value, start);
  },
  handleKeyDown(event) {
    switch (event.which) {
    case 9: // Tab
      return this.handleTab(event);
    case 8: // Backspace
      return this.handleBackspace(event);
    case 46: // Forward
      return this.handleForwardSpace(event);
    case 27: // Esc
      return this.handleEscape(event);
    case 38: // Left
    case 40: // Right
      return this.handleArrows(event);
    default:
      break;
    }
  },
  handleChange(event) {
    let value = this.props.value;
    let newValue = this.input.value;
    let diff = newValue.length - value.length;
    let end = caret.start(this.input);
    let insertion;
    let start = end - Math.abs(diff);
    event.preventDefault();
    if (diff > 0) {
      insertion = newValue.slice(end - diff, end);
      while (diff--) {
        let oldChar = value.charAt(start);
        let newChar = insertion.charAt(0);
        if (isSeparator(oldChar)) {
          Iif (isSeparator(newChar)) {
            insertion = insertion.slice(1);
            start++;
          } else {
            start++;
            diff++;
            end++;
          }
        } else {
          value = replaceCharAt(value, start, newChar);
          insertion = insertion.slice(1);
          start++;
        }
      }
      newValue = value;
    } else {
      if (newValue.charAt(start) === ':') start++;
      // apply default to selection
      let result = value;
      for (var i = start; i < end; i++) {
        result = replaceCharAt(result, i, newValue.charAt(i));
      }
      newValue = result;
    }
    Eif (validate(newValue)) {
      if (newValue.charAt(end) === ':') end++;
      this.onChange(newValue, end);
    } else {
      var caretIndex = this.props.value.length - (newValue.length - end);
      if (this.mounted) this.setState({ caretIndex: caretIndex });
    }
  },
  onChange(str, caretIndex) {
    if (this.props.onChange) this.props.onChange(this.format(str));
    Eif (this.mounted && typeof caretIndex === 'number')
      this.setState({ caretIndex: caretIndex });
  }
});
 
export default TimeInput;