1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __assign, __extends } from "tslib";
|
17 | import * as React from "react";
|
18 | import { AbstractPureComponent2, Classes } from "../../common";
|
19 | import * as Errors from "../../common/errors";
|
20 | import { DISPLAYNAME_PREFIX } from "../../common/props";
|
21 | import { isElementOfType } from "../../common/utils";
|
22 | import { Radio } from "./controls";
|
23 | var counter = 0;
|
24 | function nextName() {
|
25 | return "".concat(RadioGroup.displayName, "-").concat(counter++);
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | var RadioGroup = (function (_super) {
|
33 | __extends(RadioGroup, _super);
|
34 | function RadioGroup() {
|
35 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
36 |
|
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));
|
83 | export { RadioGroup };
|
84 |
|
\ | No newline at end of file |