# Combobox examples - Tailwind (`/tw/combobox`) These examples use custom variants the demo declares in its Tailwind input CSS: ```css @custom-variant data-active (&[data-active]); @custom-variant data-selected (&[data-selected]); @custom-variant data-disabled (&[data-disabled]); ``` ## Basic Combobox Type to filter, use Up / Down to navigate, Enter to select, Escape to close. The combobox emits an `OnQueryChange` event; you do the filtering in your own code and pass only the matching options as children. ```razor
@if (filteredFruits.Count == 0) {
  • No matches.
  • } else { @foreach (var fruit in filteredFruits) { } }
    @code { private string? selectedFruit; private readonly string[] allFruits = { "Apple", "Apricot", "Banana", "Blueberry", "Cherry", "Coconut", "Cranberry", "Grape", "Kiwi", "Lemon", "Lime", "Mango", "Orange", "Papaya", "Peach", "Pear", "Pineapple", "Plum", "Raspberry", "Strawberry", }; private List filteredFruits = new(); private void HandleFruitQuery(string q) => filteredFruits = string.IsNullOrWhiteSpace(q) ? allFruits.ToList() : allFruits.Where(f => f.Contains(q, StringComparison.OrdinalIgnoreCase)).ToList(); protected override void OnInitialized() => filteredFruits = allFruits.ToList(); } ``` ## Multi-select with chips Set `Multiple="true"` to allow many selections. Selected values are shown as chips before the input (each with a remove button). The panel stays open after each pick. ```razor
    @foreach (var tag in pickedTags) { @tag }
    @if (filteredTags.Count == 0) {
  • No matches.
  • } else { @foreach (var tag in filteredTags) { } }
    @code { private IReadOnlyCollection pickedTags = Array.Empty(); private BitBhCombobox tagCombobox = default!; private readonly string[] allTags = { "design", "engineering", "marketing", "sales", "support", "research", "product", "operations", "finance", "legal", }; private List filteredTags = new(); private void HandleTagQuery(string q) => filteredTags = string.IsNullOrWhiteSpace(q) ? allTags.ToList() : allTags.Where(t => t.Contains(q, StringComparison.OrdinalIgnoreCase)).ToList(); // The chip's remove button leaves with the chip: give the focus back to the input. private async Task RemoveTag(string tag) { pickedTags = pickedTags.Where(t => t != tag).ToList(); await tagCombobox.FocusAsync(); } protected override void OnInitialized() => filteredTags = allTags.ToList(); } ``` ## Object values with rich content Bind to objects rather than strings. Provide a `DisplayValue` function that converts the selected object back to the input text, and render rich content inside each option. ```razor
    @if (filteredPeople.Count == 0) {
  • No matches.
  • } else { @foreach (var person in filteredPeople) { } }
    @code { public sealed record Person(string Name, string Email) { public string Initials => string.Concat(Name.Split(' ', StringSplitOptions.RemoveEmptyEntries).Take(2).Select(p => p[0])); } private Person? selectedPerson; private readonly Person[] allPeople = { new("Alice Anderson", "alice@example.com"), new("Benjamin Brown", "ben@example.com"), new("Caterina Costa", "cat@example.com"), new("Daniel Davis", "dan@example.com"), new("Eleanor Evans", "ellie@example.com"), new("Felix Foster", "felix@example.com"), new("Genevieve Gomez", "gen@example.com"), new("Henry Hughes", "henry@example.com"), }; private List filteredPeople = new(); private void HandlePersonQuery(string q) => filteredPeople = string.IsNullOrWhiteSpace(q) ? allPeople.ToList() : allPeople.Where(p => p.Name.Contains(q, StringComparison.OrdinalIgnoreCase) || p.Email.Contains(q, StringComparison.OrdinalIgnoreCase)).ToList(); protected override void OnInitialized() => filteredPeople = allPeople.ToList(); } ``` ## Anchor Positioning Set `Anchor` on `BitBhComboboxOptions` to automatically position the dropdown relative to the input using `position: fixed`. The panel flips to the opposite side when there's not enough space, shifts to stay within the viewport, and auto-updates on scroll/resize. No `position: relative` on the parent is needed. ```razor
    @if (anchorFilteredFruits.Count == 0) {
  • No matches.
  • } else { @foreach (var fruit in anchorFilteredFruits) { } }
    @code { private string? anchorFruit; private readonly string[] allFruits = { "Apple", "Apricot", "Banana", "Blueberry", "Cherry", "Coconut", "Cranberry", "Grape", "Kiwi", "Lemon", "Lime", "Mango", "Orange", "Papaya", "Peach", "Pear", "Pineapple", "Plum", "Raspberry", "Strawberry", }; private List anchorFilteredFruits = new(); private void HandleAnchorFruitQuery(string q) => anchorFilteredFruits = string.IsNullOrWhiteSpace(q) ? allFruits.ToList() : allFruits.Where(f => f.Contains(q, StringComparison.OrdinalIgnoreCase)).ToList(); protected override void OnInitialized() => anchorFilteredFruits = allFruits.ToList(); } ``` ## Large lists (virtualized) Pass the matching data to `BitBhComboboxOptions` through `Items` with an `ItemTemplate`, and set `Virtualize="true"` so only the options in view are rendered in a scrollable panel. Every option must be exactly `ItemSize` tall (36px here): the CSS version sizes the generated `[role="option"]` elements, the Tailwind version gives the template's content `h-9`. Filtering stays yours: `OnQueryChange` runs `BitBhFilter.Contains` — case- and accent-insensitive — over 10,000 items on every keystroke. Child content such as `BitBhComboboxEmpty` still renders after the data options; it shows only while nothing matches. ```razor
    @* Bounded scroll container; each option exactly ItemSize (36px = h-9) *@ @o.Value @if (o.IsSelected) { ✓ } No animals match.
    @code { // 10 adjectives × 10 animals × 100 = 10,000 items. private static readonly string[] VirtualItems = BuildVirtualItems(); private IReadOnlyList virtualMatches = VirtualItems; private string? virtualPick; private static string[] BuildVirtualItems() { string[] adjectives = ["Amber", "Brave", "Calm", "Dusty", "Eager", "Fuzzy", "Gentle", "Hazy", "Icy", "Jolly"]; string[] animals = ["Badger", "Crane", "Dingo", "Falcon", "Gecko", "Heron", "Ibis", "Jaguar", "Koala", "Lynx"]; return Enumerable.Range(0, 10_000) .Select(i => $"{adjectives[i / 1000]} {animals[i / 100 % 10]} {i % 100 + 1}") .ToArray(); } // Case- and accent-insensitive "contains" match. private void HandleVirtualQuery(string query) => virtualMatches = VirtualItems.Where(item => BitBhFilter.Contains(item, query)).ToArray(); } ``` ## Options of different heights (virtualized) With `VariableItemSize="true"` the options of a virtualized panel can differ in height - here each article shows an excerpt of one to three lines. Every option is measured as it renders and again when it resizes; `ItemSize` (60px here) is only the estimate for the options not rendered yet, and a new query starts over with the matches it found. The scroll position holds still as measurements arrive, and the arrow keys and Page Up / Page Down keep the highlighted option fully in view. ```razor
    @* Measured as they render: ItemSize is the estimate for the rest *@ @o.Value.Title @o.Value.Excerpt No articles match.
    @code { private sealed record Article(int Number, string Title, string Excerpt); private static readonly Article[] Articles = …; // 3,000 articles, excerpts of one to three lines private IReadOnlyList
    articleMatches = Articles; private Article? article; private void HandleArticleQuery(string query) => articleMatches = Articles.Where(a => BitBhFilter.Contains(a.Title, query)).ToArray(); } ```