diff --git a/src/Blazored.Typeahead/BlazoredTypeahead.razor.cs b/src/Blazored.Typeahead/BlazoredTypeahead.razor.cs index 511f6a7..d2957dd 100644 --- a/src/Blazored.Typeahead/BlazoredTypeahead.razor.cs +++ b/src/Blazored.Typeahead/BlazoredTypeahead.razor.cs @@ -16,12 +16,25 @@ public partial class BlazoredTypeahead : ComponentBase, IDisposab private bool _eventsHookedUp = false; private ElementReference _searchInput; private ElementReference _mask; + private IEqualityComparer _valueComparer = EqualityComparer.Default; [Inject] private IJSRuntime JSRuntime { get; set; } [CascadingParameter] private EditContext CascadedEditContext { get; set; } - [Parameter] public TValue Value { get; set; } + private TValue _value; + [Parameter] public TValue Value + { + get => _value; + set + { + if (_valueComparer.Equals(_value, value) == false) + { + _value = value; + Initialize(); + } + } + } [Parameter] public EventCallback ValueChanged { get; set; } [Parameter] public Expression> ValueExpression { get; set; } @@ -130,7 +143,6 @@ protected override async Task OnAfterRenderAsync(bool firstRender) protected override void OnParametersSet() { - Initialize(); } private void Initialize() @@ -143,7 +155,7 @@ private void Initialize() private async Task RemoveValue(TValue item) { var valueList = Values ?? new List(); - if (valueList.Contains(item)) + if (valueList.Contains(item, _valueComparer)) { valueList.Remove(item); } @@ -409,7 +421,7 @@ private async Task SelectResult(TItem item) { var valueList = Values ?? new List(); - if (valueList.Contains(value)) + if (valueList.Contains(value, _valueComparer)) valueList.Remove(value); else valueList.Add(value); @@ -418,7 +430,7 @@ private async Task SelectResult(TItem item) } else { - if (Value != null && Value.Equals(value)) return; + if (_valueComparer.Equals(Value, value)) return; Value = value; await ValueChanged.InvokeAsync(value); }