File size: 2,012 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import type { CheckboxProps } from '@mui/material/Checkbox';
import Checkbox from '@mui/material/Checkbox';
import FormControlLabel from '@mui/material/FormControlLabel';
import FormGroup from '@mui/material/FormGroup';
import FormLabel from '@mui/material/FormLabel';
import type { SwitchProps } from '@mui/material/Switch';
import Switch from '@mui/material/Switch';
import omit from 'lodash/omit';
import type { Ref } from 'react';
import React from 'react';
import type { FieldProps } from 'uniforms';
import { connectField, filterDOMProps } from 'uniforms';
import wrapField from './wrapField';
export type BoolFieldProps = FieldProps<
boolean,
CheckboxProps | SwitchProps,
{
appearance?: 'checkbox' | 'switch';
fullWidth?: boolean;
helperText?: string;
legend?: string;
margin?: 'dense' | 'normal' | 'none';
transform?: (label: string) => string;
}
>;
function Bool(props: BoolFieldProps) {
const {
appearance,
disabled,
inputRef,
label,
legend,
name,
onChange,
readOnly,
transform,
value,
} = props;
const SelectionControl =
appearance === 'checkbox' || appearance === undefined ? Checkbox : Switch;
return wrapField(
{ fullWidth: true, ...props },
legend && (
<FormLabel component="legend" htmlFor={name}>
{legend}
</FormLabel>
),
<FormGroup>
<FormControlLabel
control={
<SelectionControl
checked={!!value}
name={name}
onChange={(event) =>
!disabled &&
!readOnly &&
onChange &&
onChange(event.target.checked)
}
ref={inputRef as Ref<HTMLButtonElement>}
value={name}
{...omit(filterDOMProps(props), ['helperText', 'fullWidth'])}
/>
}
label={transform ? transform(label as string) : label}
/>
</FormGroup>
);
}
export default connectField<BoolFieldProps>(Bool, { kind: 'leaf' });
|