Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(panel, flow-item): add beforeClose property #9770

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/calcite-components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,10 @@ export namespace Components {
* When provided, the method will be called before it is removed from its parent `calcite-flow`.
*/
"beforeBack": () => Promise<void>;
/**
* Passes a function to run before the component closes.
*/
"beforeClose": () => Promise<void>;
/**
* When `true`, displays a close button in the trailing side of the component's header.
*/
Expand Down Expand Up @@ -3672,6 +3676,10 @@ export namespace Components {
"totalItems": number;
}
interface CalcitePanel {
/**
* Passes a function to run before the component closes.
*/
"beforeClose": () => Promise<void>;
/**
* When `true`, displays a close button in the trailing side of the header.
*/
Expand Down Expand Up @@ -9733,6 +9741,10 @@ declare namespace LocalJSX {
* When provided, the method will be called before it is removed from its parent `calcite-flow`.
*/
"beforeBack"?: () => Promise<void>;
/**
* Passes a function to run before the component closes.
*/
"beforeClose"?: () => Promise<void>;
/**
* When `true`, displays a close button in the trailing side of the component's header.
*/
Expand Down Expand Up @@ -11630,6 +11642,10 @@ declare namespace LocalJSX {
"totalItems"?: number;
}
interface CalcitePanel {
/**
* Passes a function to run before the component closes.
*/
"beforeClose"?: () => Promise<void>;
/**
* When `true`, displays a close button in the trailing side of the header.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ describe("calcite-flow-item", () => {

describe("defaults", () => {
defaults("calcite-flow-item", [
{
propertyName: "beforeClose",
defaultValue: undefined,
},
{
propertyName: "closable",
defaultValue: false,
Expand Down Expand Up @@ -199,6 +203,24 @@ describe("calcite-flow-item", () => {
expect(calciteFlowItemBack).toHaveReceivedEvent();
});

it("sets beforeClose on internal panel", async () => {
const page = await newE2EPage();
await page.exposeFunction("beforeClose", () => Promise.reject());
await page.setContent("<calcite-flow-item closable></calcite-flow-item>");

await page.$eval(
"calcite-flow-item",
(el: HTMLCalciteFlowItemElement) =>
(el.beforeClose = (window as typeof window & Pick<typeof el, "beforeClose">).beforeClose),
);

await page.waitForChanges();

const panel = await page.find(`calcite-flow-item >>> calcite-panel`);

expect(await panel.getProperty("beforeClose")).toBeDefined();
});

it("sets collapsible and collapsed on internal panel", async () => {
const page = await newE2EPage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ export class FlowItem
*/
@Prop() beforeBack: () => Promise<void>;

/** Passes a function to run before the component closes. */
@Prop() beforeClose: () => Promise<void>;

/** A description for the component. */
@Prop() description: string;

Expand Down Expand Up @@ -365,11 +368,13 @@ export class FlowItem
menuOpen,
messages,
overlayPositioning,
beforeClose,
} = this;
return (
<Host>
<InteractiveContainer disabled={disabled}>
<calcite-panel
beforeClose={beforeClose}
closable={closable}
closed={closed}
collapseDirection={collapseDirection}
Expand Down
47 changes: 47 additions & 0 deletions packages/calcite-components/src/components/panel/panel.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ describe("calcite-panel", () => {

describe("defaults", () => {
defaults("calcite-panel", [
{
propertyName: "beforeClose",
defaultValue: undefined,
},
{
propertyName: "widthScale",
defaultValue: undefined,
Expand Down Expand Up @@ -129,6 +133,49 @@ describe("calcite-panel", () => {
expect(await container.isVisible()).toBe(false);
});

it("should handle rejected 'beforeClose' promise'", async () => {
const page = await newE2EPage();

const mockCallBack = jest.fn().mockReturnValue(() => Promise.reject());
await page.exposeFunction("beforeClose", mockCallBack);

await page.setContent(`<calcite-panel closable></calcite-panel>`);

await page.$eval(
"calcite-panel",
(el: HTMLCalcitePanelElement) =>
(el.beforeClose = (window as typeof window & Pick<typeof el, "beforeClose">).beforeClose),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, could you use our custom utility type to define custom props on window (example)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as part of #9778

);
await page.waitForChanges();

const panel = await page.find("calcite-panel");
expect(await panel.getProperty("closed")).toBe(false);
panel.setProperty("closed", true);
await page.waitForChanges();

expect(mockCallBack).toHaveBeenCalledTimes(1);
});

it("should remain open with rejected 'beforeClose' promise'", async () => {
const page = await newE2EPage();

await page.exposeFunction("beforeClose", () => Promise.reject());
await page.setContent(`<calcite-panel closable></calcite-panel>`);

await page.$eval(
"calcite-panel",
(el: HTMLCalcitePanelElement) =>
(el.beforeClose = (window as typeof window & Pick<typeof el, "beforeClose">).beforeClose),
);

const panel = await page.find("calcite-panel");
panel.setProperty("closed", true);
await page.waitForChanges();

expect(await panel.getProperty("closed")).toBe(false);
expect(panel.getAttribute("closed")).toBe(null); // Makes sure attribute is added back
});

it("honors collapsed & collapsible properties", async () => {
const page = await newE2EPage();

Expand Down
40 changes: 35 additions & 5 deletions packages/calcite-components/src/components/panel/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,17 @@ export class Panel
//
// --------------------------------------------------------------------------

/** Passes a function to run before the component closes. */
@Prop() beforeClose: () => Promise<void>;

/** When `true`, the component will be hidden. */
@Prop({ mutable: true, reflect: true }) closed = false;

@Watch("closed")
toggleDialog(value: boolean): void {
value ? this.close() : this.open();
}

/**
* When `true`, interaction is prevented and the component is displayed with lower opacity.
*/
Expand Down Expand Up @@ -206,6 +214,8 @@ export class Panel

resizeObserver = createObserver("resize", () => this.resizeHandler());

@State() isClosed = false;

@State() hasStartActions = false;

@State() hasEndActions = false;
Expand Down Expand Up @@ -288,16 +298,36 @@ export class Panel

panelKeyDownHandler = (event: KeyboardEvent): void => {
if (this.closable && event.key === "Escape" && !event.defaultPrevented) {
this.close();
this.closed = true;
event.preventDefault();
}
};

close = (): void => {
private handleCloseClick = (): void => {
this.closed = true;
this.calcitePanelClose.emit();
};

open = (): void => {
this.isClosed = false;
};

close = async (): Promise<void> => {
const beforeClose = this.beforeClose ?? (() => Promise.resolve());

try {
await beforeClose();
} catch (_error) {
driskull marked this conversation as resolved.
Show resolved Hide resolved
// close prevented
requestAnimationFrame(() => {
this.closed = false;
});
return;
}

this.isClosed = true;
};

collapse = (): void => {
this.collapsed = !this.collapsed;
this.calcitePanelToggle.emit();
Expand Down Expand Up @@ -486,7 +516,7 @@ export class Panel
aria-label={close}
data-test="close"
icon={ICONS.close}
onClick={this.close}
onClick={this.handleCloseClick}
scale={this.scale}
text={close}
title={close}
Expand Down Expand Up @@ -651,13 +681,13 @@ export class Panel
}

render(): VNode {
const { disabled, loading, panelKeyDownHandler, closed, closable } = this;
const { disabled, loading, panelKeyDownHandler, isClosed, closable } = this;

const panelNode = (
<article
aria-busy={toAriaBoolean(loading)}
class={CSS.container}
hidden={closed}
hidden={isClosed}
ref={this.setContainerRef}
tabIndex={closable ? 0 : -1}
>
Expand Down
Loading