Skip to content

DateTimePicker: Open at Today if null or default - #644

Open
w3ori wants to merge 31 commits into
CodeBeamOrg:devfrom
w3ori:fix/datetimepicker-dateonly-default
Open

w3ori wants to merge 31 commits into
CodeBeamOrg:devfrom
w3ori:fix/datetimepicker-dateonly-default

Conversation

@w3ori

@w3ori w3ori commented Aug 26, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.59%. Comparing base (7b5faf7) to head (43c15c2).

Files with missing lines Patch % Lines
...mponents/DateTimePicker/MudDateTimePicker.razor.cs 34.78% 11 Missing and 4 partials ⚠️
...ns/Components/DateTimePicker/MudBaseDatePickerX.cs 81.81% 0 Missing and 2 partials ⚠️

❌ Your patch check has failed because the patch coverage (50.00%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #644      +/-   ##
==========================================
+ Coverage   65.42%   65.59%   +0.16%     
==========================================
  Files         111      111              
  Lines        8384     8399      +15     
  Branches     1480     1486       +6     
==========================================
+ Hits         5485     5509      +24     
+ Misses       2391     2378      -13     
- Partials      508      512       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@w3ori

w3ori commented Sep 2, 2026

Copy link
Copy Markdown
Author

@mckaragoz Can you look at this please?

@mckaragoz

mckaragoz commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

This PR is not the ultimate solution. With this solution, we only hide the real value and show something else. And with this PR, the min value of types will be unusuable.

I offer to add a new parameter like:

[Parameter]
public T? ValueOnClear { get; set; } = default;

And change the clear process:

public override async Task ClearAsync(bool close = true)
{
    await SetDateAsync(ToDateTime(ValueOnClear), true);

    if (AutoClose || close)
        await CloseAsync(false);
}

With this offered solution, you can easily set the date when user click on clear button. If no value set with parameter, it still goes to null for nullable types and min value for non nullable types. I think it's the best option, because you shouldn't set null value on non nullable types.

Could you change your PR with this solution?

Introduce a new parameter ValueOnClear (T?) on MudBaseDatePickerX to allow specifying the value applied when the clear button is used (defaults to default(T), null for nullable types). Update MudDateTimePicker.ClearAsync to call SetDateAsync(ToDateTime(ValueOnClear), true) instead of passing null, and change the close logic to honor the explicit close parameter (if AutoClose || close). This lets consumers control the cleared value and whether the picker closes when clearing.
@mckaragoz mckaragoz changed the title Fix/datetimepicker dateonly default DateTimePicker: Add ValueOnClear Parameter as an optional default Sep 14, 2026
@mckaragoz

Copy link
Copy Markdown
Contributor

Looks good. Did you manually check the behavior also?

@w3ori

w3ori commented Sep 16, 2026

Copy link
Copy Markdown
Author

It still needs some improvements, because for non nullable types it starts from year 1 when you clear it.

DateOnly
image

DateOnly?
image

@w3ori

w3ori commented Sep 18, 2026

Copy link
Copy Markdown
Author

@mckaragoz Can you check the changes please?
Default was a good idea, but wasn't working for not nullable date types, because the default for those is 0001/01/01.

@mckaragoz

Copy link
Copy Markdown
Contributor

I understand what you want to achieve. But it brings more problem. Non-nullable types can't be null. You hide the text on the component but the value still persists. So if user saves the cleared time, it sends 01/01/0001 to database, not null. It may cause data inconsistency.

Normally we should use nullable types for better results. For non-nullable types we should find another solution like optional parameter etc. The value and text should always be the same. The component should only have one truth.

@mckaragoz

Copy link
Copy Markdown
Contributor

Default was a good idea, but wasn't working for not nullable date types, because the default for those is 0001/01/01.

The idea is "dynamic" default that can change with a parameter. So the component goes to a certain date when it reset or clear. With this way both developer and user can easily understand what is the true date for the component.

@w3ori

w3ori commented Sep 21, 2026

Copy link
Copy Markdown
Author

ValueOnClear defines the replacement value assigned when the clear icon is clicked.

Nullable values clear to null by default.

<MudDateTimePicker @bind-Value="_dateTime"
                   ValueOnClear="@DateTime.Now" />

<MudDateTimePicker @bind-Value="_dateTimeOffset"
                   ValueOnClear="@DateTimeOffset.Now" />

<MudDateTimePicker @bind-Value="_dateOnly"
                   ValueOnClear="@DateOnly.FromDateTime(DateTime.Now)" />

@w3ori w3ori changed the title DateTimePicker: Add ValueOnClear Parameter as an optional default WIP: DateTimePicker: Add ValueOnClear Parameter as an optional default Sep 21, 2026
@w3ori
w3ori marked this pull request as draft September 22, 2026 06:52
@w3ori w3ori changed the title WIP: DateTimePicker: Add ValueOnClear Parameter as an optional default DateTimePicker: Add ValueOnClear Parameter as an optional default Sep 22, 2026
@w3ori
w3ori marked this pull request as ready for review September 22, 2026 08:03
@w3ori

w3ori commented Sep 22, 2026

Copy link
Copy Markdown
Author

@mckaragoz I came up with another solution from a different perspective.
Please take a look at it. It only affects where the picker opens.

@mckaragoz

Copy link
Copy Markdown
Contributor

It's nice. The only thing is the header date value. When we clear date, it correctly shows the min date on input, but when we open popover header shows today's date. I think header value should also be the same as input's. We should only change the picker time, not the header value.

…es when editing time; treat default non-nullable value as absent (BoundDateOrNull)
@w3ori

w3ori commented Sep 29, 2026

Copy link
Copy Markdown
Author

Prefer MinDate in the header and use BoundDateOrNull to treat default non‑nullable sentinels as absent, keeping the header consistent with the input and preventing the year selector from jumping to year 0001.

@w3ori

w3ori commented Oct 1, 2026

Copy link
Copy Markdown
Author

Improve test coverage

@w3ori w3ori changed the title DateTimePicker: Add ValueOnClear Parameter as an optional default DateTimePicker: Open at Today if null or default Oct 3, 2026
@mckaragoz

Copy link
Copy Markdown
Contributor

I think it's ok. Could you fix the test that failed on CI?

@w3ori

w3ori commented Oct 5, 2026

Copy link
Copy Markdown
Author

DateTimePicker header fallback change

Problem

When Value is null and no MinDate is provided, the picker header fell back to the component's internally-initialized PickerMonth (first day of the month) instead of DateTime.Today. This made the header show e.g. 2026-10-01 instead of 2026-10-05 (today), which is surprising when the consumer never set PickerMonth.

Change

  • Added an internal flag _picker_month_set to MudBaseDatePickerX.
  • The PickerMonth property setter sets _picker_month_set = true when assigned.
  • GetPickerHeaderDate(...) now uses PickerMonth only when _picker_month_set is true; otherwise it falls back to the current local date (TimeProvider.GetLocalNow().Date).

Why

This preserves the public PickerMonth API for consumers who explicitly set or bind it, while preventing internal initialization (component setting the month for view purposes) from affecting the header's intended fallback to today.

Files changed

  • src/CodeBeam.MudBlazor.Extensions/Components/DateTimePicker/MudBaseDatePickerX.cs
    • Added _picker_month_set flag and updated PickerMonth setter.
  • src/CodeBeam.MudBlazor.Extensions/Components/DateTimePicker/MudDateTimePicker.razor.cs
    • Updated GetPickerHeaderDate to consult _picker_month_set.

Testing

  • Project builds successfully.
  • Unit tests asserting header fallback to DateTime.Today should now pass for the case where PickerMonth was not explicitly set.
  • Consider adding a unit test to verify that consumer-set PickerMonth still controls the header when provided.

Notes / Alternatives

  • Alternative approaches considered: keep a private internal display-month field (never write public PickerMonth internally), or detect parameter origin via SetParametersAsync. The chosen approach is minimal and preserves external API/behavior for explicit consumer usage.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants