diff --git a/src/components/select/bl-select.stories.mdx b/src/components/select/bl-select.stories.mdx index bc0fa702..f189be6d 100644 --- a/src/components/select/bl-select.stories.mdx +++ b/src/components/select/bl-select.stories.mdx @@ -89,6 +89,20 @@ export const selectOpener = async ({ canvasElement }) => { } } +export const SpecialUseCaseTemplate = () => html` + Option 1 + +
+ Option 2 + + Recommended + Some tooltip text + +
+
+ Option 3 +
`; + export const SelectTemplate = (args) => html` +## Special Use Cases + +Select component can render a custom option label if `bl-select-option` has a `label` attribute. + +This is useful when you want to show a custom label for an option inside a `bl-select`, but you want to show original content inside the option itself. + + + + {SpecialUseCaseTemplate.bind({})} + + + ## `bl-select` Event Select component fires `bl-select` event once selection changes. This event has a payload in the type of diff --git a/src/components/select/bl-select.test.ts b/src/components/select/bl-select.test.ts index 10636950..01cfa2ff 100644 --- a/src/components/select/bl-select.test.ts +++ b/src/components/select/bl-select.test.ts @@ -93,7 +93,29 @@ describe("bl-select", () => { expect(el.options.length).to.equal(2); expect(el.selectedOptions.length).to.equal(1); }); + it("should render bl-select-option label correctly on bl-select", async () => { + const el = await fixture(html` + Option 1 + Option 2 + `); + const selectedOptions = el.shadowRoot?.querySelector(".selected-options"); + + expect(selectedOptions?.children[0].textContent).to.equal("custom-label-1"); + }); + it("should render bl-select-option label(s) correctly on bl-select when select is multiple", async () => { + const el = await fixture(html` + Option 1 + Option 2 + Option 3 + Option 4 + `); + + const selectedOptions = el.shadowRoot?.querySelector(".selected-options"); + + expect(selectedOptions?.textContent).contains("custom-label-1"); + expect(selectedOptions?.textContent).contains("Option 3"); + }); it("should open select menu", async () => { const el = await fixture(html`button`); diff --git a/src/components/select/bl-select.ts b/src/components/select/bl-select.ts index 03718514..30c4c343 100644 --- a/src/components/select/bl-select.ts +++ b/src/components/select/bl-select.ts @@ -358,7 +358,9 @@ export default class BlSelect extends Form private inputTemplate() { const inputSelectedOptions = html`
    - ${this._selectedOptions.map(item => html`
  • ${item.textContent}
  • `)} + ${this._selectedOptions.map( + item => html`
  • ${item.getAttribute("label") || item.textContent}
  • ` + )}
`; const isAllSelectedDisabled = diff --git a/src/components/select/option/bl-select-option.ts b/src/components/select/option/bl-select-option.ts index 94cf5b53..4107a6a3 100644 --- a/src/components/select/option/bl-select-option.ts +++ b/src/components/select/option/bl-select-option.ts @@ -26,6 +26,12 @@ export default class BlSelectOption extend this._value = val; } + /** + * Sets the label for bl-select, and bl-select renders this value instead of the option's textContent + */ + @property({ type: String, reflect: true, attribute: "label" }) + label = ""; + /** * Sets option as disabled */