Skip to content

Commit

Permalink
fix(header): emit event from header-dropdown-list-item
Browse files Browse the repository at this point in the history
  • Loading branch information
max-umain committed Jan 21, 2025
1 parent c2adb3a commit 555b33a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Element, h, Host, Prop } from '@stencil/core';
import { Component, Element, h, Host, Prop, Event, EventEmitter } from '@stencil/core';

/**
* @slot <default> - <b>Unnamed slot.</b> For a link or button element.
Expand All @@ -11,6 +11,8 @@ import { Component, Element, h, Host, Prop } from '@stencil/core';
export class TdsHeaderDropdownListItem {
@Element() host: HTMLElement;

@Event() closeDropdownFromListItem: EventEmitter<void>;

/** If the link should appear selected. */
@Prop() selected: boolean = false;

Expand All @@ -25,6 +27,8 @@ export class TdsHeaderDropdownListItem {
'component': true,
'component-selected': this.selected,
}}
onClick={() => this.closeDropdownFromListItem.emit()}
onKeyDown={(e) => e.key === 'Enter' && this.closeDropdownFromListItem.emit()}
>
<slot></slot>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
| `size` | `size` | The size of the component. | `"lg" \| "md"` | `'md'` |


## Events

| Event | Description | Type |
| --------------------------- | ----------- | ------------------- |
| `closeDropdownFromListItem` | | `CustomEvent<void>` |


## Slots

| Slot | Description |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ export class TdsHeaderDropdown {
this.open = !this.open;
}

handleSlottedItemClick = (event: MouseEvent | KeyboardEvent) => {
const eventSource = (event.target as HTMLElement).tagName.toLowerCase();
if (['a', 'button', 'div'].includes(eventSource)) {
this.open = false;
}
};
@Listen('closeDropdownFromListItem')
handleCloseDropdownFromListItem() {
this.open = false;
}

render() {
return (
Expand Down Expand Up @@ -95,14 +93,7 @@ export class TdsHeaderDropdown {
},
]}
>
{this.open ? (
<span
onClick={(e) => this.handleSlottedItemClick(e)}
onKeyDown={(e) => e.key === 'Enter' && this.handleSlottedItemClick(e)}
>
<slot></slot>
</span>
) : null}
{this.open ? <slot></slot> : null}
</tds-popover-canvas>
)}
</div>
Expand Down

0 comments on commit 555b33a

Please sign in to comment.