Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/components/Chip/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const getDefaultBackgroundColor = ({
return colors.surface;
}

return colors.secondaryContainer;
return 'transparent';
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that before changing the background color we should lay the foundation for the updated chips which should be splitting into types - there is no "flat" in the specs.
Also, please double check the specs and visual examples: https://m3.material.io/components/chips/specs.
This PR removes the background from flat chips but there's no equivalent for that in the specs where all chips have at least an outline.

};

const getBackgroundColor = ({
Expand All @@ -93,11 +93,11 @@ const getBackgroundColor = ({
}: BaseProps & {
customBackgroundColor?: ColorValue;
}) => {
const { colors } = md3(theme);
if (typeof customBackgroundColor === 'string') {
if (customBackgroundColor !== undefined) {
return customBackgroundColor;
}

const { colors } = md3(theme);
if (disabled) {
if (isOutlined) {
return 'transparent';
Expand All @@ -116,12 +116,23 @@ const getSelectedBackgroundColor = ({
}: BaseProps & {
customBackgroundColor?: ColorValue;
}) => {
return getBackgroundColor({
theme,
disabled,
isOutlined,
customBackgroundColor,
});
if (customBackgroundColor !== undefined) {
return customBackgroundColor;
}

const { colors } = md3(theme);
if (disabled) {
if (isOutlined) {
return 'transparent';
}
return colors.surfaceContainerLow;
}

if (isOutlined) {
return colors.surface;
}

return colors.secondaryContainer;
};

const getIconColor = ({
Expand Down
61 changes: 59 additions & 2 deletions src/components/__tests__/Chip.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Animated } from 'react-native';
import { Animated, PlatformColor } from 'react-native';

import { act } from '@testing-library/react-native';
import color from 'color';
Expand Down Expand Up @@ -217,6 +217,20 @@ describe('getChipColor - selected background color', () => {
});
});

it('should return non-string custom color (e.g. PlatformColor / OpaqueColorValue), flat mode', () => {
const opaque = PlatformColor('systemBlue');
expect(
getChipColors({
theme: getTheme(),
customBackgroundColor: opaque,
isOutlined: false,
})
).toMatchObject({
backgroundColor: opaque,
selectedBackgroundColor: opaque,
});
});

it('should return theme color, for theme version 3, flat mode', () => {
expect(
getChipColors({
Expand All @@ -227,6 +241,41 @@ describe('getChipColor - selected background color', () => {
selectedBackgroundColor: getTheme().colors.secondaryContainer,
});
});

it('should return surface color, for theme version 3, outlined mode', () => {
expect(
getChipColors({
theme: getTheme(),
isOutlined: true,
})
).toMatchObject({
selectedBackgroundColor: getTheme().colors.surface,
});
});

it('should return disabled fill, for theme version 3, flat mode + disabled', () => {
expect(
getChipColors({
theme: getTheme(),
isOutlined: false,
disabled: true,
})
).toMatchObject({
selectedBackgroundColor: getTheme().colors.surfaceContainerLow,
});
});

it('should return transparent for theme version 3, outlined mode + disabled', () => {
expect(
getChipColors({
theme: getTheme(),
isOutlined: true,
disabled: true,
})
).toMatchObject({
selectedBackgroundColor: 'transparent',
});
});
});

describe('getChipColor - background color', () => {
Expand Down Expand Up @@ -260,8 +309,16 @@ describe('getChipColor - background color', () => {
isOutlined: false,
})
).toMatchObject({
backgroundColor: getTheme().colors.secondaryContainer,
backgroundColor: 'transparent',
});
});

it('should differ from selectedBackgroundColor for theme version 3, flat mode', () => {
const { backgroundColor, selectedBackgroundColor } = getChipColors({
theme: getTheme(),
isOutlined: false,
});
expect(backgroundColor).not.toBe(selectedBackgroundColor);
});
});

Expand Down
16 changes: 8 additions & 8 deletions src/components/__tests__/__snapshots__/Chip.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`renders chip with close button 1`] = `
collapsable={false}
style={
{
"backgroundColor": "rgba(232, 222, 248, 1)",
"backgroundColor": "transparent",
"borderRadius": 8,
"shadowColor": "rgba(0, 0, 0, 1)",
"shadowOffset": {
Expand All @@ -22,7 +22,7 @@ exports[`renders chip with close button 1`] = `
collapsable={false}
style={
{
"backgroundColor": "rgba(232, 222, 248, 1)",
"backgroundColor": "transparent",
"borderColor": "transparent",
"borderRadius": 8,
"borderStyle": "solid",
Expand Down Expand Up @@ -303,7 +303,7 @@ exports[`renders chip with custom close button 1`] = `
collapsable={false}
style={
{
"backgroundColor": "rgba(232, 222, 248, 1)",
"backgroundColor": "transparent",
"borderRadius": 8,
"shadowColor": "rgba(0, 0, 0, 1)",
"shadowOffset": {
Expand All @@ -320,7 +320,7 @@ exports[`renders chip with custom close button 1`] = `
collapsable={false}
style={
{
"backgroundColor": "rgba(232, 222, 248, 1)",
"backgroundColor": "transparent",
"borderColor": "transparent",
"borderRadius": 8,
"borderStyle": "solid",
Expand Down Expand Up @@ -601,7 +601,7 @@ exports[`renders chip with icon 1`] = `
collapsable={false}
style={
{
"backgroundColor": "rgba(232, 222, 248, 1)",
"backgroundColor": "transparent",
"borderRadius": 8,
"shadowColor": "rgba(0, 0, 0, 1)",
"shadowOffset": {
Expand All @@ -618,7 +618,7 @@ exports[`renders chip with icon 1`] = `
collapsable={false}
style={
{
"backgroundColor": "rgba(232, 222, 248, 1)",
"backgroundColor": "transparent",
"borderColor": "transparent",
"borderRadius": 8,
"borderStyle": "solid",
Expand Down Expand Up @@ -806,7 +806,7 @@ exports[`renders chip with onPress 1`] = `
collapsable={false}
style={
{
"backgroundColor": "rgba(232, 222, 248, 1)",
"backgroundColor": "transparent",
"borderRadius": 8,
"shadowColor": "rgba(0, 0, 0, 1)",
"shadowOffset": {
Expand All @@ -823,7 +823,7 @@ exports[`renders chip with onPress 1`] = `
collapsable={false}
style={
{
"backgroundColor": "rgba(232, 222, 248, 1)",
"backgroundColor": "transparent",
"borderColor": "transparent",
"borderRadius": 8,
"borderStyle": "solid",
Expand Down
4 changes: 2 additions & 2 deletions src/components/__tests__/__snapshots__/ListItem.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ exports[`renders list item with custom description 1`] = `
collapsable={false}
style={
{
"backgroundColor": "rgba(232, 222, 248, 1)",
"backgroundColor": "transparent",
"borderRadius": 8,
"shadowColor": "rgba(0, 0, 0, 1)",
"shadowOffset": {
Expand All @@ -138,7 +138,7 @@ exports[`renders list item with custom description 1`] = `
collapsable={false}
style={
{
"backgroundColor": "rgba(232, 222, 248, 1)",
"backgroundColor": "transparent",
"borderColor": "transparent",
"borderRadius": 8,
"borderStyle": "solid",
Expand Down
Loading