1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __assign, __rest } from "tslib";
|
17 | import classNames from "classnames";
|
18 | import * as React from "react";
|
19 | import { Classes, mergeRefs } from "../../common";
|
20 | import { DISPLAYNAME_PREFIX } from "../../common/props";
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function 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 |
|
37 |
|
38 |
|
39 |
|
40 | export 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 | });
|
52 | Switch.displayName = "".concat(DISPLAYNAME_PREFIX, ".Switch");
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | export var Radio = React.forwardRef(function (props, ref) {
|
59 | return renderControl(__assign(__assign({}, props), { type: "radio", typeClassName: Classes.RADIO }), ref);
|
60 | });
|
61 | Radio.displayName = "".concat(DISPLAYNAME_PREFIX, ".Radio");
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | export 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 |
|
74 | if (indeterminate === undefined) {
|
75 | setIsIndeterminate(evt.target.indeterminate);
|
76 | }
|
77 |
|
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 | });
|
92 | Checkbox.displayName = "".concat(DISPLAYNAME_PREFIX, ".Checkbox");
|
93 |
|
\ | No newline at end of file |