From 792fdad779f26b5985c2034d03d47b9b985c4e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Ooms?= Date: Wed, 2 Mar 2022 12:30:51 +0100 Subject: [PATCH] :recycle: refactor(StaticAllergyChip): Extract ColorChip. --- imports/ui/allergies/StaticAllergyChip.tsx | 38 ++------------- imports/ui/chips/ColorChip.tsx | 57 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 imports/ui/chips/ColorChip.tsx diff --git a/imports/ui/allergies/StaticAllergyChip.tsx b/imports/ui/allergies/StaticAllergyChip.tsx index 4270f9d08..a3040c7bc 100644 --- a/imports/ui/allergies/StaticAllergyChip.tsx +++ b/imports/ui/allergies/StaticAllergyChip.tsx @@ -1,18 +1,13 @@ import React from 'react'; -import makeStyles from '@mui/styles/makeStyles'; -import {darken} from '@mui/material/styles'; -import classNames from 'classnames'; - import {Link} from 'react-router-dom'; -import Chip, {ChipProps} from '@mui/material/Chip'; - -import {colord} from 'colord'; +import {ChipProps} from '@mui/material/Chip'; import {myEncodeURIComponent} from '../../util/uri'; import {AllergyDocument} from '../../api/collection/allergies'; +import ColorChip from '../chips/ColorChip'; interface AddedProps { loading?: boolean; @@ -21,35 +16,12 @@ interface AddedProps { type StaticAllergyChipProps = ChipProps & AddedProps; -const styles = { - chip({loading, color}) { - const backgroundColor = loading ? '#999' : color; - const computedColor = loading - ? '#eee' - : color - ? colord(color).isLight() - ? '#111' - : '#ddd' - : undefined; - return { - backgroundColor, - color: computedColor, - '&:hover, &:focus': { - backgroundColor: backgroundColor && darken(backgroundColor, 0.1), - }, - }; - }, -}; - -const useStyles = makeStyles(styles); - const StaticAllergyChip = React.forwardRef( - ({loading = false, item, className, ...rest}, ref) => { + ({loading = false, item, ...rest}, ref) => { let component: React.ElementType; let to: string; const clickable = Boolean(item && !rest.onDelete); - const classes = useStyles({loading, color: item?.color}); if (clickable) { component = Link; @@ -57,9 +29,9 @@ const StaticAllergyChip = React.forwardRef( } return ( - , 'color'>, + ref, + ) => { + const classes = useStyles({color}); + return ( + + ); + }, +); + +export default ColorChip;