Skip to content

Commit

Permalink
feat(chip): add getValue method that returns either the value or the …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
daenub authored Jul 16, 2024
1 parent 1f8e957 commit 78eb332
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/chip/ChipGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/components/chip/ChipRemovable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
}
Expand Down
14 changes: 13 additions & 1 deletion src/components/chip/ChipSelectable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -62,14 +63,25 @@ 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,
})
)
}
}

/**
* 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`<button
@click=${() => this.handleClick()}
Expand Down
8 changes: 6 additions & 2 deletions src/components/chip/stories/chip-removable.stories.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { html } from "lit"
import { action } from "@storybook/addon-actions"

import "../leu-chip-removable.js"

export default {
title: "Chip/Removable",
component: "leu-chip-removable",
args: {
label: "Publikationen",
label: "Daten",
onRemove: action("leu:remove"),
},
parameters: {
design: {
Expand All @@ -27,7 +29,9 @@ function Template(args) {
: "var(--leu-color-black-5)"}; padding: 1rem;"
data-root
>
<leu-chip-removable ?inverted=${args.inverted}
<leu-chip-removable
@leu:remove=${args.onRemove}
?inverted=${args.inverted}
>${args.label}</leu-chip-removable
>
</div>
Expand Down
38 changes: 36 additions & 2 deletions src/components/chip/test/chip-removable.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { html } from "lit"
import { fixture, expect, oneEvent } from "@open-wc/testing"
import { sendKeys } from "@web/test-runner-commands"
import { ifDefined } from "lit/directives/if-defined.js"

import "../leu-chip-removable.js"

async function defaultFixture() {
return fixture(html` <leu-chip-removable>Daten</leu-chip-removable> `)
async function defaultFixture(args = {}) {
return fixture(
html`
<leu-chip-removable value=${ifDefined(args.value)}
>${args.label ?? "Daten"}</leu-chip-removable
>
`
)
}

describe("LeuChipRemovable", () => {
Expand Down Expand Up @@ -70,4 +77,31 @@ describe("LeuChipRemovable", () => {

expect(event).to.exist
})

it("sends the value in the remove event", async () => {
const el = await defaultFixture({ label: `Daten  ` }) // eslint-disable-line no-irregular-whitespace
const button = el.shadowRoot.querySelector("button")

setTimeout(() => button.click())
const event = await oneEvent(el, "leu:remove")

expect(event.detail.value).to.equal("Daten")

el.value = "test"

setTimeout(() => button.click())
const event2 = await oneEvent(el, "leu:remove")

expect(event2.detail.value).to.equal("test")
})

it("returns the value or label when getValue is called", async () => {
const el = await defaultFixture({ label: `Daten  ` }) // eslint-disable-line no-irregular-whitespace

expect(el.getValue()).to.equal("Daten")

el.value = "daten-01"

expect(el.getValue()).to.equal("daten-01")
})
})
12 changes: 11 additions & 1 deletion src/components/chip/test/chip-selectable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function defaultFixture(args = {}) {
return fixture(
html`
<leu-chip-selectable
value="Publikationen"
value=${ifDefined(args.value)}
variant=${ifDefined(args.variant)}
?checked=${args.checked}
>Publikationen</leu-chip-selectable
Expand Down Expand Up @@ -90,4 +90,14 @@ describe("LeuChipSelectable", () => {

expect(el.checked).to.be.true
})

it("returns the value or label when getValue is called", async () => {
const el = await defaultFixture()

expect(el.getValue()).to.equal("Publikationen")

el.value = "publikationen-01"

expect(el.getValue()).to.equal("publikationen-01")
})
})
2 changes: 1 addition & 1 deletion src/components/menu/MenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class LeuMenuItem extends LeuElement {
* @returns {string}
*/
getValue() {
return this.value || this.innerText
return this.value || this.textContent.trim()
}

_getAria() {
Expand Down

0 comments on commit 78eb332

Please sign in to comment.