Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a message when there are no filter matches in combobox #1539

Merged
merged 5 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-papayas-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sl-design-system/combobox': patch
---

Show a message when there are no filter matches
21 changes: 21 additions & 0 deletions packages/components/combobox/src/combobox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,27 @@ describe('sl-combobox', () => {
expect(options[1]).to.be.displayed;
expect(options[2]).to.be.displayed;
});

it('should show a message when there are no matches', async () => {
input.focus();
await sendKeys({ type: 'Foo' });

const noMatch = el.querySelector('sl-combobox-no-match');
expect(noMatch).to.exist;
expect(noMatch?.renderRoot).to.have.text('No options starting with "Foo" have been found.');
});

it('should remove the message when there are matches', async () => {
input.focus();
await sendKeys({ type: 'Foo' });

expect(el.querySelector('sl-combobox-no-match')).to.exist;

input.select();
await sendKeys({ press: 'Backspace' });

expect(el.querySelector('sl-combobox-no-match')).not.to.exist;
});
});

describe('group selected', () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/components/combobox/src/combobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { repeat } from 'lit/directives/repeat.js';
import styles from './combobox.scss.js';
import { CreateCustomOption } from './create-custom-option.js';
import { CustomOption } from './custom-option.js';
import { NoMatch } from './no-match.js';
import { SelectedGroup } from './selected-group.js';

declare global {
Expand Down Expand Up @@ -57,6 +58,7 @@ export class Combobox<T = unknown> extends FormControlMixin(ScopedElementsMixin(
return {
'sl-combobox-create-custom-option': CreateCustomOption,
'sl-combobox-custom-option': CustomOption,
'sl-combobox-no-match': NoMatch,
'sl-combobox-selected-group': SelectedGroup,
'sl-icon': Icon,
'sl-option': Option,
Expand All @@ -75,6 +77,9 @@ export class Combobox<T = unknown> extends FormControlMixin(ScopedElementsMixin(
/** Event controller. */
#events = new EventsController(this, { click: this.#onClick });

/** Message element for when filtering results did not yield any results. */
#noMatch?: NoMatch;

/** Monitor the DOM for new options. */
#observer = new MutationObserver(() => this.#updateOptions());

Expand Down Expand Up @@ -670,16 +675,31 @@ export class Combobox<T = unknown> extends FormControlMixin(ScopedElementsMixin(
}

#updateFilteredOptions(value?: string): void {
let noMatch = true;

this.options.forEach(option => {
let match = !this.filterResults || !value;
if (!match) {
match = option.content.toLowerCase().startsWith(value!.toLowerCase());
}

if (noMatch && match) {
noMatch = false;
}

if (option.element) {
option.element.style.display = match ? '' : 'none';
}
});

if (noMatch && value) {
this.#noMatch ||= this.shadowRoot!.createElement('sl-combobox-no-match') as NoMatch;
this.#noMatch.value = value;
this.listbox?.prepend(this.#noMatch);
} else {
this.#noMatch?.remove();
this.#noMatch = undefined;
}
}

/** Updates the list of options and the listbox link with the text input. */
Expand Down
4 changes: 4 additions & 0 deletions packages/components/combobox/src/no-match.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:host {
color: var(--sl-color-text-plain);
padding-inline: var(--sl-space-new-md);
}
26 changes: 26 additions & 0 deletions packages/components/combobox/src/no-match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { localized, msg, str } from '@lit/localize';
import { type CSSResultGroup, LitElement } from 'lit';
import { property } from 'lit/decorators.js';
import styles from './no-match.scss.js';

declare global {
interface HTMLElementTagNameMap {
'sl-combobox-no-match': NoMatch;
}
}

/**
* Message component for when filtering a listbox yields no matches.
*/
@localized()
export class NoMatch extends LitElement {
/** @internal */
static override styles: CSSResultGroup = styles;

/** The string that did not yield any matches. */
@property() value?: string;

override render(): string {
return msg(str`No options starting with "${this.value}" have been found.`);
}
}
1 change: 1 addition & 0 deletions packages/locales/src/nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const templates = {
s129fac0ae1df7c8f: 'waarschuwing',
s13fdff160ffad275: 'Wis tekst',
s19fe06e3408e53d0: 'succes',
s2362b49263f76ec8: str`Geen opties gevonden die beginnen met "${0}".`,
s2ceb11be2290bb1b: 'Annuleren',
s39bba60b440fcf00: 'verplicht',
s4215e0aa2fa8f5b4: 'Type hier om te filteren',
Expand Down
4 changes: 4 additions & 0 deletions packages/locales/src/nl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@
<source>Please select at least one option.</source>
<target>Selecteer tenminste één optie.</target>
</trans-unit>
<trans-unit id="s2362b49263f76ec8">
<source>No options starting with "<x id="0" equiv-text="${this.value}"/>" have been found.</source>
<target>Geen opties gevonden die beginnen met "<x id="0" equiv-text="${this.value}"/>".</target>
</trans-unit>
</body>
</file>
</xliff>