'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports["default"] = undefined;

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } }

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

function noop() {}

var TextareaItem = function (_React$Component) {
    (0, _inherits3["default"])(TextareaItem, _React$Component);

    function TextareaItem(props) {
        (0, _classCallCheck3["default"])(this, TextareaItem);

        var _this = (0, _possibleConstructorReturn3["default"])(this, _React$Component.call(this, props));

        _this.componentDidMount = function () {
            if (_this.props.autoHeight) {
                _this.initialTextHeight = _this.refs.textarea.offsetHeight;
                _this.componentDidUpdate();
            }
        };
        _this.componentDidUpdate = function () {
            if (_this.props.autoHeight) {
                var textareaDom = _this.refs.textarea;
                textareaDom.style.height = '';
                textareaDom.style.height = Math.max(_this.initialTextHeight, textareaDom.scrollHeight) + 'px';
            }
        };
        _this.onChange = function (e) {
            var value = e.target.value;
            var _this$props = _this.props;
            var count = _this$props.count;
            var onChange = _this$props.onChange;

            if (count > 0) {
                value = value.substring(0, count);
            }
            onChange(value);
        };
        _this.onBlur = function (e) {
            setTimeout(function () {
                _this.setState({
                    focus: false
                });
            }, 500);
            var value = e.target.value;
            _this.props.onBlur(value);
        };
        _this.onFocus = function (e) {
            _this.setState({
                focus: true
            });
            var value = e.target.value;
            _this.props.onFocus(value);
        };
        _this.onErrorClick = function () {
            _this.props.onErrorClick();
        };
        _this.clearInput = function () {
            _this.props.onChange('');
        };
        _this.state = {
            focus: false
        };
        return _this;
    }

    TextareaItem.prototype.render = function render() {
        var _classNames, _classNames2;

        var _props = this.props;
        var prefixCls = _props.prefixCls;
        var prefixListCls = _props.prefixListCls;
        var style = _props.style;
        var title = _props.title;
        var name = _props.name;
        var value = _props.value;
        var placeholder = _props.placeholder;
        var clear = _props.clear;
        var rows = _props.rows;
        var count = _props.count;
        var editable = _props.editable;
        var error = _props.error;
        var className = _props.className;
        var labelNumber = _props.labelNumber;
        var focus = this.state.focus;

        var wrapCls = (0, _classnames2["default"])((_classNames = {}, (0, _defineProperty3["default"])(_classNames, prefixListCls + '-item', true), (0, _defineProperty3["default"])(_classNames, prefixCls + '-item', true), (0, _defineProperty3["default"])(_classNames, prefixCls + '-error', error), (0, _defineProperty3["default"])(_classNames, prefixCls + '-focus', focus), (0, _defineProperty3["default"])(_classNames, className, className), _classNames));
        var labelCls = (0, _classnames2["default"])((_classNames2 = {}, (0, _defineProperty3["default"])(_classNames2, prefixCls + '-label', true), (0, _defineProperty3["default"])(_classNames2, prefixCls + '-label-2', labelNumber === 2), (0, _defineProperty3["default"])(_classNames2, prefixCls + '-label-3', labelNumber === 3), (0, _defineProperty3["default"])(_classNames2, prefixCls + '-label-4', labelNumber === 4), (0, _defineProperty3["default"])(_classNames2, prefixCls + '-label-5', labelNumber === 5), (0, _defineProperty3["default"])(_classNames2, prefixCls + '-label-6', labelNumber === 6), (0, _defineProperty3["default"])(_classNames2, prefixCls + '-label-7', labelNumber === 7), _classNames2));
        return React.createElement(
            'div',
            { className: wrapCls, style: style },
            title ? React.createElement(
                'div',
                { className: labelCls },
                title
            ) : null,
            React.createElement(
                'div',
                { className: prefixCls + '-control' },
                React.createElement('textarea', { ref: 'textarea', name: name, rows: rows, placeholder: placeholder, onChange: this.onChange, onBlur: this.onBlur, onFocus: this.onFocus, value: value, readOnly: !editable })
            ),
            clear && editable && value.length > 0 ? React.createElement('div', { className: prefixCls + '-clear', onClick: this.clearInput, onTouchStart: this.clearInput }) : null,
            error ? React.createElement('div', { className: prefixCls + '-error-extra', onClick: this.onErrorClick }) : null,
            count > 0 && rows > 1 ? React.createElement(
                'span',
                { className: prefixCls + '-count' },
                React.createElement(
                    'span',
                    null,
                    value.length
                ),
                '/',
                count
            ) : null
        );
    };

    return TextareaItem;
}(React.Component);

exports["default"] = TextareaItem;

TextareaItem.propTypes = {
    type: _react.PropTypes.oneOf(['hasLine']),
    title: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.node]),
    labelNumber: _react.PropTypes.oneOf([2, 3, 4, 5, 6, 7])
};
TextareaItem.defaultProps = {
    prefixCls: 'am-textarea',
    prefixListCls: 'am-list',
    title: '',
    type: 'hasLine',
    autoHeight: false,
    editable: true,
    name: '',
    value: '',
    placeholder: '',
    clear: false,
    rows: 1,
    count: 0,
    onChange: noop,
    onBlur: noop,
    onFocus: noop,
    onErrorClick: noop,
    error: false,
    labelNumber: 4
};
module.exports = exports['default'];