1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __assign, __decorate, __extends } from "tslib";
|
17 | import * as React from "react";
|
18 | import { polyfill } from "react-lifecycles-compat";
|
19 | import { AbstractPureComponent2, Classes } from "../../common";
|
20 | import * as Errors from "../../common/errors";
|
21 | import { DISPLAYNAME_PREFIX } from "../../common/props";
|
22 | import { isElementOfType } from "../../common/utils";
|
23 | import { Radio } from "./controls";
|
24 | var counter = 0;
|
25 | function nextName() {
|
26 | return RadioGroup.displayName + "-" + counter++;
|
27 | }
|
28 | var RadioGroup = (function (_super) {
|
29 | __extends(RadioGroup, _super);
|
30 | function RadioGroup() {
|
31 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
32 |
|
33 | _this.autoGroupName = nextName();
|
34 | return _this;
|
35 | }
|
36 | RadioGroup.prototype.render = function () {
|
37 | var label = this.props.label;
|
38 | return (React.createElement("div", { className: this.props.className },
|
39 | label == null ? null : React.createElement("label", { className: Classes.LABEL }, label),
|
40 | Array.isArray(this.props.options) ? this.renderOptions() : this.renderChildren()));
|
41 | };
|
42 | RadioGroup.prototype.validateProps = function () {
|
43 | if (this.props.children != null && this.props.options != null) {
|
44 | console.warn(Errors.RADIOGROUP_WARN_CHILDREN_OPTIONS_MUTEX);
|
45 | }
|
46 | };
|
47 | RadioGroup.prototype.renderChildren = function () {
|
48 | var _this = this;
|
49 | return React.Children.map(this.props.children, function (child) {
|
50 | if (isElementOfType(child, Radio)) {
|
51 | return React.cloneElement(child, _this.getRadioProps(child.props));
|
52 | }
|
53 | else {
|
54 | return child;
|
55 | }
|
56 | });
|
57 | };
|
58 | RadioGroup.prototype.renderOptions = function () {
|
59 | var _this = this;
|
60 | var _a;
|
61 | 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 }))); });
|
62 | };
|
63 | RadioGroup.prototype.getRadioProps = function (optionProps) {
|
64 | var name = this.props.name;
|
65 | var className = optionProps.className, disabled = optionProps.disabled, value = optionProps.value;
|
66 | return {
|
67 | checked: value === this.props.selectedValue,
|
68 | className: className,
|
69 | disabled: disabled || this.props.disabled,
|
70 | inline: this.props.inline,
|
71 | name: name == null ? this.autoGroupName : name,
|
72 | onChange: this.props.onChange,
|
73 | value: value,
|
74 | };
|
75 | };
|
76 | RadioGroup.displayName = DISPLAYNAME_PREFIX + ".RadioGroup";
|
77 | RadioGroup = __decorate([
|
78 | polyfill
|
79 | ], RadioGroup);
|
80 | return RadioGroup;
|
81 | }(AbstractPureComponent2));
|
82 | export { RadioGroup };
|
83 |
|
\ | No newline at end of file |