Support descriptions in "CheckboxInput"

This commit is contained in:
schroda
2026-05-17 13:36:41 +02:00
parent 710e7c0deb
commit 6564484540
2 changed files with 47 additions and 76 deletions

View File

@@ -10,12 +10,43 @@ import type { CheckboxProps } from '@mui/material/Checkbox';
import Checkbox from '@mui/material/Checkbox';
import type { FormControlLabelProps } from '@mui/material/FormControlLabel';
import FormControlLabel from '@mui/material/FormControlLabel';
import React from 'react';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
interface IProps extends CheckboxProps {
label?: FormControlLabelProps['label'];
}
type BaseProps = CheckboxProps;
type LabelProps = { label?: FormControlLabelProps['label'] };
type PrimaryTextProps = {
primaryText?: FormControlLabelProps['label'];
secondaryText?: FormControlLabelProps['label'];
};
export const CheckboxInput: React.FC<IProps> = ({ label, sx, ...rest }) => (
<FormControlLabel control={<Checkbox {...rest} />} label={label} sx={sx} />
type Props = BaseProps &
((LabelProps & PropertiesNever<PrimaryTextProps>) | (PropertiesNever<LabelProps> & PrimaryTextProps));
export const CheckboxInput = ({ primaryText, secondaryText, sx, ...rest }: Props) => (
<FormControlLabel
control={<Checkbox {...rest} />}
label={
primaryText && !secondaryText ? (
primaryText
) : (
<Stack
sx={{
// Padding comes from the MUI Checkbox component
pt: '9px',
}}
>
{typeof primaryText === 'string' ? <Typography>{primaryText}</Typography> : primaryText}
{typeof secondaryText === 'string' ? (
<Typography variant="body2" color="textSecondary">
{secondaryText}
</Typography>
) : (
secondaryText
)}
</Stack>
)
}
sx={sx}
/>
);