UNPKG

4.1 kBJavaScriptView Raw
1/*
2 * Copyright 2017 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __assign, __decorate, __extends, __rest } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { polyfill } from "react-lifecycles-compat";
20import { AbstractPureComponent2, Classes, refHandler, setRef } from "../../common";
21import { DISPLAYNAME_PREFIX } from "../../common/props";
22// this component is simple enough that tests would be purely tautological.
23/* istanbul ignore next */
24var TextArea = /** @class */ (function (_super) {
25 __extends(TextArea, _super);
26 function TextArea() {
27 var _this = _super !== null && _super.apply(this, arguments) || this;
28 _this.state = {};
29 // used to measure and set the height of the component on first mount
30 _this.textareaElement = null;
31 _this.handleRef = refHandler(_this, "textareaElement", _this.props.inputRef);
32 _this.handleChange = function (e) {
33 if (_this.props.growVertically) {
34 _this.setState({
35 height: e.target.scrollHeight,
36 });
37 }
38 if (_this.props.onChange != null) {
39 _this.props.onChange(e);
40 }
41 };
42 return _this;
43 }
44 TextArea.prototype.componentDidMount = function () {
45 var _a;
46 if (this.props.growVertically && this.textareaElement !== null) {
47 // HACKHACK: this should probably be done in getSnapshotBeforeUpdate
48 /* eslint-disable-next-line react/no-did-mount-set-state */
49 this.setState({
50 height: (_a = this.textareaElement) === null || _a === void 0 ? void 0 : _a.scrollHeight,
51 });
52 }
53 };
54 TextArea.prototype.componentDidUpdate = function (prevProps) {
55 if (prevProps.inputRef !== this.props.inputRef) {
56 setRef(prevProps.inputRef, null);
57 this.handleRef = refHandler(this, "textareaElement", this.props.inputRef);
58 setRef(this.props.inputRef, this.textareaElement);
59 }
60 };
61 TextArea.prototype.render = function () {
62 var _a;
63 var _b = this.props, className = _b.className, fill = _b.fill, inputRef = _b.inputRef, intent = _b.intent, large = _b.large, small = _b.small, growVertically = _b.growVertically, htmlProps = __rest(_b, ["className", "fill", "inputRef", "intent", "large", "small", "growVertically"]);
64 var rootClasses = classNames(Classes.INPUT, Classes.intentClass(intent), (_a = {},
65 _a[Classes.FILL] = fill,
66 _a[Classes.LARGE] = large,
67 _a[Classes.SMALL] = small,
68 _a), className);
69 // add explicit height style while preserving user-supplied styles if they exist
70 var _c = htmlProps.style, style = _c === void 0 ? {} : _c;
71 if (growVertically && this.state.height != null) {
72 // this style object becomes non-extensible when mounted (at least in the enzyme renderer),
73 // so we make a new one to add a property
74 style = __assign(__assign({}, style), { height: this.state.height + "px" });
75 }
76 return (React.createElement("textarea", __assign({}, htmlProps, { className: rootClasses, onChange: this.handleChange, ref: this.handleRef, style: style })));
77 };
78 TextArea.displayName = DISPLAYNAME_PREFIX + ".TextArea";
79 TextArea = __decorate([
80 polyfill
81 ], TextArea);
82 return TextArea;
83}(AbstractPureComponent2));
84export { TextArea };
85//# sourceMappingURL=textArea.js.map
\No newline at end of file