Choices

Selects, radio groups, segmented controls, checkbox groups and toggle groups

Introduction

StellarAdmin provides five different kinds of editors for selecting items(s) from a range of options.

EditorUse it for
UseSelectEditor()Select a single item from a list. This is the default for enums and bool?.
UseRadioGroupEditor()Select a single value, with every option visible, and room for descriptions.
UseSegmentedControlEditor()Select a single value from two to five short options, displayed in a row.
UseCheckboxGroupEditor()Select multiple values from a list. Useful when the backing property is an array or collection.
UseToggleGroupEditor()Select a single or multiple values displayed as chips or buttons. Single or multiple selection depends on the type of the backing property.
Each kind of choice editor with a value selected: Select
Select
Each kind of choice editor with a value selected: Radio group
Radio group
Each kind of choice editor with a value selected: Segmented control
Segmented control
Each kind of choice editor with a value selected: Checkbox group
Checkbox group
Each kind of choice editor with a value selected: Toggle group
Toggle group

These choice editors display all the available options on the form. For long lists coming from a database table, such as customers or airports, use a lookup instead. See selecting a related record for selecting a single value, and selecting several related records for selecting multiple values.

Supplying the options

You can supply the items displayed to the user explicitly by calling the UseItems() method in the editor builder to supply the list of options.

using StellarAdmin.Resources.Editors;

fields.Add(guide => guide.HomeAirport).UseRadioGroupEditor(radio =>
{
    radio.UseItems([
        new ChoiceItem("LIS", "Lisbon"),
        new ChoiceItem("KIX", "Kyoto"),
        new ChoiceItem("CPT", "Cape Town"),
    ]);
});
A radio group with Not set, Lisbon, Kyoto and Cape Town, and Kyoto selected

In this case, the underlying property is nullable, so the radio group also has an empty option, "Not set". See changing the empty option.

A ChoiceItem requires a value and a title. Values are strings, and converted to the property's type when the form is saved. You can also set the following properties:

PropertyUse it for
DescriptionSecondary text displayed below the option's title.
GroupThe ChoiceGroup the option belongs to. Only selects display groups, as option groups.
MediaAn icon, image or short code beside the title. See displaying an icon or image beside each option.
DisabledDisplaying the option without allowing people to select it.

UseItems() has a few different overloads:

OverloadUse it for
UseItems(items)A fixed list of ChoiceItem or SelectListItem.
UseItems((services, cancellationToken) => ...)Useful for loading the options from a service that must be resolved via DI.
UseItems<TProvider>()A provider that implements IChoiceItemsProvider. Register the provider as a service.
UseItems((form, services, cancellationToken) => ...)Options that depend on other fields. See filtering options by another field.
UseItems<TEntity>(value, title)Rows of an EF Core entity that isn't a resource. The method is in the StellarAdmin.EntityFrameworkCore namespace.

The example below loads the options from a service registered with dependency injection.

fields.Add(guide => guide.HomeAirport).UseSelectEditor(select =>
    select.UseItems(async (services, cancellationToken) =>
    {
        var airports = services.GetRequiredService<AirportDirectory>();
        return (await airports.ListAsync(cancellationToken))
            .Select(airport => new ChoiceItem(airport.Code, airport.City))
            .ToList();
    }));

Inferring options from the property type

When you don't call UseItems(), StellarAdmin will attempt to infer the options from the type of the property:

  • Enums list their members. An enum with [Flags] has no options.
  • Booleans get Yes and No.
  • Collections of enums, such as List<Climate>, list the members of the enum and allow users to select multiple items, so use them with a checkbox group or a toggle group.

Enums

StellarAdmin generates an option for each member of an enum. Use the [Display] attribute on each member to set the title of the option with Name, and its description with Description. You can set GroupName in the [Display] attribute to divide the options into groups (only displayed in select editors).

public enum TripDifficulty
{
    [Display(Name = "Easy", Description = "Gentle walks, a few hours a day.")]
    Easy,

    [Display(Name = "Moderate", Description = "Full days on foot, some climbs.")]
    Moderate,

    [Display(Name = "Challenging", Description = "Long days and steep trails.")]
    Challenging,
}

Using the radio group editor with a property of type TripDifficulty will display the following:

A radio group with Easy, Moderate and Challenging, each with its description

When the property is a collection of an enum, StellarAdmin generates the same options, and people can select several of them. Use a checkbox group or a toggle group for these properties. In the example below, a trip has a list of climates:

public class Trip
{
    public List<Climate> Climates { get; set; } = [];
}

public enum Climate
{
    Tropical,
    Arid,
    Temperate,
    Mediterranean,
    Continental,
    Polar,
    Alpine,
}

The checkbox group lists each member of Climate, and saves the checked ones to the list:

fields.Add(trip => trip.Climates).UseCheckboxGroupEditor();

Use another resource as options

Call UseResource<TResource>() to list the records of another resource. Each option takes its title, description and media from that resource's title and reference information.

fields.Add(trip => trip.DestinationId).UseRadioGroupEditor(radio =>
    radio.UseResource<Destination>());

Foreign keys and many-to-many navigations on EF Core forms list the related resource's records without calling UseResource(). See selecting a related record and selecting several related records.

Displaying an icon or image beside each option

Each option can display an icon, image or short code beside its title, to help people recognize it at a glance. Set the Media of the ChoiceItem to the media you want to display.

fields.Add(trip => trip.Transport).UseToggleGroupEditor(toggle =>
{
    toggle.UseItems([
        new ChoiceItem("Plane", "Plane") { Media = new ItemMedia.Icon("plane") },
        new ChoiceItem("Train", "Train") { Media = new ItemMedia.Icon("train-front") },
        new ChoiceItem("Ship", "Ship") { Media = new ItemMedia.Icon("ship") },
    ]);
});
A toggle group with Plane, Train and Ship, each with its icon

There are four kinds of media:

MediaDisplays
new ItemMedia.Icon(name)The icon with the specified name, in the text color.
new ItemMedia.Avatar(url)A round image from the specified URL. When the URL is null, the initials of the option's title.
new ItemMedia.Image(url)A square image from the specified URL, with rounded corners.
new ItemMedia.Code(value)A short code, such as an airport or currency code, as a badge.

Radio groups, checkbox groups, segmented controls and toggle groups display the media. Selects only display the title, since a browser's native select cannot display images.

Configuring the empty option

The empty option is available when the user can clear or unset a value. It will be enabled by default on nullable types and will display the text "Not set" as its label. You can use the EmptyChoiceText property to change the label text, and EmptyChoice to change when the empty option is added.

fields.Add(trip => trip.LeadGuideId).UseSelectEditor(select =>
{
    select.EmptyChoice = EmptyChoice.Include;
    select.EmptyChoiceText = "No guide yet";
});
ValueEmpty option
EmptyChoice.AutoAdded when the property type is nullable. This is the default.
EmptyChoice.IncludeAlways added, even when the value is required.
EmptyChoice.OmitNever added, even when the property is nullable.

A select also displays the empty option while a required value matches no option, such as a new record's foreign key of 0, so that no other option appears selected. Once the value matches an option, the empty option no longer appears.

Changing the layout of the options

Columns

Radio groups and checkbox groups display their options in a single column. Call Columns() to spread the options over more columns.

fields.Add(trip => trip.Tags).UseCheckboxGroupEditor(tags =>
{
    tags.Columns(3);
});

With Columns(3), the options are displayed in one column when the field is narrow, and in three columns once the field is at least 40rem wide.

A trip's tags as a checkbox group in three columns

To set the number of columns at each width, use the same breakpoints as form layout:

tags.Columns(columns => columns.Small(2).Large(3));

Options fill each row from left to right before starting the next. Set Flow to Down to fill each column from top to bottom instead, so that people can read an alphabetical list down each column.

using StellarAdmin.UI;

fields.Add(trip => trip.Tags).UseCheckboxGroupEditor(tags =>
{
    tags.Columns(3);
    tags.Flow = CheckboxGroupFlow.Down;
});

Radio and Checkbox Cards

Radio groups and checkbox groups display a list of options with labels. Set Appearance to Cards to display each option as a selectable card instead, which leaves more room for a description and media.

The example below displays the trip's difficulty as cards in three columns, each with its title and description.

fields.Add(trip => trip.Difficulty).UseRadioGroupEditor(difficulty =>
{
    difficulty.Appearance = RadioGroupAppearance.Cards;
    difficulty.Columns(3);
});
Easy, Moderate and Challenging as three cards side by side, each with its description, and Moderate selected

Toggle groups

The toggle group supports 3 different appearances.

ToggleGroupAppearanceDisplays
ChipsSeparate rounded chips, with a check mark on the selected ones. This is the default.
JoinedButtons joined into a single connected group.
ButtonsSeparate buttons.
A toggle group of trip difficulties with Moderate selected: Chips
Chips
A toggle group of trip difficulties with Moderate selected: Joined
Joined
A toggle group of trip difficulties with Moderate selected: Buttons
Buttons

When a toggle group uses Chips, use CheckPlacement to specify where the chips display their check mark:

ToggleGroupCheckPlacementDisplays the check mark
ReplaceMediaIn place of the option's media, so the chip keeps its width. Before the title when the option has no media. This is the default.
StartBefore the media and title.
EndAfter the title.
fields.Add(guide => guide.Languages).UseToggleGroupEditor(languages =>
{
    languages.Appearance = ToggleGroupAppearance.Chips;
    languages.CheckPlacement = ToggleGroupCheckPlacement.End;
});

On this page