import { FocusEvent } from 'react'; import { Checkbox, CheckboxProps } from 'antd'; import { ariaDescribedByIds, labelValue, FormContextType, RJSFSchema, StrictRJSFSchema, WidgetProps, GenericObjectType, } 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 { autofocus, disabled, registry, id, htmlName, label, hideLabel, onBlur, onChange, onFocus, readonly, value } = props; const { formContext } = registry; const { readonlyAsDisabled = true } = formContext as GenericObjectType; const handleChange: NonNullable = ({ target }) => onChange(target.checked); const handleBlur = ({ target }: FocusEvent) => onBlur(id, target && target.checked); const handleFocus = ({ target }: FocusEvent) => onFocus(id, target && target.checked); // Antd's typescript definitions do not contain the following props that are actually necessary and, if provided, // they are used, so hacking them in via by spreading `extraProps` on the component to avoid typescript errors const extraProps = { onBlur: !readonly ? handleBlur : undefined, onFocus: !readonly ? handleFocus : undefined, }; return ( {labelValue(label, hideLabel, '')} ); }