0

I have created a custom button component in a Blazor Server App as follows:

<button type="button" @attributes="@CustomeAttribute" @onclick="@OnButtonClick" class="styled-btn" style="width: @Width; height: @Height;
                border-radius: @BorderRadius;">
    <i class="@IconClass" style="font-size: @IconSize; color: @IconColor"></i>
</button>

code {
    [Parameter]
    public string Width { get; set; }
    [Parameter]
    public string Height { get; set; }
    [Parameter]
    public string BorderRadius { get; set; }
    [Parameter]
    public string IconColor { get; set; }
    [Parameter]
    public string IconClass { get; set; }
    [Parameter]
    public string IconSize { get; set; }
    [Parameter]
    public bool IsDisabled { get; set; }
    [Parameter]
    public EventCallback<MouseEventArgs> OnButtonClick { get; set; }
    public Dictionary<string, object> CustomeAttribute { get; set; }

I use this component in a parent component:

<StyledButton IsDisabled="EqSelectedRowCount == 0 ? true : false" BorderRadius="10px" Width="40px" Height="40px" IconColor="Black" IconSize="20px" IconClass="fa-solid fa-power-off" OnButtonClick="@ToggleEquipmentStatusWarn"></StyledButton>

IsDisabled value can be changed dynamicall based on some business rules. It can take true or false values. I want my button component add disabled attribute to the button whenever the IsDisabled value changes to true. I studies this post and added the following code to the button component:

 private bool _isDisabled;
    
protected override void OnInitialized()
    {
        _isDisabled = IsDisabled;
    }

    protected override void OnParametersSet()
    {
        if(_isDisabled != IsDisabled)
        {
            _isDisabled = IsDisabled;
            AddDisabledAttribute();
        }
    }

    void AddDisabledAttribute()
    {
        var dic = new Dictionary<string, object>();
        if (_isDisabled)
        {
            dic.Add("disabled", true);
        }
        else if (!_isDisabled && dic.ContainsKey("disabled"))
        {
            dic.Remove("disabled");
        }

        CustomeAttribute = dic;
    }

But It does not work and there should be a better solution. How can disable my button component conditionally?

Alex Wright
  • 421
  • 1
  • 11

1 Answers1

1

Try this: https://try.mudblazor.com/snippet/QuwnOQQlUlyNKMAs

It has to be

disabled=@IsDisabled

and IsDisabled has to be bool.

<div>Value1 = @Value1</div>
<button disabled=@Disabled1 onclick="@OnClick1" style="border: 1px solid black;">First</button>
<div>Value2 = @Value2</div>
<button disabled=@Disabled2 onclick="@OnClick2" style="border: 1px solid black;">Second</button>
@code {
    int Value1 = 0;
    int Value2 = 0;

    bool Disabled1 = true;
    bool Disabled2 = false;
    void OnClick1()
    {
        Value1 = 2;
    }

    void OnClick2()
    {
        Value2 = 2;
    }
}

First button is disabled, the sencond one is not.

maciek
  • 724
  • 2
  • 4
  • 16
  • I'm using `.NET 6`. In my project when I set `disabled=false` the button is still disabled. Why this happens? I tried to `rebuild` the project but the result is the same. – Alex Wright Feb 21 '23 at 17:09
  • 1
    Probably because you assign a value to an attribute that is not parsed to true or false value of class bool. But yes, it happens. Even when you just declare disabled attribute without a value. At least we have the right way to do it. – maciek Feb 21 '23 at 17:15
  • 1
    I removed `""` from `IsDisabled=@(EqSelectedRowCount == 0 ? true : false)` and it worked. Thanks. – Alex Wright Feb 21 '23 at 17:31