all files / src/ InputFormField.jsx

94.08% Statements 159/169
80% Branches 68/85
89.19% Functions 33/37
97.16% Lines 137/141
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310          18×                                           10×     18×   14×   14×                         14×   14×                                                                         19×         19× 19× 19× 19×     19×                 16×     19× 19× 19× 19×     19×           18×     19× 19× 19× 19×       19×       18×     19× 19× 19× 19× 19× 19× 19× 18× 18×   18× 18× 18× 18×                                                       18× 17×             19×                                                      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
 
var _react = require('react');
 
var _react2 = _interopRequireDefault(_react);
 
var _propTypes = require('prop-types');
 
var _propTypes2 = _interopRequireDefault(_propTypes);
 
var _uxcoreFormField = require('uxcore-form-field');
 
var _uxcoreFormField2 = _interopRequireDefault(_uxcoreFormField);
 
var _uxcoreConst = require('uxcore-const');
 
var _uxcoreConst2 = _interopRequireDefault(_uxcoreConst);
 
var _classnames2 = require('classnames');
 
var _classnames3 = _interopRequireDefault(_classnames2);
 
var _objectAssign = require('object-assign');
 
var _objectAssign2 = _interopRequireDefault(_objectAssign);
 
var _util = require('./util');
 
var _util2 = _interopRequireDefault(_util);
 
var _FormCount = require('./addons/FormCount');
 
var _FormCount2 = _interopRequireDefault(_FormCount);
 
var _LeftAddon = require('./addons/LeftAddon');
 
var _LeftAddon2 = _interopRequireDefault(_LeftAddon);
 
var _RightAddon = require('./addons/RightAddon');
 
var _RightAddon2 = _interopRequireDefault(_RightAddon);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
 
function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
 
function _defineProperty(obj, key, value) { Iif (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
function _possibleConstructorReturn(self, call) { Iif (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); } /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * InputFormField Component for uxcore
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * @author eternalsky
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * Copyright 2015-2016, Uxcore Team, Alinw.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * All rights reserved.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                */
 
/**
 * extend FormField, rewrite renderField method
 */
var InputFormField = function (_FormField) {
  _inherits(InputFormField, _FormField);
 
  function InputFormField() {
    _classCallCheck(this, InputFormField);
 
    return _possibleConstructorReturn(this, _FormField.apply(this, arguments));
  }
 
  InputFormField.prototype.componentWillUnmount = function componentWillUnmount() {
    _FormField.prototype.componentWillUnmount.call(this);
    this.clearTimer();
  };
 
  InputFormField.prototype.clearTimer = function clearTimer() {
    if (this.timer) {
      clearTimeout(this.timer);
      this.timer = null;
    }
  };
 
  InputFormField.prototype.handleChange = function handleChange(e) {
    var me = this;
    var autoTrim = me.props.autoTrim;
 
    var value = e.currentTarget.value;
    Eif (autoTrim) {
      me.clearTimer();
      me.timer = setTimeout(function () {
        value = _util2['default'].trim(value);
        me.handleDataChange(me.deFormatValue(value));
      }, 500);
    }
    me.handleDataChange(me.deFormatValue(value));
  };
 
  InputFormField.prototype.handleFocus = function handleFocus(e) {
    this.setState({
      focus: true
    });
    this.props.onFocus(e);
  };
 
  InputFormField.prototype.handleBlur = function handleBlur(e) {
    var me = this;
    me.setState({
      focus: false
    });
    var pass = true;
    Eif (me.props.validateOnBlur) {
      pass = me.doValidate();
    }
    me.props.onBlur(e, pass);
  };
 
  InputFormField.prototype.handleMouseEnter = function handleMouseEnter() {
    this.setState({
      hover: true
    });
  };
 
  InputFormField.prototype.handleMouseLeave = function handleMouseLeave() {
    this.setState({
      hover: false
    });
  };
 
  InputFormField.prototype.handleKeyDown = function handleKeyDown(e) {
    var me = this;
    me.props.onKeyDown(e);
  };
  /* eslint-disable class-methods-use-this */
 
  InputFormField.prototype.deFormatValue = function deFormatValue(value) {
    return value;
  };
 
  InputFormField.prototype.addSpecificClass = function addSpecificClass() {
    return 'kuma-input-uxform-field';
  };
 
  /* eslint-enable class-methods-use-this */
 
  InputFormField.prototype.getCount = function getCount() {
    var me = this;
    var children = me.props.children;
    var element = void 0;
    _react2['default'].Children.map(children, function (child) {
      if (child && typeof child.type === 'function' && child.type.displayName === 'FormCount') {
        element = child;
      }
    });
    if (element) {
      var total = element.props.total;
      var Count = _react2['default'].cloneElement(element, {
        length: me.state.value ? me.state.value.length : 0,
        key: 'count'
      });
 
      return {
        element: Count,
        total: total
      };
    }
    return null;
  };
 
  InputFormField.prototype.renderLeftAddon = function renderLeftAddon() {
    var me = this;
    var children = me.props.children;
    var element = void 0;
    _react2['default'].Children.map(children, function (child) {
      if (child && typeof child.type === 'function' && child.type.displayName === 'LeftAddon') {
        element = child;
      }
    });
    if (element) {
      return _react2['default'].cloneElement(element, {
        focus: me.state.focus,
        hover: me.state.hover,
        key: 'left'
      });
    }
    return null;
  };
 
  InputFormField.prototype.renderRightAddon = function renderRightAddon() {
    var me = this;
    var children = me.props.children;
    var element = void 0;
    _react2['default'].Children.map(children, function (child) {
      if (child && typeof child.type === 'function' && child.type.displayName === 'RightAddon') {
        element = child;
      }
    });
 
    if (element) {
      return _react2['default'].cloneElement(element, {
        key: 'right'
      });
    }
    return null;
  };
 
  InputFormField.prototype.renderField = function renderField() {
    var me = this;
    var arr = [];
    var mode = me.props.jsxmode || me.props.mode;
    var count = me.getCount();
    var leftAddon = me.renderLeftAddon();
    var rightAddon = me.renderRightAddon();
    if (mode === _uxcoreConst2['default'].MODE.EDIT) {
      var otherOptions = {};
      if (leftAddon) {
        arr.push(leftAddon);
      }
      var IEver = _util2['default'].getIEVer();
      var placeholder = IEver >= 10 && me.props.IECompatible ? '' : me.props.jsxplaceholder;
      var size = this.getSize();
      arr.push(_react2['default'].createElement('input', _extends({
        className: (0, _classnames3['default'])(_defineProperty({
          'kuma-input': true,
          'kuma-uxform-input-has-right': !!count || !!rightAddon,
          'kuma-uxform-input-has-left': !!leftAddon
        }, 'kuma-input-' + size + '-size', !!size)),
        key: me.props.inputType,
        type: me.props.inputType,
        placeholder: placeholder,
        disabled: me.props.jsxdisabled === 'disabled' || me.props.jsxdisabled === true ? 'disabled' : '',
        name: me.props.jsxname,
        value: me.state.formatValue,
        onMouseEnter: function onMouseEnter(e) {
          me.handleMouseEnter(e);
        },
        onMouseLeave: function onMouseLeave(e) {
          me.handleMouseLeave(e);
        },
        onFocus: function onFocus(e) {
          me.handleFocus(e);
        },
        onBlur: function onBlur(e) {
          me.handleBlur(e);
        },
        onChange: function onChange(e) {
          me.handleChange(e);
        },
        onKeyDown: function onKeyDown(e) {
          me.handleKeyDown(e);
        }
      }, otherOptions)));
 
      if (rightAddon) {
        arr.push(rightAddon);
      } else if (count) {
        arr.push(count.element);
      }
    } else Eif (mode === _uxcoreConst2['default'].MODE.VIEW) {
      arr.push(_react2['default'].createElement(
        'span',
        { key: 'text' },
        me.props.renderView(me.state.formatValue)
      ));
    }
    return _react2['default'].createElement(
      'div',
      { className: 'kuma-uxform-input' },
      arr
    );
  };
 
  return InputFormField;
}(_uxcoreFormField2['default']);
 
InputFormField.Count = _FormCount2['default'];
InputFormField.LeftAddon = _LeftAddon2['default'];
InputFormField.RightAddon = _RightAddon2['default'];
InputFormField.propTypes = (0, _objectAssign2['default'])({}, _uxcoreFormField2['default'].propTypes, {
  IECompatible: _propTypes2['default'].bool,
  onBlur: _propTypes2['default'].func,
  onFocus: _propTypes2['default'].func,
  onKeyDown: _propTypes2['default'].func,
  renderView: _propTypes2['default'].func,
  validateOnBlur: _propTypes2['default'].bool,
  autoTrim: _propTypes2['default'].bool,
  inputType: _propTypes2['default'].string
});
InputFormField.defaultProps = (0, _objectAssign2['default'])({}, _uxcoreFormField2['default'].defaultProps, {
  IECompatible: true,
  onBlur: function onBlur() {},
  onFocus: function onFocus() {},
  onKeyDown: function onKeyDown() {},
  renderView: function renderView(value) {
    return value;
  },
  validateOnBlur: false,
  inputType: 'text'
});
InputFormField.displayName = 'InputFormField';
exports['default'] = InputFormField;
module.exports = exports['default'];