UNPKG

sc-react-ions

Version:

An open source set of React components that implement Ambassador's Design and UX patterns.

167 lines (137 loc) 5.52 kB
'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 _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _bind = require('classnames/bind'); var _bind2 = _interopRequireDefault(_bind); var _style = require('./style.scss'); var _style2 = _interopRequireDefault(_style); 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; } /** * The Textarea component. */ var Textarea = function (_React$Component) { _inherits(Textarea, _React$Component); function Textarea(props) { _classCallCheck(this, Textarea); var _this = _possibleConstructorReturn(this, (Textarea.__proto__ || Object.getPrototypeOf(Textarea)).call(this, props)); _this.state = { value: _this.props.value }; _this.componentWillReceiveProps = function (nextProps) { if (nextProps.value !== _this.props.value) { _this.setState({ value: nextProps.value }); } }; _this.handleChange = function (event) { event.persist(); _this.setState({ value: event.target.value }, function () { if (typeof _this.props.changeCallback === 'function') { _this.props.changeCallback({ target: { name: _this.props.name, value: event.target.value } }); } }); }; _this.handleFocus = function (event) { if (typeof _this.props.focusCallback === 'function') { _this.props.focusCallback(event); } }; _this.handleBlur = function (event) { if (typeof _this.props.blurCallback === 'function') { _this.props.blurCallback(event); } }; return _this; } _createClass(Textarea, [{ key: 'render', value: function render() { var _props = this.props, disabled = _props.disabled, label = _props.label, value = _props.value, optClass = _props.optClass, className = _props.className; var cx = _bind2.default.bind(_style2.default); var disabledClass = disabled ? _style2.default['textarea-disabled'] : ''; var textareaClass = cx(_style2.default['textarea-component'], optClass, className, disabledClass); return _react2.default.createElement( 'div', { className: textareaClass }, label ? _react2.default.createElement( 'label', null, label ) : null, _react2.default.createElement('textarea', { value: this.state.value, onFocus: this.handleFocus, onChange: this.handleChange, onBlur: this.handleBlur, disabled: this.props.disabled, placeholder: this.props.placeholder }) ); } }]); return Textarea; }(_react2.default.Component); Textarea.defaultProps = { disabled: false, value: '' }; Textarea.propTypes = { /** * A class name to be used for local styles or integrations (required to support styled-components) */ className: _propTypes2.default.string, /** * Name of the textarea. */ name: _propTypes2.default.string, /** * Whether the textarea is disabled. */ disabled: _propTypes2.default.bool, /** * Text shown above the textarea. */ label: _propTypes2.default.string, /** * Value of the textarea. */ value: _propTypes2.default.string, /** * Optional placeholder text. */ placeholder: _propTypes2.default.string, /** * Optional styles to add to the textarea. */ optClass: _propTypes2.default.string, /** * A callback function to be called when the textarea changes. */ changeCallback: _propTypes2.default.func, /** * A callback function to be called when the textarea is focused. */ focusCallback: _propTypes2.default.func, /** * A callback function to be called when the textarea is blurred. */ blurCallback: _propTypes2.default.func }; exports.default = Textarea;