true-perfect-code
Version: 1.1.69

P11Range Component

The P11Range component provides an accessible and customizable range slider, wrapping the native HTML <input type="range"> element. It allows users to select a value within a specified numeric range, with options for visual feedback and integration with Blazor's validation system.
Note: The visual styling of native HTML range inputs can vary across browsers and operating systems. This component enhances functionality and accessibility, particularly with the AriaValueTextFormatter for screen reader support.


P11Range Component Examples

These examples demonstrate various configurations and functionalities of the P11Range component, showcasing its flexibility for different use cases.

1. Basic Range Slider (int)

A simple slider with default settings (Min=0, Max=100, Step=1), without explicit value display.

50
Adjust the volume (0-100).

Current Value: 50

Implementation

<h4 class="mb-3">1. Basic Range Slider (int)</h4>
<p>A simple slider with default settings (Min=0, Max=100, Step=1), without explicit value display.</p>
<P11Range TValue="int"
          Label="Volume"
          Step="1"
          Description="Adjust the volume (0-100)."
          ShowDevelopmentErrors="false"
          @bind-Value="Volume" />
<p>Current Value: @Volume</p>
@* You may need the using *@
@* @using System.Globalization *@
@code {
    // Properties for each example
    private int Volume { get; set; } = 50;
}


2. Range Slider with Custom Min/Max/Step and Value Display (int)

Slider from 1 to 10 with steps of 1. Since TValue is an integer, only whole steps are supported.

3

Current Value: 3

Implementation

<h4 class="mb-3">2. Range Slider with Custom Min/Max/Step and Value Display (int)</h4>
<p>Slider from 1 to 10 with steps of 1. Since TValue is an integer, only whole steps are supported.</p>
<P11Range TValue="int"
          Label="Number of People"
          Min="1"
          Max="10"
          Step="1"
          ShowValue="true"
          @bind-Value="NumberOfPeople" />
<p>Current Value: @NumberOfPeople</p>
@* You may need the using *@
@* @using System.Globalization *@
@code {
    private int NumberOfPeople { get; set; } = 3;
}


3. Range Slider with Decimal Values and Value Display (double)

Slider for floating-point numbers (0.0 to 1.0 with 0.01 steps).

0.50

Current Value: 0.50



Error Demonstration: This slider is intentionally configured to trigger an error because 'TValue' is set to 'uInt32', but supported types are: int, long, double, float, decimal, byte, short.

4

Current Value (String): 4

Implementation

<h4 class="mb-3">3. Range Slider with Decimal Values and Value Display (double)</h4>
<p>Slider for floating-point numbers (0.0 to 1.0 with 0.01 steps).</p>
<P11Range TValue="double"
          Label="Zoom Factor"
          Min="0.0"
          Max="1.0"
          Step="0.01"
          ShowValue="true"
          @bind-Value="ZoomFactor" />
<p>Current Value: @ZoomFactor.ToString("F2")</p>

<br />
<br />

<p>
    Error Demonstration: This slider is intentionally configured to trigger an error because 'TValue' is set to 'uInt32', 
    but supported types are: int, long, double, float, decimal, byte, short.
</p>
<P11Range TValue="uint"
          Label="Error Slider"
          ShowValue="true"
          Step="1"
          @bind-Value="valueForError"
          ShowDevelopmentErrors="true" /> @* Ensure ShowDevelopmentErrors is true *@
<p>Current Value (String): @valueForError.ToString()</p>
@* You may need the using *@
@* @using System.Globalization *@
@code {
    private double ZoomFactor { get; set; } = 0.5;
    private uint valueForError { get; set; } = 4;
}


4. Disabled Range Slider

A disabled slider.

25

Current Value: 25

Implementation

<h4 class="mb-3">4. Disabled Range Slider</h4>
<p>A disabled slider.</p>
<P11Range TValue="int"
          Label="Disabled Slider"
          Step="1"
          @bind-Value="DisabledValue"
          Disabled="true"
          ShowValue="true" />
<p>Current Value: @DisabledValue</p>
@* You may need the using *@
@* @using System.Globalization *@
@code {
    private int DisabledValue { get; set; } = 25;
}


5. Range Slider with Additional Attributes / CSS

A slider with custom attributes (e.g., data-test) and inline style. Inspect it in the browser's developer tools.

10

Current Value: 10



An example of how to customize the range button via CSS using different vendor prefixes.

50
Adjust the volume (0-100).

Current Value: 50

Implementation

<h4 class="mb-3">5. Range Slider with Additional Attributes</h4>
<p>A slider with custom attributes (e.g., <code>data-test</code>) and inline style. Inspect it in the browser's developer tools.</p>
<P11Range TValue="int"
          Label="With Extra Attribute"
          Step="1"
          @bind-Value="ExtraAttributeValue"
          @attributes='new Dictionary<string, object> { { "data-test", "custom-data" }, { "style", "background-color: #e6ffe6;" } }' />
<p>Current Value: @ExtraAttributeValue</p>

<br />
<br />

<p>An example of how to customize the range button via CSS using different vendor prefixes.</p>
<P11Range TValue="int"
          Label="Volume"
          Step="1"
          CssClass="my-range-class"
          Description="Adjust the volume (0-100)."
          ShowDevelopmentErrors="false"
          @bind-Value="Volume" />
<p>Current Value: @Volume</p>

<style>
    /* Chrome, Edge, Safari, Opera */
    .my-range-class input[type="range"]::-webkit-slider-thumb {
        -webkit-appearance: none;
        appearance: none;
        height: 20px;
        width: 20px;
        border-radius: 50%;
        background: #ff4500;
        cursor: pointer;
        border: 2px solid #fff;
        box-shadow: 0 2px 4px rgba(0,0,0,0.3);
    }

    /* Firefox */
    .my-range-class input[type="range"]::-moz-range-thumb {
        height: 20px;
        width: 20px;
        border-radius: 50%;
        background: #ff4500;
        cursor: pointer;
        border: 2px solid #fff;
        box-shadow: 0 2px 4px rgba(0,0,0,0.3);
    } 
</style>
@* You may need the using *@
@* @using System.Globalization *@
@code {
    private int ExtraAttributeValue { get; set; } = 10;
    private int ValueCss { get; set; } = 50;
}


6. Range Slider with `AriaValueTextFormatter` (Decimal with Percentage)

This slider formats its value as a percentage for screen readers (aria-valuetext). Verify this with a screen reader or in the browser's inspector.

0.25

Current Value: 25.00%

Implementation

<h4 class="mb-3">6. Range Slider with `AriaValueTextFormatter` (Decimal with Percentage)</h4>
<p>This slider formats its value as a percentage for screen readers (<code>aria-valuetext</code>). Verify this with a screen reader or in the browser's inspector.</p>
<P11Range TValue="decimal"
          Label="Progress"
          Min="0.0m"
          Max="1.0m"
          Step="0.05m"
          ShowValue="true"
          @bind-Value="Progress"
          AriaValueTextFormatter="@((value) => value is decimal d ? d.ToString("P0", CultureInfo.CurrentCulture) : "0%")" />
<p>Current Value: @Progress.ToString("P2")</p>
@* You may need the using *@
@* @using System.Globalization *@
@code {
    private decimal Progress { get; set; } = 0.25m; // Example value for progress in percent
}


7. ValueChanged Event Example

This example demonstrates how to use the ValueChanged event to react to changes in the slider's value without using @bind-Value.

Selected Value (via ValueChanged): 50

Last ValueChanged Event Fired: Never

50

Implementation

<h4 class="mb-3">7. ValueChanged Event Example</h4>
<p>This example demonstrates how to use the <code>ValueChanged</code> event to react to changes in the slider's value without using <code>@@bind-Value</code>.</p>
<p>Selected Value (via ValueChanged): <strong>@(SelectedRangeValue != null ? SelectedRangeValue.Value.ToString() : "N/A")</strong></p>
<p>Last ValueChanged Event Fired: <strong>@(LastRangeValueChangedEvent ?? "Never")</strong></p>

<P11Range TValue="int?"
          Label="Select a value (0-100)"
          Min="0"
          Max="100"
          Step="5"
          Value="@SelectedRangeValue"
          ValueChanged="(int? val) => HandleRangeValueChanged(val)"
          ShowValue="true"
          CssClass="mb-3" />
@* You may need the using *@
@* @using System.Globalization *@
@* @using System.ComponentModel.DataAnnotations *@
@code {
    // Properties and method for ValueChanged Event Example (Example 9)
    private int? SelectedRangeValue { get; set; } = 50;
    private string? LastRangeValueChangedEvent { get; set; }

    private void HandleRangeValueChanged(int? newValue)
    {
        // When using ValueChanged directly (not @bind-Value), you need to manually update the property
        SelectedRangeValue = newValue;
        LastRangeValueChangedEvent = $"Value changed to: {newValue} at {DateTime.Now:HH:mm:ss}";
        Console.WriteLine($"Range value changed to: {newValue}");
    }
}


Component API

Parameter Type Default Description
Label string? null Gets or sets the label text displayed next to the input field.
Description string? null Gets or sets a descriptive text displayed below the input field.
Disabled bool false Gets or sets a value indicating whether the input field is disabled.
CssClass string? null Gets or sets custom CSS classes to be applied to the component's container div.
ShowValue bool true Gets or sets whether to display the current numeric value next to the slider.
ShowDevelopmentErrors bool true Gets or sets a value indicating whether development-time configuration errors should be displayed. Defaults to true. Set to false for production environments.
Min TValue 0 (or 0.0) Gets or sets the minimum value for the range slider. Must be a numeric type matching TValue.
Max TValue 100 (or 1.0) Gets or sets the maximum value for the range slider. Must be a numeric type matching TValue.
Step TValue? null (browser default) Gets or sets the step value for the range slider. If null, the browser default step is used (typically 1 for integers, 0.01 for floats). Must be a numeric type matching TValue.
Value TValue? null Gets or sets the current value of the range slider. Uses two-way binding.
ValidationFor Expression<Func<TValue>>? null Gets or sets an expression that identifies the field for validation.
AdditionalAttributes Dictionary<string, object>? null Captures all unmatched attributes that are not explicitly defined as component parameters. These attributes are applied to the component's container div.
AriaValueTextFormatter Func<TValue?, string>? null Gets or sets a function that formats the current value into a human-readable string for the aria-valuetext attribute. If not provided, aria-valuetext is not set. The function receives the current value and should return the formatted string.
Events
ValueChanged EventCallback<TValue?> - Event callback for when the Value changes. Used for two-way binding.
An unhandled error has occurred. Reload 🗙