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] Fix input integration #20368

Merged
merged 4 commits into from
Apr 1, 2020
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
1 change: 1 addition & 0 deletions packages/material-ui/src/ButtonBase/ButtonBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ const ButtonBase = React.forwardRef(function ButtonBase(props, ref) {
}
}
});

const handleKeyUp = useEventCallback((event) => {
// calling preventDefault in keyUp on a <button> will not dispatch a click event if Space is pressed
// https://codesandbox.io/s/button-keyup-preventdefault-dn7f0
Expand Down
28 changes: 14 additions & 14 deletions packages/material-ui/src/Chip/Chip.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ export const styles = (theme) => {
};
};

function isDeleteKeyboardEvent(keyboardEvent) {
return keyboardEvent.key === 'Backspace' || keyboardEvent.key === 'Delete';
}

/**
* Chips represent complex entities in small blocks, such as a contact.
*/
Expand Down Expand Up @@ -295,11 +299,9 @@ const Chip = React.forwardRef(function Chip(props, ref) {
}
};

const isDeleteKeyboardEvent = (keyboardEvent) =>
keyboardEvent.key === 'Backspace' || keyboardEvent.key === 'Delete';

const handleKeyDown = (event) => {
if (isDeleteKeyboardEvent(event)) {
// Ignore events from children of `Chip`.
if (event.currentTarget === event.target && isDeleteKeyboardEvent(event)) {
// will be handled in keyUp, otherwise some browsers
// might init navigation
event.preventDefault();
Expand All @@ -311,19 +313,17 @@ const Chip = React.forwardRef(function Chip(props, ref) {
};

const handleKeyUp = (event) => {
if (onKeyUp) {
onKeyUp(event);
}

// Ignore events from children of `Chip`.
if (event.currentTarget !== event.target) {
return;
if (event.currentTarget === event.target) {
if (onDelete && isDeleteKeyboardEvent(event)) {
onDelete(event);
} else if (event.key === 'Escape' && chipRef.current) {
chipRef.current.blur();
}
}

if (onDelete && isDeleteKeyboardEvent(event)) {
onDelete(event);
} else if (event.key === 'Escape' && chipRef.current) {
chipRef.current.blur();
if (onKeyUp) {
onKeyUp(event);
}
};

Expand Down
15 changes: 12 additions & 3 deletions packages/material-ui/src/Chip/Chip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,7 @@ describe('<Chip />', () => {
['Backspace', 'Delete'].forEach((key) => {
it(`should call onDelete '${key}' is released`, () => {
const handleDelete = spy();
const handleKeyDown = spy((event) => {
return event.defaultPrevented;
});
const handleKeyDown = spy((event) => event.defaultPrevented);
const { getAllByRole } = render(
<Chip onClick={() => {}} onKeyDown={handleKeyDown} onDelete={handleDelete} />,
);
Expand All @@ -382,6 +380,17 @@ describe('<Chip />', () => {
expect(handleDelete.callCount).to.equal(1);
});
});

it('should not prevent default on input', () => {
const handleKeyDown = spy((event) => event.defaultPrevented);
const { container } = render(<Chip label={<input />} onKeyDown={handleKeyDown} />);
const input = container.querySelector('input');
input.focus();
fireEvent.keyDown(document.activeElement, { key: 'Backspace' });

// defaultPrevented?
expect(handleKeyDown.returnValues[0]).to.equal(false);
});
});

describe('with children that generate events', () => {
Expand Down