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] Ability for TokenizingTextBox to keep the typed text after submitting or selecting #168

Open
smumovic opened this issue Aug 2, 2023 · 3 comments · May be fixed by CommunityToolkit/WindowsCommunityToolkit#4922
Labels
enhancement New feature or request

Comments

@smumovic
Copy link

smumovic commented Aug 2, 2023

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
Copy link

ghost commented Aug 2, 2023

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!

@smumovic smumovic changed the title [Feature] Ability to instruct TokenizingTextBox to keep the typed text after submitting or selecting [Feature] Ability for TokenizingTextBox to keep the typed text after submitting or selecting Aug 2, 2023
smumovic referenced this issue in smumovic/WindowsCommunityToolkit Aug 6, 2023
@michael-hawker michael-hawker transferred this issue from CommunityToolkit/WindowsCommunityToolkit Aug 8, 2023
@michael-hawker michael-hawker added the enhancement New feature or request label Aug 8, 2023
@michael-hawker
Copy link
Member

@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.

internal async Task AddTokenAsync(object data, bool? atEnd = null)
{
if (ReadLocalValue(MaximumTokensProperty) != DependencyProperty.UnsetValue && (MaximumTokens <= 0 || MaximumTokens <= _innerItemsSource.ItemsSource.Count))
{
// No tokens for you
return;
}
if (data is string str && TokenItemAdding != null)
{
var tiaea = new TokenItemAddingEventArgs(str);
await TokenItemAdding.InvokeAsync(this, tiaea);
if (tiaea.Cancel)
{
return;
}
if (tiaea.Item != null)
{
data = tiaea.Item; // Transformed by event implementor
}
}

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:

await Owner.AddTokenAsync(chosenItem); // TODO: Need to pass index?
sender.Text = string.Empty;
Owner.Text = string.Empty;

@niels9001
Copy link
Collaborator

@niels9001 thoughts on the property name? Maybe IsTextClearedOnSubmit?

Sounds good to me 👍!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants