Skip to content

Commit

Permalink
fix(combobox-item): fix rendering tied to named-slot content (#10450)
Browse files Browse the repository at this point in the history
**Related Issue:** #6059

## Summary

- remove use of `getSlotted` utility
- replace with `slotchange` event and `@State` variables to update the
display of elements.
- existing tests should suffice
  • Loading branch information
driskull authored Oct 1, 2024
1 parent a53540b commit fcd4ed0
Showing 1 changed file with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ import {
h,
Host,
Prop,
State,
VNode,
Watch,
} from "@stencil/core";
import {
ConditionalSlotComponent,
connectConditionalSlotComponent,
disconnectConditionalSlotComponent,
} from "../../utils/conditionalSlot";
import { getSlotted } from "../../utils/dom";
import { guid } from "../../utils/guid";
import {
InteractiveComponent,
Expand All @@ -26,6 +21,7 @@ import { getAncestors, getDepth, isSingleLike } from "../combobox/utils";
import { Scale, SelectionMode } from "../interfaces";
import { getIconScale } from "../../utils/component";
import { IconNameOrString } from "../icon/interfaces";
import { slotChangeHasContent } from "../../utils/dom";
import { CSS, SLOTS } from "./resources";

/**
Expand All @@ -37,7 +33,7 @@ import { CSS, SLOTS } from "./resources";
styleUrl: "combobox-item.scss",
shadow: true,
})
export class ComboboxItem implements ConditionalSlotComponent, InteractiveComponent {
export class ComboboxItem implements InteractiveComponent {
// --------------------------------------------------------------------------
//
// Properties
Expand Down Expand Up @@ -158,6 +154,8 @@ export class ComboboxItem implements ConditionalSlotComponent, InteractiveCompon

@Element() el: HTMLCalciteComboboxItemElement;

@State() hasContent = false;

// --------------------------------------------------------------------------
//
// Lifecycle
Expand All @@ -166,11 +164,6 @@ export class ComboboxItem implements ConditionalSlotComponent, InteractiveCompon

connectedCallback(): void {
this.ancestors = getAncestors(this.el);
connectConditionalSlotComponent(this);
}

disconnectedCallback(): void {
disconnectConditionalSlotComponent(this);
}

componentDidRender(): void {
Expand Down Expand Up @@ -202,6 +195,10 @@ export class ComboboxItem implements ConditionalSlotComponent, InteractiveCompon
//
// --------------------------------------------------------------------------

private handleDefaultSlotChange = (event: Event): void => {
this.hasContent = slotChangeHasContent(event);
};

toggleSelected(): Promise<void> {
const isSinglePersistSelect = this.selectionMode === "single-persist";

Expand Down Expand Up @@ -262,15 +259,11 @@ export class ComboboxItem implements ConditionalSlotComponent, InteractiveCompon
}

renderChildren(): VNode {
if (getSlotted(this.el)) {
return (
<ul key="default-slot-container">
<slot />
</ul>
);
}

return null;
return (
<ul hidden={!this.hasContent} key="default-slot-container">
<slot onSlotchange={this.handleDefaultSlotChange} />
</ul>
);
}

render(): VNode {
Expand Down

0 comments on commit fcd4ed0

Please sign in to comment.