UNPKG

tiramisu-react-form

Version:

form component for reactjs

125 lines (98 loc) 4.31 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Textarea = undefined; 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 _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); 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; } var Textarea = function (_Component) { _inherits(Textarea, _Component); function Textarea() { _classCallCheck(this, Textarea); var _this = _possibleConstructorReturn(this, (Textarea.__proto__ || Object.getPrototypeOf(Textarea)).call(this)); _this.onChange = _this.onChange.bind(_this); _this.onKeyDown = _this.onKeyDown.bind(_this); _this.state = { value: '', height: 'auto' }; return _this; } _createClass(Textarea, [{ key: 'onChange', value: function onChange(e) { this.setState({ value: e.target.value }); } }, { key: 'onKeyDown', value: function onKeyDown(e) { if (this.props.autoExpand) { var scrollHeight = e.target.scrollHeight; this.setState({ height: scrollHeight }); } } }, { key: 'render', value: function render() { var _state = this.state, value = _state.value, height = _state.height; var _props = this.props, classNames = _props.classNames, name = _props.name, placeholder = _props.placeholder, autoFocus = _props.autoFocus, cols = _props.cols, rows = _props.rows, isDisabled = _props.isDisabled, width = _props.width, autoExpand = _props.autoExpand; var overflow = 'visible'; if (autoExpand) overflow = 'hidden'; return _react2.default.createElement( 'div', { className: classNames.group, style: { width: width } }, _react2.default.createElement('textArea', { name: name, value: value, placeholder: placeholder, onChange: this.onChange, className: classNames.textarea, autoFocus: autoFocus, cols: cols, rows: rows, disabled: isDisabled, style: { height: height, overflow: overflow }, onKeyDown: this.onKeyDown }) ); } }]); return Textarea; }(_react.Component); Textarea.propTypes = { autoFocus: _propTypes2.default.bool.isRequired, cols: _propTypes2.default.number, rows: _propTypes2.default.number, isDisabled: _propTypes2.default.bool, maxLength: _propTypes2.default.number, minLength: _propTypes2.default.number, maxWords: _propTypes2.default.number, minWords: _propTypes2.default.number, isRequired: _propTypes2.default.bool, wrap: _propTypes2.default.string, autoExpand: _propTypes2.default.bool, width: _propTypes2.default.string }; Textarea.defaultProps = { autoFocus: false, isRequired: false, isDisabled: false, wrap: 'hard', autoExpand: false, width: '100%' }; exports.Textarea = Textarea;