Skip to content

Commit

Permalink
fix: add hidden input with name attribute (#2212)
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-ico authored Nov 24, 2023
1 parent 3fdafab commit ebd325f
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export class DropdownSelect {
this.currentIndex = readOptions(this.hostElement).findIndex(
({ value }) => value === newValue
);
this.updateInputHidden(newValue);
}

connectedCallback() {
Expand Down Expand Up @@ -259,6 +260,27 @@ export class DropdownSelect {
});
}

// this workaround is needed to make the component work with form
// https://github.com/ionic-team/stencil/issues/2284
componentDidLoad() {
this.appendInputHidden();
}

appendInputHidden(): void {
const input = document.createElement('input');
input.name = this.name;
input.id = this.name;
input.value = this.value;
input.type = 'hidden';
this.hostElement.appendChild(input);
}

updateInputHidden(value: string = this.value): void {
this.hostElement.querySelector<HTMLInputElement>(
`input[name=${this.name}]`
).value = value;
}

selectOption = (index) => {
this.currentIndex = index;
this.value = readOptions(this.hostElement)[index].value;
Expand Down

0 comments on commit ebd325f

Please sign in to comment.