Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made MaxLength property consistent across all platforms #5581

Merged
merged 7 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 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,22 @@ public async Task TextTransformUpdated(string text, TextTransform transform, str
var platformText = await GetPlatformText(handler);
Assert.Equal(expected, platformText);
}

[Fact]
public async Task MaxLengthIsReadOnlyValueTest()
{
Editor editor = new Editor();
editor.MaxLength = 0;

await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<EditorHandler>(editor);
var platformControl = GetPlatformControl(handler);
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
editor.MaxLength = 10;
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
#if WINDOWS
Assert.False(platformControl.IsReadOnly);
#endif
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
}
17 changes: 17 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,22 @@ await InvokeOnMainThreadAsync(() =>
Assert.Equal(0, cursorPosition);
#endif
}

[Fact]
public async Task MaxLengthIsReadOnlyValueTest()
{
Entry entry = new Entry();
entry.MaxLength = 0;

await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<EntryHandler>(entry);
var platformControl = GetPlatformControl(handler);
entry.MaxLength = 10;
#if WINDOWS
Assert.False(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,22 @@ public async Task TextTransformUpdated(string text, TextTransform transform, str
var platformText = await GetPlatformText(handler);
Assert.Equal(expected, platformText);
}

[Fact]
public async Task MaxLengthIsReadOnlyValueTest()
{
SearchBar searchBar = new SearchBar();
searchBar.MaxLength = 0;

await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<SearchBarHandler>(searchBar);
var platformControl = GetPlatformControl(handler);
searchBar.MaxLength = 10;
#if WINDOWS
Assert.False(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);
Comment on lines +102 to +103
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the text box below, does it re-enable after reverting MaxLength?

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);
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
}

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;
Comment on lines +131 to +132
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I set MaxLength to 0 nd then back to 10? Does it re-enable?

else
textBox.IsReadOnly = textInput.IsReadOnly;

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

Expand Down