stencil icon indicating copy to clipboard operation
stencil copied to clipboard

Button form attribute does not render/missing after compiled in DOM

Open liho98 opened this issue 4 years ago • 10 comments

Stencil version:

 @stencil/core@ <^1.17.3>

I'm submitting a:

[x] bug report [ ] feature request [ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/

Current behavior: image image you can see the form attribute is missing after compile in DOM.

Expected behavior:

I want the form attribute to work, so i can handle multiple form submission.

Steps to reproduce: just add form attribute in

Related code:

      <Host style={{ all: "unset" }}>
        <button
          type={this.type}
          style={this.buttonStyle}
          class={`rm-button--button ${this.class}`}
          value="Submit"
          form="form1"
        >
          <slot></slot>
        </button>
      </Host>

Other information:

liho98 avatar Oct 14 '20 16:10 liho98

Confirmed with a minimal component:

import { Component, h } from '@stencil/core';

@Component({
  tag: 'my-button',
  shadow: true,
})
export class AppHome {
  render() {
    return <button form="form-id-here">Submit</button>;
  }
}

The resulting markup looks like this:

CleanShot 2020-11-17 at 10 55 21@2x

claviska avatar Nov 17 '20 15:11 claviska

@adamdbradley any idea what might be causing this? I'm happy to take a stab at it if you can point me in the right direction.

claviska avatar Nov 17 '20 16:11 claviska

Thanks for the issue! This issue is being closed due to inactivity. If this is still an issue with the latest version of Stencil, please create a new issue and ensure the template is fully filled out.

Thank you for using Stencil!

ionitron-bot[bot] avatar Dec 17 '20 16:12 ionitron-bot[bot]

As a workaround I managed to use ref to get it to work:

<button
  ref={(btn) => btn?.setAttribute('form', 'form-id-here')}
>
  Submit
</button>

But it is a little ugly, I know.

thccorni avatar Feb 25 '22 15:02 thccorni

FYI, still having the issue in Stencil 2.17.3 :)

@thccorni workaround is working ;)

KevinCarnaille2 avatar Aug 19 '22 12:08 KevinCarnaille2

Hello guys! Yesterday I found this issue while having some trouble setting the form attribute on this component, based on Stencil.

The walkaround from @thccorni seems to work (thank you!)

If it could help, I debugged a bit the @stencil/core/internal/client/index.js; at line 398 you could see that all params looks good and the set should be done.. but if I move one line ahead, and check for elm[memberName], it's null. I'm adding two screenshots.

form-button-bug form-button-bug2

DavideMininni-Fincons avatar Aug 24 '22 07:08 DavideMininni-Fincons

Thanks all! I've confirmed this is an issue with the minimal reproduction case above. I'm going to label this for the team to look into further

rwaskiewicz avatar Aug 25 '22 13:08 rwaskiewicz

Try adding a unique key attribute and see if that keeps the element from being recreated.

The key prop is not yet well documented [1][2], but I stumbled onto it when dealing with a similar issue (integrating Stripe's Elements UI DOM mounting into a ref'd div) and it seems to have addressed the issue for me (where the div was being seemingly recreated and the referenced DOM element was lost).

PS - I'm not 100% certain of my exact issue, but I know enough to feel confident that key helped.

EDIT: this may be relevant.

brendensoares avatar Sep 11 '22 20:09 brendensoares

This also applied to inputs

timsnadden avatar Jan 19 '23 02:01 timsnadden

This issue is still present in @stencil/core@^4.7.2

We worked around it by checking after render and adding 'form' with setAttribute function.

componentDidRender() {
  if (this.form) {
    this.hostElement.querySelector('button').setAttribute('form', this.form);
  }
}

Adamkillander96 avatar Dec 15 '23 10:12 Adamkillander96