From 7b3b18a0bcf704c28c19f9c1d81f9732cdb57318 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Thu, 18 Jul 2024 21:59:55 +0800 Subject: [PATCH 1/2] fix: fix date picker lost focus behavior. --- .../Controls/DateTimePicker/DatePicker.cs | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Ursa/Controls/DateTimePicker/DatePicker.cs b/src/Ursa/Controls/DateTimePicker/DatePicker.cs index eef47d9..b70c152 100644 --- a/src/Ursa/Controls/DateTimePicker/DatePicker.cs +++ b/src/Ursa/Controls/DateTimePicker/DatePicker.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Runtime.CompilerServices; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Metadata; @@ -116,7 +117,13 @@ public class DatePicker: DatePickerBase, IClearControl SetCurrentValue(IsDropdownOpenProperty, true); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void OnTextChanged(object sender, TextChangedEventArgs e) + { + SetSelectedDate(true); + } + + private void SetSelectedDate(bool fromText = false) { if (string.IsNullOrEmpty(_textBox?.Text)) { @@ -145,6 +152,15 @@ public class DatePicker: DatePickerBase, IClearControl } _calendar?.MarkDates(startDate: date, endDate: date); } + else + { + SetCurrentValue(SelectedDateProperty, null); + if (!fromText) + { + _textBox?.SetValue(TextBox.TextProperty, null); + } + _calendar?.ClearSelection(); + } } } @@ -158,7 +174,14 @@ public class DatePicker: DatePickerBase, IClearControl } SetCurrentValue(IsDropdownOpenProperty, true); } - + + protected override void OnLostFocus(RoutedEventArgs e) + { + base.OnLostFocus(e); + SetCurrentValue(IsDropdownOpenProperty, false); + SetSelectedDate(); + } + protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.Escape) @@ -186,6 +209,6 @@ public class DatePicker: DatePickerBase, IClearControl public void Clear() { - + SetCurrentValue(SelectedDateProperty, null); } } \ No newline at end of file From 35933eb1cdd8a06d970f061fd67db41a648382e4 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Fri, 19 Jul 2024 00:46:15 +0800 Subject: [PATCH 2/2] feat: make sure clear button works. make sure text cleared if lost focus with invalid data. --- demo/Ursa.Demo/Pages/DatePickerDemo.axaml | 4 +- .../Controls/DatePicker.axaml | 14 +- .../Controls/DateRangePicker.axaml | 8 +- .../DateTimePicker/DateRangePicker.cs | 136 ++++++++++-------- 4 files changed, 92 insertions(+), 70 deletions(-) diff --git a/demo/Ursa.Demo/Pages/DatePickerDemo.axaml b/demo/Ursa.Demo/Pages/DatePickerDemo.axaml index bcf1797..0319ad6 100644 --- a/demo/Ursa.Demo/Pages/DatePickerDemo.axaml +++ b/demo/Ursa.Demo/Pages/DatePickerDemo.axaml @@ -8,7 +8,7 @@ - - + + diff --git a/src/Ursa.Themes.Semi/Controls/DatePicker.axaml b/src/Ursa.Themes.Semi/Controls/DatePicker.axaml index 518c63c..6ddb92c 100644 --- a/src/Ursa.Themes.Semi/Controls/DatePicker.axaml +++ b/src/Ursa.Themes.Semi/Controls/DatePicker.axaml @@ -1,6 +1,7 @@ @@ -30,7 +31,7 @@ + ColumnDefinitions="*, Auto">