Skip to content
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
6 changes: 6 additions & 0 deletions src/Step.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ export default function Step(props: StepProps) {
};

accessibilityProps.onKeyDown = (e) => {
// Only handle keys pressed on the step itself. Events bubbling up from
// nested focusable content (e.g. an `<input>` in the step content) must be
// left untouched, otherwise Space / Enter would be swallowed there.
if (e.target !== e.currentTarget) {
return;
}
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
e.currentTarget.click();
Expand Down
33 changes: 33 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,39 @@ describe('Steps', () => {
expect(spaceEvent.defaultPrevented).toBe(true);
});

it('should leave Enter/Space untouched when they bubble up from a nested input', () => {
const onChange = jest.fn();
const onItemClick = jest.fn();
const { container } = render(
<Steps
current={0}
onChange={onChange}
items={[
Comment thread
yoyo837 marked this conversation as resolved.
Resolved
{
title: 'Finished',
},
{
title: 'Waiting',
content: <input />,
onClick: onItemClick,
},
]}
/>,
);

const input = container.querySelector('input');
const enterEvent = createEvent.keyDown(input, { key: 'Enter' });
const spaceEvent = createEvent.keyDown(input, { key: ' ' });

fireEvent(input, enterEvent);
fireEvent(input, spaceEvent);

expect(enterEvent.defaultPrevented).toBe(false);
expect(spaceEvent.defaultPrevented).toBe(false);
expect(onChange).not.toHaveBeenCalled();
expect(onItemClick).not.toHaveBeenCalled();
});

it('itemRender', () => {
const { container } = render(
<Steps
Expand Down
Loading