-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2621 from AlchemyCMS/alchemy-button-component
Add alchemy-button web component
- Loading branch information
Showing
17 changed files
with
131 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Spinner from "../spinner" | ||
|
||
class Button extends HTMLButtonElement { | ||
connectedCallback() { | ||
if (this.form) { | ||
this.form.addEventListener("submit", (event) => { | ||
const isDisabled = this.getAttribute("disabled") === "disabled" | ||
|
||
if (isDisabled) { | ||
event.preventDefault() | ||
event.stopPropagation() | ||
} else { | ||
this.disable() | ||
} | ||
}) | ||
|
||
if (this.form.dataset.remote == "true") { | ||
this.form.addEventListener("ajax:complete", () => { | ||
this.enable() | ||
}) | ||
} | ||
} else { | ||
console.warn("No form for button found!", this) | ||
} | ||
} | ||
|
||
disable() { | ||
const spinner = new Spinner("small") | ||
const rect = this.getBoundingClientRect() | ||
|
||
this.dataset.initialButtonText = this.innerHTML | ||
this.setAttribute("disabled", "disabled") | ||
this.setAttribute("tabindex", "-1") | ||
this.classList.add("disabled") | ||
this.style.width = `${rect.width}px` | ||
this.style.height = `${rect.height}px` | ||
this.innerHTML = " " | ||
|
||
spinner.spin(this) | ||
} | ||
|
||
enable() { | ||
this.classList.remove("disabled") | ||
this.removeAttribute("disabled") | ||
this.removeAttribute("tabindex") | ||
this.style.width = null | ||
this.style.height = null | ||
this.innerHTML = this.dataset.initialButtonText | ||
} | ||
} | ||
|
||
customElements.define("alchemy-button", Button, { extends: "button" }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
Alchemy.growl('<%= j @notice %>', 'error'); | ||
Alchemy.Buttons.enable(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import "alchemy_admin/components/button" | ||
import { renderComponent } from "./component.helper" | ||
|
||
describe("alchemy-button", () => { | ||
it("disables button on form submit", () => { | ||
const html = ` | ||
<form> | ||
<button type="submit" is="alchemy-button">Save</button> | ||
</form> | ||
` | ||
const button = renderComponent("alchemy-button", html) | ||
const submit = new Event("submit", { bubbles: true }) | ||
|
||
button.form.dispatchEvent(submit) | ||
|
||
expect(button.getAttribute("disabled")).toEqual("disabled") | ||
expect(button.getAttribute("tabindex")).toEqual("-1") | ||
expect(button.classList.contains("disabled")).toBeTruthy() | ||
expect(button.innerHTML).toEqual( | ||
' <alchemy-spinner size="small" color="currentColor"></alchemy-spinner>' | ||
) | ||
}) | ||
|
||
it("logs warning if no form found", () => { | ||
global.console = { | ||
...console, | ||
warn: jest.fn() | ||
} | ||
|
||
const html = ` | ||
<button is="alchemy-button">Save</button> | ||
` | ||
const button = renderComponent("alchemy-button", html) | ||
|
||
expect(console.warn).toHaveBeenCalledWith( | ||
"No form for button found!", | ||
button | ||
) | ||
}) | ||
|
||
describe("on remote forms", () => { | ||
it("re-enables button on ajax complete", () => { | ||
const html = ` | ||
<form data-remote="true"> | ||
<button type="submit" is="alchemy-button">Save</button> | ||
</form> | ||
` | ||
const button = renderComponent("alchemy-button", html) | ||
|
||
const submit = new Event("submit", { bubbles: true }) | ||
button.form.dispatchEvent(submit) | ||
|
||
expect(button.getAttribute("disabled")).toEqual("disabled") | ||
|
||
const ajaxComplete = new CustomEvent("ajax:complete", { bubbles: true }) | ||
button.form.dispatchEvent(ajaxComplete) | ||
|
||
expect(button.getAttribute("disabled")).toBeNull() | ||
expect(button.getAttribute("tabindex")).toBeNull() | ||
expect(button.classList.contains("disabled")).toBeFalsy() | ||
expect(button.innerHTML).toEqual("Save") | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters