簡體   English   中英

Blazor 擴展 InputSelect 組件

[英]Blazor extend InputSelect component

如何擴展InputSelect blazor 組件以便預設值?

我有這個枚舉:

enum EnumStatus {
  Published,
  Unpublished,
  Concept,
  Deleted
}

現在我想創建一個InputSelectStatus ,我想將它綁定到EditForm中的EnumStatus 根據狀態值,我想顯示不同的東西。

我到了某個地方,但最終刪除了我的代碼,因為綁定在表單中沒有正確反映。

例如,如果狀態為Deleted ,那么我只希望輸入字段為只讀。 如果有任何其他情況,我只希望它綁定到元素。


示例用例:


<InputSelectStatus @bind-Value="status" />

@code {

  private EnumStatus status;
}

我希望它是 output 一個帶有預設選項的<select></select>或一個<input readonly /> @bind-Value=""應該只接受一種EnumStatus

這是一個完整的示例,它也支持來自此repoFocusAsync()

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;
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM