Skip to content

MudDatePicker: Add keyboard navigation#12028

Merged
danielchalmers merged 5 commits intoMudBlazor:devfrom
spingee:feature/datepicker_key_nav
Nov 23, 2025
Merged

MudDatePicker: Add keyboard navigation#12028
danielchalmers merged 5 commits intoMudBlazor:devfrom
spingee:feature/datepicker_key_nav

Conversation

@spingee
Copy link
Contributor

@spingee spingee commented Nov 1, 2025

Features:
When popup is opened it can be navigated with keys:

  • Arrows keys:
    Navigates years/months/days in respective views

  • Shift + arrows keys:
    Navigates years in month view
    Navigates years and months in date view

  • Enter:
    Selects value in current view and moves to next view or commits value to input and closes popup on last view

  • Backspace
    Closes popup when first view and doesnt commit value to input
    Moves to previous view if not first view

Fixes:
Datepicker now retains focus when value is selected

Recording.2025-11-10.130047.mp4

@spingee spingee changed the title Ability to navigate datepicker with keys DatePicker: Ability to navigate datepicker with keys Nov 1, 2025
@mudbot mudbot bot added accessibility Accessibility concerns (ARIA, keyboard, focus, screen readers, contrast) enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library labels Nov 1, 2025
gemini-code-assist[bot]

This comment was marked as spam.

@spingee spingee force-pushed the feature/datepicker_key_nav branch from d14ba74 to 88ec782 Compare November 1, 2025 14:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances keyboard navigation in the MudDatePicker component by implementing arrow key navigation across different picker views (Date, Month, Year) and enabling Enter key selection. The changes also include focus management improvements and mouse event handling adjustments.

  • Implemented arrow key navigation (left/right/up/down) for navigating dates, months, and years
  • Enhanced Enter key handling to select the currently highlighted date/month/year based on the current view
  • Added FocusAsync() calls when clicking on dates, months, and years for better accessibility
  • Modified event handlers to prevent default mouse behavior on certain picker elements

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
MudDatePicker.cs Core logic for keyboard navigation: arrow key handlers for navigating dates/months/years, Enter key selection for highlighted items, and FocusAsync calls in click handlers
MudBaseDatePicker.razor.cs Changed visibility of helper methods/fields from private to protected, updated ScrollToYear to accept optional date parameter, trailing whitespace cleanup
MudBaseDatePicker.razor Added mousedown preventDefault attributes to toolbar and calendar content, cleaned up lambda expressions
DatePickerTests.cs Added comprehensive test for keyboard navigation and date selection workflow

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@spingee spingee force-pushed the feature/datepicker_key_nav branch from 88ec782 to d696c07 Compare November 2, 2025 13:48
Copy link
Member

@danielchalmers danielchalmers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is fairly dense and nested with no comments. Could you also expand on the PR description with images/videos?

@mudbot mudbot bot added needs: changes A maintainer has asked for further modifications to be made to this pull request needs: info This issue/PR lacks key context (goal, setup, environment) needs: visuals This issue/PR needs screenshots or video for UI bugs or design changes labels Nov 7, 2025
@spingee spingee force-pushed the feature/datepicker_key_nav branch 4 times, most recently from db8b8b0 to 966ebda Compare November 9, 2025 16:30
Copy link
Member

@igotinfected igotinfected left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool feature, thanks!

Added some minor code suggestions.

Here's a list of issues I came across while testing (none of them blocking for the PR I believe):

  • keyboard navigation doesn't work on a DatePicker with PickerVariant="PickerVariant.Static"
  • in the fixed values usage section
    • when the month is fixed you can get into a weird state by using the Shift+Left/Right Arrow shortcuts
Image
  • when the year is fixed, when you click on the year while you're in the day view you can get into a weird state (click any day, then click on the year)
Image
  • because of the change to the MudPicker.razor (KeyDownPreventDefault="Open") file, when any picker popup is open you can no longer type into the text field (probably not an issue but good to know)

We should also add a section to the DatePicker docs listing all the new shortcuts, you can refer to MudRating for an example.

protected override RenderFragment PickerContent =>
@<CascadingValue Value="@this" IsFixed="true">
<MudPickerToolbar Class="mud-picker-datepicker-toolbar" ShowToolbar="@ShowToolbar" Orientation="@Orientation" PickerVariant="@PickerVariant" Color="@Color">
<MudPickerToolbar Class="mud-picker-datepicker-toolbar" ShowToolbar="@ShowToolbar" Orientation="@Orientation" PickerVariant="@PickerVariant" Color="@Color" @onmousedown:preventDefault="true">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity -- why is the onmousedown needed?

Copy link
Contributor Author

@spingee spingee Nov 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@igotinfected to not lose focus on input field, when clicking buttons in toolbar

@@ -259,17 +276,67 @@ protected internal override async Task OnHandleKeyDownAsync(KeyboardEventArgs ar
case "ArrowRight":
if (Open)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could invert the if to reduce nesting

if (Open)
{

if (args.ShiftKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this if and the switch + case below could be a single if

}

break;
case "ArrowLeft":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments as for ArrowRight case

@@ -279,12 +346,52 @@ protected internal override async Task OnHandleKeyDownAsync(KeyboardEventArgs ar
}
else if (args.ShiftKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if/switch can be simplified

@@ -293,35 +400,93 @@ protected internal override async Task OnHandleKeyDownAsync(KeyboardEventArgs ar
}
else if (args.ShiftKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be simplified

/// Scrolls to the current year.
/// </summary>
public async void ScrollToYear()
public async Task ScrollToYear(DateTime? date = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add Async suffix to indicate it's an async method

@spingee spingee force-pushed the feature/datepicker_key_nav branch from 966ebda to 8fa05bd Compare November 22, 2025 13:05
@danielchalmers danielchalmers changed the title DatePicker: Ability to navigate datepicker with keys MudDatePicker: Add keyboard navigation Nov 22, 2025
@danielchalmers danielchalmers removed needs: info This issue/PR lacks key context (goal, setup, environment) needs: visuals This issue/PR needs screenshots or video for UI bugs or design changes needs: changes A maintainer has asked for further modifications to be made to this pull request labels Nov 23, 2025
@danielchalmers
Copy link
Member

Thank you for this!

This was referenced Feb 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accessibility Accessibility concerns (ARIA, keyboard, focus, screen readers, contrast) enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants