UNPKG

@blueprintjs/core

Version:

Core styles & components

80 lines 4.44 kB
"use strict"; /* * Copyright 2025 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RadioGroup = void 0; var tslib_1 = require("tslib"); var classnames_1 = tslib_1.__importDefault(require("classnames")); var React = tslib_1.__importStar(require("react")); var common_1 = require("../../common"); var Errors = tslib_1.__importStar(require("../../common/errors")); var utils_1 = require("../../common/utils"); var useValidateProps_1 = require("../../hooks/useValidateProps"); var radioCard_1 = require("../control-card/radioCard"); var controls_1 = require("./controls"); /** * Radio group component. * * @see https://blueprintjs.com/docs/#core/components/radio.radiogroup */ var RadioGroup = function (props) { var children = props.children, className = props.className, disabled = props.disabled, inline = props.inline, label = props.label, name = props.name, onChange = props.onChange, options = props.options, selectedValue = props.selectedValue, htmlProps = tslib_1.__rest(props, ["children", "className", "disabled", "inline", "label", "name", "onChange", "options", "selectedValue"]); // a unique name for this group, which can be overridden by `name` prop. var autoGroupName = React.useMemo(function () { return nextName(); }, []); var labelId = React.useMemo(function () { return (0, utils_1.uniqueId)("label"); }, []); (0, useValidateProps_1.useValidateProps)(function () { if (children != null && options != null) { console.warn(Errors.RADIOGROUP_WARN_CHILDREN_OPTIONS_MUTEX); } }, [children, options]); var getRadioProps = React.useCallback(function (optionProps) { var optionClassName = optionProps.className, optionDisabled = optionProps.disabled, value = optionProps.value; return { checked: value === selectedValue, className: optionClassName, disabled: optionDisabled || disabled, inline: inline, name: name == null ? autoGroupName : name, onChange: onChange, value: value, }; }, [autoGroupName, disabled, inline, name, onChange, selectedValue]); var renderChildren = function () { return React.Children.map(children, function (child) { if ((0, utils_1.isElementOfType)(child, controls_1.Radio) || (0, utils_1.isElementOfType)(child, radioCard_1.RadioCard)) { return React.cloneElement( // Need this cast here to suppress a TS error caused by differing `ref` types for the Radio and // RadioCard components. We aren't injecting a ref, so we don't need to be strict about that // incompatibility. child, getRadioProps(child.props)); } return child; }); }; var renderOptions = function () { return options === null || options === void 0 ? void 0 : options.map(function (option) { return (React.createElement(controls_1.Radio, tslib_1.__assign({}, getRadioProps(option), { key: option.value, labelElement: option.label || option.value }))); }); }; return (React.createElement("div", tslib_1.__assign({ role: "radiogroup", "aria-labelledby": label ? labelId : undefined }, (0, common_1.removeNonHTMLProps)(htmlProps), { className: (0, classnames_1.default)(common_1.Classes.RADIO_GROUP, className) }), label && (React.createElement("label", { className: common_1.Classes.LABEL, id: labelId }, label)), Array.isArray(options) ? renderOptions() : renderChildren())); }; exports.RadioGroup = RadioGroup; exports.RadioGroup.displayName = "".concat(common_1.DISPLAYNAME_PREFIX, ".RadioGroup"); var counter = 0; function nextName() { return "".concat(exports.RadioGroup.displayName, "-").concat(counter++); } //# sourceMappingURL=radioGroup.js.map