CommunityToolkit / Windows

Collection of controls for WinUI 2, WinUI 3, and Uno Platform developers. Simplifies and demonstrates common developer tasks building experiences for Windows with .NET.
https://aka.ms/windowstoolkitdocs
Other
540 stars 70 forks source link

[Feature] Ability for TokenizingTextBox to keep the typed text after submitting or selecting #168

Open smumovic opened 1 year ago

smumovic commented 1 year ago

Describe the problem

I'm using the TokenizingTextBox to implement an advanced quick search in my application. When a user starts typing, they first get a list of fields they can search through, and when selecting one the selected field appears as a token in the search box letting the user know they are searching only for that field. They can select several fields to search through all the selected ones.

The trouble is that every time the TTB is submitted (by pressing enter or by selecting an item from the suggestions) the Text property is emptied. I have to resort to cheating it via "TokenItemAdding" and "GotFocus" events (to store the text in a temp string, and restore it after the "QuerySubmitted" event has executed, as I noticed in the source code for the TTB that the QuerySubmitted event actually empties the Text property.

Describe the solution

I found the exact place the TTB empties the Text field (lines 156 and 157 of the TokenizingTextBoxItem.cs):

QuerySubmittedMethod

My proposal is to surround the two lines that empty the text with a conditional statement dependent on a top-level property that can be set in XAML, something like KeepTextInputAfterQuerySubmit defaulted to false to keep it backward compatible.

I tested it in a branch ad it behaves exactly like I need it to behave (I commented out the two lines to get a quick feel).

I can put in the work to properly introduce the property in the XAML and all the in-between code in order to make it proper and submit a PR. I just need acceptance by the community to get the PR approved.

Alternatives

One alternative is to introduce a PostQuerySubmitted event that i can use to restor the text box content. But this is less elegant and there will be a slight delay when the filed will be empty.

Yet another alternative is to enable the component to be extended and the original QuerySubmitted method overridden, but this is much more trouble than its worth IMHO.

Additional info

This is an example from my modified component that keeps the text:

ExampleOfTtbKeepingTheText

I achieved this by commenting out the two lines and testing it in the Sample App. The text is kept in the text box after typing and selecting one of the offered options. It creates the token, but the text can be used to refine the search after the results have been loaded (in my use case).

Help us help you

Yes, I'd like to be assigned to work on this item.

ghost commented 1 year ago

Hello, 'smumovic! Thanks for submitting a new feature request. I've automatically added a vote 👍 reaction to help get things started. Other community members can vote to help us prioritize this feature in the future!

michael-hawker commented 1 year ago

@niels9001 thoughts on the property name? Maybe IsTextClearedOnSubmit?

We could theoretically add this to the TokenItemAddingEventArgs as another text property (defaults to empty), so you could do it programmatically per result or even change it to match whatever item you're choosing. Gives a few different possibilities.

https://github.com/CommunityToolkit/Windows/blob/acb5a1f9cbc166ed9c71604b66171a29b56dd3d7/components/TokenizingTextBox/src/TokenizingTextBox.cs#L494-L516

Main difficulty there is passing back the result of that to the call from AddTokenAsync so that it can be used to set the values here:

https://github.com/CommunityToolkit/Windows/blob/acb5a1f9cbc166ed9c71604b66171a29b56dd3d7/components/TokenizingTextBox/src/TokenizingTextBoxItem.AutoSuggestBox.cs#L154-L156

niels9001 commented 1 year ago

@niels9001 thoughts on the property name? Maybe IsTextClearedOnSubmit?

Sounds good to me 👍!