MudBlazor
MudBlazor copied to clipboard
Entered Value in MudAutocomplete does not save
Bug type
Component
Component name
MudAutocomplete
What happened?
in MudAutocomplete component with defined SearchFunc, when the entered value (like ap) matches part of Dropdown Items text (like apple), then the entered value doesn't save to Bound field.(CoerceValue="true" and Immediate="true") for example you type "app" and want to save it(app). because the filtered dropdown items contain apple word and app matches 3 characters of apple,you can not save app.
Expected behavior
with CoerceValue="true" and Immediate="true, it is expected any entered text into Editable section can be saved via Bind-Value property.
Reproduction link
https://try.mudblazor.com/snippet/wacwkUlZgrgKCJvY
Reproduction steps
1.Create a New Blazor Webassembly Project 2.Add MudAutoComplete Component
<MudAutocomplete T="string" Label="Name" Clearable="true" @bind-Value="@Name" Variant="Variant.Outlined" CoerceValue="true" ResetValueOnEmptyText="true"
CoerceText="false" SearchFunc="@SearchForName" Immediate="true" />
<MudTextField T="string" @bind-Value="@Name"></MudTextField>
@code{
private IEnumerable<string> items=new List<string>
{
"apple","banana","orange","pear"
};
public string Name {get;set;}
private Task<IEnumerable<string>> SearchForName(string name)
{
if (string.IsNullOrEmpty(name)) return Task.FromResult(items);
var result = items.Where(i => i.ToLower().Contains(name.ToLower()));
return Task.FromResult(result);
}
}
Relevant log output
No response
Version (bug)
6.0.11-dev.4
Version (working)
No response
What browsers are you seeing the problem on?
Firefox, Chrome, Microsoft Edge
On what operating system are you experiencing the issue?
Windows
Pull Request
- [x] I would like to do a Pull Request
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
As a workaround you can use @bind-Text.
this workaround doesn't work:
using @bind-Text, actually you can not use any item from dropdown menu.
in fact,the component is not an Autocomplete anymore.
this workaround doesn't work: using
@bind-Text, actually you can not use any item from dropdown menu. in fact,the component is not anAutocompleteanymore.
No i mean use both https://try.mudblazor.com/snippet/macckAcYfSqtHubR
Thanks for your reply. but this way, you can not select any items from drop down menu. let say you want to choose apple item from drop down: if you click it from opened menu, nothing happens and text section remains empty. thank you.
I've asked about this issue before, but never received an answer. In the past this would have worked. https://github.com/MudBlazor/MudBlazor/discussions/4134
Hi. the action I did that may also help you:
1-download source code of MudBlazor
2-call await CoerceValueToText(); in ChangeMenu function in Autocomplete.cs Source file. (before await CoerceTextToValue();)
3.compile Source code of Mudblazor and use it in my project
4-set CoerceValue="true" in MudAutComplet component.
thanks.
Thanks @hamiddd1980 ! That worked for me as well.
The ChangeMenu fix described above didn't do anything for my scenario. In version 6.1.2 I made the following change in the OnSearchAsync() method in MudAutocomplete.razor.cs which made it work for me.
...
// HACK: Fix for value not being coerced when search result isn't empty.
//if (_items?.Length == 0)
if (!CoerceText)
{
await CoerceValueToText();
StateHasChanged();
return;
}
...
I'm guessing the original check for _items.Length == 0 was there to avoid an issue when CoerceText is being used, giving it precedence over CoerceValue. The fix I made only works if CoerceText is set to false. Without knowing the original intent of that line I hesitate to just remove it. Maybe the 2 coercion options should be mutually exclusive?