Skip to content

Commit

Permalink
[Joy][Chip] Fix unbinded onClick prop (#34455)
Browse files Browse the repository at this point in the history
  • Loading branch information
zignis committed Sep 26, 2022
1 parent 2890881 commit 5578d1d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
12 changes: 11 additions & 1 deletion packages/mui-joy/src/Chip/Chip.test.js
@@ -1,6 +1,7 @@
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer, describeConformance } from 'test/utils';
import { spy } from 'sinon';
import { createRenderer, describeConformance, fireEvent } from 'test/utils';
import { ThemeProvider } from '@mui/joy/styles';
import Chip, { chipClasses as classes } from '@mui/joy/Chip';
import { unstable_capitalize as capitalize } from '@mui/utils';
Expand Down Expand Up @@ -100,6 +101,15 @@ describe('<Chip />', () => {
expect(getByRole('button')).toBeVisible();
});

it('should call onClick', () => {
const handleClick = spy();
const { getByRole } = render(<Chip onClick={handleClick} />);

fireEvent.click(getByRole('button'));

expect(handleClick.callCount).to.equal(1);
});

it('renders action element when `componentsProps.action` is provided', () => {
const { getByRole } = render(<Chip componentsProps={{ action: {} }} />);

Expand Down
4 changes: 2 additions & 2 deletions packages/mui-joy/src/Chip/Chip.tsx
Expand Up @@ -215,7 +215,6 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
const ownerState: ChipOwnerState = {
...props,
component,
onClick,
disabled,
size,
color,
Expand Down Expand Up @@ -256,6 +255,7 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
additionalProps: {
'aria-labelledby': id,
as: resolvedActionProps?.component,
onClick,
},
ownerState,
className: classes.action,
Expand Down Expand Up @@ -347,7 +347,7 @@ Chip.propTypes /* remove-proptypes */ = {
*/
endDecorator: PropTypes.node,
/**
* @ignore
* Element action click handler.
*/
onClick: PropTypes.func,
/**
Expand Down
4 changes: 4 additions & 0 deletions packages/mui-joy/src/Chip/ChipProps.ts
Expand Up @@ -46,6 +46,10 @@ export interface ChipTypeMap<P = {}, D extends React.ElementType = 'div'> {
* Element placed after the children.
*/
endDecorator?: React.ReactNode;
/**
* Element action click handler.
*/
onClick?: React.MouseEventHandler<HTMLButtonElement>;
/**
* The size of the component.
* It accepts theme values between 'sm' and 'lg'.
Expand Down

0 comments on commit 5578d1d

Please sign in to comment.