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 8 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
26 changes: 26 additions & 0 deletions docs/data/material/migration/migration-v5/migration-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,29 @@ The steps you need to take to migrate from Material UI v5 to v6 are described
This list is a work in progress.
Expect updates as new breaking changes are introduced.
:::

## Chip Component Changes
DiegoAndai marked this conversation as resolved.
Show resolved Hide resolved

The chip component's behavior has been updated to match the standard behavior of other components like buttons.
DiegoAndai marked this conversation as resolved.
Show resolved Hide resolved
Previously, the chip component would lose focus when the escape button was pressed. This issue has been resolved, and the chip component retains focus as expected.
DiegoAndai marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -383,8 +383,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
13 changes: 0 additions & 13 deletions packages/mui-material/src/Chip/Chip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,19 +450,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();
Expand Down
Loading