UNPKG

@geneui/components

Version:

The Gene UI components library designed for BI tools

105 lines (102 loc) 2.88 kB
import { _ as _extends } from '../_rollupPluginBabelHelpers-e8fb2e5c.js'; import React__default, { useState, useCallback } from 'react'; import PropTypes from 'prop-types'; import { c as classnames } from '../index-031ff73c.js'; import { c as childrenOf } from '../index-a0e4e333.js'; import Radio from '../Radio/index.js'; import '../dateValidation-67caec66.js'; import '../_commonjsHelpers-24198af3.js'; import 'react-dom'; import '../configs-00612ce0.js'; import '../checkboxRadioSwitcher-5b69d7bd.js'; import '../style-inject.es-746bb8ed.js'; import '../guid-8ddf77b3.js'; function RadioGroup(props) { const { defaultValue, name, options, descriptionKey, disabled, onChange, required, type, ...restProps } = props; const [valueState, setValueState] = useState(defaultValue); const isControlled = !!props.value; const value = isControlled ? props.value : valueState; const handleChange = useCallback(e => { !isControlled && setValueState(e.target.value); onChange && onChange(e); }, [isControlled, onChange]); return /*#__PURE__*/React__default.createElement("div", { className: classnames('cha-ra-group', "t-".concat(type)) }, options.map(option => { const isOptionsString = typeof option === 'string'; const optionValue = isOptionsString ? option : option.value; const label = isOptionsString ? option : option.label; return /*#__PURE__*/React__default.createElement(Radio, _extends({ key: optionValue, name: name, label: label, description: option[descriptionKey], disabled: disabled || option.disabled, checked: optionValue === value, onChange: handleChange }, restProps, { value: optionValue, type: type })); })); } RadioGroup.propTypes = { /** * Disabled state */ disabled: PropTypes.bool, /** * Validation state */ required: PropTypes.bool, /** * Options/items for radio group */ options: PropTypes.arrayOf(PropTypes.oneOfType([childrenOf([Radio]), PropTypes.shape({ label: Radio.propTypes.label, value: Radio.propTypes.value, disabled: Radio.propTypes.disabled })])), /** * Initial value. */ defaultValue: Radio.propTypes.value, /** * Name of appropriate radio group */ name: Radio.propTypes.name.isRequired, /** * Selected option's value */ value: Radio.propTypes.value, /** * Fires an event on change((event: Event) => void) */ onChange: PropTypes.func, /** * Type defines the appearance of the radio */ type: PropTypes.oneOf(['default', 'tab']), /** * Description definer: one of keys from your object */ descriptionKey: PropTypes.string }; RadioGroup.defaultProps = { disabled: false, required: false, options: [], type: 'default', descriptionKey: 'description' }; export { RadioGroup as default };