|
| 1 | +import { ListItem, MenuItem, ListSeparator } from '@limetech/lime-elements'; |
| 2 | +import { Component, h, Host, State } from '@stencil/core'; |
| 3 | + |
| 4 | +/** |
| 5 | + * List item with action menu |
| 6 | + * |
| 7 | + * This example shows how a list item can have an action menu |
| 8 | + * with multiple actions that can be triggered by the user. |
| 9 | + * |
| 10 | + * ⚠️ _TODO: verify that this implementation is correct_ 👇 |
| 11 | + * The consumer (e.g. `limel-list`) component should handle the interaction with the action menu. |
| 12 | + * - Action menu items are `MenuItem` objects where `value` contains the actual `Action` |
| 13 | + * - **Event bubbling:** `limel-menu` emits `select` events with the `MenuItem` as `event.detail` |
| 14 | + * - **Natural forwarding:** The select event bubbles up and `limel-list` re-emits it |
| 15 | + * - **Value extraction:** the final consumer of `limel-list` |
| 16 | + * will eventually extract `event.detail.value` to get the actual `Action` |
| 17 | + */ |
| 18 | +@Component({ |
| 19 | + tag: 'limel-example-list-item-actions', |
| 20 | + shadow: true, |
| 21 | + styleUrl: 'list-item-basic.scss', |
| 22 | +}) |
| 23 | +export class ListItemActionsExample { |
| 24 | + @State() |
| 25 | + private lastInteraction: string = ''; |
| 26 | + |
| 27 | + @State() |
| 28 | + private selectedItems: Set<number> = new Set(); |
| 29 | + |
| 30 | + private actionItems: Array<MenuItem | ListSeparator> = [ |
| 31 | + { text: 'Edit item', value: 'edit', icon: 'edit' }, |
| 32 | + { text: 'Duplicate item', value: 'duplicate', icon: 'copy' }, |
| 33 | + { text: 'Share item', value: 'share', icon: 'share' }, |
| 34 | + { separator: true }, |
| 35 | + { |
| 36 | + text: 'Delete item', |
| 37 | + value: 'delete', |
| 38 | + icon: 'trash', |
| 39 | + disabled: false, |
| 40 | + }, |
| 41 | + ]; |
| 42 | + |
| 43 | + public render() { |
| 44 | + return ( |
| 45 | + <Host> |
| 46 | + <ul> |
| 47 | + <limel-list-item |
| 48 | + value={1} |
| 49 | + selectable={true} |
| 50 | + selected={this.selectedItems.has(1)} |
| 51 | + text="King of Tokyo" |
| 52 | + secondaryText="A fun dice game for 2-6 players" |
| 53 | + icon="gorilla" |
| 54 | + actions={this.actionItems} |
| 55 | + onInteract={this.onListItemInteraction} |
| 56 | + /> |
| 57 | + <limel-list-item |
| 58 | + value={2} |
| 59 | + selectable={true} |
| 60 | + selected={this.selectedItems.has(2)} |
| 61 | + text="Pandemic" |
| 62 | + secondaryText="Cooperative board game where players work together to save the world" |
| 63 | + icon="virus" |
| 64 | + actions={this.actionItems} |
| 65 | + onInteract={this.onListItemInteraction} |
| 66 | + /> |
| 67 | + </ul> |
| 68 | + <limel-example-value |
| 69 | + label="Last interaction" |
| 70 | + value={this.lastInteraction} |
| 71 | + /> |
| 72 | + </Host> |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + private onListItemInteraction = ( |
| 77 | + event: CustomEvent<{ selected: boolean; item: ListItem }> |
| 78 | + ) => { |
| 79 | + const itemValue = event.detail.item.value as number; |
| 80 | + const isSelected = event.detail.selected; |
| 81 | + |
| 82 | + if (isSelected) { |
| 83 | + this.selectedItems = new Set([...this.selectedItems, itemValue]); |
| 84 | + } else { |
| 85 | + this.selectedItems = new Set( |
| 86 | + [...this.selectedItems].filter((id) => id !== itemValue) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + this.lastInteraction = `Item "${event.detail.item.text}" interaction: selected=${isSelected}`; |
| 91 | + }; |
| 92 | +} |
0 commit comments