UNPKG

5.32 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, __rest } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { Classes, mergeRefs } from "../../common";
20import { DISPLAYNAME_PREFIX } from "../../common/props";
21/**
22 * Renders common control elements, with additional props to customize appearance.
23 * This function is not exported and is only used within this module for `Checkbox`, `Radio`, and `Switch` below.
24 */
25function renderControl(props, ref) {
26 var _a;
27 var alignIndicator = props.alignIndicator, children = props.children, className = props.className, indicatorChildren = props.indicatorChildren, inline = props.inline, inputRef = props.inputRef, label = props.label, labelElement = props.labelElement, large = props.large, style = props.style, type = props.type, typeClassName = props.typeClassName, _b = props.tagName, tagName = _b === void 0 ? "label" : _b, htmlProps = __rest(props, ["alignIndicator", "children", "className", "indicatorChildren", "inline", "inputRef", "label", "labelElement", "large", "style", "type", "typeClassName", "tagName"]);
28 var classes = classNames(Classes.CONTROL, typeClassName, (_a = {},
29 _a[Classes.DISABLED] = htmlProps.disabled,
30 _a[Classes.INLINE] = inline,
31 _a[Classes.LARGE] = large,
32 _a), Classes.alignmentClass(alignIndicator), className);
33 return React.createElement(tagName, { className: classes, style: style, ref: ref }, React.createElement("input", __assign({}, htmlProps, { ref: inputRef, type: type })), React.createElement("span", { className: Classes.CONTROL_INDICATOR }, indicatorChildren), label, labelElement, children);
34}
35/**
36 * Switch component.
37 *
38 * @see https://blueprintjs.com/docs/#core/components/switch
39 */
40export var Switch = React.forwardRef(function (_a, ref) {
41 var innerLabelChecked = _a.innerLabelChecked, innerLabel = _a.innerLabel, controlProps = __rest(_a, ["innerLabelChecked", "innerLabel"]);
42 var switchLabels = innerLabel || innerLabelChecked
43 ? [
44 React.createElement("div", { key: "checked", className: Classes.CONTROL_INDICATOR_CHILD },
45 React.createElement("div", { className: Classes.SWITCH_INNER_TEXT }, innerLabelChecked ? innerLabelChecked : innerLabel)),
46 React.createElement("div", { key: "unchecked", className: Classes.CONTROL_INDICATOR_CHILD },
47 React.createElement("div", { className: Classes.SWITCH_INNER_TEXT }, innerLabel)),
48 ]
49 : null;
50 return renderControl(__assign(__assign({}, controlProps), { indicatorChildren: switchLabels, type: "checkbox", typeClassName: Classes.SWITCH }), ref);
51});
52Switch.displayName = "".concat(DISPLAYNAME_PREFIX, ".Switch");
53/**
54 * Radio component.
55 *
56 * @see https://blueprintjs.com/docs/#core/components/radio
57 */
58export var Radio = React.forwardRef(function (props, ref) {
59 return renderControl(__assign(__assign({}, props), { type: "radio", typeClassName: Classes.RADIO }), ref);
60});
61Radio.displayName = "".concat(DISPLAYNAME_PREFIX, ".Radio");
62/**
63 * Checkbox component.
64 *
65 * @see https://blueprintjs.com/docs/#core/components/checkbox
66 */
67export var Checkbox = React.forwardRef(function (props, ref) {
68 var defaultIndeterminate = props.defaultIndeterminate, indeterminate = props.indeterminate, onChange = props.onChange, controlProps = __rest(props, ["defaultIndeterminate", "indeterminate", "onChange"]);
69 var _a = React.useState(indeterminate || defaultIndeterminate || false), isIndeterminate = _a[0], setIsIndeterminate = _a[1];
70 var localInputRef = React.useRef(null);
71 var inputRef = props.inputRef === undefined ? localInputRef : mergeRefs(props.inputRef, localInputRef);
72 var handleChange = React.useCallback(function (evt) {
73 // update state immediately only if uncontrolled
74 if (indeterminate === undefined) {
75 setIsIndeterminate(evt.target.indeterminate);
76 }
77 // otherwise wait for props change. always invoke handler.
78 onChange === null || onChange === void 0 ? void 0 : onChange(evt);
79 }, [onChange]);
80 React.useEffect(function () {
81 if (indeterminate !== undefined) {
82 setIsIndeterminate(indeterminate);
83 }
84 }, [indeterminate]);
85 React.useEffect(function () {
86 if (localInputRef.current != null) {
87 localInputRef.current.indeterminate = isIndeterminate;
88 }
89 }, [localInputRef, isIndeterminate]);
90 return renderControl(__assign(__assign({}, controlProps), { inputRef: inputRef, onChange: handleChange, type: "checkbox", typeClassName: Classes.CHECKBOX }), ref);
91});
92Checkbox.displayName = "".concat(DISPLAYNAME_PREFIX, ".Checkbox");
93//# sourceMappingURL=controls.js.map
\No newline at end of file