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

<sl-button type="submit" form="formId"> throws exception when it's unmounted #1806

Closed
raquo opened this issue Jan 8, 2024 · 1 comment
Closed
Labels
bug Things that aren't working right in the library.

Comments

@raquo
Copy link

raquo commented Jan 8, 2024

Describe the bug

When a Button with form attribute is unmounted / detached, an unhandled exception is thrown (see details below).

Use case: I have a Button component that is outside of the form, so it has a form attribute. When I click the button, my code does some work, and then renders a different page, so the submit button is eventually unmounted (detached) from the DOM. The form itself may or may not remain mounted.

To Reproduce

Steps to reproduce the behavior:

  1. Go to https://jsfiddle.net/j3t1rnL6/5/
  2. Open browser dev console
  3. Click on 'Submit' button
  4. See the error in the browser console.

Screenshots

Screen Shot 2024-01-07 at 18 09 21

Screen Shot 2024-01-07 at 18 09 31

Browser / OS

  • OS: Mac
  • Browser: Chrome
  • Browser version: 120

Additional information

  1. Note that the demo has a setTimeout delay to be more realistic (async submit), but it throws without setTimeout just as well.

  2. Note that the element in the doc variable mentioned below may or may not contain the form. It depends on which element(s) in the tree are getting unmounted, so merely replacing getElementById with querySelector will not solve the issue.

When button is unmounted, internally this snippet from button.component.ts is run:

     if (input.hasAttribute('form')) {
        const doc = input.getRootNode() as Document | ShadowRoot;
        const formId = input.getAttribute('form')!;
        return doc.getElementById(formId) as HTMLFormElement;
      }

It seems that because input is already unmounted (detached from the DOM) when this is run, and input itself not inside shadow DOM, input.getRootNode() returns its parent element / detached root (<div id="actionButtons">), not the document, as per MDN. However, the code assumes that doc is a document, and fails when the regular element does not have a getElementById method.

I don't know enough about Shoelace to say if this is an individual edge case, or a general problem that could happen with other form components too.

@raquo raquo added the bug Things that aren't working right in the library. label Jan 8, 2024
cyantree added a commit to cyantree/shoelace that referenced this issue Feb 20, 2024
claviska pushed a commit that referenced this issue Mar 6, 2024
Co-authored-by: cyantree <cyantree@users.noreply.github.com>
@claviska
Copy link
Member

Fixed in #1881

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Things that aren't working right in the library.
Projects
None yet
Development

No branches or pull requests

2 participants