This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[iOS] Allow empty format in DatePicker (#14695)
* Allow (again) empty format in iOS DatePicker * Update Xamarin.Forms.ControlGallery.iOS/Xamarin.Forms.ControlGallery.iOS.csproj * Update Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue13577.xaml Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
- Loading branch information
1 parent
3a4f974
commit bbbe6cd
Showing
6 changed files
with
235 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using CoreGraphics; | ||
using UIKit; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.ControlGallery.iOS; | ||
using Xamarin.Forms.Controls.Issues; | ||
using Xamarin.Forms.Platform.iOS; | ||
|
||
[assembly: ExportRenderer(typeof(NullableDatePicker), typeof(_13577CustomRenderer))] | ||
namespace Xamarin.Forms.ControlGallery.iOS | ||
{ | ||
public class _13577CustomRenderer : DatePickerRenderer | ||
{ | ||
public NullableDatePicker Entry => Element as NullableDatePicker; | ||
|
||
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e) | ||
{ | ||
base.OnElementChanged(e); | ||
|
||
if (Control != null) | ||
{ | ||
AddClearButton(); | ||
|
||
// Border style; Remove corner radius and change colors | ||
Control.BorderStyle = UITextBorderStyle.Line; | ||
Control.Layer.CornerRadius = 0; | ||
Control.Layer.BorderWidth = 1; | ||
|
||
// Add padding | ||
Control.LeftView = new UIView(new CGRect(0, 0, 10, 0)); | ||
Control.LeftViewMode = UITextFieldViewMode.Always; | ||
Control.RightView = new UIView(new CGRect(0, 0, 10, 0)); | ||
Control.RightViewMode = UITextFieldViewMode.Always; | ||
|
||
if (e.NewElement != null) | ||
{ | ||
try | ||
{ | ||
if (UIDevice.CurrentDevice.CheckSystemVersion(14, 0)) | ||
{ | ||
UIDatePicker picker = (UIDatePicker)Control.InputView; | ||
picker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels; | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
// Do nothing | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void AddClearButton() | ||
{ | ||
if (Control.InputAccessoryView is UIToolbar originalToolbar && originalToolbar.Items.Length <= 2) | ||
{ | ||
var clearButton = new UIBarButtonItem("Delete", UIBarButtonItemStyle.Plain, (sender, ev) => | ||
{ | ||
NullableDatePicker baseDatePicker = this.Element as NullableDatePicker; | ||
Element.Unfocus(); | ||
baseDatePicker.CleanDate(); | ||
}); | ||
|
||
var newItems = new List<UIBarButtonItem>(); | ||
foreach (var item in originalToolbar.Items) | ||
{ | ||
newItems.Add(item); | ||
} | ||
|
||
newItems.Insert(0, clearButton); | ||
|
||
originalToolbar.Items = newItems.ToArray(); | ||
originalToolbar.SetNeedsDisplay(); | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue13577.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<local:TestContentPage | ||
xmlns="http://xamarin.com/schemas/2014/forms" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:d="http://xamarin.com/schemas/2014/forms/design" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d" | ||
Title="Test 13577" xmlns:local="using:Xamarin.Forms.Controls" | ||
xmlns:issues="using:Xamarin.Forms.Controls.Issues" | ||
x:Class="Xamarin.Forms.Controls.Issues.Issue13577"> | ||
<StackLayout> | ||
<Label | ||
Padding="12" | ||
BackgroundColor="Black" | ||
TextColor="White" | ||
Text="If you can clear the selected Date, the test has passed."/> | ||
<issues:NullableDatePicker | ||
Format="dd-MM-yyyy" | ||
NullableDate="{Binding NullableDate}" /> | ||
</StackLayout> | ||
</local:TestContentPage> |
126 changes: 126 additions & 0 deletions
126
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue13577.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using Xamarin.Forms.CustomAttributes; | ||
using Xamarin.Forms.Internals; | ||
|
||
#if UITEST | ||
using Xamarin.UITest; | ||
using NUnit.Framework; | ||
using Xamarin.Forms.Core.UITests; | ||
#endif | ||
|
||
namespace Xamarin.Forms.Controls.Issues | ||
{ | ||
[Preserve(AllMembers = true)] | ||
[Issue(IssueTracker.Github, 13577, | ||
"[Bug][DatePicker][XF5] DatePicker empty format now invalid", | ||
PlatformAffected.iOS)] | ||
public partial class Issue13577 : TestContentPage | ||
{ | ||
public Issue13577() | ||
{ | ||
#if APP | ||
InitializeComponent(); | ||
#endif | ||
} | ||
|
||
protected override void Init() | ||
{ | ||
BindingContext = new Issue13577ViewModel(); | ||
} | ||
} | ||
|
||
public class Issue13577ViewModel | ||
{ | ||
public Issue13577ViewModel() | ||
{ | ||
NullableDate = null; | ||
} | ||
|
||
public DateTime? NullableDate { get; set; } | ||
} | ||
|
||
public class NullableDatePicker : Xamarin.Forms.DatePicker | ||
{ | ||
public static readonly BindableProperty NullableDateProperty = BindableProperty.Create("NullableDate", typeof(DateTime?), typeof(NullableDatePicker), null, BindingMode.TwoWay); | ||
|
||
public DateTime? NullableDate | ||
{ | ||
get | ||
{ | ||
return (DateTime?)GetValue(NullableDateProperty); | ||
} | ||
set | ||
{ | ||
if (value != NullableDate) | ||
{ | ||
SetValue(NullableDateProperty, value); | ||
UpdateDate(); | ||
} | ||
} | ||
} | ||
|
||
public void CleanDate() | ||
{ | ||
NullableDate = null; | ||
UpdateDate(); | ||
} | ||
|
||
public void AssignValue() | ||
{ | ||
NullableDate = Date; | ||
UpdateDate(); | ||
} | ||
|
||
protected override void OnBindingContextChanged() | ||
{ | ||
base.OnBindingContextChanged(); | ||
UpdateDate(); | ||
} | ||
|
||
protected override void OnPropertyChanged(string propertyName = null) | ||
{ | ||
base.OnPropertyChanged(propertyName); | ||
|
||
if (propertyName == IsFocusedProperty.PropertyName) | ||
{ | ||
if (!IsFocused) | ||
{ | ||
OnPropertyChanged(DateProperty.PropertyName); | ||
} | ||
} | ||
|
||
if (propertyName == DateProperty.PropertyName) | ||
{ | ||
NullableDate = Date; | ||
} | ||
|
||
if (propertyName == NullableDateProperty.PropertyName) | ||
{ | ||
if (NullableDate.HasValue) | ||
{ | ||
Date = NullableDate.Value; | ||
Format = "dd-MM-yyyy"; | ||
} | ||
else | ||
{ | ||
Format = " "; | ||
} | ||
} | ||
} | ||
|
||
private void UpdateDate() | ||
{ | ||
if (NullableDate.HasValue) | ||
{ | ||
Date = NullableDate.Value; | ||
Format = "dd-MM-yyyy"; | ||
} | ||
else | ||
{ | ||
Format = " "; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters