fix button disabled state

Signed-off-by: Tobias Erbshäußer <tobias@tesoft.dev>
This commit is contained in:
2026-05-24 09:22:57 +02:00
parent 07b35bfc0b
commit c020592c8d
+21 -11
View File
@@ -4,6 +4,7 @@ export class TesoftButton extends TesoftComponent {
static observedAttributes = ["disabled", "href", "selected"];
private readonly rootDiv: HTMLDivElement;
private linkOrButton: HTMLElement;
constructor() {
super();
@@ -15,33 +16,42 @@ export class TesoftButton extends TesoftComponent {
shadowRoot.appendChild(templateContent.cloneNode(true));
this.rootDiv = shadowRoot.querySelector<HTMLDivElement>("div")!;
this.linkOrButton = shadowRoot.querySelector<HTMLButtonElement>("button")!;
}
// noinspection JSUnusedGlobalSymbols
attributeChangedCallback(name: string, _oldValue: string | null, newValue: string | null) {
if (name === "disabled") {
if (newValue === null) {
this.rootDiv.onclick = null;
this.linkOrButton.removeAttribute("disabled");
} else {
this.rootDiv.onclick = (e) => {
e.stopPropagation();
};
this.linkOrButton.setAttribute("disabled", "");
}
} else if (name === "href") {
this.rootDiv.innerHTML = "";
let linkOrButton: HTMLElement;
if (newValue) {
linkOrButton = document.createElement("a");
(linkOrButton as HTMLAnchorElement).href = newValue;
this.linkOrButton = document.createElement("a");
(this.linkOrButton as HTMLAnchorElement).href = newValue;
} else {
linkOrButton = document.createElement("button");
this.linkOrButton = document.createElement("button");
}
linkOrButton.part = "children";
this.rootDiv.appendChild(linkOrButton);
this.linkOrButton.part = "children";
if (this.hasAttribute("disabled")) {
this.linkOrButton.setAttribute("disabled", "");
}
this.rootDiv.appendChild(this.linkOrButton);
const slot = document.createElement("slot");
linkOrButton.appendChild(slot);
this.linkOrButton.appendChild(slot);
}
}
set disabled(value: string | null) {
if (value === null) {
this.removeAttribute("disabled");
} else {
this.setAttribute("disabled", "");
}
}