@yandex/ui
Version:
Yandex UI components
34 lines (33 loc) • 1.82 kB
JavaScript
import { __assign, __read, __rest } from "tslib";
import React, { useState, useEffect } from 'react';
import { useComponentRegistry } from '@bem-react/di';
import { cn } from '@bem-react/classname';
import { RadioGroupProvider } from './Radiobox.context';
import './Radiobox.css';
export var cnRadiobox = cn('Radiobox');
/**
* Компонент для создания радиопереключателя.
* @param {RadioboxProps} props Свойства компонента.
*/
export var Radiobox = function (_a) {
var children = _a.children, className = _a.className, disabled = _a.disabled, innerRef = _a.innerRef, name = _a.name, onChange = _a.onChange, _b = _a.options, options = _b === void 0 ? [] : _b, value = _a.value,
// Извлекаем свойства, т.к. они не нужны на DOM узле
// FIXME: https://github.com/bem/bem-react/issues/381
// @ts-ignore
_view = _a.view,
// @ts-ignore
_size = _a.size,
// @ts-ignore
_theme = _a.theme;
var Radio = useComponentRegistry(cnRadiobox()).Radio;
var _c = __read(useState({ onChange: onChange, value: value, disabled: disabled, name: name }), 2), context = _c[0], setContext = _c[1];
useEffect(function () {
setContext({ onChange: onChange, value: value, disabled: disabled, name: name });
}, [onChange, value, disabled, name]);
return (React.createElement("span", { className: cnRadiobox(null, [className]), ref: innerRef, role: "radiogroup" },
React.createElement(RadioGroupProvider, { value: context }, children || options.map(function (_a) {
var label = _a.label, option = __rest(_a, ["label"]);
return (React.createElement(Radio, __assign({ key: option.value }, option), label));
}))));
};
Radiobox.displayName = cnRadiobox();