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

AutoForm submits when pressing Return key, even in TextArea fields #2697

Closed
sahrmann opened this issue Sep 2, 2024 · 4 comments · Fixed by #2741
Closed

AutoForm submits when pressing Return key, even in TextArea fields #2697

sahrmann opened this issue Sep 2, 2024 · 4 comments · Fixed by #2741

Comments

@sahrmann
Copy link

sahrmann commented Sep 2, 2024

Describe the bug

When using a TextArea in an AutoForm like this

renderer: ({ field }) => <TextArea {...field} label="Full description" />,

and the return key is pressed in the field, the form is submitted.

Expected-behavior

In a TextArea, the return key should only create a new line.

Reproduction

Can be observed in the examples at https://vaadin.com/docs/latest/components/auto-form#customizing-field-properties.

System Info

Vaadin 24.4.10

@sahrmann sahrmann added bug Something isn't working hilla Issues related to Hilla labels Sep 2, 2024
@Legioth
Copy link
Member

Legioth commented Sep 3, 2024

I haven't tested it, but I suspect that a workaround could be to add a simple key down listener to the text area that only does event.stopPropagation().

@nisha8c
Copy link

nisha8c commented Sep 4, 2024

I tried something like this:

const handleKeyDown = (event: React.KeyboardEvent) => {
const target = event.target as HTMLElement;
if (event.key === 'Enter' && target.tagName !== 'TEXTAREA') {
// Prevent form submission if Enter is pressed outside the TextArea
event.preventDefault();
}
};

<TextArea {...field(model[fieldName] as unknown as AbstractModel)} className={'w-full'} label={'Message:'} required={true} style={{ width: '100%', minHeight: '200px', maxHeight: '250px' }} placeholder={'Write a message'} onValueChanged={handleTextAreaChange} onBlur={handleTextAreaBlur} onKeyDown={handleKeyDown} // Attach the refined handler here ></TextArea>

But it does not work. I tried stopPropagation as well.

if there is a way to access event of form submission, I could have added a check to see if the textArea is in Focus and enter key is pressed then form won't be submitted.

Would like to get a solution on this.

@nisha8c
Copy link

nisha8c commented Sep 4, 2024

it works now

<TextArea {...field(model[fieldName] as unknown as AbstractModel)} className={'w-full'} label={'Message:'} required={true} style={{ width: '100%', minHeight: '200px', maxHeight: '250px' }} placeholder={'Write a message'} onValueChanged={handleTextAreaChange} onBlur={handleTextAreaBlur} onKeyDown={(event) => { event.stopPropagation(); }} ></TextArea>
        In this case event: React.KeyboardEvent<TextArea>

@krissvaa krissvaa self-assigned this Sep 18, 2024
platosha added a commit that referenced this issue Sep 18, 2024
Do not submit auto form on key-down if pressed in textarea
Fixes #2697

Co-authored-by: Anton Platonov <platosha@gmail.com>
@vaadin-bot
Copy link
Collaborator

This ticket/PR has been released with Hilla 24.5.0.beta2 and is also targeting the upcoming stable 24.5.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants