import Checkbox, { CheckboxProps } from '@mui/material/Checkbox'; import FormControlLabel from '@mui/material/FormControlLabel'; import FormGroup from '@mui/material/FormGroup'; import FormLabel from '@mui/material/FormLabel'; import Switch, { SwitchProps } from '@mui/material/Switch'; import { useThemeProps } from '@mui/system'; import omit from 'lodash/omit'; import React, { Ref } from 'react'; import { FieldProps, connectField, filterDOMProps } from 'uniforms'; import wrapField from './wrapField'; export type BoolFieldProps = FieldProps< boolean, CheckboxProps | SwitchProps, { label: string; appearance?: 'checkbox' | 'switch'; fullWidth?: boolean; helperText?: string; legend?: string; margin?: string | number; transform?: (label: string) => string; } >; function Bool(props: BoolFieldProps) { const { appearance, disabled, inputRef, label, legend, name, onChange, readOnly, transform, value } = props; const formControlThemeProps = useThemeProps({ props, name: 'MuiFormControl' }); const SelectionControl = appearance === 'checkbox' || appearance === undefined ? Checkbox : Switch; return wrapField( { ...(formControlThemeProps?.fullWidth === undefined && { fullWidth: true }), ...(formControlThemeProps?.margin === undefined && { margin: 'dense' }), ...props, component: 'fieldset' }, legend && ( {legend} ), !disabled && !readOnly && onChange && onChange(event.target.checked) } ref={inputRef as Ref} value={name} {...omit(filterDOMProps(props), ['helperText', 'fullWidth'])} /> } label={transform ? transform(label as string) : label} /> ); } export default connectField(Bool, { kind: 'leaf' });