Skip to content

Commit

Permalink
Made MaxLength property consistent across all platforms (#5581)
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelkang authored Apr 5, 2022
1 parent ec89cfc commit 4d9c3a4
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 5 deletions.
25 changes: 25 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,30 @@ public async Task TextTransformUpdated(string text, TextTransform transform, str
var platformText = await GetPlatformText(handler);
Assert.Equal(expected, platformText);
}

#if WINDOWS
// Only Windows needs the IsReadOnly workaround for MaxLength==0 to prevent text from being entered
[Fact]
public async Task MaxLengthIsReadOnlyValueTest()
{
Editor editor = new Editor();

await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<EditorHandler>(editor);
var platformControl = GetPlatformControl(handler);

editor.MaxLength = 0;
Assert.True(platformControl.IsReadOnly);
editor.IsReadOnly = false;
Assert.True(platformControl.IsReadOnly);

editor.MaxLength = 10;
Assert.False(platformControl.IsReadOnly);
editor.IsReadOnly = true;
Assert.True(platformControl.IsReadOnly);
});
}
#endif
}
}
25 changes: 25 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,30 @@ await InvokeOnMainThreadAsync(() =>
Assert.Equal(0, cursorPosition);
#endif
}

#if WINDOWS
// Only Windows needs the IsReadOnly workaround for MaxLength==0 to prevent text from being entered
[Fact]
public async Task MaxLengthIsReadOnlyValueTest()
{
Entry entry = new Entry();

await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<EntryHandler>(entry);
var platformControl = GetPlatformControl(handler);

entry.MaxLength = 0;
Assert.True(platformControl.IsReadOnly);
entry.IsReadOnly = false;
Assert.True(platformControl.IsReadOnly);

entry.MaxLength = 10;
Assert.False(platformControl.IsReadOnly);
entry.IsReadOnly = true;
Assert.True(platformControl.IsReadOnly);
});
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Xunit;

namespace Microsoft.Maui.DeviceTests
Expand All @@ -28,5 +29,30 @@ public async Task TextTransformUpdated(string text, TextTransform transform, str
var platformText = await GetPlatformText(handler);
Assert.Equal(expected, platformText);
}

#if WINDOWS
// Only Windows needs the IsReadOnly workaround for MaxLength==0 to prevent text from being entered
[Fact]
public async Task MaxLengthIsReadOnlyValueTest()
{
SearchBar searchBar = new SearchBar();

await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<SearchBarHandler>(searchBar);
var platformControl = GetPlatformControl(handler);

searchBar.MaxLength = 0;
Assert.True(MauiAutoSuggestBox.GetIsReadOnly(platformControl));
searchBar.IsReadOnly = false;
Assert.True(MauiAutoSuggestBox.GetIsReadOnly(platformControl));

searchBar.MaxLength = 10;
Assert.False(MauiAutoSuggestBox.GetIsReadOnly(platformControl));
searchBar.IsReadOnly = true;
Assert.True(MauiAutoSuggestBox.GetIsReadOnly(platformControl));
});
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ void OnLoaded(object sender, UI.Xaml.RoutedEventArgs e)
PlatformView?.UpdatePlaceholderColor(VirtualView);
PlatformView?.UpdateHorizontalTextAlignment(VirtualView);
PlatformView?.UpdateMaxLength(VirtualView);
PlatformView?.UpdateIsReadOnly(VirtualView);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/Core/src/Platform/Android/EditTextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public static void UpdateMaxLength(this EditText editText, int maxLength)

public static void SetLengthFilter(this EditText editText, int maxLength)
{
if (maxLength == -1)
maxLength = int.MaxValue;

var currentFilters = new List<IInputFilter>(editText.GetFilters() ?? new IInputFilter[0]);
var changed = false;

Expand All @@ -105,7 +108,7 @@ public static void SetLengthFilter(this EditText editText, int maxLength)
}
}

if (maxLength > 0)
if (maxLength >= 0)
{
currentFilters.Add(new InputFilterLengthFilter(maxLength));
changed = true;
Expand Down
34 changes: 34 additions & 0 deletions src/Core/src/Platform/Windows/MauiAutoSuggestBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#nullable enable
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui.Platform
{
public static class MauiAutoSuggestBox
{
public static void InvalidateAttachedProperties(DependencyObject obj)
{
OnIsReadOnlyPropertyChanged(obj);
}

// IsReadOnly

public static bool GetIsReadOnly(DependencyObject obj) =>
(bool)obj.GetValue(IsReadOnlyProperty);

public static void SetIsReadOnly(DependencyObject obj, bool value) =>
obj.SetValue(IsReadOnlyProperty, value);

public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.RegisterAttached(
"IsReadOnly", typeof(bool), typeof(MauiTextBox),
new PropertyMetadata(true, OnIsReadOnlyPropertyChanged));

static void OnIsReadOnlyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs? e = null)
{
var element = d as FrameworkElement;
var textBox = element?.GetDescendantByName<TextBox>("TextBox");
if (textBox != null)
textBox.IsReadOnly = true;
}
}
}
18 changes: 14 additions & 4 deletions src/Core/src/Platform/Windows/SearchBarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,25 @@ public static void UpdateVerticalTextAlignment(this AutoSuggestBox platformContr

public static void UpdateMaxLength(this AutoSuggestBox platformControl, ISearchBar searchBar)
{
var maxLength = searchBar.MaxLength;

if (maxLength == -1)
maxLength = int.MaxValue;

if (maxLength == 0)
MauiAutoSuggestBox.SetIsReadOnly(platformControl, true);
else
MauiAutoSuggestBox.SetIsReadOnly(platformControl, searchBar.IsReadOnly);

var currentControlText = platformControl.Text;

if (currentControlText.Length > searchBar.MaxLength)
platformControl.Text = currentControlText.Substring(0, searchBar.MaxLength);
if (currentControlText.Length > maxLength)
platformControl.Text = currentControlText.Substring(0, maxLength);
}

public static void UpdateIsReadOnly(this AutoSuggestBox platformControl, ISearchBar searchBar)
{
platformControl.IsEnabled = searchBar.IsReadOnly;
MauiAutoSuggestBox.SetIsReadOnly(platformControl, searchBar.IsReadOnly);
}

public static void UpdateIsTextPredictionEnabled(this AutoSuggestBox platformControl, ISearchBar searchBar)
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Platform/Windows/TextBoxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ public static void UpdateMaxLength(this TextBox textBox, ITextInput textInput)
{
var maxLength = textInput.MaxLength;

if (maxLength == 0)
textBox.IsReadOnly = true;
else
textBox.IsReadOnly = textInput.IsReadOnly;

if (maxLength == -1)
maxLength = int.MaxValue;

Expand Down

0 comments on commit 4d9c3a4

Please sign in to comment.