'use strict';

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

var _jsx = function () { var REACT_ELEMENT_TYPE = typeof Symbol === "function" && Symbol.for && Symbol.for("react.element") || 0xeac7; return function createRawReactElement(type, props, key, children) { var defaultProps = type && type.defaultProps; var childrenLength = arguments.length - 3; if (!props && childrenLength !== 0) { props = {}; } if (props && defaultProps) { for (var propName in defaultProps) { if (props[propName] === void 0) { props[propName] = defaultProps[propName]; } } } else if (!props) { props = defaultProps || {}; } if (childrenLength === 1) { props.children = children; } else if (childrenLength > 1) { var childArray = Array(childrenLength); for (var i = 0; i < childrenLength; i++) { childArray[i] = arguments[i + 3]; } props.children = childArray; } return { $$typeof: REACT_ELEMENT_TYPE, type: type, key: key === undefined ? null : '' + key, ref: null, props: props, _owner: null }; }; }();

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 _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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _View = require('../View');

var _View2 = _interopRequireDefault(_View);

var _TextInputControl = require('./TextInputControl');

var _TextInputControl2 = _interopRequireDefault(_TextInputControl);

var _theme = require('../const/theme');

var _styles = require('../helpers/styles');

var _helpers = require('./helpers');

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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; }

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

	function TextInput(props) {
		_classCallCheck(this, TextInput);

		var _this = _possibleConstructorReturn(this, (TextInput.__proto__ || Object.getPrototypeOf(TextInput)).call(this, props));

		_this.state = {
			isFocused: false,
			value: _this.props.value
		};

		_this._onFocusHandler = _this._onFocusHandler.bind(_this);
		_this._onBlurHandler = _this._onBlurHandler.bind(_this);
		_this._onChangeHandler = _this._onChangeHandler.bind(_this);
		_this._onKeyDownHandler = _this._onKeyDownHandler.bind(_this);
		_this._onKeyPressHandler = _this._onKeyPressHandler.bind(_this);
		_this._onKeyUpHandler = _this._onKeyUpHandler.bind(_this);
		return _this;
	}

	_createClass(TextInput, [{
		key: 'componentWillReceiveProps',
		value: function componentWillReceiveProps(newProps) {
			this.setState({ value: newProps.value });
		}
	}, {
		key: '_onFocusHandler',
		value: function _onFocusHandler(event) {
			var props = this.props;

			this.setState({ isFocused: true });
			props.onFocus && props.onFocus(event);
		}
	}, {
		key: '_onBlurHandler',
		value: function _onBlurHandler(event) {
			var props = this.props;

			this.setState({ isFocused: false });
			props.onBlur && props.onBlur(event);
		}
	}, {
		key: '_onChangeHandler',
		value: function _onChangeHandler(event) {
			var value = event.target.value;
			this.setState({ value: value });

			this.props.onChange && this.props.onChange(value);
		}
	}, {
		key: '_onKeyDownHandler',
		value: function _onKeyDownHandler(event) {
			var props = this.props;

			props.onKeyDown && props.onKeyDown(event);
		}
	}, {
		key: '_onKeyPressHandler',
		value: function _onKeyPressHandler(event) {
			var props = this.props;

			props.onKeyPress && props.onKeyPress(event);
		}
	}, {
		key: '_onKeyUpHandler',
		value: function _onKeyUpHandler(event) {
			var props = this.props;

			props.onKeyPress && props.onKeyPress(event);
		}
	}, {
		key: '_cloneControl',
		value: function _cloneControl(control) {
			var props = this.props;

			return _react2.default.cloneElement(control, _extends({}, props, {
				value: this.state.value,
				rows: props.rows,
				isDisabled: props.isDisabled,
				autoFocus: props.autoFocus,
				onFocus: this._onFocusHandler,
				onBlur: this._onBlurHandler,
				onChange: this._onChangeHandler,
				onKeyDown: this._onKeyDownHandler,
				onKeyPress: this._onKeyPressHandler,
				onKeyUp: this._onKeyUpHandler
			}));
		}
	}, {
		key: '_renderChildren',
		value: function _renderChildren() {
			var _this2 = this;

			return _react2.default.Children.map(this.props.children, function (item) {
				if (item.type.__TEXT_INPUT_CONTROL__) {
					return _this2._cloneControl(item);
				}
				return item;
			});
		}
	}, {
		key: 'render',
		value: function render() {
			var props = this.props;
			return _jsx(_View2.default, {
				style: { width: this.props.width },
				onClick: props.onClick,
				styles: [STYLE.textInput, props.isMultiLine && STYLE.isMultiLine, this.state.isFocused && STYLE.isFocused, props.isDisabled && STYLE.isDisabled].concat(_toConsumableArray(props.styles || []))
			}, void 0, this.props.children ? this._renderChildren() : this._cloneControl(_jsx(_TextInputControl2.default, {})));
		}
	}]);

	return TextInput;
}(_react2.default.Component);

TextInput.defaultProps = {
	value: '',
	size: 'm',
	placeholder: '',
	isDisabled: false,
	textAlign: 'left',
	multiLine: false,
	rows: 3
};
exports.default = TextInput;


var STYLE = _styles.StyleSheet.create(Object.assign((0, _helpers.generateTextStyles)(), {
	textInput: {
		position: 'relative',
		background: '#fff',
		width: '100%',
		borderRadius: '4px',
		boxSizing: 'border-box',
		alignItems: 'center',
		border: '1px solid',
		borderColor: _theme.COLOR.BORDER,
		boxShadow: _theme.SHADOW.INPUT,
		fontFamily: _theme.FONT.FAMILY,
		fontWeight: '400',
		flexWrap: 'wrap'
	},
	isDisabled: {
		color: _theme.COLOR.MUTED,
		background: _theme.COLOR.DISABLED,
		cursor: 'not-allowed'
	},
	isFocused: {
		boxShadow: _theme.SHADOW.FOCUS + ', inset 0px 1px 2px 0px rgba(27, 42, 48, 0.3)'
	},
	isMultiLine: {
		height: 'auto',
		paddingRight: 0
	}
}));