diff --git a/src/Material.Components.Maui/Components/TextField/TextField.cs b/src/Material.Components.Maui/Components/TextField/TextField.cs index 6080eeb..1ad25b1 100644 --- a/src/Material.Components.Maui/Components/TextField/TextField.cs +++ b/src/Material.Components.Maui/Components/TextField/TextField.cs @@ -1,5 +1,5 @@ -using Microsoft.Maui.Animations; -using System.ComponentModel; +using System.ComponentModel; +using Microsoft.Maui.Animations; namespace Material.Components.Maui; @@ -23,7 +23,14 @@ protected override void ChangeVisualState() var state = this.ViewState switch { ViewState.Disabled => "disabled", - _ => this.IsFocused ? "focused" : "normal", + _ + => this.IsFocused + ? this.IsError + ? "error_focused" + : "focused" + : this.IsError + ? "error_normal" + : "normal", }; VisualStateManager.GoToState(this, state); @@ -53,6 +60,23 @@ protected override void ChangeVisualState() else (view as IElement).OnPropertyChanged(); } + + view.TextChanged?.Invoke(view, new(ov as string, nv as string)); + view.Command?.Execute( + view.CommandParameter ?? new TextChangedEventArgs(ov as string, nv as string) + ); + } + ); + + public static readonly BindableProperty IsErrorProperty = BindableProperty.Create( + nameof(IsError), + typeof(bool), + typeof(IEditableElement), + default, + propertyChanged: (bo, ov, nv) => + { + var view = bo as TextField; + view.ChangeVisualState(); } ); @@ -108,6 +132,12 @@ public string Text set => this.SetValue(TextProperty, value); } + public bool IsError + { + get => (bool)this.GetValue(IsErrorProperty); + set => this.SetValue(IsErrorProperty, value); + } + public TextRange SelectionRange { get => (TextRange)this.GetValue(SelectionRangeProperty); @@ -132,7 +162,6 @@ public bool IsReadOnly set => this.SetValue(IsReadOnlyProperty, value); } - [EditorBrowsable(EditorBrowsableState.Never)] public Thickness EditablePadding { @@ -358,8 +387,10 @@ public void StartLabelTextAnimation() return; } - this.animationManager ??= - this.Handler.MauiContext?.Services.GetRequiredService(); + this.animationManager ??= this.Handler + .MauiContext + ?.Services + .GetRequiredService(); var start = 0f; var end = 1f; @@ -411,8 +442,10 @@ protected override void StartRippleEffect() && y <= this.Bounds.Center.Y + 20f ) { - this.animationManager ??= - this.Handler.MauiContext?.Services.GetRequiredService(); + this.animationManager ??= this.Handler + .MauiContext + ?.Services + .GetRequiredService(); this.animationManager?.Add( new Microsoft.Maui.Animations.Animation( diff --git a/src/Material.Components.Maui/Styles/TextFieldStyles.xaml b/src/Material.Components.Maui/Styles/TextFieldStyles.xaml index 16c9f3f..f815498 100644 --- a/src/Material.Components.Maui/Styles/TextFieldStyles.xaml +++ b/src/Material.Components.Maui/Styles/TextFieldStyles.xaml @@ -55,6 +55,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +