feat: implement verificationcodeitem and corresponding delete operation.
This commit is contained in:
@@ -28,13 +28,13 @@ public class VerificationCode: TemplatedControl
|
||||
set => SetValue(CompleteCommandProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<int> CountOfDigitProperty = AvaloniaProperty.Register<VerificationCode, int>(
|
||||
nameof(CountOfDigit));
|
||||
public static readonly StyledProperty<int> CountProperty = AvaloniaProperty.Register<VerificationCode, int>(
|
||||
nameof(Count));
|
||||
|
||||
public int CountOfDigit
|
||||
public int Count
|
||||
{
|
||||
get => GetValue(CountOfDigitProperty);
|
||||
set => SetValue(CountOfDigitProperty, value);
|
||||
get => GetValue(CountProperty);
|
||||
set => SetValue(CountProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<char> PasswordCharProperty =
|
||||
@@ -47,19 +47,29 @@ public class VerificationCode: TemplatedControl
|
||||
set => SetValue(PasswordCharProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DirectProperty<VerificationCode, AvaloniaList<string>> DigitsProperty = AvaloniaProperty.RegisterDirect<VerificationCode, AvaloniaList<string>>(
|
||||
public static readonly DirectProperty<VerificationCode, IList<string>> DigitsProperty = AvaloniaProperty.RegisterDirect<VerificationCode, IList<string>>(
|
||||
nameof(Digits), o => o.Digits, (o, v) => o.Digits = v);
|
||||
|
||||
private AvaloniaList<string> _digits = [];
|
||||
internal AvaloniaList<string> Digits
|
||||
private IList<string> _digits = [];
|
||||
internal IList<string> Digits
|
||||
{
|
||||
get => _digits;
|
||||
set => SetAndRaise(DigitsProperty, ref _digits, value);
|
||||
}
|
||||
|
||||
public static readonly RoutedEvent<VerificationCodeCompleteEventArgs> CompleteEvent =
|
||||
RoutedEvent.Register<VerificationCode, VerificationCodeCompleteEventArgs>(
|
||||
nameof(Complete), RoutingStrategies.Bubble);
|
||||
|
||||
public event EventHandler<VerificationCodeCompleteEventArgs> Complete
|
||||
{
|
||||
add => AddHandler(CompleteEvent, value);
|
||||
remove => RemoveHandler(CompleteEvent, value);
|
||||
}
|
||||
|
||||
static VerificationCode()
|
||||
{
|
||||
CountOfDigitProperty.Changed.AddClassHandler<VerificationCode, int>((code, args) => code.OnCountOfDigitChanged(args));
|
||||
CountProperty.Changed.AddClassHandler<VerificationCode, int>((code, args) => code.OnCountOfDigitChanged(args));
|
||||
FocusableProperty.OverrideDefaultValue<VerificationCode>(true);
|
||||
}
|
||||
|
||||
@@ -73,9 +83,8 @@ public class VerificationCode: TemplatedControl
|
||||
var newValue = args.NewValue.Value;
|
||||
if (newValue > 0)
|
||||
{
|
||||
Digits = new AvaloniaList<string>(Enumerable.Repeat(string.Empty, newValue));
|
||||
Digits = new List<string>(Enumerable.Repeat(string.Empty, newValue));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
@@ -89,29 +98,48 @@ public class VerificationCode: TemplatedControl
|
||||
{
|
||||
if (e.Source is Control t)
|
||||
{
|
||||
var text = t.FindLogicalAncestorOfType<TextBox>();
|
||||
var text = t.FindLogicalAncestorOfType<VerificationCodeItem>();
|
||||
if (text != null)
|
||||
{
|
||||
text.Focus();
|
||||
_currentIndex = _itemsControl?.IndexFromContainer(text) ?? 0;
|
||||
}
|
||||
}
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
protected override void OnTextInput(TextInputEventArgs e)
|
||||
{
|
||||
base.OnTextInput(e);
|
||||
if (e.Text?.Length == 1 && _currentIndex < CountOfDigit)
|
||||
if (e.Text?.Length == 1 && _currentIndex < Count)
|
||||
{
|
||||
Digits[_currentIndex] = e.Text;
|
||||
var presenter = _itemsControl?.ContainerFromIndex(_currentIndex) as TextBox;
|
||||
|
||||
var presenter = _itemsControl?.ContainerFromIndex(_currentIndex) as VerificationCodeItem;
|
||||
if (presenter is null) return;
|
||||
_currentIndex++;
|
||||
var newPresenter = _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus();
|
||||
presenter.Text = e.Text;
|
||||
if (_currentIndex == CountOfDigit)
|
||||
Digits[_currentIndex] = e.Text;
|
||||
_currentIndex++;
|
||||
_itemsControl?.ContainerFromIndex(_currentIndex)?.Focus();
|
||||
if (_currentIndex == Count)
|
||||
{
|
||||
CompleteCommand?.Execute(Digits);
|
||||
RaiseEvent(new VerificationCodeCompleteEventArgs(Digits, CompleteEvent));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
base.OnKeyDown(e);
|
||||
if (e.Key == Key.Back && _currentIndex >= 0)
|
||||
{
|
||||
var presenter = _itemsControl?.ContainerFromIndex(_currentIndex) as VerificationCodeItem;
|
||||
if (presenter is null) return;
|
||||
Digits[_currentIndex] = string.Empty;
|
||||
presenter.Text = string.Empty;
|
||||
if (_currentIndex == 0) return;
|
||||
_currentIndex--;
|
||||
_itemsControl?.ContainerFromIndex(_currentIndex)?.Focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,14 +11,13 @@ public class VerificationCodeCollection: ItemsControl
|
||||
{
|
||||
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
|
||||
{
|
||||
return NeedsContainer<TextBox>(item, out recycleKey);
|
||||
return NeedsContainer<VerificationCodeItem>(item, out recycleKey);
|
||||
}
|
||||
|
||||
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
|
||||
{
|
||||
return new TextBox()
|
||||
return new VerificationCodeItem()
|
||||
{
|
||||
TextAlignment = TextAlignment.Center,
|
||||
[InputMethod.IsInputMethodEnabledProperty] = false,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using Avalonia.Collections;
|
||||
using Avalonia.Interactivity;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class VerificationCodeCompleteEventArgs(IList<string> code, RoutedEvent? @event) : RoutedEventArgs(@event)
|
||||
{
|
||||
public IList<string> Code { get; } = code;
|
||||
}
|
||||
26
src/Ursa/Controls/VerificationCode/VerificationCodeItem.cs
Normal file
26
src/Ursa/Controls/VerificationCode/VerificationCodeItem.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Data;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class VerificationCodeItem: TemplatedControl
|
||||
{
|
||||
public static readonly StyledProperty<string> TextProperty = AvaloniaProperty.Register<VerificationCodeItem, string>(
|
||||
nameof(Text), defaultBindingMode: BindingMode.TwoWay);
|
||||
|
||||
public string Text
|
||||
{
|
||||
get => GetValue(TextProperty);
|
||||
set => SetValue(TextProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<char> PasswordCharProperty = AvaloniaProperty.Register<VerificationCodeItem, char>(
|
||||
nameof(PasswordChar), defaultBindingMode: BindingMode.TwoWay);
|
||||
|
||||
public char PasswordChar
|
||||
{
|
||||
get => GetValue(PasswordCharProperty);
|
||||
set => SetValue(PasswordCharProperty, value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user