1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __assign, __decorate, __extends } from "tslib";
|
17 | import classNames from "classnames";
|
18 | import * as React from "react";
|
19 | import { polyfill } from "react-lifecycles-compat";
|
20 | import { AbstractPureComponent2, Classes } from "../../common";
|
21 | import * as Errors from "../../common/errors";
|
22 | import { DISPLAYNAME_PREFIX, removeNonHTMLProps, } from "../../common/props";
|
23 | import { Icon } from "../icon/icon";
|
24 | import { AsyncControllableInput } from "./asyncControllableInput";
|
25 | var InputGroup = (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 |
|
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 |
|
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));
|
115 | export { InputGroup };
|
116 |
|
\ | No newline at end of file |