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

[material-ui][Chip] Fix focus issue related with the Escape event #41578

Merged
merged 15 commits into from
Jun 5, 2024
Merged
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
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