Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Implemented MaxLength property on Entry and Editor #1880

Merged

Conversation

jfversluis
Copy link
Member

@jfversluis jfversluis commented Feb 14, 2018

Description of Change

Implementation of MaxLength on both the Entry as well as the Editor (by implementing it on the InputView) for all platforms supported today. Fixes #1663.

No unit tests have been added since I would just be testing the getter/setter of the property.

Bugs Fixed

  • N/A

API Changes

Added:

  • static BindableProperty InputView.MaxLengthProperty;
  • int InputView.MaxLength { get; set; } //Bindable Property, default value int.MaxValue

Behavioral Changes

There shouldn't be any behavioral changes by just upgrading. But they now have the ability to limit the input length of the Editor and Entry controls.

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Rebased on top of master at time of PR
  • Changes adhere to coding standard
  • Consolidate commits as makes sense

@@ -5,6 +5,14 @@ public class InputView : View
public static readonly BindableProperty KeyboardProperty = BindableProperty.Create("Keyboard", typeof(Keyboard), typeof(InputView), Keyboard.Default,
coerceValue: (o, v) => (Keyboard)v ?? Keyboard.Default);

public static readonly BindableProperty MaxLengthProperty = BindableProperty.Create("MaxLength", typeof(int), typeof(int), int.MaxValue);
Copy link
Member

Choose a reason for hiding this comment

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

I'd use -1 as default value

Copy link
Member

Choose a reason for hiding this comment

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

use nameof(MaxLength)

Copy link
Member

Choose a reason for hiding this comment

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

we usually use tabs for indentation

Copy link
Member Author

Choose a reason for hiding this comment

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

In the #1663 we sort of decided to go with int.MaxValue. What would be the benefit of using -1?
Fixed nameof and tabs.

Copy link
Member

@StephaneDelcroix StephaneDelcroix Feb 15, 2018

Choose a reason for hiding this comment

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

I'll discuss the defaultValue with @hartez. both are fine, both make sense. But having Int32.MaxValue prevents us from having a really long text in, while -1 does not.

@@ -60,6 +62,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
Control.Changed += HandleChanged;
Control.Started += OnStarted;
Control.Ended += OnEnded;
Control.ShouldChangeText += ShouldChangeText;
Copy link
Member

Choose a reason for hiding this comment

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

it looks like you do not do anything on property change

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure what you mean here. The event is hooked up? If you mean that I don't act on the MaxLength being changed you're right. Wasn't sure what to do with that. The logical thing to do would be to cut off the current value if its length exceeds the new MaxLength value?

Copy link
Member

@StephaneDelcroix StephaneDelcroix Feb 15, 2018

Choose a reason for hiding this comment

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

The logical thing to do would be to cut off the current value if its length exceeds the new MaxLength value

that's what I'd do

Copy link
Member Author

Choose a reason for hiding this comment

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

Definitely makes sense, implemented it

@rmarinho
Copy link
Member

@jfversluis the build is failing in the Tizen platform

Xamarin.Forms.Platform.Tizen\Renderers\EditorRenderer.cs (31, 31)
Xamarin.Forms.Platform.Tizen\Renderers\EditorRenderer.cs(31,31): Error CS1503: Argument 1: cannot convert from 'method group' to 'Func<Entry, string, string>'

@jfversluis
Copy link
Member Author

@rmarinho should be resolved now

@@ -35,6 +35,9 @@ protected override void Build (StackLayout stackLayout)
var textColorDisabledContainer = new ViewContainer<Editor> (Test.Editor.TextColor,
new Editor { Text = "I should have the default disabled text color", TextColor = Color.Red, IsEnabled = false });

var maxLengthContainer = new ViewContainer<Editor>(Test.Editor.MaxLength,
new Editor { MaxLength = 3 });

Choose a reason for hiding this comment

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

Funky whitespace for fun and profit :)


Control?.SetFilters(currentFilters.ToArray());

if (Control?.Text.Length > Element.MaxLength)

Choose a reason for hiding this comment

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

You fetch Control.Text 3 times here. This causes 3 GetValue calls to be sent down to BindableObject. In this case it should be trivial to use a temporary variable to avoid that :)


Control?.SetFilters(currentFilters.ToArray());

if (Control?.Text.Length > Element.MaxLength)

Choose a reason for hiding this comment

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

GetValue calls here as well


void UpdateMaxLength()
{
if (Control?.StringValue?.Length > Element?.MaxLength)

Choose a reason for hiding this comment

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

Multiple calls to Control.StringValue again :)


void UpdateMaxLength()
{
if (Control?.StringValue?.Length > Element?.MaxLength)

Choose a reason for hiding this comment

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

Here too

{
Control.MaxLength = Element.MaxLength;

if (Control.Text.Length > Element.MaxLength)

Choose a reason for hiding this comment

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

And here

@@ -105,6 +108,14 @@ void UpdateTextColor()
Control.UpdateDependencyColor(System.Windows.Controls.Control.ForegroundProperty, Element.TextColor);
}

void UpdateMaxLength()
{
Control.MaxLength = Element.MaxLength;

Choose a reason for hiding this comment

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

Not going to call anymore of these out, but there are quite a few more :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I think you got pretty much all of them? :P But thanks! Learning a lot here :)

Choose a reason for hiding this comment

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

Yeah its fun, just doing these little optimizations on our first pass once we realized this was a problem gave Forms a fairly sizeable performance boost. Now its about making sure we dont end up needing to do that again :)

@jassmith
Copy link

Scheduling builds now

@jfversluis
Copy link
Member Author

That’s curious. Does anyone have details on the failing build?

@rmarinho
Copy link
Member

@jfversluis is failing on docs, you need to generate the docs needed and commit also.

@jfversluis
Copy link
Member Author

@rmarinho done!

@@ -451,9 +451,6 @@
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute>
Copy link
Member

Choose a reason for hiding this comment

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

no need to commit this, build in Debug, then generate docs

Copy link
Member Author

Choose a reason for hiding this comment

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

@rmarinho I'm not sure what went wrong..? Rebuild the whole solution in Debug and then commit the results? Or what am I supposed to do here?

@@ -512,9 +512,6 @@
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute>
Copy link
Member

Choose a reason for hiding this comment

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

no need to commit this

@@ -3,13 +3,13 @@
<Assembly Name="Xamarin.Forms.Core" Version="2.0.0.0">
<Attributes>
<Attribute>
Copy link
Member

Choose a reason for hiding this comment

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

no need to commit this

@@ -3,13 +3,13 @@
<Assembly Name="Xamarin.Forms.Maps" Version="2.0.0.0">
<Attributes>
<Attribute>
<AttributeName>System.Diagnostics.Debuggable(System.Diagnostics.DebuggableAttribute+DebuggingModes.Default | System.Diagnostics.DebuggableAttribute+DebuggingModes.DisableOptimizations | System.Diagnostics.DebuggableAttribute+DebuggingModes.EnableEditAndContinue | System.Diagnostics.DebuggableAttribute+DebuggingModes.IgnoreSymbolStoreSequencePoints)</AttributeName>
Copy link
Member

Choose a reason for hiding this comment

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

No need to commit this or the others similar

@jfversluis
Copy link
Member Author

@rmarinho better like this? :)

@jfversluis
Copy link
Member Author

Is there anything I need to do for the failing UI tests?

@rmarinho
Copy link
Member

@jfversluis no it's good

@jassmith jassmith merged commit 1f770f6 into xamarin:master Feb 19, 2018
@jfversluis jfversluis deleted the feature/1663-entry-maxlength branch February 19, 2018 22:30
jsuarezruiz added a commit to XamarinFormsCommunity/Xamarin.Forms that referenced this pull request Feb 28, 2018
* 'master' of https://github.com/xamarin/Xamarin.Forms: (23 commits)
  [C] use direct cast
  [Core, iOS, Android, UWP, WPF] Hide scroll view scroll bars (xamarin#1910)
  Allow users to specify resolution method for handlers, effects, and services (xamarin#1870) fixes xamarin#1739
  [Build] Update submodule
  Capitalization keyboard flag additions for Entry/Editor (xamarin#1683) (xamarin#1833)
  [Build] Don't specify .net sdk version
  Simplify event raising invocation pattern (xamarin#1971)
  [iOS Maps] Pin rendering customization (xamarin#1065)
  set csharp_space_after_keywords_in_control_flow_statements to true to fit our design guide lines (xamarin#1964)
  [iOS] Add shadow effect (xamarin#1896)
  Added support for ListView full width separators on iOS (xamarin#1854) fixes xamarin#1665
  Support CascadeInputTransparent to Tizen (xamarin#1916)
  [Android]?Remove UserVisibleHint (xamarin#1550) fixes xamarin#1438
  [iOS] ViewDidLayoutSubviews after removing page (xamarin#1532) fixes xamarin#1426
  Use relative URL to support recursive checkout in VSTS (xamarin#1926)
  [UITest] Fix test for UITest package update (xamarin#1923)
  Implemented MaxLength property on Entry and Editor (xamarin#1880)
  [Build]Fix master and build (xamarin#1920)
  [Build] Fix windows cert
  Fix to absolute URL
  ...

# Conflicts:
#	Xamarin.Forms.Controls/CoreGalleryPages/EditorCoreGalleryPage.cs
#	Xamarin.Forms.Controls/CoreGalleryPages/EntryCoreGalleryPage.cs
#	Xamarin.Forms.Core/InputView.cs
#	Xamarin.Forms.CustomAttributes/TestAttributes.cs
#	Xamarin.Forms.Platform.Android/Renderers/EditorRenderer.cs
#	Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs
#	Xamarin.Forms.Platform.MacOS/Renderers/EditorRenderer.cs
#	Xamarin.Forms.Platform.MacOS/Renderers/EntryRenderer.cs
#	Xamarin.Forms.Platform.Tizen/Renderers/EditorRenderer.cs
#	Xamarin.Forms.Platform.Tizen/Renderers/EntryRenderer.cs
#	Xamarin.Forms.Platform.WPF/Renderers/EditorRenderer.cs
#	Xamarin.Forms.Platform.WPF/Renderers/EntryRenderer.cs
#	Xamarin.Forms.Platform.iOS/Renderers/EditorRenderer.cs
#	Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs
@samhouts samhouts added the API-change Heads-up to reviewers that this PR may contain an API change label Apr 3, 2018
@samhouts samhouts added this to the 3.0.0 milestone May 5, 2018
@samhouts samhouts modified the milestones: 3.0.0, 2.5.0 Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API-change Heads-up to reviewers that this PR may contain an API change community-sprint F100 t/enhancement ➕
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enhancement] Entry.MaxLength
5 participants