mirror of
https://github.com/radzenhq/radzen-blazor.git
synced 2026-02-04 05:35:44 +00:00
176 lines
6.1 KiB
C#
176 lines
6.1 KiB
C#
using Radzen.Blazor.Rendering;
|
|
using Microsoft.AspNetCore.Components;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Radzen.Blazor
|
|
{
|
|
/// <summary>
|
|
/// Interface that represents the context of the form field.
|
|
/// </summary>
|
|
public interface IFormFieldContext
|
|
{
|
|
/// <summary>
|
|
/// Notifies the form field that the disabled state of the component has changed.
|
|
/// </summary>
|
|
Action<bool> DisabledChanged { get; set; }
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether the label is floating or fixed on top.
|
|
/// </summary>
|
|
bool AllowFloatingLabel { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Represents the context of the form field.
|
|
/// </summary>
|
|
public class FormFieldContext : IFormFieldContext
|
|
{
|
|
/// <summary>
|
|
/// Notifies the form field that the disabled state of the component has changed.
|
|
/// </summary>
|
|
public Action<bool> DisabledChanged { get; set; }
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether the label is floating or fixed on top.
|
|
/// </summary>
|
|
public bool AllowFloatingLabel { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// A Blazor component that wraps another component and adds a label, helper text, start and end content.
|
|
/// </summary>
|
|
/// <example>
|
|
/// <code>
|
|
/// <RadzenFormField Text="Search">
|
|
/// <RadzenTextBox @bind-Value="@text" />
|
|
/// </RadzenFormField>
|
|
/// </code>
|
|
/// </example>
|
|
public partial class RadzenFormField : RadzenComponent
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the child content. The child content is wrapped by the form field. Can be used with RadzenTextBox, RadzenTextArea, RadzenPassword, RadzenDropDown, RadzenDropDownList, RadzenNumeric.
|
|
/// </summary>
|
|
[Parameter]
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the optional content that will be rendered before the child content. Usually used with RadzenIcon.
|
|
/// </summary>
|
|
/// <example>
|
|
/// <code>
|
|
/// <RadzenFormField Text="Search">
|
|
/// <Start>
|
|
/// <RadzenIcon Icon="search" />
|
|
/// </Start>
|
|
/// <ChildContent>
|
|
/// <RadzenTextBox @bind-Value="@text" />
|
|
/// </ChildContent>
|
|
/// </RadzenFormField>
|
|
/// </code>
|
|
/// </example>
|
|
[Parameter]
|
|
public RenderFragment Start { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the optional content that will be rendered after the child content. Usually used with RadzenIcon.
|
|
/// </summary>
|
|
/// <example>
|
|
/// <code>
|
|
/// <RadzenFormField>
|
|
/// <End>
|
|
/// <RadzenIcon Icon="search" />
|
|
/// </End>
|
|
/// <ChildContent>
|
|
/// <RadzenTextBox @bind-Value="@text" />
|
|
/// </ChildContent>
|
|
/// </RadzenFormField>
|
|
/// </code>
|
|
/// </example>
|
|
[Parameter]
|
|
public RenderFragment End { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the optional content that will be rendered below the child content. Used with a validator or to display some additional information.
|
|
/// </summary>
|
|
/// <example>
|
|
/// <code>
|
|
/// <RadzenFormField>
|
|
/// <Helper>
|
|
/// <RadzenRequiredValidator Component="Text" />
|
|
/// </Helper>
|
|
/// <ChildContent>
|
|
/// . <RadzenTextBox @bind-Value="@text" Name="Text" />
|
|
/// </ChildContent>
|
|
/// </RadzenFormField>
|
|
/// </code>
|
|
/// </example>
|
|
[Parameter]
|
|
public RenderFragment Helper { get; set; }
|
|
/// <summary>
|
|
/// Gets or sets the custom content for the label using a Razor template.
|
|
/// When provided, this template will be rendered instead of the plain text specified in the Text parameter.
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
public RenderFragment TextTemplate { get; set; }
|
|
/// <summary>
|
|
/// Gets or sets the label text.
|
|
/// </summary>
|
|
[Parameter]
|
|
public string Text { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether the label is floating or fixed on top.
|
|
/// </summary>
|
|
/// <value><c>true</c> if floating is allowed; otherwise, <c>false</c>.</value>
|
|
[Parameter]
|
|
public bool AllowFloatingLabel { get; set; } = true;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the name of the form field. Used to associate the label with a component.
|
|
/// </summary>
|
|
[Parameter]
|
|
public string Component { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the design variant of the form field.
|
|
/// </summary>
|
|
/// <value>The variant of the form field.</value>
|
|
[Parameter]
|
|
public Variant Variant { get; set; } = Variant.Outlined;
|
|
|
|
private bool disabled;
|
|
|
|
private readonly IFormFieldContext context;
|
|
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RadzenFormField"/> class.
|
|
/// </summary>
|
|
public RadzenFormField()
|
|
{
|
|
context = new FormFieldContext { DisabledChanged = DisabledChanged, AllowFloatingLabel = AllowFloatingLabel };
|
|
}
|
|
|
|
private void DisabledChanged(bool value)
|
|
{
|
|
disabled = value;
|
|
StateHasChanged();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void OnParametersSet()
|
|
{
|
|
base.OnParametersSet();
|
|
|
|
context.AllowFloatingLabel = AllowFloatingLabel;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override string GetComponentCssClass()
|
|
{
|
|
return ClassList.Create($"rz-form-field rz-variant-{Enum.GetName(typeof(Variant), Variant).ToLowerInvariant()}").AddDisabled(disabled).Add("rz-floating-label", AllowFloatingLabel == true).ToString();
|
|
}
|
|
}
|
|
}
|