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

[LiveComponent] Form input in focus does not reset after submit by hitting enter #2438

Open
michilehr opened this issue Dec 10, 2024 · 5 comments
Labels
Bug Bug Fix LiveComponent Status: Needs Review Needs to be reviewed Status: Waiting Feedback Needs feedback from the author

Comments

@michilehr
Copy link

Given I have a form with one field which should reset after submit, the field is not updated (emptied in my case) after hitting the enter button. By clicking the submit button, it works.

Any chance to solve this without writing JavaScript?

Thanks for any help!

#[AsLiveComponent]
class Search extends AbstractController
{
    use DefaultActionTrait;
    use ComponentToolsTrait;
    use ComponentWithFormTrait;

    #[LiveAction]
    public function submit(): void
    {
        $this->submitForm();
        $this->resetForm();
    }

    protected function instantiateForm(): FormInterface
    {
        // we can extend AbstractController to get the normal shortcuts
        return $this->createForm(DeviceBarcodeType::class, ['barcode' => '']);
    }

    private function getDataModelValue(): ?string
    {
        return 'norender|*';
    }
}
<div>
    {{ form_start(form, {
        attr: {
            'data-action': 'live#action:prevent',
            'data-live-action-param': 'submit',
        }
    }) }}
    {{ form_row(form.barcode) }}
    <button
            type="submit"
    >Submit
    </button>
    {{ form_end(form) }}
</div>
@michilehr michilehr added the Bug Bug Fix label Dec 10, 2024
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Dec 10, 2024
@smnandre
Copy link
Member

resetForm reinitialize the form in the state it was when you created it normally... Can you explain precisely the suite of steps here (when it's created first, what actions are done, what is the result you get..) ?

@smnandre smnandre added LiveComponent Status: Waiting Feedback Needs feedback from the author labels Dec 18, 2024
@smnandre smnandre changed the title Form input in focus does not reset after submit by hitting enter [LiveComponent] Form input in focus does not reset after submit by hitting enter Dec 18, 2024
@michilehr
Copy link
Author

Hi Simon,

thank you very much for your reply!

The above code is the minimal setup for when it does not work as expected. The form is created by the live component on first render.

The component gets imported via {{ component('CheckApp') }} into the template.

The interessting thing: When I blur the element before the submitting with the enter key, it works almost all the time as expected: the form gets emtpied.

What I do now to make it more reliable: I did add another div as parent of the form with a random id. After each submit, I reset the form and regenerate this id (which is also a live prop). By this, it works as expected.

Please let me know if you need more feedback!

@smnandre
Copy link
Member

I'm not entirely sure, but the norender|* might be one reason, and the absence of initialFormData could be another. At this point, it's hard to say for certain 🤷

That said, providing a reproducer is more important than it might seem. Without it, I’d need to make several assumptions to match your scenario:

  • What version are you using?
  • Do you have any other JavaScript libraries installed (e.g., Turbo)?
  • Did you include {{ attributes }} in the template?
  • What is the device or barcode type?
  • Are you passing any initial data?
  • ... and so on.

Quickly, this can turn into 10 minutes just trying to set up something that might resemble your case.

The chances of getting help or clear answers are much higher if you provide a repository that others can directly install and test.

I know it may not seem like a big deal, but those extra 10 minutes make a real difference. They allow contributors to focus their (often limited) free time on fixing bugs or adding new features instead of reconstructing your issue.

Nothing personal or against you, just needed to clarify :)

Let me know if none of these suggestions work for you!

michilehr added a commit to michilehr/symfony-ux-demo that referenced this issue Dec 19, 2024
@michilehr
Copy link
Author

Thank you very much for the explanation. I really understand the issue with helping me and I will of course do anything which helps to resolve the issue (maybe also on my side!)

  • Barcode is a simple TextType
  • {{ attributes }} is included (sorry - forgot that in the example)

Here is the repo you asked for with the steps to reproduce my issue.

https://github.com/michilehr/symfony-ux-demo

Again, thanks a lot for taking the time @smnandre !

@smnandre
Copy link
Member

Thank you very much, that helps digging directly to the code / install.

So indeed there is something here. The problem is .... it is "expected".

Well.. let me explain.

There is a special treatment for the active element of the page during the morph (the process replacing every element from the live component HTML in the page with its new value from the server).

In your specific case, i agree there is something amiss, but that could be handle with stimulus easily i suppose?

But this has been done for the scenarios where there is some time during morph, or to not lose the current content / input position when user is typing).

That is also why we cannot do something on the back-end there..

So i don't know what would be the best way to improve this. Custom JS ? Some specific case to lose/regain focus ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix LiveComponent Status: Needs Review Needs to be reviewed Status: Waiting Feedback Needs feedback from the author
Projects
None yet
Development

No branches or pull requests

3 participants