hdv212
hdv212

Reputation: 93

MudAutoComplete : Problem to get current text without any suggest selection

I'm using MudAutoComplete to implement suggestions based on user entering text, It works perfectly. My problem is that when user write a text & press Enter, in KeyDown eventHandler i've get the first suggestion in the list instead of current text which user entered!! For example, when user enter "Alfki", The following suggestions show : Hi Alfki Alfki Bye.. Alfki Bye 2

Now, when user hit Enter after Alfki, In KeyDown eventHandler, I've got "Hi Alfki" in bound-value instead of "Alfki"! Where is my problem and how to solve it? Thanks ...

Updated .. Here is my sample code :

<MudAutocomplete T="string" 
                 Variant="Variant.Outlined"
                 @ref="@_txtAutoCompelete"
                 ResetValueOnEmptyText="true"
                 CoerceText="true"
                 @bind-Text="_strSearchQuery"
                 SearchFunc="@SearchQuery"
                 OnKeyUp="@SearchQueryResult"
                 Adornment="Adornment.End"
                 AdornmentIcon="@Icons.Filled.Search"
                 AdornmentColor="Color.Success">
</MudAutocomplete>

@code{
private string _strSearchQuery;
    private async Task SearchQueryResult(KeyboardEventArgs e)
    {
            if (e.Code == "Enter" || e.Code == "NumpadEnter")
            {
                    string strText = _txtAutoCompelete.Text; // first suggestion, not current text!
                    string strValue = _txtAutoCompelete.Value; // first suggestion, not current text!
                    string strCurrentText = _strSearchQuery; // first suggestion, not current text!
            }
    }
}

Upvotes: 1

Views: 1189

Answers (1)

Eduardo
Eduardo

Reputation: 182

To solve your problem set CoerceText to "false" and CoerceValue to "true" (if not found)

<MudAutocomplete T="string" 
             Variant="Variant.Outlined"
             @ref="@_txtAutoCompelete"
             ResetValueOnEmptyText="true"
             CoerceText="false"
             CoarceValue="true"
             @bind-Text="_strSearchQuery"
             SearchFunc="@SearchQuery"
             OnKeyUp="@SearchQueryResult"
             Adornment="Adornment.End"
             AdornmentIcon="@Icons.Filled.Search"
             AdornmentColor="Color.Success">

Upvotes: 0

Related Questions