Blazor扩展InputSelect组件



如何扩展InputSelectblazor组件以便可以预设值?

我有这个枚举:

enum EnumStatus {
Published,
Unpublished,
Concept,
Deleted
}

现在我想创建一个InputSelectStatus,并将其绑定到EditForm中的EnumStatus。根据状态值,我希望显示不同的内容。

我找到了一些地方,但我最终删除了我的代码,因为绑定没有在表单中正确反映。

例如,如果状态为Deleted,那么我只希望输入字段为只读。如果是其他情况,我只希望它绑定到元素。


示例用例:


<InputSelectStatus @bind-Value="status" />
@code {
private EnumStatus status;
}

我希望它输出一个带有预设选项的<select></select>,或者一个<input readonly />@bind-Value=""应该只接受一种类型的EnumStatus

这里有一个完整的例子,也支持来自这个repo的FocusAsync()

namespace OrakTech.Inputs.Components
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Rendering;
public class FInputEnum<TValue> : InputBase<TValue>, IFocusInput
{
public ElementReference Element { get; set; }
[Parameter]
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public RenderFragment<TValue>? ChildContent { get; set; }
#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "select");
builder.AddMultipleAttributes(1, AdditionalAttributes);
builder.AddAttribute(2, "class", CssClass);
builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValueAsString));
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder<string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString));
#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
if (ChildContent is not null)
{
foreach (var enumValue in enumValues)
{
builder.AddContent(1, ChildContent(enumValue));
}
}
else
{
foreach (var enumValue in enumValues)
{
builder.OpenElement(1, "option");
builder.AddAttribute(2, "value", enumValue);
builder.AddContent(3, enumValue);
builder.CloseElement();
}
}
builder.AddElementReferenceCapture(5, (__ref) => { Element = __ref; });
builder.CloseElement();
}
IReadOnlyList<TValue> enumValues => Enum.GetValues(typeof(TValue)).Cast<TValue>().ToArray();
protected override bool TryParseValueFromString(
string value,
[MaybeNullWhen(false)] out TValue result,
[NotNullWhen(false)] out string validationErrorMessage)
{
validationErrorMessage = "";
var parseOk = Enum.TryParse(typeof(TValue), value, out var parseResult);
if (parseOk)
{
result = (TValue)parseResult;
return parseOk;
}
else
{
validationErrorMessage = $"Failed to pass value {value}";
result = default;
return false;
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新