Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] TokenizingTextBox - Make TokenItemAdding event to be invoked even if the adding object is not a string to allow pre-check and cancellation #529

Open
kaismic opened this issue Oct 6, 2024 · 0 comments · May be fixed by #525
Labels
feature request 📬 A request for new changes to improve functionality

Comments

@kaismic
Copy link

kaismic commented Oct 6, 2024

Describe the problem

Currently, TokenizingTextBox does not fire TokenItemAdding event when the data parameter in AddTokenAsync(object data) is not a string.

So when the user selects the already added item from SuggestedItemsSource, only QuerySubmitted event is invoked and I cannot prevent the duplicate item adding.

Thus, allowing TokenItemAdding to be invoked even if the data parameter is not a string would allow me to prevent duplicate items adding by checking the adding object and cancelling the event.

Describe the solution

Modify AddTokenAsync(object data)

        if (TokenItemAdding != null)
        {
            TokenItemAddingEventArgs tiaea;
            if (data is string str)
            {
                tiaea = new TokenItemAddingEventArgs(str);
            }
            else
            {
                tiaea = new TokenItemAddingEventArgs(null) { Item = data };
            }
            await TokenItemAdding.InvokeAsync(this, tiaea);
            ...
        }

Alternatives

  1. Create new TokenizingTextBoxQuerySubmittedEventArgs class and either remove sealed from AutoSuggestBoxQuerySubmittedEventArgs and derive from it like this:
    public class TokenizingTextBoxQuerySubmittedEventArgs : AutoSuggestBoxQuerySubmittedEventArgs
    {
        public bool Cancel { get; set; }
    }

Or just create a similar class with additional Cancel property:

    public class TokenizingTextBoxQuerySubmittedEventArgs
        {
        public object ChosenSuggestion { get; }
        public string QueryText { get; }
        public bool Cancel { get; set; } = false;
    }
  1. Create a new TokenizingTextBoxQuerySubmitted event in TokenizingTextBox.Events.cs
public event TypedEventHandler<AutoSuggestBox, TokenizingTextBoxQuerySubmittedEventArgs> TokenizingTextBoxQuerySubmitted;
  1. Modify RaiseQuerySubmitted
    internal void RaiseQuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs 
 args, TokenizingTextBoxQuerySubmittedEventArgs args)
    {
        TokenizingTextBoxQuerySubmitted?.Invoke(sender, ttbArgs);
        QuerySubmitted?.Invoke(sender, args);
    }
  1. Modify AutoSuggestBox_QuerySubmitted
    private async void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
    {
        var ttbArgs = new TokenizingTextBoxQuerySubmittedEventArgs()
        {
            ChosenSuggestion = args.ChosenSuggestion,
            QueryText = args.QueryText
        };
        Owner.RaiseQuerySubmitted(sender, args, ttbArgs);
        if (ttbArgs.Cancel)
        {
            return;
        }
        object? chosenItem = null;
        ....
    }

Additional info

No response

Help us help you

None

@kaismic kaismic added the feature request 📬 A request for new changes to improve functionality label Oct 6, 2024
@kaismic kaismic changed the title [Feature] TokenizingTextBox - Make TokenItemAdding event to be invoked even if the adding object is not a string to prevent duplicate items adding [Feature] TokenizingTextBox - Make TokenItemAdding event to be invoked even if the adding object is not a string to allow pre-check and cancellation Oct 6, 2024
@michael-hawker michael-hawker transferred this issue from CommunityToolkit/WindowsCommunityToolkit Oct 8, 2024
@Arlodotexe Arlodotexe moved this to 📋 Backlog in Toolkit 8.x Oct 9, 2024
@Arlodotexe Arlodotexe moved this from 📋 Backlog to 🆕 New in Toolkit 8.x Oct 10, 2024
@Arlodotexe Arlodotexe moved this from 🆕 New to 📋 Backlog in Toolkit 8.x Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request 📬 A request for new changes to improve functionality
Projects
Status: 📋 Backlog
Development

Successfully merging a pull request may close this issue.

1 participant