UNPKG

5.93 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; }; }();
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _propTypes = require('prop-types');
14
15var _propTypes2 = _interopRequireDefault(_propTypes);
16
17var _bind = require('classnames/bind');
18
19var _bind2 = _interopRequireDefault(_bind);
20
21var _style = require('./style.scss');
22
23var _style2 = _interopRequireDefault(_style);
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29function _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; }
30
31function _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; }
32
33/**
34 * The Textarea component.
35 */
36var Textarea = function (_React$Component) {
37 _inherits(Textarea, _React$Component);
38
39 function Textarea(props) {
40 _classCallCheck(this, Textarea);
41
42 var _this = _possibleConstructorReturn(this, (Textarea.__proto__ || Object.getPrototypeOf(Textarea)).call(this, props));
43
44 _this.state = {
45 value: _this.props.value
46 };
47
48 _this.UNSAFE_componentWillReceiveProps = function (nextProps) {
49 if (nextProps.value !== _this.props.value) {
50 _this.setState({ value: nextProps.value });
51 }
52 };
53
54 _this.handleChange = function (event) {
55 event.persist();
56
57 _this.setState({ value: event.target.value }, function () {
58 if (typeof _this.props.changeCallback === 'function') {
59 _this.props.changeCallback({
60 target: {
61 name: _this.props.name,
62 value: event.target.value
63 }
64 });
65 }
66 });
67 };
68
69 _this.handleFocus = function (event) {
70 if (typeof _this.props.focusCallback === 'function') {
71 _this.props.focusCallback(event);
72 }
73 };
74
75 _this.handleBlur = function (event) {
76 if (typeof _this.props.blurCallback === 'function') {
77 _this.props.blurCallback(event);
78 }
79 };
80
81 return _this;
82 }
83
84 _createClass(Textarea, [{
85 key: 'render',
86 value: function render() {
87 var _props = this.props,
88 disabled = _props.disabled,
89 label = _props.label,
90 optClass = _props.optClass,
91 className = _props.className,
92 height = _props.height,
93 readOnly = _props.readOnly,
94 placeholder = _props.placeholder;
95
96
97 var cx = _bind2.default.bind(_style2.default);
98 var heightStyle = height ? { height: height } : null;
99 var disabledClass = disabled ? _style2.default['textarea-disabled'] : '';
100 var textareaClass = cx(_style2.default['textarea-component'], optClass, className, disabledClass);
101
102 return _react2.default.createElement(
103 'div',
104 { className: textareaClass },
105 label ? _react2.default.createElement(
106 'label',
107 null,
108 label
109 ) : null,
110 _react2.default.createElement('textarea', {
111 value: this.state.value,
112 onFocus: this.handleFocus,
113 onChange: this.handleChange,
114 onBlur: this.handleBlur,
115 disabled: disabled,
116 readOnly: readOnly,
117 placeholder: placeholder,
118 style: heightStyle
119 })
120 );
121 }
122 }]);
123
124 return Textarea;
125}(_react2.default.Component);
126
127Textarea.defaultProps = {
128 disabled: false,
129 value: ''
130};
131Textarea.propTypes = {
132 /**
133 * A class name to be used for local styles or integrations (required to support styled-components)
134 */
135 className: _propTypes2.default.string,
136 /**
137 * Name of the textarea.
138 */
139 name: _propTypes2.default.string,
140 /**
141 * Whether the textarea is disabled.
142 */
143 disabled: _propTypes2.default.bool,
144 /**
145 * Whether the textarea is read only.
146 */
147 readOnly: _propTypes2.default.bool,
148 /**
149 * Text shown above the textarea.
150 */
151 label: _propTypes2.default.string,
152 /**
153 * Value of the textarea.
154 */
155 value: _propTypes2.default.string,
156 /**
157 * Optional placeholder text.
158 */
159 placeholder: _propTypes2.default.string,
160 /**
161 * Optional styles to add to the textarea.
162 */
163 optClass: _propTypes2.default.string,
164 /**
165 * A callback function to be called when the textarea changes.
166 */
167 changeCallback: _propTypes2.default.func,
168 /**
169 * A callback function to be called when the textarea is focused.
170 */
171 focusCallback: _propTypes2.default.func,
172 /**
173 * A callback function to be called when the textarea is blurred.
174 */
175 blurCallback: _propTypes2.default.func,
176 /**
177 * An attribute will add an explicit height (in pixels) to the textarea.
178 */
179 height: _propTypes2.default.string
180};
181exports.default = Textarea;
\No newline at end of file