import type { FocusEvent } from 'react'; import type { WidgetProps, StrictRJSFSchema, RJSFSchema, FormContextType } from '@rjsf/utils'; import { ariaDescribedByIds, descriptionId, getTemplate, schemaRequiresTrueValue } from '@rjsf/utils'; import Form from 'react-bootstrap/Form'; export default function CheckboxWidget< T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any, >(props: WidgetProps) { const { id, htmlName, value, disabled, readonly, label, hideLabel, schema, autofocus, options, onChange, onBlur, onFocus, registry, uiSchema, required, } = props; // 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 trueValueRequired = schemaRequiresTrueValue(schema) && required; const DescriptionFieldTemplate = getTemplate<'DescriptionFieldTemplate', T, S, F>( 'DescriptionFieldTemplate', registry, options, ); const handleChange = ({ target: { checked } }: FocusEvent) => onChange(checked); const handleBlur = ({ target }: FocusEvent) => onBlur(id, target?.checked); const handleFocus = ({ target }: FocusEvent) => onFocus(id, target?.checked); const description = options.description || schema.description; return ( {!hideLabel && description && ( )} {label} {trueValueRequired && *} ) : undefined } checked={typeof value === 'undefined' ? false : value} required={trueValueRequired} disabled={disabled || readonly} autoFocus={autofocus} onChange={handleChange} type='checkbox' onBlur={handleBlur} onFocus={handleFocus} /> ); }