2021-10-07 09:27:28 +03:00
using Microsoft.AspNetCore.Components ;
2024-02-19 10:31:27 +02:00
using Microsoft.AspNetCore.Components.Web ;
2025-05-07 13:11:30 +03:00
using Radzen.Blazor.Rendering ;
2021-10-07 09:27:28 +03:00
using System ;
using System.Threading.Tasks ;
namespace Radzen.Blazor
{
2021-10-07 11:35:29 +03:00
/// <summary>
2025-10-24 16:28:11 +03:00
/// A fieldset container component that groups related form fields with a legend/header and optional collapse functionality.
/// RadzenFieldset provides semantic form grouping with visual borders, useful for organizing complex forms into logical sections.
/// Fieldsets are HTML form elements that semantically group related inputs, improving form structure and accessibility.
2025-10-27 09:22:20 +02:00
/// Features visual and semantic grouping of related form fields, customizable header via Text or HeaderTemplate, optional expand/collapse to hide/show grouped fields,
/// optional icon in the legend, optional summary content shown when collapsed, and screen reader announcement of fieldset legends for grouped fields.
/// Use to organize forms into sections like "Personal Information", "Address", "Payment Details". When AllowCollapse is enabled, users can collapse sections they don't need to see.
/// </summary>
2021-10-15 11:50:31 +03:00
/// <example>
2025-10-24 16:28:11 +03:00
/// Basic fieldset grouping form fields:
2021-10-15 11:50:31 +03:00
/// <code>
2025-10-24 16:28:11 +03:00
/// <RadzenFieldset Text="Personal Information">
/// <RadzenStack Gap="1rem">
/// <RadzenFormField Text="First Name">
/// <RadzenTextBox @bind-Value=@model.FirstName />
/// </RadzenFormField>
/// <RadzenFormField Text="Last Name">
/// <RadzenTextBox @bind-Value=@model.LastName />
/// </RadzenFormField>
/// </RadzenStack>
/// </RadzenFieldset>
/// </code>
/// Collapsible fieldset:
/// <code>
/// <RadzenFieldset Text="Advanced Options" Icon="settings" AllowCollapse="true">
/// Advanced configuration fields...
2021-10-15 11:50:31 +03:00
/// </RadzenFieldset>
/// </code>
/// </example>
2021-10-07 09:27:28 +03:00
public partial class RadzenFieldset : RadzenComponent
{
2021-10-25 10:30:41 +03:00
/// <inheritdoc />
2025-05-07 13:11:30 +03:00
protected override string GetComponentCssClass ( ) = >
ClassList . Create ( "rz-fieldset" )
. Add ( "rz-fieldset-toggleable" , AllowCollapse )
. ToString ( ) ;
2021-10-07 09:27:28 +03:00
2021-10-07 11:35:29 +03:00
/// <summary>
2021-10-25 11:20:57 +03:00
/// Gets or sets a value indicating whether collapsing is allowed. Set to <c>false</c> by default.
2021-10-07 11:35:29 +03:00
/// </summary>
2021-10-25 11:20:57 +03:00
/// <value><c>true</c> if collapsing is allowed; otherwise, <c>false</c>.</value>
2021-10-07 09:27:28 +03:00
[Parameter]
public bool AllowCollapse { get ; set ; }
private bool collapsed ;
2021-10-14 10:32:00 +03:00
2021-10-07 11:35:29 +03:00
/// <summary>
/// Gets or sets a value indicating whether this <see cref="RadzenFieldset"/> is collapsed.
/// </summary>
/// <value><c>true</c> if collapsed; otherwise, <c>false</c>.</value>
2021-10-07 09:27:28 +03:00
[Parameter]
public bool Collapsed { get ; set ; }
2023-10-20 02:56:18 -04:00
/// <summary>
/// Gets or sets the title attribute of the expand button.
/// </summary>
/// <value>The title attribute value of the expand button.</value>
[Parameter]
2026-01-07 12:29:58 +05:00
public string? ExpandTitle { get ; set ; }
2023-10-20 02:56:18 -04:00
/// <summary>
/// Gets or sets the title attribute of the collapse button.
/// </summary>
/// <value>The title attribute value of the collapse button.</value>
[Parameter]
2026-01-07 12:29:58 +05:00
public string? CollapseTitle { get ; set ; }
2023-10-20 02:56:18 -04:00
/// <summary>
/// Gets or sets the aria-label attribute of the expand button.
/// </summary>
/// <value>The aria-label attribute value of the expand button.</value>
[Parameter]
2026-01-07 12:29:58 +05:00
public string? ExpandAriaLabel { get ; set ; }
2023-10-20 02:56:18 -04:00
/// <summary>
/// Gets or sets the aria-label attribute of the collapse button.
/// </summary>
/// <value>The aria-label attribute value of the collapse button.</value>
[Parameter]
2026-01-07 12:29:58 +05:00
public string? CollapseAriaLabel { get ; set ; }
2023-10-20 02:56:18 -04:00
2021-10-07 11:35:29 +03:00
/// <summary>
/// Gets or sets the icon.
/// </summary>
/// <value>The icon.</value>
2021-10-07 09:27:28 +03:00
[Parameter]
2026-01-07 12:29:58 +05:00
public string? Icon { get ; set ; }
2021-10-07 09:27:28 +03:00
2023-10-06 11:18:29 +03:00
/// <summary>
/// Gets or sets the icon color.
/// </summary>
/// <value>The icon color.</value>
[Parameter]
2026-01-07 12:29:58 +05:00
public string? IconColor { get ; set ; }
2023-10-06 11:18:29 +03:00
2021-10-07 11:35:29 +03:00
/// <summary>
/// Gets or sets the text.
/// </summary>
/// <value>The text.</value>
2021-10-07 09:27:28 +03:00
[Parameter]
public string Text { get ; set ; } = "" ;
2021-10-07 11:35:29 +03:00
/// <summary>
/// Gets or sets the header template.
/// </summary>
/// <value>The header template.</value>
2021-10-07 09:27:28 +03:00
[Parameter]
2026-01-07 12:29:58 +05:00
public RenderFragment ? HeaderTemplate { get ; set ; }
2021-10-07 09:27:28 +03:00
2021-10-07 11:35:29 +03:00
/// <summary>
2021-10-14 09:09:16 +03:00
/// Gets or sets the child content.
2021-10-07 11:35:29 +03:00
/// </summary>
2021-10-14 09:09:16 +03:00
/// <value>The child content.</value>
2021-10-07 09:27:28 +03:00
[Parameter]
2026-01-07 12:29:58 +05:00
public RenderFragment ? ChildContent { get ; set ; }
2021-10-07 09:27:28 +03:00
2021-10-07 11:35:29 +03:00
/// <summary>
/// Gets or sets the summary template.
/// </summary>
/// <value>The summary template.</value>
2021-10-07 09:27:28 +03:00
[Parameter]
2026-01-07 12:29:58 +05:00
public RenderFragment ? SummaryTemplate { get ; set ; } = null ;
2021-10-07 09:27:28 +03:00
2021-10-07 11:35:29 +03:00
/// <summary>
2021-10-14 10:32:00 +03:00
/// Gets or sets the expand callback.
2021-10-07 11:35:29 +03:00
/// </summary>
2021-10-14 10:32:00 +03:00
/// <value>The expand callback.</value>
2021-10-07 09:27:28 +03:00
[Parameter]
public EventCallback Expand { get ; set ; }
2021-10-07 11:35:29 +03:00
/// <summary>
2021-10-14 10:32:00 +03:00
/// Gets or sets the collapse callback.
2021-10-07 11:35:29 +03:00
/// </summary>
2021-10-14 10:32:00 +03:00
/// <value>The collapse callback.</value>
2021-10-07 09:27:28 +03:00
[Parameter]
public EventCallback Collapse { get ; set ; }
2025-05-07 13:11:30 +03:00
async Task Toggle ( EventArgs args )
2021-10-07 09:27:28 +03:00
{
collapsed = ! collapsed ;
if ( collapsed )
{
await Collapse . InvokeAsync ( args ) ;
}
else
{
await Expand . InvokeAsync ( args ) ;
}
StateHasChanged ( ) ;
}
2021-10-25 10:30:41 +03:00
/// <inheritdoc />
2021-10-07 09:27:28 +03:00
protected override void OnInitialized ( )
{
2024-02-19 10:31:27 +02:00
base . OnInitialized ( ) ;
2021-10-07 09:27:28 +03:00
collapsed = Collapsed ;
}
2023-10-20 02:56:18 -04:00
private string TitleAttribute ( )
{
if ( collapsed )
{
return string . IsNullOrWhiteSpace ( ExpandTitle ) ? "Expand" : ExpandTitle ;
}
return string . IsNullOrWhiteSpace ( CollapseTitle ) ? "Collapse" : CollapseTitle ;
}
private string AriaLabelAttribute ( )
{
if ( collapsed )
{
return string . IsNullOrWhiteSpace ( ExpandAriaLabel ) ? "Expand" : ExpandAriaLabel ;
}
return string . IsNullOrWhiteSpace ( CollapseAriaLabel ) ? "Collapse" : CollapseAriaLabel ;
}
2021-10-25 10:30:41 +03:00
/// <inheritdoc />
2021-10-07 09:27:28 +03:00
public override async Task SetParametersAsync ( ParameterView parameters )
{
if ( parameters . DidParameterChange ( nameof ( Collapsed ) , Collapsed ) )
{
collapsed = parameters . GetValueOrDefault < bool > ( nameof ( Collapsed ) ) ;
}
await base . SetParametersAsync ( parameters ) ;
}
2026-01-07 12:29:58 +05:00
bool preventKeyPress ;
2024-02-19 10:31:27 +02:00
async Task OnKeyPress ( KeyboardEventArgs args , Task task )
{
var key = args . Code ! = null ? args . Code : args . Key ;
if ( key = = "Space" | | key = = "Enter" )
{
preventKeyPress = true ;
await task ;
}
else
{
preventKeyPress = false ;
}
}
2021-10-07 09:27:28 +03:00
}
2021-10-25 10:06:11 +03:00
}