question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DatePicker Focused, UnFocused not working as expected

See original GitHub issue

Description

Preciously on XF DatePicker Focused and UnFocused events were triggered when opening and closing the date picker dialog

Now in Maui is not longer the case

Steps to Reproduce

  1. Open MauiApp2
  2. Set breakpoints on DatePicker Focus and Unfocused events
  3. Run the app
  4. Interact with DatePicker

Note: See the video attached for more info

Expected:

  • Focused should trigger when the DatePicker dialog is opening
  • UnFocused should trigger when the DatePicker dialog is closing

Current:

  • Focused is not trigger when the DatePicker dialog is opening
  • UnFocused is not trigger when the DatePicker dialog is closing

https://user-images.githubusercontent.com/31915729/214537202-0d8d9cc0-f851-4a4a-9e5d-30641af9711f.mov

Link to public reproduction project repository

https://github.com/edgarfgp/DatePickerBugRepro

Version with bug

7.0 (current)

Last version that worked well

7.0 (current)

Affected platforms

iOS, Android

Affected platform versions

iOS 15, Android 11

Did you find any workaround?

Write a Custom DatePicker Handler to set the VirtualView.IsFocused

Relevant log output

No response

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
TimLarivierecommented, Jan 25, 2023

Workaround:

On iOS, we can directly listen to EditingDidBegin and EditingDidEnd. On Android, it’s a little bit more complicated. FocusChange doesn’t trigger because Maui.DatePickerHandler sets FocusableOnTouchMode to false. So instead we can intercept the DatePickerDialog and listen for ShowEvent and DismissEvent.

DatePickerWithFocusHandler.cs

using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;

public partial class DatePickerWithFocusHandler : DatePickerHandler
{
    public static PropertyMapper<IDatePicker, DatePickerWithFocusHandler> Mapper = new (DatePickerHandler.Mapper)
    {
        [nameof(IDatePicker.IsFocused)] = MapIsFocused
    };

    public DatePickerWithFocusHandler() : base(Mapper)
    {
    }
}

DatePickerWithFocusHandler.Android.cs

using Android.App;

public partial class DatePickerWithFocusHandler
{
    public static void MapIsFocused(DatePickerWithFocusHandler handler, IDatePicker datePicker)
    {
        if (handler.PlatformView.IsFocused == datePicker.IsFocused) return;
        
        if (datePicker.IsFocused)
        {
            handler.PlatformView.RequestFocus();
        }
        else
        {
            handler.PlatformView.ClearFocus();
        }
    }

    private DatePickerDialog? _dialog;

    protected override DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
    {
        _dialog = base.CreateDatePickerDialog(year, month, day);
        return _dialog;
    }

    protected override void ConnectHandler(MauiDatePicker platformView)
    {
        base.ConnectHandler(platformView);
        if (_dialog != null)
        {
            _dialog.ShowEvent += OnDialogShown;
            _dialog.DismissEvent += OnDialogDismissed;
        }
    }

    protected override void DisconnectHandler(MauiDatePicker platformView)
    {
        if (_dialog != null)
        {
            _dialog.ShowEvent -= OnDialogShown;
            _dialog.DismissEvent -= OnDialogDismissed;
        }
        base.DisconnectHandler(platformView);

        _dialog = null;
    }

    private void OnDialogShown(object sender, EventArgs e)
    {
        this.VirtualView.IsFocused = true;
    }

    private void OnDialogDismissed(object sender, EventArgs e)
    {
        this.VirtualView.IsFocused = false;
    }
}

DatePickerWithFocusHandler.iOS.cs

public partial class DatePickerWithFocusHandler
{
    public static void MapIsFocused(DatePickerWithFocusHandler handler, IDatePicker datePicker)
    {
        if (handler.PlatformView.Focused == datePicker.IsFocused) return;
        
        if (datePicker.IsFocused)
        {
            handler.PlatformView.BecomeFirstResponder();
        }
        else
        {
            handler.PlatformView.ResignFirstResponder();
        }
    }

    protected override void ConnectHandler(MauiDatePicker platformView)
    {
        base.ConnectHandler(platformView);
        platformView.EditingDidBegin += OnEditingDidBegin;
        platformView.EditingDidEnd += OnEditingDidEnd;
    }

    private void OnEditingDidBegin(object sender, EventArgs e)
    {
        this.VirtualView.IsFocused = true;
    }

    private void OnEditingDidEnd(object sender, EventArgs e)
    {
        this.VirtualView.IsFocused = false;
    }
}

MauiProgram.cs

builder
    .UseMauiApp<App>()
    .ConfigureMauiHandlers(handlers =>
        handlers.AddHandler<DatePicker, DatePickerWithFocusHandler>()
    )
1reaction
SagarPanwalacommented, Apr 18, 2023

@PureWeen @jsuarezruiz As this issue has been resolved now, when it’s going to be released 😬?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Focusing on DatePicker does not work MAUI
This is an issue on Github: Fix (or don't) the behavior of calling "Focus" on Android to open the picker . From the...
Read more >
Untitled
... I don't know. easyexcel maven DatePicker Focused, UnFocused not working as expected … ... is not focused. - Expected behavior: DatePicker is...
Read more >
JQuery UI Datepicker IE focus fix
At my current client we have multiple controls on the page that listen for onblur and onchange events to notify of changes to...
Read more >
Datepicker focuses on date closest to today
Hi, I'd like to know why the Datepicker auto focuses on the date closest to today's date? ... How can the initial focused...
Read more >
How to build an accessible datepicker component in React
In this article, we will be focusing on making the component ... When the datepicker is visible, the calendar will not be visible...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found