Coder Social home page Coder Social logo

Comments (11)

ScarletKuro avatar ScarletKuro commented on July 22, 2024 1

Also I think a lot of components work this way currently:

Yeah, almost all input components are broken right now and doing fundamentally wrong things like updating the Value parameter and the ValueChanged parameter. They should use ParameterState and call the FieldChanged method from the state change handler, which already handles the change logic. However, modifying input components is difficult because they are tightly coupled.

from mudblazor.

ScarletKuro avatar ScarletKuro commented on July 22, 2024

This does not currently seem possible.

Hi,
Why does it seem not possible if MudSwitch supports two-way binding, which means you can intercept the state?
https://try.mudblazor.com/snippet/wEQokAvYgcmyfVZx

from mudblazor.

KieranIrelandPD avatar KieranIrelandPD commented on July 22, 2024

https://try.mudblazor.com/snippet/QOwSYqlbmDVZThXv

When using a MudForm and tracking FieldChanged, it does not honor the cancel.

from mudblazor.

ScarletKuro avatar ScarletKuro commented on July 22, 2024

Yeah, in v6 the FieldChanged event is not consistent. It might fire when the field has not changed and might not fire when it has.

In v7, it's at least consistent after this change: #8693. It fires in both scenarios—whether changed or not—but the formFieldChangedEventArgs.NewValue contains the correct value, and you can use this workaround:

<MudSwitch @ref="_mudSwitch" T="bool" Value="@Switch" ValueChanged="@OpenDialogAsync" />
private MudSwitch<bool> _mudSwitch = null!;
private async Task OnFormFieldChanged(FormFieldChangedEventArgs formFieldChangedEventArgs)
{
    if (formFieldChangedEventArgs.Field is MudSwitch<bool> mudSwitch)
    {
        if (mudSwitch == _mudSwitch)
        {
            if (!EqualityComparer<bool>.Default.Equals((bool)formFieldChangedEventArgs.NewValue, Switch))
            {
                HasChanges = true;
            }
        }
    }
    await InvokeAsync(StateHasChanged);
}

I don't think there will be any fix for v6 unless you want to make a hotfix PR.

@igotinfected, do you know if it's still a bug in v7 that for the switch, the FieldChanged event fires regardless of whether it was changed or not, or is this intended? I'm not much of a MudForm user.

from mudblazor.

igotinfected avatar igotinfected commented on July 22, 2024

Don't think it's necessary to trigger the FieldChanged event if the user decides to revert a value change. Currently we just don't handle that case. The change was already applied in the component, so form validation is called. In the meantime the user changes the value again (with the dialog in this case) but that is considered to be an extra change.

To handle this case we would have to not touch the Value of a component if a ValueChanged is provided and only do form validation after the component parameters are updated (with the parameter state framework for example). I tried messing around with the MudBooleanInput implementation to see if I could get this to work, I got it working halfway but always end up with an infinite update loop :/ Need to try again with a fresh mind 🤪

Also I think a lot of components work this way currently:

image

from mudblazor.

ScarletKuro avatar ScarletKuro commented on July 22, 2024

@henon @igotinfected Also, what I noticed while doing this workaround #9158 (comment) is that the FieldId is always different for the same instance whenever you access it.

It's because of this code:

public string FieldId => UserAttributes.TryGetValue("id", out var id) && id != null
? id.ToString() ?? $"mudinput-{Guid.NewGuid()}"
: $"mudinput-{Guid.NewGuid()}";

If the "id" attribute is not found, it will assign a random GUID, but this GUID will always be different because the property with => will be reevaluated.

Is this intended, or is it actually a bug and it should be something like this?

private readonly string _fieldId = Guid.NewGuid().ToString();
public string FieldId => UserAttributes.TryGetValue("id", out var id) && id != null 
             ? id.ToString() ?? $"mudinput-{_fieldId}" 
             : $"mudinput-{_fieldId}"; 

from mudblazor.

igotinfected avatar igotinfected commented on July 22, 2024

If the "id" attribute is not found, it will assign a random GUID, but this GUID will always be different because the property with => will be reevaluated.

Yeah looks like a bug, I think I did more or less the same logic for one of the input classes MudInput or MudBaseInput using the parameter state framework. That FieldId is only used in two places too, I remember wanting to get rid of it eventually

from mudblazor.

henon avatar henon commented on July 22, 2024

yes it is a bug. the id should be cached

from mudblazor.

igotinfected avatar igotinfected commented on July 22, 2024

FYI working on a fix for MudBooleanInput, got it working for MudSwitch, just adding tests and checking for regressions, targetting v7+ though.

from mudblazor.

igotinfected avatar igotinfected commented on July 22, 2024

The MudCheckBox component is causing me trouble in conjunction with the parameter state, will most likely request some help / thoughts on that in the next few days :p

from mudblazor.

ScarletKuro avatar ScarletKuro commented on July 22, 2024

The MudCheckBox component is causing me trouble in conjunction with the parameter state, will most likely request some help / thoughts on that in the next few days :p

Your struggles also could be related to missing this: #9225 as it breaks ParameterState, or maybe something else. You can always ask me.

from mudblazor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.