From 78eb332e6b0da18cfdff45cbd9f3e7cc9514b9ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20B=C3=BCschlen?= Date: Tue, 16 Jul 2024 18:20:34 +0200 Subject: [PATCH] feat(chip): add getValue method that returns either the value or the text content of the chip docs(chip-removable): show custom remove event as an action in storybook fix(menu-item): use textContent instead of innerText to avoid triggering a reflow --- src/components/chip/ChipGroup.js | 2 +- src/components/chip/ChipRemovable.js | 18 +++++++++ src/components/chip/ChipSelectable.js | 14 ++++++- .../chip/stories/chip-removable.stories.js | 8 +++- .../chip/test/chip-removable.test.js | 38 ++++++++++++++++++- .../chip/test/chip-selectable.test.js | 12 +++++- src/components/menu/MenuItem.js | 2 +- 7 files changed, 86 insertions(+), 8 deletions(-) diff --git a/src/components/chip/ChipGroup.js b/src/components/chip/ChipGroup.js index ca381f61..7d5cd57b 100644 --- a/src/components/chip/ChipGroup.js +++ b/src/components/chip/ChipGroup.js @@ -62,7 +62,7 @@ export class LeuChipGroup extends LeuElement { } get value() { - return this.items.filter((i) => i.checked).map((i) => i.value) + return this.items.filter((i) => i.checked).map((i) => i.getValue()) } /** diff --git a/src/components/chip/ChipRemovable.js b/src/components/chip/ChipRemovable.js index adc19b91..d522237a 100644 --- a/src/components/chip/ChipRemovable.js +++ b/src/components/chip/ChipRemovable.js @@ -7,6 +7,7 @@ import { LeuIcon } from "../icon/Icon.js" * @slot - The content of the chip * @tagname leu-chip-removable * @fires remove - Dispatched when the user clicks on the chip + * @prop {string} value - The value of the chip. */ export class LeuChipRemovable extends LeuChipBase { static dependencies = { @@ -15,12 +16,29 @@ export class LeuChipRemovable extends LeuChipBase { static properties = { ...LeuChipBase.properties, + value: { type: String, reflect: true }, + } + + constructor() { + super() + this.value = "" + } + + /** + * Returns the value of the chip. If `value` is not set, it will return the text content + * @returns {string} + */ + getValue() { + return this.value || this.textContent.trim() } handleClick() { const customEvent = new CustomEvent("leu:remove", { bubbles: true, composed: true, + detail: { + value: this.getValue(), + }, }) this.dispatchEvent(customEvent) } diff --git a/src/components/chip/ChipSelectable.js b/src/components/chip/ChipSelectable.js index 03262d12..b11a9dcc 100644 --- a/src/components/chip/ChipSelectable.js +++ b/src/components/chip/ChipSelectable.js @@ -43,6 +43,7 @@ export class LeuChipSelectable extends LeuChipBase { */ this.variant = VARIANTS.toggle this.checked = false + this.value = "" if (this.variant === VARIANTS.radio && this.size === SIZES.small) { console.warn("Small size has no effect on radio variant") @@ -62,7 +63,10 @@ export class LeuChipSelectable extends LeuChipBase { this.checked = nextcheckedState this.dispatchEvent( new CustomEvent("input", { - detail: { checked: this.checked }, + detail: { + checked: this.checked, + value: this.getValue(), + }, bubbles: true, composed: true, }) @@ -70,6 +74,14 @@ export class LeuChipSelectable extends LeuChipBase { } } + /** + * Returns the value of the chip. If `value` is not set, it will return the text content + * @returns {string} + */ + getValue() { + return this.value || this.textContent.trim() + } + render() { return html`