Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

MultiTreeSelect

Since 0.1.0-alpha.22
Opens a tree from a field to pick several nodes, shown as chips in the space of one input.

Choose several items from a tree, bound to a collection.

Teams to share with

A MultiTreeSelect binds to a collection, and ItemValue is what each checked item adds. Checking a department checks its teams, and the value contains every checked item, departments included.

Static

Preview

Code

Usage.razor
@using System.ComponentModel.DataAnnotations

<Form Model="Model" FormName="multi-tree-select-usage" OnValidSubmit="Save" MaxWidth="28.Rem" Grow>
    <MultiTreeSelect @bind-Value="Model!.TeamIds" Items="Departments" ItemChildren="d => d.Teams"
                     ItemValue="d => d.Id" ItemText="d => d.Name" Placeholder="Choose teams" Searchable Clearable />
    <Row><Button Submit>Share report</Button></Row>
    @if (_shared is not null)
    {
        <Alert Status="Status.Success">Shared with @_shared.</Alert>
    }
</Form>

@code {
    [SupplyParameterFromForm(FormName = "multi-tree-select-usage")]
    private Report? Model { get; set; }

    private string? _shared;

    // No starting teams: with none checked a static post sends no field, and a default here would
    // come back.
    protected override void OnInitialized() => Model ??= new();

    // Checking a department checks its teams, so the teams are what's shared.
    private void Save() => _shared = string.Join(", ", Departments.SelectMany(d => d.Teams).Where(t => Model!.TeamIds!.Contains(t.Id)).Select(t => t.Name));

    public sealed class Report
    {
        // [Required] for a static post with nothing checked (no field arrives); [MinLength] on an
        // interactive page, where it's an empty list.
        [Required(ErrorMessage = "Choose at least one team")]
        [MinLength(1, ErrorMessage = "Choose at least one team")]
        [Display(Name = "Visible to")]
        public List<string>? TeamIds { get; set; }
    }

}

Static: Checking a department checks its teams and the chips follow at once; a chip's × or Backspace on the field unchecks it.

Add this example to a page with the izzy CLI:

bash
izzy add component MultiTreeSelect --at <file>:<line>

A department checked whole shows as one chip. To require a choice, put [Required] and [MinLength(1)] on the property.

Details

Checking a parent checks its branch, and a parent with part of its branch checked shows a mixed box; Cascade="false" makes each box its own. The value contains every checked item, parents included. The field shows the choice as chips, and a branch checked whole as one chip, its parent's.

For a large tree, ItemHasChildren and ChildrenProvider load a branch's children the first time it opens, as in TreeView; ValueText names a chosen value whose item is still to load.

How it works three ways:

  • No script: the button opens the panel (the Popover API), whose checkboxes post with the form as repeated fields, which Blazor binds to the collection. Checking a parent doesn't check its branch, and the chips change once the form posts.
  • With IzzyBlazor's script: checking a parent checks its branch, the panel stays open while you check, the chips follow at once, and a chip's × or Backspace on the field unchecks it.
  • Interactive: the same, and @bind-Value follows each change.

Mode support

What works in each mode, MultiTreeSelect
FeatureStaticInteractive
Choose several items from a tree, posted with the formYesYes
Checking a parent checks its branch; mixed parentsYesYes
Chips that follow the choice, a branch checked whole as oneYesYes
A search box that narrows the treeYesYes
@bind-Value follows each changeNoYes

Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.

Accessibility

  • The field is a button named by its label and the chosen items ("Visible to Web, Billing"), with aria-haspopup="dialog", the hint and errors as its description. Without a label, give it an aria-label. The placeholder isn't part of its name.
  • The panel is a non-modal dialog named by the label. With IzzyBlazor's script the tree in it is an ARIA tree (one Tab stop, the arrow keys between items, each treeitem saying whether it's checked, "mixed" for a part-checked parent, and an item that can't be chosen saying nothing); with JavaScript off it's checkboxes in branches.
  • With IzzyBlazor's script, opening it moves focus to the search box with Searchable, else the tree's first checked item, else the first; Escape closes it and puts focus back on the field, and Tab out of it closes it. With JavaScript off, focus stays on the field.
  • A chip's × is for the mouse and hidden from screen readers; Backspace on the field, the tree's boxes and the clear button ("Clear", IzzyText.Clear) do the same from the keyboard. A chip for an item that can't be changed (ItemDisabled, ItemSelectable) has no ×.

Keyboard navigation

MultiTreeSelect keys
Press this keyTo do this
Enter or Space or Down or UpOpen the tree (on the field)
BackspaceUncheck the last chip (on the field)
DownMove from the search box to the first match
Up or DownMove to the item above or below
RightOpen an item's branch, or move to its first child (Left on right-to-left pages)
LeftClose an item's branch, or move to its parent (Right on right-to-left pages)
Home or EndMove to the first or last item
*Open every branch beside the focused item
Space or EnterCheck or uncheck the item; on one that can't be, open or close its branch
A letterMove to the next item starting with it
Escape or TabClose the tree

Parameters

MultiTreeSelect parameters
PropTypeDefault
ItemValueThe value an item adds to the collection when checked, such as its id.Func<TItem, TItemValue>?Required
CascadeChecking a parent checks its branch, and a parent shows how much of its branch is checked.booltrue
ValueTextNames a chosen value when its item isn't in the tree yet: in a branch still to load (ChildrenProvider).Func<TItemValue, string>?computed
ItemsThe items at the top of the tree.IEnumerable<TItem>?
ItemChildrenAn item's children, or null or empty when it has none.Func<TItem, IEnumerable<TItem>?>?
ItemTextAn item's text in the tree and in the field once chosen.Func<TItem, string?>?computed
ItemDescriptionA muted second line under an item's text in the tree.Func<TItem, string?>?
ItemIconAn icon before an item's text in the tree.Func<TItem, IconData?>?
ItemTemplateMarkup for each item in the tree, in place of its text, description and icon.RenderFragment<TItem>?
ItemExpandedStarts an item's children open.Func<TItem, bool>?
ItemDisabledDims an item and stops it being chosen.Func<TItem, bool>?
ItemSelectableWhether an item can be chosen; one that can't is a label for its branch, and with IzzyBlazor's script a click on it opens or closes the branch.Func<TItem, bool>?
SearchableA search box at the top of the panel that narrows the tree as you type: matches and their parents stay, their branches open.boolfalse
SearchLabelThe search box's name and placeholder.string?computed
ItemHasChildrenSays an item has children to load with ChildrenProvider when its branch first opens, for items whose ItemChildren is empty.Func<TItem, bool>?
ChildrenProviderLoads an item's children, given its key (its value's string form), the first time its branch opens: in the component on interactive pages, and on static pages by the script fetching the page with the key.Func<string, CancellationToken, ValueTask<IEnumerable<TItem>>>?
PlaceholderText in the field while nothing is chosen, such as "Choose a category".string?
EmptyTextShown in the panel when there are no items.string?computed
ClearableA button in the field that empties it, for a value that can be empty, there while one is chosen.boolfalse
ReadOnlyMakes the field read-only: the chosen text shows in a read-only text field, and the value still posts.boolfalse
@bind-ValueThe value, bound with @bind-Value.TValue
DisplayNameThe field's name in validation messages from the input itself, such as a value that can't be parsed.string?

See also

  • TreeSelect: opens a tree from a field, so a choice from a deep hierarchy takes the space of one input.
  • TreeView: shows items nested in items, such as folders, as branches that open and close.
  • MultiSelect: lets the user choose several options, shown as chips, and type to filter them.
  • CheckboxGroup: lets the user pick several choices from a list of checkboxes.

Connection lost

Rejoining the server…