'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _desc, _value, _class, _VIEW$EDIT; /**
                                        * Created by baoyinghai on 12/7/16.
                                        */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _coreDecorators = require('core-decorators');

var _StaticDisplay = require('./StaticDisplay');

var _StaticDisplay2 = _interopRequireDefault(_StaticDisplay);

var _validateTypes = require('../../formValidator/validateTypes');

var _validateTypes2 = _interopRequireDefault(_validateTypes);

var _constant = require('../../constant');

var _mxa = require('../../../../../components/mxa');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (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) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!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) { if (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 } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
  var desc = {};
  Object['ke' + 'ys'](descriptor).forEach(function (key) {
    desc[key] = descriptor[key];
  });
  desc.enumerable = !!desc.enumerable;
  desc.configurable = !!desc.configurable;

  if ('value' in desc || desc.initializer) {
    desc.writable = true;
  }

  desc = decorators.slice().reverse().reduce(function (desc, decorator) {
    return decorator(target, property, desc) || desc;
  }, desc);

  if (context && desc.initializer !== void 0) {
    desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
    desc.initializer = undefined;
  }

  if (desc.initializer === void 0) {
    Object['define' + 'Property'](target, property, desc);
    desc = null;
  }

  return desc;
}

var TextArea = (_class = function (_React$Component) {
  _inherits(TextArea, _React$Component);

  function TextArea(porps) {
    _classCallCheck(this, TextArea);

    var _this = _possibleConstructorReturn(this, (TextArea.__proto__ || Object.getPrototypeOf(TextArea)).call(this, porps));

    _this.state = {
      len: _this.props.value && _this.props.value.length || 0,
      value: _this.props.value
    };
    return _this;
  }

  _createClass(TextArea, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(next) {
      if (next.value !== this.state.value) {
        this.state.value = next.value;
        this.state.len = next.value && next.value.length || 0;
      }
    }
  }, {
    key: '_onChange',
    value: function _onChange(e) {
      var onChange = this.props.onChange;

      this.setState({ len: e.target.value.length, value: e.target.value });
      onChange && onChange(e.target.value);
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(_mxa.Input, { value: this.state.value, type: 'textarea', disabled: this.props.disabled, rows: 4, onChange: this._onChange }),
        _react2.default.createElement(
          'div',
          { className: 'text-area-length' },
          this.props.length && _react2.default.createElement(
            'span',
            { style: this.state.len > this.props.length ? { color: '#f50' } : {} },
            this.state.len + '/' + this.props.length
          )
        )
      );
    }
  }]);

  return TextArea;
}(_react2.default.Component), (_applyDecoratedDescriptor(_class.prototype, '_onChange', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_onChange'), _class.prototype)), _class);


var compRender = function compRender(config) {
  var record = config.record,
      props = config.props;

  var formValidate = record.formValidates && record.formValidates.filter(function (item) {
    return item.validateType === _validateTypes2.default.ISSTRING;
  })[0];
  var length = formValidate && formValidate.length || record.length;
  return _react2.default.createElement(TextArea, { disabled: record.isReadonly || props.isModalInputMappingItem && props.isLeadInto && props.modalInputIsReadonly, length: length });
};

exports.default = (_VIEW$EDIT = {}, _defineProperty(_VIEW$EDIT, _constant.VIEW, _StaticDisplay2.default), _defineProperty(_VIEW$EDIT, _constant.EDIT, compRender), _VIEW$EDIT);