UNPKG

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