parent
8fa54b7290
commit
e3ebbac07e
2 changed files with 94 additions and 0 deletions
@ -0,0 +1,74 @@ |
||||
import { |
||||
FormControl as MUIFormControl, |
||||
FormControlLabel as MUIFormControlLabel, |
||||
FormLabel as MUIFormLabel, |
||||
Radio as MUIRadio, |
||||
radioClasses as muiRadioClasses, |
||||
RadioGroup as MUIRadioGroup, |
||||
} from '@mui/material'; |
||||
import { FC, useMemo } from 'react'; |
||||
|
||||
import { GREY } from '../lib/consts/DEFAULT_THEME'; |
||||
|
||||
import { BodyText } from './Text'; |
||||
|
||||
const RadioGroupWithLabel: FC<RadioGroupWithLabelProps> = ({ |
||||
formControlProps, |
||||
formControlLabelProps, |
||||
formLabelProps, |
||||
id, |
||||
label, |
||||
onChange: onRadioGroupChange, |
||||
radioItems, |
||||
radioProps: { sx: radioSx, ...restRadioProps } = {}, |
||||
radioGroupProps, |
||||
}) => { |
||||
const labelElement = useMemo( |
||||
() => (typeof label === 'string' ? <BodyText>{label}</BodyText> : label), |
||||
[label], |
||||
); |
||||
const itemElements = useMemo(() => { |
||||
const items = Object.entries(radioItems); |
||||
|
||||
return items.map(([itemId, { label: itemLabel, value }]) => { |
||||
const itemLabelElement = |
||||
typeof itemLabel === 'string' ? ( |
||||
<BodyText>{itemLabel}</BodyText> |
||||
) : ( |
||||
itemLabel |
||||
); |
||||
|
||||
return ( |
||||
<MUIFormControlLabel |
||||
control={ |
||||
<MUIRadio |
||||
{...restRadioProps} |
||||
sx={{ |
||||
[`&.${muiRadioClasses.root}`]: { |
||||
color: GREY, |
||||
}, |
||||
|
||||
...radioSx, |
||||
}} |
||||
/> |
||||
} |
||||
key={`${id}-${itemId}`} |
||||
value={value} |
||||
label={itemLabelElement} |
||||
{...formControlLabelProps} |
||||
/> |
||||
); |
||||
}); |
||||
}, [formControlLabelProps, id, radioItems, radioSx, restRadioProps]); |
||||
|
||||
return ( |
||||
<MUIFormControl {...formControlProps}> |
||||
<MUIFormLabel {...formLabelProps}>{labelElement}</MUIFormLabel> |
||||
<MUIRadioGroup onChange={onRadioGroupChange} row {...radioGroupProps}> |
||||
{itemElements} |
||||
</MUIRadioGroup> |
||||
</MUIFormControl> |
||||
); |
||||
}; |
||||
|
||||
export default RadioGroupWithLabel; |
@ -0,0 +1,20 @@ |
||||
type RadioItem<RadioItemValue> = { |
||||
label: import('@mui/material').FormControlLabelProps['label']; |
||||
value: RadioItemValue; |
||||
}; |
||||
|
||||
type RadioGroupWithLabelOptionalProps = { |
||||
formControlProps?: import('@mui/material').FormControlProps; |
||||
formControlLabelProps?: import('@mui/material').FormControlLabelProps; |
||||
formLabelProps?: import('@mui/material').FormLabelProps; |
||||
label?: import('react').ReactNode; |
||||
onChange?: import('@mui/material').RadioGroupProps['onChange']; |
||||
radioProps?: import('@mui/material').RadioProps; |
||||
radioGroupProps?: import('@mui/material').RadioGroupProps; |
||||
}; |
||||
|
||||
type RadioGroupWithLabelProps<RadioItemValue = string> = |
||||
RadioGroupWithLabelOptionalProps & { |
||||
id: string; |
||||
radioItems: { [id: string]: RadioItem<RadioItemValue> }; |
||||
}; |
Loading…
Reference in new issue