Skip to content

DateTimePicker: Add ValueOnClear Parameter as an optional default - #644

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

w3ori wants to merge 23 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 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 65.48%. Comparing base (7b5faf7) to head (88ddd02).

Files with missing lines Patch % Lines
...mponents/DateTimePicker/MudDateTimePicker.razor.cs 50.00% 0 Missing and 1 partial ⚠️

❌ Your patch check has failed because the patch coverage (66.66%) 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.48%   +0.06%     
==========================================
  Files         111      111              
  Lines        8384     8385       +1     
  Branches     1480     1480              
==========================================
+ Hits         5485     5491       +6     
+ Misses       2391     2387       -4     
+ Partials      508      507       -1     

☔ 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.

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