Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

feat(action): Add icon property to display calcite-icon. #837

Merged
merged 11 commits into from
Feb 27, 2020
10 changes: 9 additions & 1 deletion src/components/calcite-action/calcite-action.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ describe("calcite-action", () => {
expect(isVisible).toBe(false);
});

it("should have icon container with icon prop", async () => {
const page = await newE2EPage();
await page.setContent(`<calcite-action icon="hamburger"></calcite-action>`);

const iconContainer = await page.find("calcite-action >>> .icon-container");
driskull marked this conversation as resolved.
Show resolved Hide resolved
expect(iconContainer).not.toBeNull();
});

it("should have icon container with calcite-icon", async () => {
const page = await newE2EPage();
await page.setContent(`<calcite-action><calcite-icon icon="hamburger" scale="s"></calcite-icon></calcite-action>`);
Expand All @@ -42,7 +50,7 @@ describe("calcite-action", () => {
expect(iconContainer).not.toBeNull();
});

it("should not have icon container if no calcite-icon present", async () => {
it("should not have icon container if no icon present", async () => {
const page = await newE2EPage();
await page.setContent(`<calcite-action></calcite-action>`);

Expand Down
7 changes: 5 additions & 2 deletions src/components/calcite-action/calcite-action.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const createAttributes: () => Attributes = () => [
name: "disabled",
value: boolean("disabled", false)
},
{
name: "icon",
value: text("icon", "beaker")
},
{
name: "indicator",
value: boolean("indicator", false)
Expand Down Expand Up @@ -60,5 +64,4 @@ const createAttributes: () => Attributes = () => [
}
];

export const basic = () =>
create("calcite-action", createAttributes(), `<calcite-icon scale="s" icon="beaker"></calcite-icon>`);
export const basic = () => create("calcite-action", createAttributes(), "");
21 changes: 12 additions & 9 deletions src/components/calcite-action/calcite-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export class CalciteAction {
*/
@Prop({ reflect: true }) disabled = false;

/**
* The name of the icon to display. The value of this property must match the icon name from https://esri.github.io/calcite-ui-icons/.
*/
@Prop() icon?: string;

/**
* Indicates unread changes.
*/
Expand Down Expand Up @@ -120,9 +125,12 @@ export class CalciteAction {
}

renderIconContainer(): VNode {
const { loading } = this;
const { el, loading, icon } = this;

const calciteLoaderNode = <calcite-loader is-active inline></calcite-loader>;
const calciteLoaderNode = loading ? <calcite-loader is-active inline></calcite-loader> : null;
driskull marked this conversation as resolved.
Show resolved Hide resolved
const calciteIconNode = icon ? <calcite-icon icon={icon} scale="s"></calcite-icon> : null;
driskull marked this conversation as resolved.
Show resolved Hide resolved
const iconNode = calciteLoaderNode || calciteIconNode;
const hasIconToDisplay = iconNode || el.querySelector("calcite-icon, svg");

const slotContainerNode = (
<div
Expand All @@ -134,15 +142,10 @@ export class CalciteAction {
</div>
);

const iconNode = loading
? calciteLoaderNode
: this.el.querySelector("calcite-icon, svg")
? slotContainerNode
: null;

return iconNode ? (
return hasIconToDisplay ? (
<div key="icon-container" aria-hidden="true" class={CSS.iconContainer}>
{iconNode}
{slotContainerNode}
</div>
) : null;
}
Expand Down