ShellUI Logo
ShellUI

Data Picker

Searchable single-select for objects, with custom templates

DataPicker<TItem, TKey> picks one object from a list, with a search box. Unlike Select, the value is the item itself, not a string.

Installation

shellui add data-picker

Usage

<DataPicker TItem="Person" TKey="int"
    Items="_people"
    @bind-Value="_selected"
    KeySelector="p => p.Id"
    DisplaySelector="p => p.Name"
    Placeholder="Choose a person" />

<p>Selected: @_selected?.Name</p>

@code {
    private Person? _selected;
}
@code {
    private List<Person> _people = new()
    {
        new(1, "Ada Lovelace", "Engineering"),
        new(2, "Grace Hopper", "Engineering"),
        new(3, "Katherine Johnson", "Research"),
    };

    record Person(int Id, string Name, string Team);
}

Custom templates

<DataPicker TItem="Person" TKey="int" Items="_people" @bind-Value="_selected"
    KeySelector="p => p.Id" DisplaySelector="p => p.Name"
    SearchPredicate="(p, q) => p.Name.Contains(q, StringComparison.OrdinalIgnoreCase) || p.Team.Contains(q, StringComparison.OrdinalIgnoreCase)">
    <OptionTemplate>
        <div class="flex flex-col">
            <span>@context.Name</span>
            <span class="text-xs text-muted-foreground">@context.Team</span>
        </div>
    </OptionTemplate>
</DataPicker>

API Reference

PropertyTypeDefaultDescription
TItemGeneric type—The item type
TKeyGeneric type—The key type returned by KeySelector
ItemsIEnumerable<TItem>requiredItems to choose from
ValueTItem?defaultSelected item (supports @bind-Value)
ValueChangedEventCallback<TItem?>—Raised when the selection changes
KeySelectorFunc<TItem, TKey>requiredUnique key for each item
DisplaySelectorFunc<TItem, string>?nullText shown for an item (defaults to ToString())
SearchPredicateFunc<TItem, string, bool>?nullCustom search match (defaults to a case-insensitive match on the display text)
SelectedTemplateRenderFragment<TItem>?nullCustom rendering of the selected item in the trigger
OptionTemplateRenderFragment<TItem>?nullCustom rendering for each option in the list
Placeholderstring"Select..."Text shown when nothing is selected
SearchPlaceholderstring"Search..."Placeholder of the search box
EmptyTextstring"No results found."Text shown when the search matches nothing
DisabledboolfalseDisables the control
CloseOnScrollboolfalseClose the list when the page scrolls
Classstring?nullAdditional CSS classes

On this page