Skip to content

Commit

Permalink
[material-ui][Chip] Fix focus issue related with the Escape event (mu…
Browse files Browse the repository at this point in the history
  • Loading branch information
shrilsharma authored Jun 5, 2024
1 parent 8f45c8e commit 213daef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
30 changes: 30 additions & 0 deletions docs/data/material/migration/migration-v5/migration-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,33 @@ This results in a reduction of the `@mui/material` package size by 2.5MB or 25%

Instead, using ESM-based CDNs such as [esm.sh](https://esm.sh/) is recommended.
For alternative installation methods, refer to the [CDN documentation](/material-ui/getting-started/installation/#cdn).

### Chip

The Chip component's behavior has been updated to match the standard behavior of other components like buttons.
Previously, the Chip component lost focus when the escape button was pressed, which differed from how other button-like components work.
This issue has been resolved, and the chip component retains focus as expected.

You can provide a custom `onKeyUp` handler to implement the previous behavior.

```js
import * as React from 'react';
import Chip from '@mui/material/Chip';

export default function ChipExample() {
const chipRef = React.useRef(null);
const keyUpHandler = (event) => {
if (event.key === 'Escape' && chipRef.current) {
chipRef.current.blur();
}
};
return (
<Chip
label="Chip Outlined"
variant="outlined"
ref={chipRef}
onKeyUp={keyUpHandler}
/>
);
}
```
2 changes: 0 additions & 2 deletions packages/mui-material/src/Chip/Chip.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,6 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
if (event.currentTarget === event.target) {
if (onDelete && isDeleteKeyboardEvent(event)) {
onDelete(event);
} else if (event.key === 'Escape' && chipRef.current) {
chipRef.current.blur();
}
}

Expand Down
14 changes: 0 additions & 14 deletions packages/mui-material/src/Chip/Chip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,20 +446,6 @@ describe('<Chip />', () => {
expect(handleKeydown.firstCall.returnValue).to.equal('p');
});

it('should unfocus when a esc key is pressed', () => {
const handleBlur = spy();
const { getByRole } = render(<Chip onBlur={handleBlur} onClick={() => {}} />);
const chip = getByRole('button');
act(() => {
chip.focus();
});

fireEvent.keyUp(chip, { key: 'Escape' });

expect(handleBlur.callCount).to.equal(1);
expect(chip).not.toHaveFocus();
});

it('should call onClick when `space` is released ', () => {
const handleClick = spy();
const { getByRole } = render(<Chip onClick={handleClick} />);
Expand Down

0 comments on commit 213daef

Please sign in to comment.