Skip to content

Commit

Permalink
refactor(action-bar, action-pad, action-group): Remove usage of getSl…
Browse files Browse the repository at this point in the history
…otted utility (#7464)

**Related Issue:** #6059

## Summary

- Depends on #7435
  • Loading branch information
driskull authored and benelan committed Aug 16, 2023
1 parent 9f35013 commit 563536a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ describe("calcite-action-bar", () => {
const page = await newE2EPage();

await page.setContent(html`<calcite-action-bar expand-disabled></calcite-action-bar>`);
await page.waitForChanges();

const buttonGroup = await page.find(`calcite-action-bar >>> .${CSS.actionGroupEnd}`);

expect(buttonGroup).toBeNull();
expect(await buttonGroup.getProperty("hidden")).toBe(true);
expect(await buttonGroup.isVisible()).toBe(false);
});

it("should modify textEnabled on actions when expanded and expandDisabled", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
connectConditionalSlotComponent,
disconnectConditionalSlotComponent,
} from "../../utils/conditionalSlot";
import { focusFirstTabbable, getSlotted, slotChangeGetAssignedElements } from "../../utils/dom";
import {
focusFirstTabbable,
slotChangeGetAssignedElements,
slotChangeHasAssignedElement,
} from "../../utils/dom";
import {
componentFocusable,
LoadableComponent,
Expand Down Expand Up @@ -176,6 +180,10 @@ export class ActionBar

@State() effectiveLocale: string;

@State() hasActionsEnd = false;

@State() expandTooltip: HTMLCalciteTooltipElement;

@Watch("effectiveLocale")
effectiveLocaleChange(): void {
updateMessages(this, this.effectiveLocale);
Expand Down Expand Up @@ -297,9 +305,7 @@ export class ActionBar
this.setGroupLayout(actionGroups);

const groupCount =
getSlotted(el, SLOTS.actionsEnd) || getSlotted(el, SLOTS.bottomActions) || !expandDisabled
? actionGroups.length + 1
: actionGroups.length;
this.hasActionsEnd || !expandDisabled ? actionGroups.length + 1 : actionGroups.length;

const { actionHeight, actionWidth } = geActionDimensions(actions);

Expand Down Expand Up @@ -339,12 +345,24 @@ export class ActionBar

handleDefaultSlotChange = (event: Event): void => {
const groups = slotChangeGetAssignedElements(event).filter((el) =>
el?.matches("calcite-action-group")
el.matches("calcite-action-group")
) as HTMLCalciteActionGroupElement[];

this.setGroupLayout(groups);
};

handleActionsEndSlotChange = (event: Event): void => {
this.hasActionsEnd = slotChangeHasAssignedElement(event);
};

handleTooltipSlotChange = (event: Event): void => {
const tooltips = slotChangeGetAssignedElements(event).filter((el) =>
el?.matches("calcite-tooltip")
) as HTMLCalciteTooltipElement[];

this.expandTooltip = tooltips[0];
};

// --------------------------------------------------------------------------
//
// Render Methods
Expand All @@ -354,8 +372,6 @@ export class ActionBar
renderBottomActionGroup(): VNode {
const { expanded, expandDisabled, el, position, toggleExpand, scale, layout, messages } = this;

const tooltip = getSlotted(el, SLOTS.expandTooltip) as HTMLCalciteTooltipElement;

const expandToggleNode = !expandDisabled ? (
<ExpandToggle
collapseText={messages.collapse}
Expand All @@ -365,22 +381,25 @@ export class ActionBar
position={position}
scale={scale}
toggle={toggleExpand}
tooltip={tooltip}
tooltip={this.expandTooltip}
// eslint-disable-next-line react/jsx-sort-props
ref={this.setExpandToggleRef}
/>
) : null;

return getSlotted(el, SLOTS.actionsEnd) ||
getSlotted(el, SLOTS.bottomActions) ||
expandToggleNode ? (
<calcite-action-group class={CSS.actionGroupEnd} layout={layout} scale={scale}>
<slot name={SLOTS.actionsEnd} />
<slot name={SLOTS.bottomActions} />
<slot name={SLOTS.expandTooltip} />
return (
<calcite-action-group
class={CSS.actionGroupEnd}
hidden={this.expandDisabled && !this.hasActionsEnd}
layout={layout}
scale={scale}
>
<slot name={SLOTS.actionsEnd} onSlotchange={this.handleActionsEndSlotChange} />
<slot name={SLOTS.bottomActions} onSlotchange={this.handleActionsEndSlotChange} />
<slot name={SLOTS.expandTooltip} onSlotchange={this.handleTooltipSlotChange} />
{expandToggleNode}
</calcite-action-group>
) : null;
);
}

render(): VNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
connectConditionalSlotComponent,
disconnectConditionalSlotComponent,
} from "../../utils/conditionalSlot";
import { getSlotted } from "../../utils/dom";
import {
componentFocusable,
LoadableComponent,
Expand All @@ -25,6 +24,7 @@ import { Columns, Layout, Scale } from "../interfaces";
import { ActionGroupMessages } from "./assets/action-group/t9n";
import { ICONS, SLOTS } from "./resources";
import { OverlayPositioning } from "../../utils/floating-ui";
import { slotChangeHasAssignedElement } from "../../utils/dom";

/**
* @slot - A slot for adding a group of `calcite-action`s.
Expand Down Expand Up @@ -124,6 +124,8 @@ export class ActionGroup

@State() defaultMessages: ActionGroupMessages;

@State() hasMenuActions = false;

//--------------------------------------------------------------------------
//
// Public Methods
Expand Down Expand Up @@ -169,22 +171,15 @@ export class ActionGroup
//
// --------------------------------------------------------------------------

renderTooltip(): VNode {
const { el } = this;
const hasTooltip = getSlotted(el, SLOTS.menuTooltip);

return hasTooltip ? <slot name={SLOTS.menuTooltip} slot={ACTION_MENU_SLOTS.tooltip} /> : null;
}

renderMenu(): VNode {
const { el, expanded, menuOpen, scale, layout, messages, overlayPositioning } = this;

const hasMenuItems = getSlotted(el, SLOTS.menuActions);
const { expanded, menuOpen, scale, layout, messages, overlayPositioning, hasMenuActions } =
this;

return hasMenuItems ? (
return (
<calcite-action-menu
expanded={expanded}
flipPlacements={["left", "right"]}
hidden={!hasMenuActions}
label={messages.more}
onCalciteActionMenuOpen={this.setMenuOpen}
open={menuOpen}
Expand All @@ -199,10 +194,10 @@ export class ActionGroup
text={messages.more}
textEnabled={expanded}
/>
<slot name={SLOTS.menuActions} />
{this.renderTooltip()}
<slot name={SLOTS.menuActions} onSlotchange={this.handleMenuActionsSlotChange} />
<slot name={SLOTS.menuTooltip} slot={ACTION_MENU_SLOTS.tooltip} />
</calcite-action-menu>
) : null;
);
}

render(): VNode {
Expand All @@ -223,4 +218,8 @@ export class ActionGroup
setMenuOpen = (event: CalciteActionMenuCustomEvent<void>): void => {
this.menuOpen = !!(event.target as HTMLCalciteActionMenuElement).open;
};

handleMenuActionsSlotChange = (event: Event): void => {
this.hasMenuActions = slotChangeHasAssignedElement(event);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
connectConditionalSlotComponent,
disconnectConditionalSlotComponent,
} from "../../utils/conditionalSlot";
import { getSlotted, slotChangeGetAssignedElements } from "../../utils/dom";
import { slotChangeGetAssignedElements } from "../../utils/dom";
import {
componentFocusable,
LoadableComponent,
Expand Down Expand Up @@ -131,6 +131,8 @@ export class ActionPad

@Element() el: HTMLCalciteActionPadElement;

@State() expandTooltip: HTMLCalciteTooltipElement;

mutationObserver = createObserver("mutation", () =>
this.setGroupLayout(Array.from(this.el.querySelectorAll("calcite-action-group")))
);
Expand Down Expand Up @@ -233,6 +235,14 @@ export class ActionPad
this.setGroupLayout(groups);
};

handleTooltipSlotChange = (event: Event): void => {
const tooltips = slotChangeGetAssignedElements(event).filter((el) =>
el?.matches("calcite-tooltip")
) as HTMLCalciteTooltipElement[];

this.expandTooltip = tooltips[0];
};

// --------------------------------------------------------------------------
//
// Component Methods
Expand All @@ -242,8 +252,6 @@ export class ActionPad
renderBottomActionGroup(): VNode {
const { expanded, expandDisabled, messages, el, position, toggleExpand, scale, layout } = this;

const tooltip = getSlotted(el, SLOTS.expandTooltip) as HTMLCalciteTooltipElement;

const expandToggleNode = !expandDisabled ? (
<ExpandToggle
collapseText={messages.collapse}
Expand All @@ -253,15 +261,15 @@ export class ActionPad
position={position}
scale={scale}
toggle={toggleExpand}
tooltip={tooltip}
tooltip={this.expandTooltip}
// eslint-disable-next-line react/jsx-sort-props
ref={this.setExpandToggleRef}
/>
) : null;

return expandToggleNode ? (
<calcite-action-group class={CSS.actionGroupEnd} layout={layout} scale={scale}>
<slot name={SLOTS.expandTooltip} />
<slot name={SLOTS.expandTooltip} onSlotchange={this.handleTooltipSlotChange} />
{expandToggleNode}
</calcite-action-group>
) : null;
Expand Down

0 comments on commit 563536a

Please sign in to comment.