UNPKG

4.16 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, __extends, __rest } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { AbstractPureComponent2, Classes, refHandler, setRef } from "../../common";
20import { DISPLAYNAME_PREFIX } from "../../common/props";
21// this component is simple enough that tests would be purely tautological.
22/* istanbul ignore next */
23/**
24 * Text area component.
25 *
26 * @see https://blueprintjs.com/docs/#core/components/text-inputs.text-area
27 */
28var TextArea = /** @class */ (function (_super) {
29 __extends(TextArea, _super);
30 function TextArea() {
31 var _this = _super !== null && _super.apply(this, arguments) || this;
32 _this.state = {};
33 // used to measure and set the height of the component on first mount
34 _this.textareaElement = null;
35 _this.handleRef = refHandler(_this, "textareaElement", _this.props.inputRef);
36 _this.maybeSyncHeightToScrollHeight = function () {
37 if (_this.props.growVertically && _this.textareaElement != null) {
38 var scrollHeight = _this.textareaElement.scrollHeight;
39 if (scrollHeight > 0) {
40 _this.setState({ height: scrollHeight });
41 }
42 }
43 };
44 _this.handleChange = function (e) {
45 var _a, _b;
46 _this.maybeSyncHeightToScrollHeight();
47 (_b = (_a = _this.props).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, e);
48 };
49 return _this;
50 }
51 TextArea.prototype.componentDidMount = function () {
52 this.maybeSyncHeightToScrollHeight();
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 if (prevProps.value !== this.props.value || prevProps.style !== this.props.style) {
61 this.maybeSyncHeightToScrollHeight();
62 }
63 };
64 TextArea.prototype.render = function () {
65 var _a;
66 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"]);
67 var rootClasses = classNames(Classes.INPUT, Classes.intentClass(intent), (_a = {},
68 _a[Classes.FILL] = fill,
69 _a[Classes.LARGE] = large,
70 _a[Classes.SMALL] = small,
71 _a), className);
72 // add explicit height style while preserving user-supplied styles if they exist
73 var _c = htmlProps.style, style = _c === void 0 ? {} : _c;
74 if (growVertically && this.state.height != null) {
75 // this style object becomes non-extensible when mounted (at least in the enzyme renderer),
76 // so we make a new one to add a property
77 style = __assign(__assign({}, style), { height: "".concat(this.state.height, "px") });
78 }
79 return (React.createElement("textarea", __assign({}, htmlProps, { className: rootClasses, onChange: this.handleChange, ref: this.handleRef, style: style })));
80 };
81 TextArea.displayName = "".concat(DISPLAYNAME_PREFIX, ".TextArea");
82 return TextArea;
83}(AbstractPureComponent2));
84export { TextArea };
85//# sourceMappingURL=textArea.js.map
\No newline at end of file