Skip to content

Commit

Permalink
[Chip] Don't override icon color (#34247)
Browse files Browse the repository at this point in the history
  • Loading branch information
emlai authored Oct 26, 2022
1 parent fd06295 commit d5feaa5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
17 changes: 10 additions & 7 deletions packages/mui-material/src/Chip/Chip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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)}`,
Expand All @@ -47,15 +47,15 @@ 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 },
{ [`& .${chipClasses.avatar}`]: styles[`avatar${capitalize(size)}`] },
{ [`& .${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)}`] },
Expand Down Expand Up @@ -129,16 +129,18 @@ 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' && {
fontSize: 18,
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}`]: {
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 20 additions & 1 deletion packages/mui-material/src/Chip/Chip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<Chip />', () => {
const { render } = createRenderer();
Expand Down Expand Up @@ -594,6 +595,24 @@ describe('<Chip />', () => {

expect(getByTestId('test-icon')).to.have.class(classes.icon);
});

it("should not override the icon's custom color", () => {
const { getByTestId } = render(
<React.Fragment>
<Chip icon={<CheckBox data-testid="test-icon" color="success" />} />,
<Chip icon={<CheckBox data-testid="test-icon2" color="success" />} color="error" />,
</React.Fragment>,
);

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', () => {
Expand Down

0 comments on commit d5feaa5

Please sign in to comment.