UNPKG

6.1 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.InputGroup = void 0;
19var tslib_1 = require("tslib");
20var classnames_1 = tslib_1.__importDefault(require("classnames"));
21var React = tslib_1.__importStar(require("react"));
22var common_1 = require("../../common");
23var Errors = tslib_1.__importStar(require("../../common/errors"));
24var props_1 = require("../../common/props");
25var icon_1 = require("../icon/icon");
26var asyncControllableInput_1 = require("./asyncControllableInput");
27/**
28 * Input group component.
29 *
30 * @see https://blueprintjs.com/docs/#core/components/text-inputs.input-group
31 */
32var InputGroup = /** @class */ (function (_super) {
33 tslib_1.__extends(InputGroup, _super);
34 function InputGroup() {
35 var _this = _super !== null && _super.apply(this, arguments) || this;
36 _this.state = {};
37 _this.leftElement = null;
38 _this.rightElement = null;
39 _this.refHandlers = {
40 leftElement: function (ref) { return (_this.leftElement = ref); },
41 rightElement: function (ref) { return (_this.rightElement = ref); },
42 };
43 return _this;
44 }
45 InputGroup.prototype.render = function () {
46 var _a;
47 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;
48 var inputGroupClasses = (0, classnames_1.default)(common_1.Classes.INPUT_GROUP, common_1.Classes.intentClass(intent), (_a = {},
49 _a[common_1.Classes.DISABLED] = disabled,
50 _a[common_1.Classes.FILL] = fill,
51 _a[common_1.Classes.LARGE] = large,
52 _a[common_1.Classes.SMALL] = small,
53 _a[common_1.Classes.ROUND] = round,
54 _a), className);
55 var style = tslib_1.__assign(tslib_1.__assign({}, this.props.style), { paddingLeft: this.state.leftElementWidth, paddingRight: this.state.rightElementWidth });
56 var inputProps = tslib_1.__assign(tslib_1.__assign({ type: "text" }, (0, props_1.removeNonHTMLProps)(this.props)), { className: common_1.Classes.INPUT, style: style });
57 var inputElement = asyncControl ? (React.createElement(asyncControllableInput_1.AsyncControllableInput, tslib_1.__assign({}, inputProps, { inputRef: inputRef }))) : (React.createElement("input", tslib_1.__assign({}, inputProps, { ref: inputRef })));
58 return React.createElement(tagName, { className: inputGroupClasses }, this.maybeRenderLeftElement(), inputElement, this.maybeRenderRightElement());
59 };
60 InputGroup.prototype.componentDidMount = function () {
61 this.updateInputWidth();
62 };
63 InputGroup.prototype.componentDidUpdate = function (prevProps) {
64 var _a = this.props, leftElement = _a.leftElement, rightElement = _a.rightElement;
65 if (prevProps.leftElement !== leftElement || prevProps.rightElement !== rightElement) {
66 this.updateInputWidth();
67 }
68 };
69 InputGroup.prototype.validateProps = function (props) {
70 if (props.leftElement != null && props.leftIcon != null) {
71 console.warn(Errors.INPUT_WARN_LEFT_ELEMENT_LEFT_ICON_MUTEX);
72 }
73 };
74 InputGroup.prototype.maybeRenderLeftElement = function () {
75 var _a = this.props, leftElement = _a.leftElement, leftIcon = _a.leftIcon;
76 if (leftElement != null) {
77 return (React.createElement("span", { className: common_1.Classes.INPUT_LEFT_CONTAINER, ref: this.refHandlers.leftElement }, leftElement));
78 }
79 else if (leftIcon != null) {
80 return React.createElement(icon_1.Icon, { icon: leftIcon, "aria-hidden": true, tabIndex: -1 });
81 }
82 return undefined;
83 };
84 InputGroup.prototype.maybeRenderRightElement = function () {
85 var rightElement = this.props.rightElement;
86 if (rightElement == null) {
87 return undefined;
88 }
89 return (React.createElement("span", { className: common_1.Classes.INPUT_ACTION, ref: this.refHandlers.rightElement }, rightElement));
90 };
91 InputGroup.prototype.updateInputWidth = function () {
92 var _a = this.state, leftElementWidth = _a.leftElementWidth, rightElementWidth = _a.rightElementWidth;
93 if (this.leftElement != null) {
94 var clientWidth = this.leftElement.clientWidth;
95 // small threshold to prevent infinite loops
96 if (leftElementWidth === undefined || Math.abs(clientWidth - leftElementWidth) > 2) {
97 this.setState({ leftElementWidth: clientWidth });
98 }
99 }
100 else {
101 this.setState({ leftElementWidth: undefined });
102 }
103 if (this.rightElement != null) {
104 var clientWidth = this.rightElement.clientWidth;
105 // small threshold to prevent infinite loops
106 if (rightElementWidth === undefined || Math.abs(clientWidth - rightElementWidth) > 2) {
107 this.setState({ rightElementWidth: clientWidth });
108 }
109 }
110 else {
111 this.setState({ rightElementWidth: undefined });
112 }
113 };
114 InputGroup.displayName = "".concat(props_1.DISPLAYNAME_PREFIX, ".InputGroup");
115 return InputGroup;
116}(common_1.AbstractPureComponent2));
117exports.InputGroup = InputGroup;
118//# sourceMappingURL=inputGroup.js.map
\No newline at end of file