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

fix: do not submit autoform on key-down if pressed in textarea #2741

Merged
merged 2 commits into from
Sep 18, 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
3 changes: 3 additions & 0 deletions packages/ts/react-crud/src/autoform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ export function AutoForm<M extends AbstractModel>({
}

const handleKeyDown = (event: KeyboardEvent): void => {
if (event.target instanceof HTMLTextAreaElement) {
return;
}
if (event.key === 'Enter' && !isSubmitDisabled) {
// eslint-disable-next-line no-void
void handleSubmit();
Expand Down
51 changes: 50 additions & 1 deletion packages/ts/react-crud/test/autoform.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ValidationError } from '@vaadin/hilla-lit-form';
import type { ValueError } from '@vaadin/hilla-lit-form/Validation.js';
import type { SelectElement } from '@vaadin/react-components/Select.js';
import { TextArea, type TextAreaElement } from '@vaadin/react-components/TextArea.js';
import type { TextFieldElement } from '@vaadin/react-components/TextField.js';
import { TextField, type TextFieldElement } from '@vaadin/react-components/TextField.js';
import { VerticalLayout } from '@vaadin/react-components/VerticalLayout.js';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
Expand Down Expand Up @@ -1165,6 +1165,55 @@ describe('@vaadin/hilla-react-crud', () => {
const field = result.queryField('Custom last name');
expect(field).to.exist;
});

it('should submit when the enter key is pressed on a custom renderer field', async () => {
const service = personService();
const saveSpy = sinon.spy(service, 'save');

const result = await populatePersonForm(
1,
{
fieldOptions: {
lastName: {
label: 'Custom last name',
renderer: ({ field }) => <TextField key={field.name} {...field} />,
},
},
},
undefined,
false,
service,
);

await result.typeInField('Custom last name', 'Maxwell Smart{enter}');

expect(saveSpy).to.have.calledOnce;
expect(saveSpy).to.have.been.calledWith(sinon.match.hasNested('lastName', 'Maxwell Smart'));
});

it('should not submit when the enter key pressed on a textarea', async () => {
const service = personService();
const saveSpy = sinon.spy(service, 'save');

const result = await populatePersonForm(
1,
{
fieldOptions: {
lastName: {
label: 'Custom last name',
renderer: ({ field }) => <TextArea key={field.name} {...field} />,
},
},
},
undefined,
false,
service,
);

await result.typeInField('Custom last name', 'Maxwell\nSmart{enter}');

expect(saveSpy).to.have.not.called;
});
});

describe('element', () => {
Expand Down