fix(striker-ui): correct label in SwitchWithLabel

main
Tsu-ba-me 2 years ago
parent 515d3b3b9e
commit cd14783c08
  1. 1
      striker-ui/components/ManageFence/CommonFenceInputGroup.tsx
  2. 47
      striker-ui/components/SwitchWithLabel.tsx
  3. 2
      striker-ui/types/SwitchWithLabel.d.ts

@ -24,7 +24,6 @@ const MAP_TO_INPUT_BUILDER: MapToInputBuilder = {
input={ input={
<SwitchWithLabel <SwitchWithLabel
checked={isChecked} checked={isChecked}
flexBoxProps={{ width: '100%' }}
id={id} id={id}
label={label} label={label}
name={name} name={name}

@ -1,47 +1,46 @@
import { Switch, SxProps, Theme } from '@mui/material'; import {
import { FC, useMemo } from 'react'; FormControlLabel as MUIFormControlLabel,
styled,
Switch as MUISwitch,
} from '@mui/material';
import { FC, ReactElement, useMemo } from 'react';
import { GREY } from '../lib/consts/DEFAULT_THEME'; import { GREY } from '../lib/consts/DEFAULT_THEME';
import FlexBox from './FlexBox';
import { BodyText } from './Text'; import { BodyText } from './Text';
const SwitchFormControlLabel = styled(MUIFormControlLabel)({
height: '3.5em',
marginLeft: 0,
width: '100%',
});
const SwitchWithLabel: FC<SwitchWithLabelProps> = ({ const SwitchWithLabel: FC<SwitchWithLabelProps> = ({
checked: isChecked, checked: isChecked,
flexBoxProps: { sx: flexBoxSx, ...restFlexBoxProps } = {}, formControlLabelProps,
id: switchId, id: switchId,
label, label,
name: switchName, name: switchName,
onChange, onChange,
switchProps, switchProps,
}) => { }) => {
const combinedFlexBoxSx = useMemo<SxProps<Theme>>( const labelElement = useMemo<ReactElement>(
() => ({
'& > :first-child': {
flexGrow: 1,
},
...flexBoxSx,
}),
[flexBoxSx],
);
const labelElement = useMemo(
() => () =>
typeof label === 'string' ? ( typeof label === 'string' ? (
<BodyText inheritColour color={`${GREY}9F`}> <BodyText inheritColour color={`${GREY}AF`}>
{label} {label}
</BodyText> </BodyText>
) : ( ) : (
label <>{label}</>
), ),
[label], [label],
); );
return ( return (
<FlexBox row {...restFlexBoxProps} sx={combinedFlexBoxSx}> <SwitchFormControlLabel
{labelElement} componentsProps={{ typography: { flexGrow: 1 } }}
<Switch control={
<MUISwitch
checked={isChecked} checked={isChecked}
edge="end" edge="end"
id={switchId} id={switchId}
@ -49,7 +48,11 @@ const SwitchWithLabel: FC<SwitchWithLabelProps> = ({
onChange={onChange} onChange={onChange}
{...switchProps} {...switchProps}
/> />
</FlexBox> }
label={labelElement}
labelPlacement="start"
{...formControlLabelProps}
/>
); );
}; };

@ -1,5 +1,5 @@
type SwitchWithLabelOptionalProps = { type SwitchWithLabelOptionalProps = {
flexBoxProps?: import('../components/FlexBox').FlexBoxProps; formControlLabelProps?: import('@mui/material').FormControlLabelProps;
switchProps?: import('@mui/material').SwitchProps; switchProps?: import('@mui/material').SwitchProps;
}; };

Loading…
Cancel
Save