Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text fields unfocusing after validation error shows #373

Closed
YBTopaz8 opened this issue Jun 10, 2023 · 12 comments · Fixed by #588
Closed

Text fields unfocusing after validation error shows #373

YBTopaz8 opened this issue Jun 10, 2023 · 12 comments · Fixed by #588
Milestone

Comments

@YBTopaz8
Copy link

I'm using textfields I wanted users to only input numeric values, so i set a validation for such but

  • whenever the validation message opens, the text field looses focus
  • when the user erases ONE or ALL or even adds a character immediately AFTER the error shows, the field looses focus as well
textfield.misbehaving.mp4

I wanted to implement a custom validation that would ensure that the textfield would simply NOT accept non-numeric characters, but it's still the same issue
As shown on the video below

textfield.misbehaving.w.custom.validation.mp4
@enisn
Copy link
Owner

enisn commented Jun 12, 2023

It's strange WinUI 3 problem,

I realized the same problem in WinUI 3 Gallery application. It automatically focuses a different element in the context when something changed in layout.

winui3-focus-issue

I can implement the following workaround for inputfields:
https://stackoverflow.com/a/61765007/7200126

@enisn enisn added this to the 2.5 milestone Jun 12, 2023
@YBTopaz8
Copy link
Author

Ah, i see!
thanks for the insight!

@enisn enisn modified the milestones: 2.5, Backlog Dec 12, 2023
@ertan2002
Copy link

I have the same issue for windows app

        <ur:TextField x:Name="EmailEntry"
                      Title="Email Address"
                      ClearButtonVisibility="WhileEditing"
                      AllowClear="True"
                      Keyboard="Email"
                      Icon="{FontImageSource FontFamily=FontAwesome, Glyph={x:Static models:FontAwesomeIcons.Envelope}, Color=#F1A516}"
                      TextColor="Black"
                      InputBackgroundColor="Blue"
                      TitleColor="Grey"
                      Background="Transparent"
                      BorderColor="#F1A516">                            
            <validation:RegexValidation Pattern="{x:Static input:AdvancedEntry.REGEX_EMAIL}"
                                        Message="Invalid email address" />
        </ur:TextField>

I just type one letter then I see directly a warning and the control loses focus!

What it should suppose to be:
I type and when I unfocus from the control, then I can see the error message.

I hope it will be fixing ASAP. Thank you

@enisn
Copy link
Owner

enisn commented Jan 16, 2024

After some research, I've discovered that Panels are focusable on windows and when a panel is focused it automatically delivers the focus event to its children recursively.

If there is another textbox inside the same layout, it automatically focuses it because of this behavior.

The same problem that dotnet/maui is dealing 👉 microsoft/microsoft-ui-xaml#3825 (comment)

@enisn
Copy link
Owner

enisn commented Jan 16, 2024

When you disable focusing for the parent element

 stackLayout.HandlerChanged+= (s,e)=>
 {
     if (stackLayout.Handler.PlatformView is Microsoft.Maui.Platform.LayoutPanel panel)
     {
         panel.AllowFocusOnInteraction = false;
         panel.GettingFocus += (s, e) =>
         {
             e.Cancel = true;
         };
     }
 };

Then none of the entries are focusable. WinUI is a little bit different from other platforms. We'll follow the dotnet/maui approach here when it's solved here dotnet/maui#11472

There is no 1 single best solution now

@enisn
Copy link
Owner

enisn commented Feb 14, 2024

@ertan2002

At least you can try this custom TextField in your project to achieve showing validations when unfocused:

public class MyTextField : TextField
{
#if WINDOWS
    protected override void RegisterForEvents()
    {
        base.RegisterForEvents();

        this.Unfocused += OnUnfocused;
    }

    protected override void ReleaseEvents()
    {
        base.ReleaseEvents();
        this.Unfocused -= OnUnfocused;
    }

    protected override void CheckAndShowValidations()
    {
        // Leave empty
    }

    private void OnUnfocused(object sender, FocusEventArgs e)
    {
        base.CheckAndShowValidations();
    }
#endif
}

@enisn
Copy link
Owner

enisn commented Feb 24, 2024

Not the best solution but a workaround has been released in v2.8.0-pre.5

@ertan2002
Copy link

@ertan2002

At least you can try this custom TextField in your project to achieve showing validations when unfocused:

public class MyTextField : TextField
{
#if WINDOWS
    protected override void RegisterForEvents()
    {
        base.RegisterForEvents();

        this.Unfocused += OnUnfocused;
    }

    protected override void ReleaseEvents()
    {
        base.ReleaseEvents();
        this.Unfocused -= OnUnfocused;
    }

    protected override void CheckAndShowValidations()
    {
        // Leave empty
    }

    private void OnUnfocused(object sender, FocusEventArgs e)
    {
        base.CheckAndShowValidations();
    }
#endif
}

thank you for your solution. I'll try it.

@enisn
Copy link
Owner

enisn commented Feb 27, 2024

You don't have to override in the latest version (v2.8.0-pre.5). A workaround was already released in that version. If you update your packages, you'll get the solution from this PR: #588

textfield-unfocusing

@ertan2002
Copy link

You don't have to override in the latest version (v2.8.0-pre.5). A workaround was already released in that version. If you update your packages, you'll get the solution from this PR: #588

Normally I don't like to use beta versions, but it seems to be ok :) I'll try that first. Thank you again

@enisn
Copy link
Owner

enisn commented Feb 27, 2024

You can wait for tte stable version, It'll be released at the end of this week. I'm working on still

DevFromDownUnder pushed a commit to DevFromDownUnder/UraniumUI that referenced this issue Mar 19, 2024
Remove existing workaround

Stop windows deleting and re-adding border, which was causing focus lose as elements were no longer attached to the current view
enisn added a commit that referenced this issue Mar 20, 2024
@YBTopaz8
Copy link
Author

THanks for the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants