diff --git a/demo/Ursa.Demo/Pages/TagInputDemo.axaml b/demo/Ursa.Demo/Pages/TagInputDemo.axaml index fc94f33..dadccf2 100644 --- a/demo/Ursa.Demo/Pages/TagInputDemo.axaml +++ b/demo/Ursa.Demo/Pages/TagInputDemo.axaml @@ -21,6 +21,7 @@ diff --git a/src/Ursa/Controls/TagInput/LostFocusBehavior.cs b/src/Ursa/Controls/TagInput/LostFocusBehavior.cs new file mode 100644 index 0000000..a3d24a7 --- /dev/null +++ b/src/Ursa/Controls/TagInput/LostFocusBehavior.cs @@ -0,0 +1,8 @@ +namespace Ursa.Controls; + +public enum LostFocusBehavior +{ + None, + Add, + Clear, +} \ No newline at end of file diff --git a/src/Ursa/Controls/TagInput/TagInput.cs b/src/Ursa/Controls/TagInput/TagInput.cs index 920d9ac..920f01c 100644 --- a/src/Ursa/Controls/TagInput/TagInput.cs +++ b/src/Ursa/Controls/TagInput/TagInput.cs @@ -50,6 +50,7 @@ public class TagInput : TemplatedControl { _textBox = new TextBox(); _textBox.AddHandler(KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel); + _textBox.AddHandler(LostFocusEvent, OnTextBox_LostFocus, RoutingStrategies.Bubble); Items = new AvaloniaList { _textBox @@ -57,6 +58,19 @@ public class TagInput : TemplatedControl Tags = new ObservableCollection(); } + private void OnTextBox_LostFocus(object? sender, RoutedEventArgs e) + { + switch (LostFocusBehavior) + { + case LostFocusBehavior.Add: + AddTags(); + break; + case LostFocusBehavior.Clear: + _textBox.Text = ""; + break; + } + } + public static readonly StyledProperty InputThemeProperty = AvaloniaProperty.Register( nameof(InputTheme)); @@ -86,6 +100,16 @@ public class TagInput : TemplatedControl set => SetValue(SeparatorProperty, value); } + public static readonly StyledProperty LostFocusBehaviorProperty = AvaloniaProperty.Register( + nameof(LostFocusBehavior)); + + public LostFocusBehavior LostFocusBehavior + { + get => GetValue(LostFocusBehaviorProperty); + set => SetValue(LostFocusBehaviorProperty, value); + } + + public static readonly StyledProperty AllowDuplicatesProperty = AvaloniaProperty.Register( nameof(AllowDuplicates), defaultValue: true); @@ -203,31 +227,7 @@ public class TagInput : TemplatedControl { if (args.Key == Key.Enter) { - if (_textBox.Text?.Length > 0) - { - string[] values; - if (!string.IsNullOrEmpty(Separator)) - { - values = _textBox.Text.Split(new string[] { Separator }, - StringSplitOptions.RemoveEmptyEntries); - } - else - { - values = new[] { _textBox.Text }; - } - - if (!AllowDuplicates && Tags != null) - values = values.Distinct().Except(Tags).ToArray(); - - for (int i = 0; i < values.Length; i++) - { - int index = Items.Count - 1; - // Items.Insert(index, values[i]); - Tags?.Insert(index, values[i]); - } - - _textBox.Text = ""; - } + AddTags(); } else if (args.Key == Key.Delete || args.Key == Key.Back) { @@ -243,6 +243,33 @@ public class TagInput : TemplatedControl } } } + + private void AddTags() + { + if (!(_textBox.Text?.Length > 0)) return; + string[] values; + if (!string.IsNullOrEmpty(Separator)) + { + values = _textBox.Text.Split(new string[] { Separator }, + StringSplitOptions.RemoveEmptyEntries); + } + else + { + values = new[] { _textBox.Text }; + } + + if (!AllowDuplicates && Tags != null) + values = values.Distinct().Except(Tags).ToArray(); + + for (int i = 0; i < values.Length; i++) + { + int index = Items.Count - 1; + // Items.Insert(index, values[i]); + Tags?.Insert(index, values[i]); + } + + _textBox.Text = ""; + } public void Close(object o) {