Skip to content

Commit

Permalink
Merge pull request #756 from enisn/textfield-selectalltext
Browse files Browse the repository at this point in the history
Add `SelectAllTextOnFocus` to TextField
  • Loading branch information
enisn authored Sep 12, 2024
2 parents 3ef9fef + 79e92f6 commit 8e1a106
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion demo/UraniumApp/Pages/InputFields/TextFieldPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<Label Text="Simple set of TextFields." FontSize="Subtitle" Margin="20"/>

<VerticalStackLayout StyleClass="ControlPreview" Spacing="12">
<material:TextField Title="Name" />
<material:TextField Title="Name" SelectAllTextOnFocus="True" />
<material:TextField Title="Surname" />
<material:TextField Title="Age" Keyboard="Numeric" />
</VerticalStackLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,12 @@ public partial class TextField
typeof(bool),
typeof(TextField),
false);

public bool SelectAllTextOnFocus { get => (bool)GetValue(SelectAllTextOnFocusProperty); set => SetValue(SelectAllTextOnFocusProperty, value); }

public static BindableProperty SelectAllTextOnFocusProperty = BindableProperty.Create(
nameof(SelectAllTextOnFocus),
typeof(bool),
typeof(TextField),
false);
}
37 changes: 31 additions & 6 deletions src/UraniumUI.Material/Controls/TextField.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Plainer.Maui.Controls;
using Microsoft.Extensions.Logging;
using Plainer.Maui.Controls;
using System.Windows.Input;
using UraniumUI.Converters;
using UraniumUI.Material.Extensions;
Expand Down Expand Up @@ -54,11 +55,6 @@ public TextField()

partial void AfterConstructor();

protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
}

protected override void OnHandlerChanged()
{
base.OnHandlerChanged();
Expand All @@ -67,11 +63,13 @@ protected override void OnHandlerChanged()
{
EntryView.TextChanged -= EntryView_TextChanged;
EntryView.Completed -= EntryView_Completed;
EntryView.Focused -= EntryView_Focused;
}
else
{
EntryView.TextChanged += EntryView_TextChanged;
EntryView.Completed += EntryView_Completed;
EntryView.Focused += EntryView_Focused;

ApplyAttachedProperties();
}
Expand Down Expand Up @@ -102,6 +100,14 @@ private void EntryView_Completed(object sender, EventArgs e)
Completed?.Invoke(this, e);
}

private void EntryView_Focused(object sender, FocusEventArgs e)
{
if (SelectAllTextOnFocus)
{
SelectAllText();
}
}

public void ClearValue()
{
if (IsEnabled)
Expand Down Expand Up @@ -170,4 +176,23 @@ protected virtual View CreateIconClear()
contentView.SetBinding(StatefulContentView.IsVisibleProperty, new Binding(nameof(Text), converter: UraniumConverters.StringIsNotNullOrEmptyConverter, source: this));
return contentView;
}

/// <summary>
/// Input has be be already focused when this method is called.
/// </summary>
public void SelectAllText()
{
if (EntryView.Text?.Length > 0)
{
EntryView.CursorPosition = 0;
EntryView.SelectionLength = EntryView.Text.Length;

#if ANDROID
if(EntryView.Handler.PlatformView is Android.Widget.EditText editText)
{
editText.SelectAll();
}
#endif
}
}
}

0 comments on commit 8e1a106

Please sign in to comment.