NvFieldslider Component - Practical Guide for Blazor Developers
The NvFieldslider component is a Blazor wrapper around Nova's range slider component, providing interactive sliders for numeric value selection with both single-value and range modes. It supports custom ticks, labels, and numeric input fields.
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 slider-specific features and patterns.
Basic Usage
Simple Single-Value Slider
<NvFieldslider Label="Volume" Min="0" Max="100" Step="1" />
Range Slider
<NvFieldslider
Label="Price Range"
Range="true"
Min="0"
Max="1000"
Step="10"
LabelBeforeValue="$" />
Two-Way Data Binding
The component supports Blazor's @bind-Value syntax for seamless two-way data binding with double arrays:
Single Value Slider
@code {
private double[] volume = new[] { 50.0 };
}
<NvFieldslider
@bind-Value="volume"
Label="Volume Level"
Min="0"
Max="100"
Step="1"
LabelAfterValue="%" />
@if (volume.Length > 0)
{
<p>Current volume: @volume[0]%</p>
@if (volume[0] > 80)
{
<p class="warning">⚠️ High volume may damage speakers</p>
}
}
Range Slider with Binding
@code {
private double[] priceRange = new[] { 100.0, 500.0 };
}
<NvFieldslider
@bind-Value="priceRange"
Label="Price Range"
Range="true"
Min="0"
Max="1000"
Step="10"
LabelBeforeValue="$" />
@if (priceRange.Length >= 2)
{
<div class="range-display">
<p>Selected range: $@priceRange[0] - $@priceRange[1]</p>
<p>Range span: $@(priceRange[1] - priceRange[0])</p>
</div>
}
Event Handling
Using ValueChanged Event
<NvFieldslider
@bind-Value="brightness"
Label="Screen Brightness"
Min="0"
Max="100"
Step="5"
ValueChanged="OnBrightnessChanged"
LabelAfterValue="%" />
@if (brightness.Length > 0)
{
<div class="brightness-info">
<p>Brightness: @brightness[0]%</p>
<div class="brightness-bar" style="width: @brightness[0]%"></div>
</div>
}
@code {
private double[] brightness = new[] { 75.0 };
private void OnBrightnessChanged(double[] value)
{
if (value.Length > 0)
{
// Could adjust actual screen brightness, save to settings, etc.
Console.WriteLine($"Brightness changed to: {value[0]}%");
}
}
}
Range Slider Events
@code {
private double[] temperatureRange = new[] { 20.0, 25.0 };
private void OnTemperatureRangeChanged(double[] value)
{
if (value.Length >= 2)
{
temperatureRange = value;
// Update thermostat settings, validate comfort zone, etc.
ValidateTemperatureRange(value[0], value[1]);
}
}
private void ValidateTemperatureRange(double min, double max)
{
if (max - min > 10)
{
// Large temperature range detected
}
}
}
<NvFieldslider
@bind-Value="temperatureRange"
Label="Comfort Temperature Range"
Range="true"
Min="15"
Max="30"
Step="0.5"
ValueChanged="OnTemperatureRangeChanged"
LabelAfterValue="°C" />
@if (temperatureRange.Length >= 2)
{
<div class="temperature-info">
<p>Target range: @temperatureRange[0]°C - @temperatureRange[1]°C</p>
<p>Average: @((temperatureRange[0] + temperatureRange[1]) / 2)°C</p>
</div>
}
Slider with Numeric Input
Slider with Input Field
<NvFieldslider
@bind-Value="fontSize"
Label="Font Size"
Min="8"
Max="72"
Step="1"
HasField="true"
LabelAfterValue="px"
Ticks="GetFontSizeTicks()" />
@code {
private double[] fontSize = new[] { 16.0 };
private NvFieldsliderTicks[] GetFontSizeTicks()
{
return new[]
{
new NvFieldsliderTicks { Value = 8, Label = "XS" },
new NvFieldsliderTicks { Value = 12, Label = "S" },
new NvFieldsliderTicks { Value = 16, Label = "M" },
new NvFieldsliderTicks { Value = 24, Label = "L" },
new NvFieldsliderTicks { Value = 32, Label = "XL" },
new NvFieldsliderTicks { Value = 48, Label = "XXL" },
new NvFieldsliderTicks { Value = 72, Label = "XXXL" }
};
}
}
Range Slider with Inputs
<NvFieldslider
@bind-Value="ageRange"
Label="Age Range"
Range="true"
Min="0"
Max="100"
Step="1"
HasField="true"
LabelAfterValue=" years"
ShowTicks="true" />
@code {
private double[] ageRange = new[] { 25.0, 65.0 };
}
Custom Ticks and Snapping
Custom Ticks
<NvFieldslider
@bind-Value="satisfaction"
Label="Customer Satisfaction"
Min="1"
Max="5"
Step="1"
Ticks="GetSatisfactionTicks()"
SnapTicks="true"
HasField="true" />
@code {
private double[] satisfaction = new[] { 4.0 };
private NvFieldsliderTicks[] GetSatisfactionTicks()
{
return new[]
{
new NvFieldsliderTicks { Value = 1, Label = "Very Poor" },
new NvFieldsliderTicks { Value = 2, Label = "Poor" },
new NvFieldsliderTicks { Value = 3, Label = "Average" },
new NvFieldsliderTicks { Value = 4, Label = "Good" },
new NvFieldsliderTicks { Value = 5, Label = "Excellent" }
};
}
}
Visual Snapping
<NvFieldslider
@bind-Value="quality"
Label="Quality Level"
Min="0"
Max="100"
Step="10"
Snap="true"
ShowTicks="true"
HasField="true"
LabelAfterValue="%" />
@code {
private double[] quality = new[] { 80.0 };
}
Form Integration
The component integrates seamlessly with Blazor's EditForm for validation and form submission:
Basic Form Integration
<EditForm Model="audioSettings" OnValidSubmit="SaveAudioSettings">
<DataAnnotationsValidator />
<NvFieldslider
@bind-Value="audioSettings.Volume"
Label="Master Volume"
Min="0"
Max="100"
Step="1"
HasField="true"
LabelAfterValue="%"
For="() => audioSettings.Volume" />
<NvFieldslider
@bind-Value="audioSettings.Bass"
Label="Bass Level"
Min="-10"
Max="10"
Step="1"
HasField="true"
For="() => audioSettings.Bass" />
<NvFieldslider
@bind-Value="audioSettings.Treble"
Label="Treble Level"
Min="-10"
Max="10"
Step="1"
HasField="true"
For="() => audioSettings.Treble" />
<button type="submit">Save Audio Settings</button>
</EditForm>
@code {
private AudioSettings audioSettings = new() { Volume = new[] { 75.0 }, Bass = new[] { 2.0 }, Treble = new[] { -1.0 } };
private void SaveAudioSettings()
{
// Process audio settings
Console.WriteLine("Audio settings saved!");
}
public class AudioSettings
{
[Required]
public double[]? Volume { get; set; }
[Required]
public double[]? Bass { get; set; }
[Required]
public double[]? Treble { get; set; }
}
}
Validation
Required Slider Validation
<EditForm Model="surveyResponse" OnValidSubmit="SubmitSurvey">
<DataAnnotationsValidator />
<NvFieldslider
@bind-Value="surveyResponse.Satisfaction"
Label="How satisfied are you?"
Min="1"
Max="5"
Step="1"
Required="true"
Ticks="GetRatingTicks()"
For="() => surveyResponse.Satisfaction" />
<NvFieldslider
@bind-Value="surveyResponse.Likelihood"
Label="How likely are you to recommend us?"
Min="0"
Max="10"
Step="1"
Required="true"
HasField="true"
For="() => surveyResponse.Likelihood" />
<button type="submit">Submit Survey</button>
</EditForm>
@code {
private SurveyResponse surveyResponse = new();
public class SurveyResponse
{
[Required(ErrorMessage = "Please rate your satisfaction")]
public double[]? Satisfaction { get; set; }
[Required(ErrorMessage = "Please indicate recommendation likelihood")]
[Range(0, 10, ErrorMessage = "Recommendation score must be between 0 and 10")]
public double[]? Likelihood { get; set; }
}
private NvFieldsliderTicks[] GetRatingTicks()
{
return new[]
{
new NvFieldsliderTicks { Value = 1, Label = "Poor" },
new NvFieldsliderTicks { Value = 2, Label = "Fair" },
new NvFieldsliderTicks { Value = 3, Label = "Good" },
new NvFieldsliderTicks { Value = 4, Label = "Very Good" },
new NvFieldsliderTicks { Value = 5, Label = "Excellent" }
};
}
}
Range Validation
<EditForm Model="budgetSettings" OnValidSubmit="SaveBudget">
<DataAnnotationsValidator />
<NvFieldslider
@bind-Value="budgetSettings.MonthlyBudget"
Label="Monthly Budget"
Min="100"
Max="10000"
Step="50"
HasField="true"
LabelBeforeValue="$"
For="() => budgetSettings.MonthlyBudget" />
<NvFieldslider
@bind-Value="budgetSettings.SavingsTarget"
Label="Savings Target Range"
Range="true"
Min="0"
Max="50"
Step="1"
HasField="true"
LabelAfterValue="%"
For="() => budgetSettings.SavingsTarget" />
<button type="submit">Save Budget Settings</button>
</EditForm>
@code {
private BudgetSettings budgetSettings = new()
{
MonthlyBudget = new[] { 2500.0 },
SavingsTarget = new[] { 15.0, 25.0 }
};
public class BudgetSettings
{
[Required]
[Range(100, 10000, ErrorMessage = "Monthly budget must be between $100 and $10,000")]
public double[]? MonthlyBudget { get; set; }
[Required]
[CustomSavingsRange]
public double[]? SavingsTarget { get; set; }
}
public class CustomSavingsRangeAttribute : ValidationAttribute
{
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
var savings = value as double[];
if (savings == null || savings.Length < 2)
return new ValidationResult("Savings target range is required");
if (savings[1] <= savings[0])
return new ValidationResult("Maximum savings target must be greater than minimum");
if (savings[0] < 0 || savings[1] > 50)
return new ValidationResult("Savings targets must be between 0% and 50%");
return ValidationResult.Success;
}
}
}
Advanced Scenarios
Dynamic Price Calculator
<NvFieldslider
@bind-Value="loanAmount"
Label="Loan Amount"
Min="1000"
Max="50000"
Step="500"
HasField="true"
LabelBeforeValue="$"
ValueChanged="CalculateLoan" />
<NvFieldslider
@bind-Value="interestRate"
Label="Interest Rate"
Min="1"
Max="15"
Step="0.25"
HasField="true"
LabelAfterValue="%"
ValueChanged="CalculateLoan" />
<NvFieldslider
@bind-Value="loanTerm"
Label="Loan Term"
Min="1"
Max="30"
Step="1"
HasField="true"
LabelAfterValue=" years"
Ticks="GetLoanTermTicks()"
ValueChanged="CalculateLoan" />
@if (loanAmount.Length > 0 && interestRate.Length > 0 && loanTerm.Length > 0)
{
<div class="loan-summary">
<h4>Loan Summary</h4>
<p>Principal: $@loanAmount[0]</p>
<p>Interest Rate: @interestRate[0]%</p>
<p>Term: @loanTerm[0] years</p>
<p>Monthly Payment: $@monthlyPayment</p>
<p>Total Interest: $@totalInterest</p>
<p>Total Amount: $@totalAmount</p>
</div>
}
@code {
private double[] loanAmount = new[] { 25000.0 };
private double[] interestRate = new[] { 5.5 };
private double[] loanTerm = new[] { 15.0 };
private decimal monthlyPayment = 0;
private decimal totalInterest = 0;
private decimal totalAmount = 0;
private void CalculateLoan(double[] value)
{
if (loanAmount.Length > 0 && interestRate.Length > 0 && loanTerm.Length > 0)
{
var principal = (decimal)loanAmount[0];
var rate = (decimal)interestRate[0] / 100 / 12; // Monthly rate
var term = (int)loanTerm[0] * 12; // Total months
if (rate == 0)
{
monthlyPayment = principal / term;
}
else
{
monthlyPayment = principal * (rate * (decimal)Math.Pow(1 + (double)rate, term)) /
((decimal)Math.Pow(1 + (double)rate, term) - 1);
}
totalAmount = monthlyPayment * term;
totalInterest = totalAmount - principal;
}
}
private NvFieldsliderTicks[] GetLoanTermTicks()
{
return new[]
{
new NvFieldsliderTicks { Value = 5, Label = "5 yrs" },
new NvFieldsliderTicks { Value = 10, Label = "10 yrs" },
new NvFieldsliderTicks { Value = 15, Label = "15 yrs" },
new NvFieldsliderTicks { Value = 20, Label = "20 yrs" },
new NvFieldsliderTicks { Value = 25, Label = "25 yrs" },
new NvFieldsliderTicks { Value = 30, Label = "30 yrs" }
};
}
}
Survey with Weighted Questions
<EditForm Model="surveyResponse" OnValidSubmit="SubmitWeightedSurvey">
<DataAnnotationsValidator />
<div class="survey-question">
<NvFieldslider
@bind-Value="surveyResponse.QualityRating"
Label="How would you rate our product quality?"
Min="1"
Max="10"
Step="1"
HasField="true"
For="() => surveyResponse.QualityRating" />
</div>
<div class="survey-question">
<NvFieldslider
@bind-Value="surveyResponse.ValueRating"
Label="How would you rate the value for money?"
Min="1"
Max="10"
Step="1"
HasField="true"
For="() => surveyResponse.ValueRating" />
</div>
<div class="survey-question">
<NvFieldslider
@bind-Value="surveyResponse.SupportRating"
Label="How would you rate our customer support?"
Min="1"
Max="10"
Step="1"
HasField="true"
For="() => surveyResponse.SupportRating" />
</div>
<div class="survey-summary">
<h4>Survey Summary</h4>
<p>Average Rating: @CalculateAverageRating()/10</p>
<p>Overall Satisfaction: @GetSatisfactionLevel()</p>
</div>
<button type="submit">Submit Survey</button>
</EditForm>
@code {
private SurveyResponse surveyResponse = new();
public class SurveyResponse
{
[Required]
[Range(1, 10)]
public double[]? QualityRating { get; set; }
[Required]
[Range(1, 10)]
public double[]? ValueRating { get; set; }
[Required]
[Range(1, 10)]
public double[]? SupportRating { get; set; }
}
private double CalculateAverageRating()
{
if (surveyResponse.QualityRating == null ||
surveyResponse.ValueRating == null ||
surveyResponse.SupportRating == null)
return 0;
var total = surveyResponse.QualityRating[0] +
surveyResponse.ValueRating[0] +
surveyResponse.SupportRating[0];
return Math.Round(total / 3, 1);
}
private string GetSatisfactionLevel()
{
var average = CalculateAverageRating();
return average switch
{
>= 9 => "Excellent",
>= 7 => "Good",
>= 5 => "Average",
>= 3 => "Poor",
_ => "Very Poor"
};
}
}
Settings Panel with Multiple Sliders
<EditForm Model="displaySettings" OnValidSubmit="ApplyDisplaySettings">
<DataAnnotationsValidator />
<fieldset>
<legend>Display Settings</legend>
<NvFieldslider
@bind-Value="displaySettings.Brightness"
Label="Screen Brightness"
Min="0"
Max="100"
Step="5"
HasField="true"
LabelAfterValue="%"
For="() => displaySettings.Brightness" />
<NvFieldslider
@bind-Value="displaySettings.Contrast"
Label="Contrast"
Min="0"
Max="100"
Step="5"
HasField="true"
LabelAfterValue="%"
For="() => displaySettings.Contrast" />
<NvFieldslider
@bind-Value="displaySettings.ColorTemperature"
Label="Color Temperature"
Min="2700"
Max="6500"
Step="100"
HasField="true"
LabelAfterValue="K"
Ticks="GetColorTemperatureTicks()"
For="() => displaySettings.ColorTemperature" />
</fieldset>
<fieldset>
<legend>Audio Settings</legend>
<NvFieldslider
@bind-Value="displaySettings.Volume"
Label="Master Volume"
Min="0"
Max="100"
Step="1"
HasField="true"
LabelAfterValue="%"
For="() => displaySettings.Volume" />
<NvFieldslider
@bind-Value="displaySettings.AudioBalance"
Label="Audio Balance"
Min="-50"
Max="50"
Step="5"
HasField="true"
For="() => displaySettings.AudioBalance" />
</fieldset>
<button type="submit">Apply Settings</button>
</EditForm>
@code {
private DisplaySettings displaySettings = new()
{
Brightness = new[] { 80.0 },
Contrast = new[] { 70.0 },
ColorTemperature = new[] { 5000.0 },
Volume = new[] { 65.0 },
AudioBalance = new[] { 0.0 }
};
public class DisplaySettings
{
[Required]
public double[]? Brightness { get; set; }
[Required]
public double[]? Contrast { get; set; }
[Required]
public double[]? ColorTemperature { get; set; }
[Required]
public double[]? Volume { get; set; }
[Required]
public double[]? AudioBalance { get; set; }
}
private NvFieldsliderTicks[] GetColorTemperatureTicks()
{
return new[]
{
new NvFieldsliderTicks { Value = 2700, Label = "Warm" },
new NvFieldsliderTicks { Value = 4000, Label = "Neutral" },
new NvFieldsliderTicks { Value = 5000, Label = "Cool" },
new NvFieldsliderTicks { Value = 6500, Label = "Cold" }
};
}
}
Best Practices
- Choose appropriate ranges that make sense for the data (0-100 for percentages, meaningful min/max for other values)
- Use step sizes that provide good granularity without being too small
- Enable HasField for precise value entry when needed
- Add custom ticks for non-linear scales or important reference points
- Use labels (before/after value) to clarify units and context
- Consider range mode when users need to select a span or range of values
- Provide visual feedback for value changes and their implications
- Test accessibility with keyboard navigation and screen readers
- Use validation to enforce business rules and constraints
- Consider mobile users - sliders should be touch-friendly with appropriate sizing
Common Patterns
Volume Control
<NvFieldslider
@bind-Value="volume"
Label="Volume"
Min="0"
Max="100"
Step="1"
HasField="true"
LabelAfterValue="%"
Ticks="GetVolumeTicks()" />
@code {
private double[] volume = new[] { 50.0 };
private NvFieldsliderTicks[] GetVolumeTicks()
{
return new[]
{
new NvFieldsliderTicks { Value = 0, Label = "Mute" },
new NvFieldsliderTicks { Value = 25, Label = "Low" },
new NvFieldsliderTicks { Value = 50, Label = "Medium" },
new NvFieldsliderTicks { Value = 75, Label = "High" },
new NvFieldsliderTicks { Value = 100, Label = "Max" }
};
}
}
Temperature Control
<NvFieldslider
@bind-Value="temperature"
Label="Room Temperature"
Min="15"
Max="30"
Step="0.5"
HasField="true"
LabelAfterValue="°C"
Ticks="GetTemperatureTicks()" />
@code {
private double[] temperature = new[] { 22.0 };
private NvFieldsliderTicks[] GetTemperatureTicks()
{
return new[]
{
new NvFieldsliderTicks { Value = 15, Label = "Cool" },
new NvFieldsliderTicks { Value = 20, Label = "Comfortable" },
new NvFieldsliderTicks { Value = 22, Label = "Optimal" },
new NvFieldsliderTicks { Value = 25, Label = "Warm" },
new NvFieldsliderTicks { Value = 30, Label = "Hot" }
};
}
}
File Size Filter
<NvFieldslider
@bind-Value="fileSizeRange"
Label="File Size Range"
Range="true"
Min="0"
Max="100"
Step="1"
HasField="true"
LabelAfterValue=" MB"
Ticks="GetFileSizeTicks()" />
@code {
private double[] fileSizeRange = new[] { 1.0, 50.0 };
private NvFieldsliderTicks[] GetFileSizeTicks()
{
return new[]
{
new NvFieldsliderTicks { Value = 0, Label = "0MB" },
new NvFieldsliderTicks { Value = 10, Label = "10MB" },
new NvFieldsliderTicks { Value = 25, Label = "25MB" },
new NvFieldsliderTicks { Value = 50, Label = "50MB" },
new NvFieldsliderTicks { Value = 100, Label = "100MB+" }
};
}
}
This guide covers the most common use cases and patterns for the NvFieldslider component in Blazor applications. For more advanced scenarios or specific requirements, refer to the component's API documentation or the Storybook examples.