UNPKG

5.87 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 { AbstractPureComponent, Classes, refHandler, setRef } from "../../common";
20import { DISPLAYNAME_PREFIX } from "../../common/props";
21import { AsyncControllableTextArea } from "./asyncControllableTextArea";
22// this component is simple enough that tests would be purely tautological.
23/* istanbul ignore next */
24/**
25 * Text area component.
26 *
27 * @see https://blueprintjs.com/docs/#core/components/text-area
28 */
29var TextArea = /** @class */ (function (_super) {
30 __extends(TextArea, _super);
31 function TextArea() {
32 var _this = _super !== null && _super.apply(this, arguments) || this;
33 _this.state = {};
34 // used to measure and set the height of the component on first mount
35 _this.textareaElement = null;
36 _this.handleRef = refHandler(_this, "textareaElement", _this.props.inputRef);
37 _this.maybeSyncHeightToScrollHeight = function () {
38 // eslint-disable-next-line deprecation/deprecation
39 var _a = _this.props, autoResize = _a.autoResize, growVertically = _a.growVertically;
40 if (_this.textareaElement != null) {
41 var scrollHeight = _this.textareaElement.scrollHeight;
42 if (autoResize) {
43 // set height to 0 to force scrollHeight to be the minimum height to fit
44 // the content of the textarea
45 _this.textareaElement.style.height = "0px";
46 _this.textareaElement.style.height = scrollHeight.toString() + "px";
47 _this.setState({ height: scrollHeight });
48 }
49 else if (growVertically && scrollHeight > 0) {
50 // N.B. this code path will be deleted in Blueprint v6.0 when `growVertically` is removed
51 _this.setState({ height: scrollHeight });
52 }
53 }
54 if (_this.props.autoResize && _this.textareaElement != null) {
55 // set height to 0 to force scrollHeight to be the minimum height to fit
56 // the content of the textarea
57 _this.textareaElement.style.height = "0px";
58 var scrollHeight = _this.textareaElement.scrollHeight;
59 _this.textareaElement.style.height = scrollHeight.toString() + "px";
60 _this.setState({ height: scrollHeight });
61 }
62 };
63 _this.handleChange = function (e) {
64 var _a, _b;
65 _this.maybeSyncHeightToScrollHeight();
66 (_b = (_a = _this.props).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, e);
67 };
68 return _this;
69 }
70 TextArea.prototype.componentDidMount = function () {
71 this.maybeSyncHeightToScrollHeight();
72 };
73 TextArea.prototype.componentDidUpdate = function (prevProps) {
74 if (prevProps.inputRef !== this.props.inputRef) {
75 setRef(prevProps.inputRef, null);
76 this.handleRef = refHandler(this, "textareaElement", this.props.inputRef);
77 setRef(this.props.inputRef, this.textareaElement);
78 }
79 if (prevProps.value !== this.props.value || prevProps.style !== this.props.style) {
80 this.maybeSyncHeightToScrollHeight();
81 }
82 };
83 TextArea.prototype.render = function () {
84 var _a;
85 var _b = this.props, asyncControl = _b.asyncControl, autoResize = _b.autoResize, className = _b.className, fill = _b.fill,
86 // eslint-disable-next-line deprecation/deprecation
87 growVertically = _b.growVertically, inputRef = _b.inputRef, intent = _b.intent, large = _b.large, small = _b.small, htmlProps = __rest(_b, ["asyncControl", "autoResize", "className", "fill", "growVertically", "inputRef", "intent", "large", "small"]);
88 var rootClasses = classNames(Classes.INPUT, Classes.TEXT_AREA, Classes.intentClass(intent), (_a = {},
89 _a[Classes.FILL] = fill,
90 _a[Classes.LARGE] = large,
91 _a[Classes.SMALL] = small,
92 _a[Classes.TEXT_AREA_AUTO_RESIZE] = autoResize,
93 _a), className);
94 // add explicit height style while preserving user-supplied styles if they exist
95 var _c = htmlProps.style, style = _c === void 0 ? {} : _c;
96 if ((autoResize || growVertically) && this.state.height != null) {
97 // this style object becomes non-extensible when mounted (at least in the enzyme renderer),
98 // so we make a new one to add a property
99 style = __assign(__assign({}, style), { height: "".concat(this.state.height, "px") });
100 }
101 var TextAreaComponent = asyncControl ? AsyncControllableTextArea : "textarea";
102 return (React.createElement(TextAreaComponent, __assign({}, htmlProps, { className: rootClasses, onChange: this.handleChange, style: style, ref: this.handleRef })));
103 };
104 TextArea.defaultProps = {
105 autoResize: false,
106 fill: false,
107 large: false,
108 small: false,
109 };
110 TextArea.displayName = "".concat(DISPLAYNAME_PREFIX, ".TextArea");
111 return TextArea;
112}(AbstractPureComponent));
113export { TextArea };
114//# sourceMappingURL=textArea.js.map
\No newline at end of file