'use strict';

exports.__esModule = true;

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

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactSeeds = require('react-seeds');

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

function sizeTextAreaToFit(el) {
	var style = el.style;

	style.height = '1px';

	var measuredHeight = el.scrollHeight + 'px';
	style.height = measuredHeight;
	style.resize = 'none';
}

exports.default = _react2.default.createClass({
	displayName: 'Field',
	setElement: function setElement(component) {
		var el = (0, _reactDom.findDOMNode)(component);

		this.el = el;

		if (el != null) {
			// null when unmounting
			sizeTextAreaToFit(el);
		}
	},
	onChange: function onChange(event) {
		var el = event.target;

		sizeTextAreaToFit(el);

		var value = el.value;
		this.props.onChange(value);
	},
	render: function render() {
		return _react2.default.createElement(_reactSeeds.Seed, _extends({ Component: 'textarea',
			ref: this.setElement
		}, this.props, {
			onChange: this.onChange
		}));
	},
	componentDidUpdate: function componentDidUpdate() {
		if (this.el) {
			sizeTextAreaToFit(this.el);
		}
	}
});
module.exports = exports['default'];