import { FocusEvent } from 'react'; import Checkbox from '@mui/material/Checkbox'; import FormControlLabel from '@mui/material/FormControlLabel'; import { ariaDescribedByIds, descriptionId, getTemplate, labelValue, schemaRequiresTrueValue, FormContextType, RJSFSchema, StrictRJSFSchema, WidgetProps, } from '@rjsf/utils'; /** The `CheckBoxWidget` is a widget for rendering boolean properties. * It is typically used to represent a boolean. * * @param props - The `WidgetProps` for this component */ export default function CheckboxWidget< T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any, >(props: WidgetProps) { const { schema, id, value, disabled, readonly, label = '', hideLabel, autofocus, onChange, onBlur, onFocus, registry, options, uiSchema, } = props; const DescriptionFieldTemplate = getTemplate<'DescriptionFieldTemplate', T, S, F>( 'DescriptionFieldTemplate', registry, options, ); // Because an unchecked checkbox will cause html5 validation to fail, only add // the "required" attribute if the field value must be "true", due to the // "const" or "enum" keywords const required = schemaRequiresTrueValue(schema); const _onChange = (_: any, checked: boolean) => onChange(checked); const _onBlur = ({ target }: FocusEvent) => onBlur(id, target && target.value); const _onFocus = ({ target }: FocusEvent) => onFocus(id, target && target.value); const description = options.description ?? schema.description; return ( <> {!hideLabel && description && ( (id)} description={description} schema={schema} uiSchema={uiSchema} registry={registry} /> )} (id)} /> } label={labelValue(label, hideLabel, false)} /> ); }