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

var _react2 = _interopRequireDefault(_react);

var _keycode = require('keycode');

var _keycode2 = _interopRequireDefault(_keycode);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; }

/**
 * Title input mode.
 * - It initializes the input value
 * - It adds handlers on form submit, ESC (cancel) and blur (submit) events
 */
var CellTitleInput = function (_React$Component) {
	_inherits(CellTitleInput, _React$Component);

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

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

		_this.onBlur = _this.onBlur.bind(_this);
		_this.onKeyUp = _this.onKeyUp.bind(_this);
		_this.onSubmit = _this.onSubmit.bind(_this);
		return _this;
	}

	_createClass(CellTitleInput, [{
		key: 'componentDidMount',
		value: function componentDidMount() {
			this.titleInput.value = this.props.cellData;
		}
	}, {
		key: 'onKeyUp',
		value: function onKeyUp(event) {
			if (event.keyCode === (0, _keycode2.default)('escape')) {
				this.props.onEditCancel(event, this.props.rowData);
			}
		}
	}, {
		key: 'onBlur',
		value: function onBlur(event) {
			this.onSubmit(event);
		}
	}, {
		key: 'onSubmit',
		value: function onSubmit(event) {
			event.preventDefault();
			this.props.onEditSubmit(event, {
				value: this.titleInput.value,
				model: this.props.rowData
			});
		}
	}, {
		key: 'render',
		value: function render() {
			var _this2 = this;

			return _react2.default.createElement(
				'form',
				{ onSubmit: this.onSubmit },
				_react2.default.createElement(
					'label',
					{
						'aria-hidden': 'true',
						hidden: true,
						className: 'sr-only',
						htmlFor: this.props.id
					},
					this.props.label || 'title'
				),
				_react2.default.createElement('input', {
					id: this.props.id,
					ref: function ref(input) {
						_this2.titleInput = input;
					},
					onBlur: this.onBlur,
					onKeyUp: this.onKeyUp,
					autoFocus: true
				})
			);
		}
	}]);

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

exports.default = CellTitleInput;


CellTitleInput.propTypes = {
	/** The id prefix. */
	id: _react.PropTypes.string,
	/** The input value. */
	cellData: _react.PropTypes.string.isRequired,
	/** The cancel callback on ESC keydown. */
	onEditCancel: _react.PropTypes.func.isRequired,
	/** The submit callback on ENTER keydown or blur. */
	onEditSubmit: _react.PropTypes.func.isRequired,
	/** The collection item. */
	rowData: _react.PropTypes.object, // eslint-disable-line react/forbid-prop-types
	/** The input label. */
	label: _react.PropTypes.string
};