UNPKG

@blueprintjs/core

Version:
70 lines (69 loc) 2.97 kB
/* * Copyright 2016 Palantir Technologies, Inc. All rights reserved. * * Licensed under the terms of the LICENSE file distributed with this project. */ import * as tslib_1 from "tslib"; import * as classNames from "classnames"; import * as PureRender from "pure-render-decorator"; import * as React from "react"; import * as Classes from "../../common/classes"; import { removeNonHTMLProps } from "../../common/props"; import { Icon } from "../icon/icon"; var InputGroup = (function (_super) { tslib_1.__extends(InputGroup, _super); function InputGroup() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = { rightElementWidth: 30, }; _this.refHandlers = { rightElement: function (ref) { return (_this.rightElement = ref); }, }; return _this; } InputGroup.prototype.render = function () { var _a = this.props, className = _a.className, intent = _a.intent, leftIconName = _a.leftIconName; var classes = classNames(Classes.INPUT_GROUP, Classes.intentClass(intent), (_b = {}, _b[Classes.DISABLED] = this.props.disabled, _b), className); var style = tslib_1.__assign({}, this.props.style, { paddingRight: this.state.rightElementWidth }); return (React.createElement("div", { className: classes }, React.createElement(Icon, { iconName: leftIconName, iconSize: "inherit" }), React.createElement("input", tslib_1.__assign({ type: "text" }, removeNonHTMLProps(this.props), { className: Classes.INPUT, ref: this.props.inputRef, style: style })), this.maybeRenderRightElement())); var _b; }; InputGroup.prototype.componentDidMount = function () { this.updateInputWidth(); }; InputGroup.prototype.componentDidUpdate = function () { this.updateInputWidth(); }; InputGroup.prototype.maybeRenderRightElement = function () { var rightElement = this.props.rightElement; if (rightElement == null) { return undefined; } return (React.createElement("span", { className: "pt-input-action", ref: this.refHandlers.rightElement }, rightElement)); }; InputGroup.prototype.updateInputWidth = function () { if (this.rightElement != null) { var clientWidth = this.rightElement.clientWidth; // small threshold to prevent infinite loops if (Math.abs(clientWidth - this.state.rightElementWidth) > 2) { this.setState({ rightElementWidth: clientWidth }); } } else { this.setState({ rightElementWidth: 0 }); } }; InputGroup.displayName = "Blueprint.InputGroup"; InputGroup = tslib_1.__decorate([ PureRender ], InputGroup); return InputGroup; }(React.Component)); export { InputGroup }; export var InputGroupFactory = React.createFactory(InputGroup);