Skip to content
Open
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
13 changes: 11 additions & 2 deletions src/lib/components/form/Form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,12 @@ const PageForm = forwardRef<HTMLFormElement, PageFormProps>(
const requireMode = useContext(RequireModeContext);
return (
<ScrollbarWrapper>
<StyledForm {...formProps} noValidate ref={ref} layout={layout}>
<StyledForm
{...formProps}
noValidate
ref={ref}
layout={layout}
>
<FixedHeader layoutKind="page">
<PaddedForHeaderAndFooterContent>
<Wrap>
Expand Down Expand Up @@ -343,7 +348,11 @@ const TabForm = forwardRef<HTMLFormElement, TabFormProps>(
({ leftActions, rightActions, children, banner, ...formProps }, ref) => {
return (
<ScrollbarWrapper>
<StyledForm {...formProps} noValidate ref={ref}>
<StyledForm
{...formProps}
noValidate
ref={ref}
>
<FixedHeader layoutKind="tab">
<Wrap>
<div>{leftActions}</div>
Expand Down
33 changes: 33 additions & 0 deletions src/lib/components/form/Form.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { act, screen, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { Form, FormSection, FormGroup } from './Form.component';

describe('Form', () => {
it('should submit when pressing Enter on a text input', async () => {
const onSubmit = jest.fn((e) => e.preventDefault());
render(
<Form
layout={{ kind: 'page', title: 'Test Form' }}
onSubmit={onSubmit}
rightActions={
<button type="submit">Submit</button>
}
>
<FormSection>
<FormGroup
id="text-field"
label="Name"
content={<input type="text" id="text-field" />}
/>
</FormSection>
</Form>,
);

const input = screen.getByRole('textbox');
await act(() => userEvent.click(input));
await act(() => userEvent.keyboard('{Enter}'));

expect(onSubmit).toHaveBeenCalled();
});
});
9 changes: 7 additions & 2 deletions src/lib/components/selectv2/Selectv2.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,12 @@ function SelectBox<

return (
<ScrollbarWrapper>
<>
<div
style={{ display: 'contents' }}
onKeyDown={(event) => {
if (event.key === 'Enter') event.preventDefault();
}}
>
{options && (
<SelectStyle
inputId={id}
Expand Down Expand Up @@ -558,7 +563,7 @@ function SelectBox<
{...rest}
/>
)}
</>
</div>
</ScrollbarWrapper>
);
}
Expand Down
35 changes: 35 additions & 0 deletions src/lib/components/selectv2/selectv2.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,41 @@ describe('SelectV2', () => {
expect(select).toHaveTextContent('Item 1');
});

it('should not trigger implicit form submission when Enter is pressed on a closed Select', async () => {
const onSubmit = jest.fn((e) => e.preventDefault());
render(
<form onSubmit={onSubmit}>
<SelectWrapper />
<button type="submit">Submit</button>
</form>,
);
await waitFor(() => screen.queryAllByRole('img', { hidden: true }));

userEvent.tab();
await act(() => userEvent.keyboard('{Enter}'));

expect(onSubmit).not.toHaveBeenCalled();
});

it('should not trigger implicit form submission when Enter is pressed on a searchable Select', async () => {
const onSubmit = jest.fn((e) => e.preventDefault());
render(
<form onSubmit={onSubmit}>
<SelectWrapper>{optionsWithScrollSearchBar}</SelectWrapper>
<button type="submit">Submit</button>
</form>,
);
await waitFor(() => screen.queryAllByRole('img', { hidden: true }));

const select = selectors.select(true);
await act(() => userEvent.click(select));
const input = selectors.input();
await act(() => userEvent.type(input, 'Item 9'));
await act(() => userEvent.keyboard('{Enter}'));

expect(onSubmit).not.toHaveBeenCalled();
});

it('should scroll to selected value when opening select', async () => {
render(
<SelectWrapper value={optionsWithScrollSearchBar[9].props.value}>
Expand Down
Loading