'use strict';

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _createReactClass = require('create-react-class');

var _createReactClass2 = _interopRequireDefault(_createReactClass);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

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

function _objectWithoutProperties(obj, keys) {
	var target = {};for (var i in obj) {
		if (keys.indexOf(i) >= 0) continue;if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;target[i] = obj[i];
	}return target;
}

module.exports = (0, _createReactClass2.default)({
	displayName: 'FormInput',
	propTypes: {
		autoFocus: _propTypes2.default.bool,
		className: _propTypes2.default.string,
		disabled: _propTypes2.default.bool,
		href: _propTypes2.default.string,
		id: _propTypes2.default.string,
		multiline: _propTypes2.default.bool,
		name: _propTypes2.default.string,
		noedit: _propTypes2.default.bool,
		onChange: _propTypes2.default.func,
		size: _propTypes2.default.oneOf(['lg', 'sm', 'xs', 'minor']),
		type: _propTypes2.default.string,
		value: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string])
	},

	getDefaultProps: function getDefaultProps() {
		return {
			type: 'text'
		};
	},

	componentDidMount: function componentDidMount() {
		if (this.props.autoFocus) {
			this.focus();
		}
	},

	focus: function focus() {
		this.refs.input.focus();
	},

	render: function render() {
		var _props = this.props;
		var noedit = _props.noedit;
		var multiline = _props.multiline;
		var size = _props.size;
		var className = _props.className;

		var rest = _objectWithoutProperties(_props, ['noedit', 'multiline', 'size', 'className']);

		// classes
		var newClassName = (0, _classnames2.default)({
			'cp-FormField-input--readOnly': noedit,
			'cp-FormField-input--readOnly--multiline': noedit && multiline,
			'cp-SearchBar-input': _props.type === 'search',
			'cp-FormField-input': !noedit && _props.type !== 'search'
		}, size ? 'cp-FormField-input--' + size : null, className);
		var props = _extends({}, rest, { className: newClassName, ref: 'input' });
		var Element = 'input';
		if (noedit && this.props.href) {
			Element = 'a';
			props.type = null;
			props.children = props.children || props.value;
		} else if (noedit) {
			Element = 'div';
			props.type = null;
			props.children = props.children || props.value;
		} else if (multiline) {
			Element = 'textarea';
		}

		return _react2.default.createElement(Element, props);
	}
});