NvFielddropdown Component - Practical Guide for Blazor Developers
The NvFielddropdown component is a Blazor wrapper around Nova's dropdown combobox component, providing a text input field with a filtered dropdown list of selectable options. It combines the flexibility of text input with the structure of dropdown selection.
General Blazor Concepts
For general information about data binding, forms, and validation in Nova Blazor components, see the Nova Blazor Integration Guide. This guide focuses on dropdown-specific features and patterns.
Basic Usage
Simple Dropdown
<NvFielddropdown Label="Country" Options="countryOptions" />
Dropdown with Placeholder
<NvFielddropdown
Label="Select Category"
Placeholder="Type to search categories..."
Options="categoryOptions"
Filterable="true" />
Two-Way Data Binding
The component supports Blazor's @bind-Value syntax for seamless two-way data binding with string values:
Basic Binding
@code {
private string selectedCountry = "";
private NvFielddropdownOptions[] countryOptions = new[]
{
new NvFielddropdownOptions { Label = "United States", Value = "us" },
new NvFielddropdownOptions { Label = "Canada", Value = "ca" },
new NvFielddropdownOptions { Label = "United Kingdom", Value = "uk" },
new NvFielddropdownOptions { Label = "Germany", Value = "de" },
new NvFielddropdownOptions { Label = "France", Value = "fr" },
new NvFielddropdownOptions { Label = "Australia", Value = "au" }
};
}
<NvFielddropdown
@bind-Value="selectedCountry"
Label="Country"
Options="countryOptions"
Filterable="true"
Placeholder="Search countries..." />
@if (!string.IsNullOrEmpty(selectedCountry))
{
<div class="selection-info">
<p>Selected country code: @selectedCountry</p>
<p>Selected country: @countryOptions.FirstOrDefault(o => o.Value == selectedCountry)?.Label</p>
</div>
}
Pre-selected Value
@code {
private string selectedPriority = "medium";
private NvFielddropdownOptions[] priorityOptions = new[]
{
new NvFielddropdownOptions { Label = "Low Priority", Value = "low" },
new NvFielddropdownOptions { Label = "Medium Priority", Value = "medium" },
new NvFielddropdownOptions { Label = "High Priority", Value = "high" },
new NvFielddropdownOptions { Label = "Critical", Value = "critical" }
};
}
<NvFielddropdown
@bind-Value="selectedPriority"
Label="Task Priority"
Options="priorityOptions" />
Event Handling
Using DropdownItemSelectedEvent
<NvFielddropdown
@bind-Value="selectedProduct"
Label="Product"
Options="productOptions"
Filterable="true"
DropdownItemSelectedEvent="OnProductSelected" />
@if (!string.IsNullOrEmpty(selectedProduct))
{
<div class="product-info">
<h4>Product Selected</h4>
<p>Code: @selectedProduct</p>
<p>Name: @productOptions.FirstOrDefault(o => o.Value == selectedProduct)?.Label</p>
</div>
}
@code {
private string selectedProduct = "";
private NvFielddropdownOptions[] productOptions = new[]
{
new NvFielddropdownOptions { Label = "Wireless Headphones", Value = "wh-001" },
new NvFielddropdownOptions { Label = "Bluetooth Speaker", Value = "bs-002" },
new NvFielddropdownOptions { Label = "Smart Watch", Value = "sw-003" },
new NvFielddropdownOptions { Label = "Laptop Stand", Value = "ls-004" },
new NvFielddropdownOptions { Label = "USB Cable", Value = "uc-005" }
};
private void OnProductSelected(NvFielddropdownDropdownItemSelectedEvent selection)
{
selectedProduct = selection.Value;
// Could trigger additional logic like loading product details, updating cart, etc.
Console.WriteLine($"Product selected: {selection.Label} ({selection.Value})");
}
}
Filter Text Events
<NvFielddropdown
@bind-Value="selectedEmployee"
Label="Employee"
Options="employeeOptions"
Filterable="true"
FilterTextChangedEvent="OnFilterChanged"
DebounceDelay="300" />
@if (!string.IsNullOrEmpty(currentFilter))
{
<div class="search-info">
<small>Searching for: "@currentFilter"</small>
</div>
}
@code {
private string selectedEmployee = "";
private string currentFilter = "";
private void OnFilterChanged(string filterText)
{
currentFilter = filterText ?? "";
// Could log analytics, perform additional search logic, etc.
}
}
Open/Close Events
<NvFielddropdown
@bind-Value="selectedCity"
Label="City"
Options="cityOptions"
Filterable="true"
OpenChangedEvent="OnDropdownToggle" />
<div class="dropdown-status">
Dropdown is @(dropdownOpen ? "open" : "closed")
</div>
@code {
private string selectedCity = "";
private bool dropdownOpen = false;
private void OnDropdownToggle(bool isOpen)
{
dropdownOpen = isOpen;
// Could trigger analytics, lazy loading, etc.
}
}
Filtering and Search
Basic Filtering
<NvFielddropdown
@bind-Value="selectedSkill"
Label="Skills"
Options="skillOptions"
Filterable="true"
Placeholder="Search skills..."
EmptyResult="No skills found" />
@code {
private string selectedSkill = "";
private NvFielddropdownOptions[] skillOptions = new[]
{
new NvFielddropdownOptions { Label = "JavaScript", Value = "js" },
new NvFielddropdownOptions { Label = "C#", Value = "csharp" },
new NvFielddropdownOptions { Label = "Python", Value = "python" },
new NvFielddropdownOptions { Label = "React", Value = "react" },
new NvFielddropdownOptions { Label = "Angular", Value = "angular" },
new NvFielddropdownOptions { Label = "Vue.js", Value = "vue" },
new NvFielddropdownOptions { Label = "Node.js", Value = "nodejs" },
new NvFielddropdownOptions { Label = "SQL", Value = "sql" },
new NvFielddropdownOptions { Label = "MongoDB", Value = "mongodb" },
new NvFielddropdownOptions { Label = "Docker", Value = "docker" }
};
}
Controlled Filtering
<NvFielddropdown
@bind-Value="selectedUser"
Label="User Search"
Options="filteredUsers"
Filterable="true"
ControlledFilter="true"
FilterTextChangedEvent="OnUserSearch"
Placeholder="Type to search users..." />
@if (searching)
{
<div class="loading">Searching...</div>
}
@code {
private string selectedUser = "";
private bool searching = false;
private NvFielddropdownOptions[] filteredUsers = Array.Empty<NvFielddropdownOptions>();
private async void OnUserSearch(string searchText)
{
if (string.IsNullOrEmpty(searchText))
{
filteredUsers = Array.Empty<NvFielddropdownOptions>();
return;
}
searching = true;
StateHasChanged();
// Simulate API call
await Task.Delay(500);
// Mock search results
filteredUsers = new[]
{
new NvFielddropdownOptions { Label = $"John Doe ({searchText})", Value = "john" },
new NvFielddropdownOptions { Label = $"Jane Smith ({searchText})", Value = "jane" },
new NvFielddropdownOptions { Label = $"Bob Johnson ({searchText})", Value = "bob" }
};
searching = false;
StateHasChanged();
}
}
Debounced Search
<NvFielddropdown
@bind-Value="selectedBook"
Label="Book Search"
Options="bookOptions"
Filterable="true"
DebounceDelay="500"
Placeholder="Search books by title or author..." />
@code {
private string selectedBook = "";
private NvFielddropdownOptions[] bookOptions = new[]
{
new NvFielddropdownOptions { Label = "The Great Gatsby - F. Scott Fitzgerald", Value = "great-gatsby" },
new NvFielddropdownOptions { Label = "To Kill a Mockingbird - Harper Lee", Value = "mockingbird" },
new NvFielddropdownOptions { Label = "1984 - George Orwell", Value = "1984" },
new NvFielddropdownOptions { Label = "Pride and Prejudice - Jane Austen", Value = "pride-prejudice" },
new NvFielddropdownOptions { Label = "The Catcher in the Rye - J.D. Salinger", Value = "catcher-rye" }
};
}
Form Integration
The component integrates seamlessly with Blazor's EditForm for validation and form submission:
Basic Form Integration
<EditForm Model="userProfile" OnValidSubmit="UpdateProfile">
<DataAnnotationsValidator />
<NvFielddropdown
@bind-Value="userProfile.Country"
Label="Country"
Options="countryOptions"
Filterable="true"
For="() => userProfile.Country" />
<NvFielddropdown
@bind-Value="userProfile.Language"
Label="Preferred Language"
Options="languageOptions"
For="() => userProfile.Language" />
<NvFieldtext
@bind-Value="userProfile.FirstName"
Label="First Name"
For="() => userProfile.FirstName" />
<button type="submit">Update Profile</button>
</EditForm>
@code {
private UserProfile userProfile = new();
private void UpdateProfile()
{
// Process profile update
Console.WriteLine("Profile updated successfully!");
}
public class UserProfile
{
[Required]
public string? Country { get; set; }
[Required]
public string? Language { get; set; }
[Required]
public string? FirstName { get; set; }
}
}
Validation
Required Dropdown Validation
<EditForm Model="projectForm" OnValidSubmit="CreateProject">
<DataAnnotationsValidator />
<NvFieldtext
@bind-Value="projectForm.Name"
Label="Project Name"
For="() => projectForm.Name" />
<NvFielddropdown
@bind-Value="projectForm.Category"
Label="Project Category"
Options="categoryOptions"
Required="true"
For="() => projectForm.Category" />
<NvFielddropdown
@bind-Value="projectForm.Priority"
Label="Priority Level"
Options="priorityOptions"
For="() => projectForm.Priority" />
<button type="submit">Create Project</button>
</EditForm>
@code {
private ProjectForm projectForm = new();
public class ProjectForm
{
[Required]
public string? Name { get; set; }
[Required(ErrorMessage = "Please select a project category")]
public string? Category { get; set; }
public string? Priority { get; set; }
}
private NvFielddropdownOptions[] categoryOptions = new[]
{
new NvFielddropdownOptions { Label = "Web Development", Value = "web" },
new NvFielddropdownOptions { Label = "Mobile App", Value = "mobile" },
new NvFielddropdownOptions { Label = "Desktop Software", Value = "desktop" },
new NvFielddropdownOptions { Label = "API Development", Value = "api" },
new NvFielddropdownOptions { Label = "Database Design", Value = "database" }
};
private NvFielddropdownOptions[] priorityOptions = new[]
{
new NvFielddropdownOptions { Label = "Low", Value = "low" },
new NvFielddropdownOptions { Label = "Medium", Value = "medium" },
new NvFielddropdownOptions { Label = "High", Value = "high" },
new NvFielddropdownOptions { Label = "Critical", Value = "critical" }
};
}
Custom Validation
<EditForm Model="bookingForm" OnValidSubmit="MakeBooking">
<DataAnnotationsValidator />
<NvFielddropdown
@bind-Value="bookingForm.Service"
Label="Service Type"
Options="serviceOptions"
For="() => bookingForm.Service" />
<NvFielddropdown
@bind-Value="bookingForm.Provider"
Label="Service Provider"
Options="GetAvailableProviders()"
Filterable="true"
For="() => bookingForm.Provider" />
<NvFielddate
@bind-Value="bookingForm.Date"
Label="Appointment Date"
For="() => bookingForm.Date" />
<button type="submit">Book Appointment</button>
</EditForm>
@code {
private BookingForm bookingForm = new();
public class BookingForm
{
[Required]
public string? Service { get; set; }
[Required]
public string? Provider { get; set; }
[Required]
public string? Date { get; set; }
}
private NvFielddropdownOptions[] serviceOptions = new[]
{
new NvFielddropdownOptions { Label = "Hair Cut", Value = "haircut" },
new NvFielddropdownOptions { Label = "Hair Color", Value = "color" },
new NvFielddropdownOptions { Label = "Manicure", Value = "manicure" },
new NvFielddropdownOptions { Label = "Massage", Value = "massage" }
};
private NvFielddropdownOptions[] GetAvailableProviders()
{
if (string.IsNullOrEmpty(bookingForm.Service))
{
return Array.Empty<NvFielddropdownOptions>();
}
// Return providers based on selected service
return bookingForm.Service switch
{
"haircut" => new[]
{
new NvFielddropdownOptions { Label = "Sarah Johnson", Value = "sarah" },
new NvFielddropdownOptions { Label = "Mike Chen", Value = "mike" }
},
"color" => new[]
{
new NvFielddropdownOptions { Label = "Emma Davis", Value = "emma" },
new NvFielddropdownOptions { Label = "Lisa Wong", Value = "lisa" }
},
"manicure" => new[]
{
new NvFielddropdownOptions { Label = "Anna Smith", Value = "anna" }
},
"massage" => new[]
{
new NvFielddropdownOptions { Label = "David Brown", Value = "david" },
new NvFielddropdownOptions { Label = "Maria Garcia", Value = "maria" }
},
_ => Array.Empty<NvFielddropdownOptions>()
};
}
}
Advanced Scenarios
Dynamic Options Loading
<NvFielddropdown
@bind-Value="selectedCity"
Label="City"
Options="cityOptions"
Filterable="true"
ControlledFilter="true"
FilterTextChangedEvent="OnCitySearch"
Placeholder="Type to search cities..." />
@if (loadingCities)
{
<div class="loading">Loading cities...</div>
}
@code {
private string selectedCity = "";
private bool loadingCities = false;
private NvFielddropdownOptions[] cityOptions = Array.Empty<NvFielddropdownOptions>();
private async void OnCitySearch(string searchText)
{
if (string.IsNullOrEmpty(searchText) || searchText.Length < 2)
{
cityOptions = Array.Empty<NvFielddropdownOptions>();
return;
}
loadingCities = true;
StateHasChanged();
// Simulate API call
await Task.Delay(300);
// Mock city search results
cityOptions = new[]
{
new NvFielddropdownOptions { Label = $"New {searchText}", Value = $"new-{searchText.ToLower()}" },
new NvFielddropdownOptions { Label = $"{searchText} City", Value = $"{searchText.ToLower()}-city" },
new NvFielddropdownOptions { Label = $"Old {searchText}", Value = $"old-{searchText.ToLower()}" },
new NvFielddropdownOptions { Label = $"{searchText}town", Value = $"{searchText.ToLower()}town" }
};
loadingCities = false;
StateHasChanged();
}
}
Cascading Dropdowns
<NvFielddropdown
@bind-Value="selectedCountry"
Label="Country"
Options="countryOptions"
DropdownItemSelectedEvent="OnCountrySelected" />
@if (!string.IsNullOrEmpty(selectedCountry))
{
<NvFielddropdown
@bind-Value="selectedState"
Label="State/Province"
Options="stateOptions"
Filterable="true"
Placeholder="Select state/province..." />
}
@if (!string.IsNullOrEmpty(selectedState))
{
<NvFielddropdown
@bind-Value="selectedCity"
Label="City"
Options="cityOptions"
Filterable="true"
Placeholder="Select city..." />
}
@code {
private string selectedCountry = "";
private string selectedState = "";
private string selectedCity = "";
private NvFielddropdownOptions[] stateOptions = Array.Empty<NvFielddropdownOptions>();
private NvFielddropdownOptions[] cityOptions = Array.Empty<NvFielddropdownOptions>();
private NvFielddropdownOptions[] countryOptions = new[]
{
new NvFielddropdownOptions { Label = "United States", Value = "us" },
new NvFielddropdownOptions { Label = "Canada", Value = "ca" },
new NvFielddropdownOptions { Label = "United Kingdom", Value = "uk" }
};
private void OnCountrySelected(NvFielddropdownDropdownItemSelectedEvent selection)
{
selectedCountry = selection.Value;
selectedState = "";
selectedCity = "";
// Load states/provinces based on country
stateOptions = selectedCountry switch
{
"us" => new[]
{
new NvFielddropdownOptions { Label = "California", Value = "ca" },
new NvFielddropdownOptions { Label = "New York", Value = "ny" },
new NvFielddropdownOptions { Label = "Texas", Value = "tx" }
},
"ca" => new[]
{
new NvFielddropdownOptions { Label = "Ontario", Value = "on" },
new NvFielddropdownOptions { Label = "British Columbia", Value = "bc" },
new NvFielddropdownOptions { Label = "Quebec", Value = "qc" }
},
"uk" => new[]
{
new NvFielddropdownOptions { Label = "England", Value = "eng" },
new NvFielddropdownOptions { Label = "Scotland", Value = "sco" },
new NvFielddropdownOptions { Label = "Wales", Value = "wal" }
},
_ => Array.Empty<NvFielddropdownOptions>()
};
}
}
Search and Autocomplete
<NvFielddropdown
@bind-Value="selectedContact"
Label="Contact Search"
Options="contactOptions"
Filterable="true"
ControlledFilter="true"
FilterTextChangedEvent="OnContactSearch"
Placeholder="Search contacts by name or email..."
EmptyResult="No contacts found" />
@if (!string.IsNullOrEmpty(selectedContact))
{
<div class="contact-details">
<h4>Contact Selected</h4>
<p>@GetContactDetails(selectedContact)</p>
</div>
}
@code {
private string selectedContact = "";
private NvFielddropdownOptions[] contactOptions = Array.Empty<NvFielddropdownOptions>();
private async void OnContactSearch(string searchText)
{
if (string.IsNullOrEmpty(searchText))
{
contactOptions = Array.Empty<NvFielddropdownOptions>();
return;
}
// Simulate search delay
await Task.Delay(200);
// Mock contact search
contactOptions = new[]
{
new NvFielddropdownOptions { Label = $"{searchText} Johnson - johnson@example.com", Value = "johnson" },
new NvFielddropdownOptions { Label = $"{searchText} Smith - smith@example.com", Value = "smith" },
new NvFielddropdownOptions { Label = $"{searchText} Brown - brown@example.com", Value = "brown" }
};
StateHasChanged();
}
private string GetContactDetails(string contactId)
{
// Mock contact details lookup
return contactId switch
{
"johnson" => "John Johnson - Senior Developer - johnson@example.com",
"smith" => "Jane Smith - Product Manager - smith@example.com",
"brown" => "Bob Brown - Designer - brown@example.com",
_ => "Contact details not available"
};
}
}
Settings Panel with Dropdowns
<EditForm Model="userSettings" OnValidSubmit="SaveSettings">
<DataAnnotationsValidator />
<fieldset>
<legend>Personal Information</legend>
<NvFielddropdown
@bind-Value="userSettings.Language"
Label="Language"
Options="languageOptions"
For="() => userSettings.Language" />
<NvFielddropdown
@bind-Value="userSettings.Timezone"
Label="Timezone"
Options="timezoneOptions"
Filterable="true"
For="() => userSettings.Timezone" />
</fieldset>
<fieldset>
<legend>Notifications</legend>
<NvFielddropdown
@bind-Value="userSettings.EmailFrequency"
Label="Email Frequency"
Options="frequencyOptions"
For="() => userSettings.EmailFrequency" />
<NvFielddropdown
@bind-Value="userSettings.Theme"
Label="Theme"
Options="themeOptions"
For="() => userSettings.Theme" />
</fieldset>
<button type="submit">Save Settings</button>
</EditForm>
@code {
private UserSettings userSettings = new()
{
Language = "en",
Timezone = "utc",
EmailFrequency = "daily",
Theme = "light"
};
public class UserSettings
{
[Required]
public string? Language { get; set; }
[Required]
public string? Timezone { get; set; }
[Required]
public string? EmailFrequency { get; set; }
[Required]
public string? Theme { get; set; }
}
}
Best Practices
- Use filtering for large option lists (>10 items) to improve usability
- Set appropriate debounce delays based on data size and API response times
- Provide clear placeholder text to guide users on what they can search for
- Use controlled filtering for server-side search or complex filtering logic
- Handle loading states when fetching options dynamically
- Consider keyboard navigation and accessibility requirements
- Provide meaningful empty states when no results are found
- Validate selections based on business requirements
- Use cascading dropdowns for hierarchical data selection
- Test with real data to ensure performance with large option lists
Common Patterns
Country-City Selection
<EditForm Model="addressForm" OnValidSubmit="SaveAddress">
<DataAnnotationsValidator />
<NvFielddropdown
@bind-Value="addressForm.Country"
Label="Country"
Options="countryOptions"
DropdownItemSelectedEvent="OnCountryChanged"
For="() => addressForm.Country" />
<NvFielddropdown
@bind-Value="addressForm.City"
Label="City"
Options="cityOptions"
Filterable="true"
Placeholder="Select a city..."
For="() => addressForm.City" />
<NvFieldtext
@bind-Value="addressForm.Street"
Label="Street Address"
For="() => addressForm.Street" />
<button type="submit">Save Address</button>
</EditForm>
@code {
private AddressForm addressForm = new();
private NvFielddropdownOptions[] cityOptions = Array.Empty<NvFielddropdownOptions>();
public class AddressForm
{
[Required]
public string? Country { get; set; }
[Required]
public string? City { get; set; }
[Required]
public string? Street { get; set; }
}
private NvFielddropdownOptions[] countryOptions = new[]
{
new NvFielddropdownOptions { Label = "United States", Value = "us" },
new NvFielddropdownOptions { Label = "Canada", Value = "ca" },
new NvFielddropdownOptions { Label = "Mexico", Value = "mx" }
};
private void OnCountryChanged(NvFielddropdownDropdownItemSelectedEvent selection)
{
addressForm.Country = selection.Value;
addressForm.City = ""; // Reset city selection
// Load cities based on country
cityOptions = selection.Value switch
{
"us" => new[]
{
new NvFielddropdownOptions { Label = "New York", Value = "nyc" },
new NvFielddropdownOptions { Label = "Los Angeles", Value = "la" },
new NvFielddropdownOptions { Label = "Chicago", Value = "chicago" }
},
"ca" => new[]
{
new NvFielddropdownOptions { Label = "Toronto", Value = "toronto" },
new NvFielddropdownOptions { Label = "Vancouver", Value = "vancouver" },
new NvFielddropdownOptions { Label = "Montreal", Value = "montreal" }
},
"mx" => new[]
{
new NvFielddropdownOptions { Label = "Mexico City", Value = "mexico-city" },
new NvFielddropdownOptions { Label = "Guadalajara", Value = "guadalajara" },
new NvFielddropdownOptions { Label = "Monterrey", Value = "monterrey" }
},
_ => Array.Empty<NvFielddropdownOptions>()
};
}
}
Product Search and Selection
<NvFielddropdown
@bind-Value="selectedProduct"
Label="Product Search"
Options="productOptions"
Filterable="true"
ControlledFilter="true"
FilterTextChangedEvent="OnProductSearch"
Placeholder="Search products by name, SKU, or category..."
EmptyResult="No products found matching your search" />
@if (!string.IsNullOrEmpty(selectedProduct))
{
<div class="product-summary">
<h4>Product Selected</h4>
<p>@GetProductInfo(selectedProduct)</p>
<button @onclick="AddToCart">Add to Cart</button>
</div>
}
@code {
private string selectedProduct = "";
private NvFielddropdownOptions[] productOptions = Array.Empty<NvFielddropdownOptions>();
private async void OnProductSearch(string searchText)
{
if (string.IsNullOrEmpty(searchText))
{
productOptions = Array.Empty<NvFielddropdownOptions>();
return;
}
// Simulate product search API call
await Task.Delay(300);
// Mock product search results
productOptions = new[]
{
new NvFielddropdownOptions { Label = $"{searchText} Wireless Headphones - Electronics", Value = "wh-001" },
new NvFielddropdownOptions { Label = $"{searchText} Bluetooth Speaker - Audio", Value = "bs-002" },
new NvFielddropdownOptions { Label = $"{searchText} Laptop Stand - Accessories", Value = "ls-003" },
new NvFielddropdownOptions { Label = $"{searchText} USB Cable - Cables", Value = "uc-004" }
};
StateHasChanged();
}
private string GetProductInfo(string productId)
{
return productId switch
{
"wh-001" => "Wireless Headphones - $99.99 - In Stock",
"bs-002" => "Bluetooth Speaker - $49.99 - In Stock",
"ls-003" => "Laptop Stand - $29.99 - Limited Stock",
"uc-004" => "USB Cable - $9.99 - In Stock",
_ => "Product information not available"
};
}
private void AddToCart()
{
// Add to cart logic
Console.WriteLine($"Added {selectedProduct} to cart");
}
}
This guide covers the most common use cases and patterns for the NvFielddropdown component in Blazor applications. For more advanced scenarios or specific requirements, refer to the component's API documentation or the Storybook examples.