diff --git a/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml b/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml index 437e85b..68336d5 100644 --- a/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml +++ b/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml @@ -10,7 +10,7 @@ x:Class="Ursa.Demo.Pages.VerificationCodeDemo"> - + diff --git a/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml.cs b/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml.cs index 61aecec..5933b5c 100644 --- a/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml.cs +++ b/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml.cs @@ -1,4 +1,5 @@ -using Avalonia; +using System; +using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; using Ursa.Controls; diff --git a/src/Ursa/Controls/VerificationCode/VerificationCode.cs b/src/Ursa/Controls/VerificationCode/VerificationCode.cs index cc1e9e1..8253898 100644 --- a/src/Ursa/Controls/VerificationCode/VerificationCode.cs +++ b/src/Ursa/Controls/VerificationCode/VerificationCode.cs @@ -47,6 +47,16 @@ public class VerificationCode: TemplatedControl set => SetValue(PasswordCharProperty, value); } + public static readonly StyledProperty ModeProperty = + AvaloniaProperty.Register( + nameof(Mode), defaultValue: VerificationCodeMode.Digit | VerificationCodeMode.Letter); + + public VerificationCodeMode Mode + { + get => GetValue(ModeProperty); + set => SetValue(ModeProperty, value); + } + public static readonly DirectProperty> DigitsProperty = AvaloniaProperty.RegisterDirect>( nameof(Digits), o => o.Digits, (o, v) => o.Digits = v); @@ -113,9 +123,10 @@ public class VerificationCode: TemplatedControl base.OnTextInput(e); if (e.Text?.Length == 1 && _currentIndex < Count) { - var presenter = _itemsControl?.ContainerFromIndex(_currentIndex) as VerificationCodeItem; if (presenter is null) return; + char c = e.Text[0]; + if (!Valid(c, this.Mode)) return; presenter.Text = e.Text; Digits[_currentIndex] = e.Text; _currentIndex++; @@ -128,6 +139,19 @@ public class VerificationCode: TemplatedControl } } + private bool Valid(char c, VerificationCodeMode mode) + { + bool isDigit = char.IsDigit(c); + bool isLetter = char.IsLetter(c); + return mode switch + { + VerificationCodeMode.Digit => isDigit, + VerificationCodeMode.Letter => isLetter, + VerificationCodeMode.Digit | VerificationCodeMode.Letter => isDigit || isLetter, + _ => true + }; + } + protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); diff --git a/src/Ursa/Controls/VerificationCode/VerificationCodeMode.cs b/src/Ursa/Controls/VerificationCode/VerificationCodeMode.cs new file mode 100644 index 0000000..6caf485 --- /dev/null +++ b/src/Ursa/Controls/VerificationCode/VerificationCodeMode.cs @@ -0,0 +1,8 @@ +namespace Ursa.Controls; + +[Flags] +public enum VerificationCodeMode +{ + Letter = 1, + Digit = 2, +} \ No newline at end of file