Skip to content

Commit 98128ef

Browse files
authored
[MacCatalyst] DatePicker null date handling (#31365)
### Description of Change Created an UITest to validate the behavior and seems to be the same on iOS MacCatalyst. When the DatePicker Date value is set to null and the date picker is opened, the picker keeps today date. ## Expected behavior - When DatePicker.Date is null, the MacCatalyst picker will display today's date when opened - The virtual view Date property remains null (as expected) - Transitions between null and specific dates work correctly - Existing functionality with non-null dates remains unchanged ### Issues Fixed Fixes #31124
2 parents e9a1ced + cee7649 commit 98128ef

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 31124, "DatePicker should maintain today's date when set to null", PlatformAffected.macOS)]
4+
public class Issue31124 : ContentPage
5+
{
6+
DatePicker _datePicker;
7+
Button _setNullButton;
8+
Label _dateLabel;
9+
10+
public Issue31124()
11+
{
12+
InitializeComponent();
13+
}
14+
15+
void InitializeComponent()
16+
{
17+
Title = "DatePicker Null Test";
18+
19+
_datePicker = new DatePicker
20+
{
21+
AutomationId = "TestDatePicker",
22+
Date = DateTime.Today
23+
};
24+
25+
_setNullButton = new Button
26+
{
27+
Text = "Set Date to Null",
28+
AutomationId = "SetNullButton"
29+
};
30+
_setNullButton.Clicked += Button_Clicked;
31+
32+
_dateLabel = new Label
33+
{
34+
Text = $"Current Date: {_datePicker.Date:d}",
35+
AutomationId = "DateLabel"
36+
};
37+
38+
// Listen for date changes to update the label
39+
_datePicker.DateSelected += (s, e) =>
40+
{
41+
_dateLabel.Text = $"Current Date: {e.NewDate:d}";
42+
};
43+
44+
Content = new VerticalStackLayout
45+
{
46+
Padding = new Thickness(20),
47+
Spacing = 20,
48+
Children =
49+
{
50+
new Label { Text = "Test: Setting DatePicker.Date to null should remove today's date" },
51+
_datePicker,
52+
_setNullButton,
53+
_dateLabel,
54+
new Label
55+
{
56+
Text = "Expected: After clicking button, date should be null",
57+
FontSize = 12,
58+
TextColor = Colors.Gray
59+
}
60+
}
61+
};
62+
}
63+
64+
void Button_Clicked(object sender, EventArgs e)
65+
{
66+
// Set datePicker.Date to null
67+
_datePicker.Date = null;
68+
69+
// Update the label to show the current date
70+
_dateLabel.Text = $"Current Date: {_datePicker.Date:d}";
71+
}
72+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Text.RegularExpressions;
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
8+
public class Issue31124 : _IssuesUITest
9+
{
10+
public Issue31124(TestDevice testDevice) : base(testDevice)
11+
{
12+
}
13+
14+
public override string Issue => "DatePicker should maintain today's date when set to null";
15+
16+
[Test]
17+
[Category(UITestCategories.DatePicker)]
18+
public void DatePickerShouldMaintainTodaysDateWhenSetToNull()
19+
{
20+
// Find the initial date label text, should show today's date
21+
var initialDateLabel = App.WaitForElement("DateLabel").GetText();
22+
Assert.That(ContainsValidDate(initialDateLabel), Is.True,
23+
"DatePicker should initially show today's date");
24+
25+
// Click the button to set date to null
26+
App.WaitForElement("SetNullButton").Click();
27+
28+
// Verify the date is empty after setting to null
29+
var dateAfterNull = App.WaitForElement("DateLabel").GetText();
30+
Assert.That(ContainsValidDate(dateAfterNull), Is.False,
31+
"DatePicker should be empty after being set to null");
32+
}
33+
34+
static bool IsValidDate(string? input)
35+
{
36+
return !string.IsNullOrWhiteSpace(input) && DateTime.TryParse(input, out _);
37+
}
38+
39+
static bool ContainsValidDate(string? labelText)
40+
{
41+
if (string.IsNullOrWhiteSpace(labelText))
42+
return false;
43+
44+
// Extract the date part after "Current Date: "
45+
var prefix = "Current Date: ";
46+
if (labelText.StartsWith(prefix))
47+
{
48+
var datePart = labelText.Substring(prefix.Length);
49+
return IsValidDate(datePart);
50+
}
51+
52+
return false;
53+
}
54+
}

0 commit comments

Comments
 (0)