UNPKG

@datalayer/primer-rjsf

Version:

React JSON Schema Form (RJSF) for Primer

22 lines (21 loc) 1.83 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { FormControl, Radio, RadioGroup } from "@primer/react"; import { ariaDescribedByIds, enumOptionsValueForIndex, optionId, } from "@rjsf/utils"; /** The `RadioWidget` is a widget for rendering a radio group. * It is typically used with a string property constrained with enum options. * * @param props - The `WidgetProps` for this component */ export default function RadioWidget({ id, schema, options, value, required, disabled, readonly, label, onChange, onBlur, onFocus, }) { const { enumOptions, enumDisabled, emptyValue } = options; const _onChange = ({ target: { value } }) => onChange(enumOptionsValueForIndex(value, enumOptions, emptyValue)); const _onBlur = ({ target: { value } }) => onBlur(id, enumOptionsValueForIndex(value, enumOptions, emptyValue)); const _onFocus = ({ target: { value }, }) => onFocus(id, enumOptionsValueForIndex(value, enumOptions, emptyValue)); return (_jsxs(RadioGroup, { name: id, required: required, children: [_jsx(RadioGroup.Label, { children: label || schema.title }), _jsx(_Fragment, { children: Array.isArray(enumOptions) && enumOptions.map((option, index) => { const itemDisabled = Array.isArray(enumDisabled) && enumDisabled.indexOf(option.value) !== -1; const radio = (_jsxs(FormControl, { id: optionId(id, index), children: [_jsx(Radio, { value: String(index), onChange: _onChange, onBlur: _onBlur, onFocus: _onFocus, "aria-describedby": ariaDescribedByIds(id), disabled: disabled || itemDisabled || readonly }, index), _jsx(FormControl.Label, { children: option.label })] }, optionId(id, index))); return radio; }) })] })); }