import { ChangeEvent, FocusEvent, useCallback } from 'react'; import { ariaDescribedByIds, descriptionId, getTemplate, labelValue, schemaRequiresTrueValue, FormContextType, RJSFSchema, StrictRJSFSchema, WidgetProps, getUiOptions, } 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 */ function CheckboxWidget({ schema, uiSchema, options, id, value, disabled, readonly, label, hideLabel, autofocus = false, onBlur, onFocus, onChange, registry, htmlName, }: WidgetProps) { 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 handleChange = useCallback( (event: ChangeEvent) => onChange(event.target.checked), [onChange], ); const handleBlur = useCallback( (event: FocusEvent) => onBlur(id, event.target.checked), [onBlur, id], ); const handleFocus = useCallback( (event: FocusEvent) => onFocus(id, event.target.checked), [onFocus, id], ); const uiOptions = getUiOptions(uiSchema); const isCheckboxWidget = uiOptions.widget === 'checkbox'; const description = isCheckboxWidget ? undefined : (options.description ?? schema.description); return (
{!hideLabel && description && ( )}
); } export default CheckboxWidget;