From d5feaa5f3b5f40ffe07dbb49fc97666308ccf993 Mon Sep 17 00:00:00 2001 From: Emil Laine Date: Wed, 26 Oct 2022 15:36:01 +0300 Subject: [PATCH] [Chip] Don't override icon color (#34247) --- packages/mui-material/src/Chip/Chip.js | 17 ++++++++++------- packages/mui-material/src/Chip/Chip.test.js | 21 ++++++++++++++++++++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/mui-material/src/Chip/Chip.js b/packages/mui-material/src/Chip/Chip.js index 097d79a43c55ca..44bbe36d98464a 100644 --- a/packages/mui-material/src/Chip/Chip.js +++ b/packages/mui-material/src/Chip/Chip.js @@ -13,7 +13,7 @@ import styled from '../styles/styled'; import chipClasses, { getChipUtilityClass } from './chipClasses'; const useUtilityClasses = (ownerState) => { - const { classes, disabled, size, color, onDelete, clickable, variant } = ownerState; + const { classes, disabled, size, color, iconColor, onDelete, clickable, variant } = ownerState; const slots = { root: [ @@ -30,7 +30,7 @@ const useUtilityClasses = (ownerState) => { ], label: ['label', `label${capitalize(size)}`], avatar: ['avatar', `avatar${capitalize(size)}`, `avatarColor${capitalize(color)}`], - icon: ['icon', `icon${capitalize(size)}`, `iconColor${capitalize(color)}`], + icon: ['icon', `icon${capitalize(size)}`, `iconColor${capitalize(iconColor)}`], deleteIcon: [ 'deleteIcon', `deleteIcon${capitalize(size)}`, @@ -47,7 +47,7 @@ const ChipRoot = styled('div', { slot: 'Root', overridesResolver: (props, styles) => { const { ownerState } = props; - const { color, clickable, onDelete, size, variant } = ownerState; + const { color, iconColor, clickable, onDelete, size, variant } = ownerState; return [ { [`& .${chipClasses.avatar}`]: styles.avatar }, @@ -55,7 +55,7 @@ const ChipRoot = styled('div', { { [`& .${chipClasses.avatar}`]: styles[`avatarColor${capitalize(color)}`] }, { [`& .${chipClasses.icon}`]: styles.icon }, { [`& .${chipClasses.icon}`]: styles[`icon${capitalize(size)}`] }, - { [`& .${chipClasses.icon}`]: styles[`iconColor${capitalize(color)}`] }, + { [`& .${chipClasses.icon}`]: styles[`iconColor${capitalize(iconColor)}`] }, { [`& .${chipClasses.deleteIcon}`]: styles.deleteIcon }, { [`& .${chipClasses.deleteIcon}`]: styles[`deleteIcon${capitalize(size)}`] }, { [`& .${chipClasses.deleteIcon}`]: styles[`deleteIconColor${capitalize(color)}`] }, @@ -129,7 +129,6 @@ const ChipRoot = styled('div', { fontSize: theme.typography.pxToRem(10), }, [`& .${chipClasses.icon}`]: { - color: theme.vars ? theme.vars.palette.Chip.defaultIconColor : textColor, marginLeft: 5, marginRight: -6, ...(ownerState.size === 'small' && { @@ -137,8 +136,11 @@ const ChipRoot = styled('div', { marginLeft: 4, marginRight: -4, }), - ...(ownerState.color !== 'default' && { - color: 'inherit', + ...(ownerState.iconColor === ownerState.color && { + color: theme.vars ? theme.vars.palette.Chip.defaultIconColor : textColor, + ...(ownerState.color !== 'default' && { + color: 'inherit', + }), }), }, [`& .${chipClasses.deleteIcon}`]: { @@ -396,6 +398,7 @@ const Chip = React.forwardRef(function Chip(inProps, ref) { disabled, size, color, + iconColor: React.isValidElement(iconProp) ? iconProp.props.color || color : color, onDelete: !!onDelete, clickable, variant, diff --git a/packages/mui-material/src/Chip/Chip.test.js b/packages/mui-material/src/Chip/Chip.test.js index 127261024d82be..1e1c2fcb23ea85 100644 --- a/packages/mui-material/src/Chip/Chip.test.js +++ b/packages/mui-material/src/Chip/Chip.test.js @@ -12,8 +12,9 @@ import { } from 'test/utils'; import Avatar from '@mui/material/Avatar'; import Chip, { chipClasses as classes } from '@mui/material/Chip'; -import { ThemeProvider, createTheme } from '@mui/material/styles'; +import { ThemeProvider, createTheme, hexToRgb } from '@mui/material/styles'; import CheckBox from '../internal/svg-icons/CheckBox'; +import defaultTheme from '../styles/defaultTheme'; describe('', () => { const { render } = createRenderer(); @@ -594,6 +595,24 @@ describe('', () => { expect(getByTestId('test-icon')).to.have.class(classes.icon); }); + + it("should not override the icon's custom color", () => { + const { getByTestId } = render( + + } />, + } color="error" />, + , + ); + + expect(getByTestId('test-icon')).to.have.class('MuiChip-iconColorSuccess'); + expect(getByTestId('test-icon2')).to.have.class('MuiChip-iconColorSuccess'); + expect(getByTestId('test-icon')).toHaveComputedStyle({ + color: hexToRgb(defaultTheme.palette.success.main), + }); + expect(getByTestId('test-icon2')).toHaveComputedStyle({ + color: hexToRgb(defaultTheme.palette.success.main), + }); + }); }); describe('prop: size', () => {