UNPKG

5.67 kBJavaScriptView Raw
1/*
2 * Copyright 2016 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 } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { polyfill } from "react-lifecycles-compat";
20import { AbstractPureComponent2, Classes } from "../../common";
21import * as Errors from "../../common/errors";
22import { DISPLAYNAME_PREFIX, removeNonHTMLProps, } from "../../common/props";
23import { Icon } from "../icon/icon";
24import { AsyncControllableInput } from "./asyncControllableInput";
25var InputGroup = /** @class */ (function (_super) {
26 __extends(InputGroup, _super);
27 function InputGroup() {
28 var _this = _super !== null && _super.apply(this, arguments) || this;
29 _this.state = {};
30 _this.leftElement = null;
31 _this.rightElement = null;
32 _this.refHandlers = {
33 leftElement: function (ref) { return (_this.leftElement = ref); },
34 rightElement: function (ref) { return (_this.rightElement = ref); },
35 };
36 return _this;
37 }
38 InputGroup.prototype.render = function () {
39 var _a;
40 var _b = this.props, _c = _b.asyncControl, asyncControl = _c === void 0 ? false : _c, className = _b.className, disabled = _b.disabled, fill = _b.fill, inputRef = _b.inputRef, intent = _b.intent, large = _b.large, small = _b.small, round = _b.round;
41 var inputGroupClasses = classNames(Classes.INPUT_GROUP, Classes.intentClass(intent), (_a = {},
42 _a[Classes.DISABLED] = disabled,
43 _a[Classes.FILL] = fill,
44 _a[Classes.LARGE] = large,
45 _a[Classes.SMALL] = small,
46 _a[Classes.ROUND] = round,
47 _a), className);
48 var style = __assign(__assign({}, this.props.style), { paddingLeft: this.state.leftElementWidth, paddingRight: this.state.rightElementWidth });
49 var inputProps = __assign(__assign({ type: "text" }, removeNonHTMLProps(this.props)), { className: Classes.INPUT, style: style });
50 return (React.createElement("div", { className: inputGroupClasses },
51 this.maybeRenderLeftElement(),
52 asyncControl ? (React.createElement(AsyncControllableInput, __assign({}, inputProps, { inputRef: inputRef }))) : (React.createElement("input", __assign({}, inputProps, { ref: inputRef }))),
53 this.maybeRenderRightElement()));
54 };
55 InputGroup.prototype.componentDidMount = function () {
56 this.updateInputWidth();
57 };
58 InputGroup.prototype.componentDidUpdate = function (prevProps) {
59 var _a = this.props, leftElement = _a.leftElement, rightElement = _a.rightElement;
60 if (prevProps.leftElement !== leftElement || prevProps.rightElement !== rightElement) {
61 this.updateInputWidth();
62 }
63 };
64 InputGroup.prototype.validateProps = function (props) {
65 if (props.leftElement != null && props.leftIcon != null) {
66 console.warn(Errors.INPUT_WARN_LEFT_ELEMENT_LEFT_ICON_MUTEX);
67 }
68 };
69 InputGroup.prototype.maybeRenderLeftElement = function () {
70 var _a = this.props, leftElement = _a.leftElement, leftIcon = _a.leftIcon;
71 if (leftElement != null) {
72 return (React.createElement("span", { className: Classes.INPUT_LEFT_CONTAINER, ref: this.refHandlers.leftElement }, leftElement));
73 }
74 else if (leftIcon != null) {
75 return React.createElement(Icon, { icon: leftIcon });
76 }
77 return undefined;
78 };
79 InputGroup.prototype.maybeRenderRightElement = function () {
80 var rightElement = this.props.rightElement;
81 if (rightElement == null) {
82 return undefined;
83 }
84 return (React.createElement("span", { className: Classes.INPUT_ACTION, ref: this.refHandlers.rightElement }, rightElement));
85 };
86 InputGroup.prototype.updateInputWidth = function () {
87 var _a = this.state, leftElementWidth = _a.leftElementWidth, rightElementWidth = _a.rightElementWidth;
88 if (this.leftElement != null) {
89 var clientWidth = this.leftElement.clientWidth;
90 // small threshold to prevent infinite loops
91 if (leftElementWidth === undefined || Math.abs(clientWidth - leftElementWidth) > 2) {
92 this.setState({ leftElementWidth: clientWidth });
93 }
94 }
95 else {
96 this.setState({ leftElementWidth: undefined });
97 }
98 if (this.rightElement != null) {
99 var clientWidth = this.rightElement.clientWidth;
100 // small threshold to prevent infinite loops
101 if (rightElementWidth === undefined || Math.abs(clientWidth - rightElementWidth) > 2) {
102 this.setState({ rightElementWidth: clientWidth });
103 }
104 }
105 else {
106 this.setState({ rightElementWidth: undefined });
107 }
108 };
109 InputGroup.displayName = DISPLAYNAME_PREFIX + ".InputGroup";
110 InputGroup = __decorate([
111 polyfill
112 ], InputGroup);
113 return InputGroup;
114}(AbstractPureComponent2));
115export { InputGroup };
116//# sourceMappingURL=inputGroup.js.map
\No newline at end of file