anvil/striker-ui/components/SwitchWithLabel.tsx

69 lines
1.4 KiB
TypeScript
Raw Normal View History

import {
FormControlLabel as MUIFormControlLabel,
styled,
Switch as MUISwitch,
} from '@mui/material';
import { FC, ReactElement, useMemo } from 'react';
2023-02-17 05:55:25 +00:00
import { GREY } from '../lib/consts/DEFAULT_THEME';
import { BodyText } from './Text';
const SwitchFormControlLabel = styled(MUIFormControlLabel)({
height: '3.5em',
marginLeft: 0,
width: '100%',
});
2023-02-17 05:55:25 +00:00
const SwitchWithLabel: FC<SwitchWithLabelProps> = ({
baseInputProps,
2023-02-17 05:55:25 +00:00
checked: isChecked,
formControlLabelProps,
2023-02-17 05:55:25 +00:00
id: switchId,
label,
name: switchName,
onChange,
switchProps,
}) => {
const labelElement = useMemo<ReactElement>(
2023-02-17 05:55:25 +00:00
() =>
typeof label === 'string' ? (
<BodyText inheritColour color={`${GREY}AF`}>
2023-02-17 05:55:25 +00:00
{label}
</BodyText>
) : (
<>{label}</>
2023-02-17 05:55:25 +00:00
),
[label],
);
return (
<>
<SwitchFormControlLabel
componentsProps={{ typography: { flexGrow: 1 } }}
control={
<MUISwitch
checked={isChecked}
edge="end"
name={switchName}
onChange={onChange}
{...switchProps}
/>
}
label={labelElement}
labelPlacement="start"
{...formControlLabelProps}
/>
<input
checked={isChecked}
hidden
id={switchId}
readOnly
{...baseInputProps}
/>
</>
2023-02-17 05:55:25 +00:00
);
};
export default SwitchWithLabel;