Skip to content

Commit

Permalink
feat(dialog): implement <dialog> interface
Browse files Browse the repository at this point in the history
adds `close` and `cancel` events to dialog
  • Loading branch information
bennypowers committed Mar 8, 2022
1 parent 7b3e2cd commit 14d17aa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("FASTDialog", function () {
// FASTDialog should focus on the first element
it("should focus on first element", async function () {
const element = await this.page.$("fast-dialog");

expect(
await this.page.evaluate(
() => document.activeElement?.id
Expand Down
46 changes: 46 additions & 0 deletions packages/web-components/fast-foundation/src/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,52 @@ describe("Dialog", () => {
await disconnect();
});

it("should fire a 'cancel' event when its overlay is clicked", async () => {
const { element, connect, disconnect } = await setup();

await connect();

const overlay = element.shadowRoot!.querySelector(".overlay")! as HTMLElement;

const wasDismissed = await new Promise(resolve => {
element.addEventListener("cancel", () => resolve(true));

overlay.click();

// Resolve false on the next update in case click hasn't happened
DOM.queueUpdate(() => resolve(false));
});

expect(wasDismissed).to.equal(true);

await disconnect();
});

it("should fire a 'close' event when its button is clicked", async () => {
const { element, connect, disconnect } = await setup();

const button = document.createElement('button');
button.textContent = 'close';
element.append(button)

await connect();

const overlay = element.shadowRoot!.querySelector(".overlay")! as HTMLElement;

const wasDismissed = await new Promise(resolve => {
element.addEventListener("close", () => resolve(true));

button.click();

// Resolve false on the next update in case click hasn't happened
DOM.queueUpdate(() => resolve(false));
});

expect(wasDismissed).to.equal(true);

await disconnect();
});

it("should fire a 'dismiss' event when keydown is invoked on the document", async () => {
const { element, connect, disconnect, document } = await setup();

Expand Down
4 changes: 4 additions & 0 deletions packages/web-components/fast-foundation/src/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export class Dialog extends FoundationElement {
*/
public dismiss(): void {
this.$emit("dismiss");
// implement `<dialog>` interface
this.$emit("cancel");
}

/**
Expand All @@ -114,6 +116,8 @@ export class Dialog extends FoundationElement {
*/
public hide(): void {
this.hidden = true;
// implement `<dialog>` interface
this.$emit("close");
}

/**
Expand Down

0 comments on commit 14d17aa

Please sign in to comment.