UNPKG

6.95 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, __decorate, __extends, __rest } from "tslib";
17// we need some empty interfaces to show up in docs
18// HACKHACK: these components should go in separate files
19/* eslint-disable max-classes-per-file, @typescript-eslint/no-empty-interface */
20import classNames from "classnames";
21import * as React from "react";
22import { polyfill } from "react-lifecycles-compat";
23import { AbstractPureComponent2, Classes, refHandler, setRef } from "../../common";
24import { DISPLAYNAME_PREFIX } from "../../common/props";
25/**
26 * Renders common control elements, with additional props to customize appearance.
27 * This component is not exported and is only used in this file for `Checkbox`, `Radio`, and `Switch` below.
28 */
29var Control = function (_a) {
30 var _b;
31 var alignIndicator = _a.alignIndicator, children = _a.children, className = _a.className, indicatorChildren = _a.indicatorChildren, inline = _a.inline, inputRef = _a.inputRef, label = _a.label, labelElement = _a.labelElement, large = _a.large, style = _a.style, type = _a.type, typeClassName = _a.typeClassName, _c = _a.tagName, tagName = _c === void 0 ? "label" : _c, htmlProps = __rest(_a, ["alignIndicator", "children", "className", "indicatorChildren", "inline", "inputRef", "label", "labelElement", "large", "style", "type", "typeClassName", "tagName"]);
32 var classes = classNames(Classes.CONTROL, typeClassName, (_b = {},
33 _b[Classes.DISABLED] = htmlProps.disabled,
34 _b[Classes.INLINE] = inline,
35 _b[Classes.LARGE] = large,
36 _b), Classes.alignmentClass(alignIndicator), className);
37 return React.createElement(tagName, { className: classes, style: style }, React.createElement("input", __assign({}, htmlProps, { ref: inputRef, type: type })), React.createElement("span", { className: Classes.CONTROL_INDICATOR }, indicatorChildren), label, labelElement, children);
38};
39var Switch = /** @class */ (function (_super) {
40 __extends(Switch, _super);
41 function Switch() {
42 return _super !== null && _super.apply(this, arguments) || this;
43 }
44 Switch.prototype.render = function () {
45 var _a = this.props, innerLabelChecked = _a.innerLabelChecked, innerLabel = _a.innerLabel, controlProps = __rest(_a, ["innerLabelChecked", "innerLabel"]);
46 var switchLabels = innerLabel || innerLabelChecked
47 ? [
48 React.createElement("div", { key: "checked", className: Classes.CONTROL_INDICATOR_CHILD },
49 React.createElement("div", { className: Classes.SWITCH_INNER_TEXT }, innerLabelChecked ? innerLabelChecked : innerLabel)),
50 React.createElement("div", { key: "unchecked", className: Classes.CONTROL_INDICATOR_CHILD },
51 React.createElement("div", { className: Classes.SWITCH_INNER_TEXT }, innerLabel)),
52 ]
53 : null;
54 return (React.createElement(Control, __assign({}, controlProps, { type: "checkbox", typeClassName: Classes.SWITCH, indicatorChildren: switchLabels })));
55 };
56 Switch.displayName = DISPLAYNAME_PREFIX + ".Switch";
57 Switch = __decorate([
58 polyfill
59 ], Switch);
60 return Switch;
61}(AbstractPureComponent2));
62export { Switch };
63var Radio = /** @class */ (function (_super) {
64 __extends(Radio, _super);
65 function Radio() {
66 return _super !== null && _super.apply(this, arguments) || this;
67 }
68 Radio.prototype.render = function () {
69 return React.createElement(Control, __assign({}, this.props, { type: "radio", typeClassName: Classes.RADIO }));
70 };
71 Radio.displayName = DISPLAYNAME_PREFIX + ".Radio";
72 Radio = __decorate([
73 polyfill
74 ], Radio);
75 return Radio;
76}(AbstractPureComponent2));
77export { Radio };
78var Checkbox = /** @class */ (function (_super) {
79 __extends(Checkbox, _super);
80 function Checkbox() {
81 var _this = _super !== null && _super.apply(this, arguments) || this;
82 _this.state = {
83 indeterminate: _this.props.indeterminate || _this.props.defaultIndeterminate || false,
84 };
85 // must maintain internal reference for `indeterminate` support
86 _this.input = null;
87 _this.handleInputRef = refHandler(_this, "input", _this.props.inputRef);
88 _this.handleChange = function (evt) {
89 var _a, _b;
90 var indeterminate = evt.target.indeterminate;
91 // update state immediately only if uncontrolled
92 if (_this.props.indeterminate == null) {
93 _this.setState({ indeterminate: indeterminate });
94 }
95 // otherwise wait for props change. always invoke handler.
96 (_b = (_a = _this.props).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, evt);
97 };
98 return _this;
99 }
100 Checkbox.getDerivedStateFromProps = function (_a) {
101 var indeterminate = _a.indeterminate;
102 // put props into state if controlled by props
103 if (indeterminate != null) {
104 return { indeterminate: indeterminate };
105 }
106 return null;
107 };
108 Checkbox.prototype.render = function () {
109 var _a = this.props, defaultIndeterminate = _a.defaultIndeterminate, indeterminate = _a.indeterminate, controlProps = __rest(_a, ["defaultIndeterminate", "indeterminate"]);
110 return (React.createElement(Control, __assign({}, controlProps, { inputRef: this.handleInputRef, onChange: this.handleChange, type: "checkbox", typeClassName: Classes.CHECKBOX })));
111 };
112 Checkbox.prototype.componentDidMount = function () {
113 this.updateIndeterminate();
114 };
115 Checkbox.prototype.componentDidUpdate = function (prevProps) {
116 this.updateIndeterminate();
117 if (prevProps.inputRef !== this.props.inputRef) {
118 setRef(prevProps.inputRef, null);
119 this.handleInputRef = refHandler(this, "input", this.props.inputRef);
120 setRef(this.props.inputRef, this.input);
121 }
122 };
123 Checkbox.prototype.updateIndeterminate = function () {
124 if (this.input != null) {
125 this.input.indeterminate = this.state.indeterminate;
126 }
127 };
128 Checkbox.displayName = DISPLAYNAME_PREFIX + ".Checkbox";
129 Checkbox = __decorate([
130 polyfill
131 ], Checkbox);
132 return Checkbox;
133}(AbstractPureComponent2));
134export { Checkbox };
135//# sourceMappingURL=controls.js.map
\No newline at end of file