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

Max length reached behavior - #451

Merged
Gerald Versluis (jfversluis) merged 11 commits into
xamarin:mainfrom
jBijsterboschNL:max-length-reached-behavior
Oct 20, 2020
Merged

Max length reached behavior#451
Gerald Versluis (jfversluis) merged 11 commits into
xamarin:mainfrom
jBijsterboschNL:max-length-reached-behavior

Conversation

@jBijsterboschNL

Copy link
Copy Markdown
Contributor

Description of Change

Implemented MaxLengthReachedBehavior. When the MaxLength on an Entry field has been reached, any configured EventHandler gets invoked and a Command gets Executed.

Bugs Fixed

API Changes

Added:

public class MaxLengthReachedBehavior : BaseBehavior<InputView>
{
	public static readonly BindableProperty CommandProperty
		= BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(UserStoppedTypingBehavior));

	public static readonly BindableProperty ShouldDismissKeyboardAutomaticallyProperty
		= BindableProperty.Create(nameof(ShouldDismissKeyboardAutomatically), typeof(bool), typeof(UserStoppedTypingBehavior), false);

	public event EventHandler<MaxLengthReachedEventArgs> MaxLengthReached;

	public ICommand Command
	{
		get => (ICommand)GetValue(CommandProperty);
		set => SetValue(CommandProperty, value);
	}

	public bool ShouldDismissKeyboardAutomatically
	{
		get => (bool)GetValue(ShouldDismissKeyboardAutomaticallyProperty);
		set => SetValue(ShouldDismissKeyboardAutomaticallyProperty, value);
	}

	...
}

Behavioral Changes

None

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Updated documentation

@jBijsterboschNL

Copy link
Copy Markdown
Contributor Author

I've noticed that the unit test UserStoppedTypingBehavior_Tests.ShouldDismissKeyboardWhenTimeThresholdHasExpired fails due to a Device.BeginInvokeOnMainThread() call being made inside the UserStoppedTypingBehavior. Andrei (@AndreiMisiukevich) could you have a look at this on what to do with this one?

@jBijsterboschNL

Copy link
Copy Markdown
Contributor Author

Also I've added a MaxLengthReachedEventArgs class inside the *.Behaviors namespace. Not sure if this is in the right namespace? And I would also like to discuss if I should even implement an EventHandler on a Behavior or not. The reason for this is that I could add an event handler that will set focus to another input field on the screen by implementing this MaxLengtReached event on the page itself, instead of from a Command execution from the ViewModel. Maybe someone has a better suggestion for this?

Comment thread XamarinCommunityToolkit/Xamarin.CommunityToolkit.csproj Outdated
Comment thread XamarinCommunityToolkit/Behaviors/MaxLengthReachedBehavior.shared.cs Outdated
Comment thread XamarinCommunityToolkit/Behaviors/MaxLengthReachedBehavior.shared.cs Outdated
Comment thread XamarinCommunityToolkit/Behaviors/MaxLengthReachedBehavior.shared.cs Outdated
Comment thread XamarinCommunityToolkit/Behaviors/MaxLengthReachedBehavior.shared.cs Outdated
Comment thread XamarinCommunityToolkit/Xamarin.CommunityToolkit.csproj Outdated
Comment thread XamarinCommunityToolkitSample/ViewModels/Behaviors/BehaviorsGalleryViewModel.cs Outdated

@pictos Pedro Jesus (pictos) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey, John Bijsterbosch (@jBijsterboschNL) good to see another PR from you! For now, I just have this small observation, let me know what do you think about it.

Comment thread XamarinCommunityToolkit/Behaviors/MaxLengthReachedBehavior.shared.cs Outdated
@AndreiMisiukevich

Copy link
Copy Markdown
Contributor

Pedro Jesus (@pictos) could you please take a look at his PR again? 👍

@pictos Pedro Jesus (pictos) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

John Bijsterbosch (@jBijsterboschNL) this is more a question than a request change. When I get the MaxLengt limit and go back to the entry, and type again the keyboard isn't dismissed. Is that expected? Please see the gif below to see how I tested.

ezgif com-gif-maker

Doing more investigation, when the MaxLength is reached, the view doesn't trigger the InputView.TextProperty.PropertChanged so the method OnTextPropertyChanged isn't called. Said that, to me it's ok to leave this way, but if you want to try something please let me know.

var newTextValue = View.Text.Substring(0, View.MaxLength);

maxLengthReachedEventManager.RaiseEvent(this, new MaxLengthReachedEventArgs(newTextValue), nameof(MaxLengthReached));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You have 3 tabs here, can you please remove it?

Suggested change

@jfversluis Gerald Versluis (jfversluis) added a/behaviors This issue/PR is related to behaviors api-change 🚨 labels Oct 20, 2020
@jfversluis

Copy link
Copy Markdown
Member

I think this has been discussed enough for now, let's merge it and see if there is anything to do after.

@jfversluis
Gerald Versluis (jfversluis) merged commit 8649546 into xamarin:main Oct 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

a/behaviors This issue/PR is related to behaviors api-change 🚨

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] Implement MaxLengthReachedBehavior

5 participants