UNPKG

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