Skip to content

Commit

Permalink
fix: do not submit autoform on key-down if pressed in textarea (#2741)
Browse files Browse the repository at this point in the history
Do not submit auto form on key-down if pressed in textarea
Fixes #2697

Co-authored-by: Anton Platonov <[email protected]>
  • Loading branch information
krissvaa and platosha authored Sep 18, 2024
1 parent 4125dac commit 224b9ae
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
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

0 comments on commit 224b9ae

Please sign in to comment.