UNPKG

3.61 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, __extends } from "tslib";
17import * as React from "react";
18import { AbstractPureComponent2, Classes } from "../../common";
19import * as Errors from "../../common/errors";
20import { DISPLAYNAME_PREFIX } from "../../common/props";
21import { isElementOfType } from "../../common/utils";
22import { Radio } from "./controls";
23var counter = 0;
24function nextName() {
25 return "".concat(RadioGroup.displayName, "-").concat(counter++);
26}
27/**
28 * Radio group component.
29 *
30 * @see https://blueprintjs.com/docs/#core/components/radio.radiogroup
31 */
32var RadioGroup = /** @class */ (function (_super) {
33 __extends(RadioGroup, _super);
34 function RadioGroup() {
35 var _this = _super !== null && _super.apply(this, arguments) || this;
36 // a unique name for this group, which can be overridden by `name` prop.
37 _this.autoGroupName = nextName();
38 return _this;
39 }
40 RadioGroup.prototype.render = function () {
41 var label = this.props.label;
42 return (React.createElement("div", { className: this.props.className },
43 label == null ? null : React.createElement("label", { className: Classes.LABEL }, label),
44 Array.isArray(this.props.options) ? this.renderOptions() : this.renderChildren()));
45 };
46 RadioGroup.prototype.validateProps = function () {
47 if (this.props.children != null && this.props.options != null) {
48 console.warn(Errors.RADIOGROUP_WARN_CHILDREN_OPTIONS_MUTEX);
49 }
50 };
51 RadioGroup.prototype.renderChildren = function () {
52 var _this = this;
53 return React.Children.map(this.props.children, function (child) {
54 if (isElementOfType(child, Radio)) {
55 return React.cloneElement(child, _this.getRadioProps(child.props));
56 }
57 else {
58 return child;
59 }
60 });
61 };
62 RadioGroup.prototype.renderOptions = function () {
63 var _this = this;
64 var _a;
65 return (_a = this.props.options) === null || _a === void 0 ? void 0 : _a.map(function (option) { return (React.createElement(Radio, __assign({}, _this.getRadioProps(option), { key: option.value, labelElement: option.label || option.value }))); });
66 };
67 RadioGroup.prototype.getRadioProps = function (optionProps) {
68 var name = this.props.name;
69 var className = optionProps.className, disabled = optionProps.disabled, value = optionProps.value;
70 return {
71 checked: value === this.props.selectedValue,
72 className: className,
73 disabled: disabled || this.props.disabled,
74 inline: this.props.inline,
75 name: name == null ? this.autoGroupName : name,
76 onChange: this.props.onChange,
77 value: value,
78 };
79 };
80 RadioGroup.displayName = "".concat(DISPLAYNAME_PREFIX, ".RadioGroup");
81 return RadioGroup;
82}(AbstractPureComponent2));
83export { RadioGroup };
84//# sourceMappingURL=radioGroup.js.map
\No newline at end of file