From 63c0b0547fc38ecbc120e8ded409b70a7a22e2b0 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Sun, 23 Jun 2024 17:39:11 +0800 Subject: [PATCH] feat: support AcceptReturn. --- demo/Ursa.Demo/Pages/TagInputDemo.axaml | 8 +++++ src/Ursa.Themes.Semi/Controls/TagInput.axaml | 1 + src/Ursa/Controls/TagInput/TagInput.cs | 37 ++++++++++++++++---- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/demo/Ursa.Demo/Pages/TagInputDemo.axaml b/demo/Ursa.Demo/Pages/TagInputDemo.axaml index aa17cb2..730921c 100644 --- a/demo/Ursa.Demo/Pages/TagInputDemo.axaml +++ b/demo/Ursa.Demo/Pages/TagInputDemo.axaml @@ -26,5 +26,13 @@ Separator="-" Tags="{Binding DistinctTags}" /> + + diff --git a/src/Ursa.Themes.Semi/Controls/TagInput.axaml b/src/Ursa.Themes.Semi/Controls/TagInput.axaml index 0357f07..f15bf93 100644 --- a/src/Ursa.Themes.Semi/Controls/TagInput.axaml +++ b/src/Ursa.Themes.Semi/Controls/TagInput.axaml @@ -29,6 +29,7 @@ Name="{x:Static u:TagInput.PART_Watermark}" Opacity="0.5" IsVisible="False" + VerticalAlignment="Center" Text="{TemplateBinding Watermark}" /> WatermarkProperty = TextBox.WatermarkProperty.AddOwner(); + + public static readonly StyledProperty AcceptsReturnProperty = + TextBox.AcceptsReturnProperty.AddOwner(); + + public bool AcceptsReturn + { + get => GetValue(AcceptsReturnProperty); + set => SetValue(AcceptsReturnProperty, value); + } + public static readonly StyledProperty MaxCountProperty = AvaloniaProperty.Register( nameof(MaxCount), int.MaxValue); @@ -82,6 +92,7 @@ public class TagInput : TemplatedControl public TagInput() { _textBox = new TextBox(); + _textBox[!AcceptsReturnProperty] = this.GetObservable(AcceptsReturnProperty).ToBinding(); _textBox.AddHandler(KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel); _textBox.AddHandler(LostFocusEvent, OnTextBox_LostFocus, RoutingStrategies.Bubble); Items = new AvaloniaList @@ -162,7 +173,7 @@ public class TagInput : TemplatedControl switch (LostFocusBehavior) { case LostFocusBehavior.Add: - AddTags(); + AddTags(_textBox.Text); break; case LostFocusBehavior.Clear: _textBox.Text = ""; @@ -257,8 +268,20 @@ public class TagInput : TemplatedControl private void OnTextBoxKeyDown(object? sender, KeyEventArgs args) { - if (args.Key == Key.Enter) - AddTags(); + if (!AcceptsReturn && args.Key == Key.Enter) + { + AddTags(_textBox.Text); + } + else if (AcceptsReturn && args.Key==Key.Enter) + { + var texts = _textBox.Text?.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries) ?? + []; + foreach (var text in texts) + { + AddTags(text); + } + args.Handled = true; + } else if (args.Key == Key.Delete || args.Key == Key.Back) if (string.IsNullOrEmpty(_textBox.Text) || _textBox.Text?.Length == 0) { @@ -269,13 +292,13 @@ public class TagInput : TemplatedControl } } - private void AddTags() + private void AddTags(string? text) { - if (!(_textBox.Text?.Length > 0)) return; + if (!(text?.Length > 0)) return; if (Tags.Count >= MaxCount) return; string[] values; if (!string.IsNullOrEmpty(Separator)) - values = _textBox.Text.Split(new[] { Separator }, + values = text.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries); else values = new[] { _textBox.Text }; @@ -290,7 +313,7 @@ public class TagInput : TemplatedControl Tags?.Insert(index, values[i]); } - _textBox.Text = ""; + _textBox.Clear(); } public void Close(object o)