Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chip] adding 'rounded' property to chip component #37576

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions packages/mui-material/src/Chip/Chip.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export interface ChipTypeMap<P = {}, D extends React.ElementType = 'div'> {
* @default 'medium'
*/
size?: OverridableStringUnion<'small' | 'medium', ChipPropsSizeOverrides>;
/**
* If 'true' the component will be rounded instead of circular
* @default true
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @default true
* @default false

*/
rounded?: boolean;
/**
* If `true`, allows the disabled chip to escape focus.
* If `false`, allows the disabled chip to receive focus.
Expand Down
16 changes: 15 additions & 1 deletion packages/mui-material/src/Chip/Chip.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import styled from '../styles/styled';
import chipClasses, { getChipUtilityClass } from './chipClasses';

const useUtilityClasses = (ownerState) => {
const { classes, disabled, size, color, iconColor, onDelete, clickable, variant } = ownerState;
const { classes, disabled, size, color, iconColor, onDelete, clickable, variant, rounded } =
ownerState;

const slots = {
root: [
Expand All @@ -27,6 +28,7 @@ const useUtilityClasses = (ownerState) => {
onDelete && 'deletable',
onDelete && `deletableColor${capitalize(color)}`,
`${variant}${capitalize(color)}`,
rounded && 'rounded',
],
label: ['label', `label${capitalize(size)}`],
avatar: ['avatar', `avatar${capitalize(size)}`, `avatarColor${capitalize(color)}`],
Expand All @@ -37,6 +39,7 @@ const useUtilityClasses = (ownerState) => {
`deleteIconColor${capitalize(color)}`,
`deleteIcon${capitalize(variant)}Color${capitalize(color)}`,
],
rounded: ['rounded', `rounded`],
};

return composeClasses(slots, getChipUtilityClass, classes);
Expand All @@ -63,6 +66,7 @@ const ChipRoot = styled('div', {
[`& .${chipClasses.deleteIcon}`]:
styles[`deleteIcon${capitalize(variant)}Color${capitalize(color)}`],
},
{ [`& .${chipClasses.rounded}`]: styles.rounded },
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{ [`& .${chipClasses.rounded}`]: styles.rounded },
rounded && styles.rounded

(Will need to destructure rounded from ownerState in line 53 above)

styles.root,
styles[`size${capitalize(size)}`],
styles[`color${capitalize(color)}`],
Expand Down Expand Up @@ -172,6 +176,9 @@ const ChipRoot = styled('div', {
...(ownerState.size === 'small' && {
height: 24,
}),
...(ownerState.rounded === true && {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
...(ownerState.rounded === true && {
...(ownerState.rounded && {

borderRadius: 32 / 4,
}),
...(ownerState.color !== 'default' && {
backgroundColor: (theme.vars || theme).palette[ownerState.color].main,
color: (theme.vars || theme).palette[ownerState.color].contrastText,
Expand Down Expand Up @@ -337,6 +344,7 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
onDelete,
onKeyDown,
onKeyUp,
rounded,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
rounded,
rounded = false,

Copy link
Contributor

Choose a reason for hiding this comment

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

just passing by, why do you need a false default value? (I think you don't)

size = 'medium',
variant = 'filled',
tabIndex,
Expand Down Expand Up @@ -397,6 +405,7 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
onDelete: !!onDelete,
clickable,
variant,
rounded,
};

const classes = useUtilityClasses(ownerState);
Expand Down Expand Up @@ -549,6 +558,11 @@ Chip.propTypes /* remove-proptypes */ = {
* @ignore
*/
onKeyUp: PropTypes.func,
/**
* Makes chips rounded instead of circular default
* @default true
*/
rounded: PropTypes.bool,
/**
* The size of the component.
* @default 'medium'
Expand Down
7 changes: 7 additions & 0 deletions packages/mui-material/src/Chip/Chip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,13 @@ describe('<Chip />', () => {
expect(icon).to.have.class(classes.deleteIcon);
expect(icon).to.have.class(classes.deleteIconSmall);
});

it('should render the chip with rounded class', () => {
const { container } = render(<Chip rounded />);

const chip = container.querySelector(`.${classes.root}`);
expect(chip).to.have.class(classes.rounded);
});
});

describe('event: focus', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/mui-material/src/Chip/chipClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export interface ChipClasses {
deleteIconFilledColorSecondary: string;
/** State class applied to the root element if keyboard focused. */
focusVisible: string;
/** Styles applied to chip if 'rounded=true' */
rounded: string;
}

export type ChipClassKey = keyof ChipClasses;
Expand Down Expand Up @@ -146,6 +148,7 @@ const chipClasses: ChipClasses = generateUtilityClasses('MuiChip', [
'deleteIconFilledColorPrimary',
'deleteIconFilledColorSecondary',
'focusVisible',
'rounded',
]);

export default chipClasses;