aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Computed disabled attribute does not rerender.

Open mellamokb opened this issue 8 years ago • 8 comments

I have tested a simple form concept. There is a checkbox called "Active", which when you check the following fields become enabled.

    <div class="form-group row">
        <label class="form-check-label col-sm-4" for="Active">Is Active</label>
        <div class="col-sm-8">
            <input class="form-check-input" type="checkbox" name="Active" id="Active" bind="Active" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-form-label col-sm-4" for="Minimum">% Minimum</label>
        <div class="col-sm-8">
            <input class="form-control" style="width:160px;" type="text" name="Minimum" id="Minimum" bind="MinimumStr" disabled="@(!Active)" />
        </div>
    </div>

However, the rendering of being disabled only works on initial load, based on the default checked status of Active checkbox. When you check or uncheck the Active box, it doesn't update the disabled status of the Minimum textbox, even if you hook into the Active setter and manually call StateHasChanged().

mellamokb avatar Apr 20 '18 02:04 mellamokb

You cannot bind to a DOMElement directly, you need to have a property declare in @function block
Your code should be:

<div class="form-group row">
        <label class="form-check-label col-sm-4" for="Active">Is Active</label>
        <div class="col-sm-8">
            <input class="form-check-input" type="checkbox" bind="@active" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-form-label col-sm-4" for="Minimum">% Minimum</label>
        <div class="col-sm-8">
            <input class="form-control" style="width:160px;" type="text" name="Minimum" id="Minimum" bind="MinimumStr" disabled="@(!active)" />
        </div>
    </div>

@functions {
 bool active;
}

aguacongas avatar Apr 20 '18 06:04 aguacongas

Thanks for answering, @aguacongas!

SteveSandersonMS avatar Apr 20 '18 07:04 SteveSandersonMS

Sorry, I wasn't providing the full source so as not to distract from the issue. I do have a functions block with the relevant properties defined. Please try to build and run the concept and you should see the issue.

There is a similar issue with the checked status of a checkbox. If you cause a checkbox to go from unchecked to checked, it works fine. But if a state change causes a checkbox to go from checked to unchecked, it doesn't work (it stays checked instead). I think it has something to do with state changes that involve attributes getting added/removed rather than just the value changing.

Thanks!

mellamokb avatar Apr 20 '18 14:04 mellamokb

@SteveSandersonMS you should reopen

That works :

<input class="form-check-input" type="checkbox" bind="@active" />
<button type="submit" class="btn btn-default" disabled="@active" onclick="@Click">Click</button>
<div>@active</div>

@functions {
    bool active;
    void Click()
    {
    }
}

That doesn't :

<input class="form-check-input" type="checkbox" bind="@active" />
<button type="submit" class="btn btn-default" onclick="@Click" disabled="@active" >Click</button>
<div>@active</div>

@functions {
    bool active;
    void Click()
    {
    }
}

aguacongas avatar Apr 20 '18 15:04 aguacongas

Thanks so much for reporting this! I've tracked it down to a bug in the attribute diffing algorithm.

For now you'll need to use the workaround by @aguacongas above. We'll have a fix in the next version.

SteveSandersonMS avatar Apr 20 '18 15:04 SteveSandersonMS

Will be fixed by aspnet/Blazor#633, so closing in favour of that.

SteveSandersonMS avatar Apr 20 '18 16:04 SteveSandersonMS

This is still No working ... please reopen,

WojcikMM avatar May 08 '19 18:05 WojcikMM

@WojcikMM Since this issue was closed with the view that it was fixed, and many aspects of the technology have moved on since then, if you think there is a problem could you please open a new issue? Please be sure to state what version of the framework you're using, and provide a clear and minimal repro example stating what you think should happen and what happens instead.

SteveSandersonMS avatar May 08 '19 18:05 SteveSandersonMS