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

import React from 'react';
import { findDOMNode } from 'react-dom';
import { Seed } from 'react-seeds';

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

	style.height = '1px';

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

export default React.createClass({
	displayName: 'Field',
	setElement: function setElement(component) {
		var el = 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 React.createElement(Seed, _extends({ Component: 'textarea',
			ref: this.setElement
		}, this.props, {
			onChange: this.onChange
		}));
	},
	componentDidUpdate: function componentDidUpdate() {
		if (this.el) {
			sizeTextAreaToFit(this.el);
		}
	}
});