import { ChangeEvent, FocusEvent, useCallback } from 'react'; 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 */ function CheckboxWidget({ schema, uiSchema, options, id, value, disabled, readonly, label, hideLabel, autofocus = false, onBlur, onFocus, onChange, registry, }: 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 description = options.description ?? schema.description; return (
{!hideLabel && !!description && ( (id)} description={description} schema={schema} uiSchema={uiSchema} registry={registry} /> )}
); } export default CheckboxWidget;