@@ -24,6 +24,8 @@ public partial class Typeahead<TItem, TValue> : StandardComponent
2424{
2525 private readonly DebounceAction < string ? > _debouncer = new ( ) ;
2626 private readonly LoadingState _loadingState = new ( ) ;
27+ private bool _suppressValueLoadedEvent ;
28+ private bool _previousDisabled = true ;
2729
2830 /// <summary>
2931 /// Gets or sets the cascading <see cref="EditContext"/> for form validation.
@@ -268,20 +270,17 @@ protected override void OnParametersSet()
268270 . AddStyle ( MenuStyle )
269271 . ToString ( ) ;
270272 } ) ;
271- }
272273
273- /// <inheritdoc />
274- protected override async Task OnInitializedAsync ( )
275- {
276- await base . OnInitializedAsync ( ) ;
277-
278- // load items if an item loader is provided
279- // will overwrite Items parameter if both are set
280- if ( ! Disabled && ItemLoader != null )
274+ // load items when component transitions from disabled to enabled
275+ if ( _previousDisabled && ! Disabled && ItemLoader != null && CurrentItems . Count == 0 )
281276 ExecuteAfterRender ( LoadItems ) ;
277+
278+ _previousDisabled = Disabled ;
282279 }
283280
284281
282+
283+
285284 /// <summary>
286285 /// Performs a search using the current search text and updates the search results.
287286 /// </summary>
@@ -353,6 +352,7 @@ public async Task SelectResult(TItem item)
353352 {
354353 valueList . Add ( value ) ;
355354 await ValuesChanged . InvokeAsync ( valueList ) ;
355+ _suppressValueLoadedEvent = true ;
356356 await SelectionAdded . InvokeAsync ( item ) ;
357357 }
358358 else
@@ -364,6 +364,7 @@ public async Task SelectResult(TItem item)
364364 else
365365 {
366366 await ValueChanged . InvokeAsync ( value ) ;
367+ _suppressValueLoadedEvent = true ;
367368 await SelectionAdded . InvokeAsync ( item ) ;
368369 }
369370
@@ -606,6 +607,24 @@ private void MoveSelection(int count)
606607 SelectedIndex = index ;
607608 }
608609
610+ /// <summary>
611+ /// Handles the <see cref="LazyValue{TKey, TValue}.ValueLoaded"/> callback,
612+ /// forwarding to <see cref="SelectionAdded"/> unless the event was already
613+ /// raised by <see cref="SelectResult"/>.
614+ /// </summary>
615+ /// <param name="item">The item resolved by the lazy loader.</param>
616+ private async Task OnValueLoaded ( TItem ? item )
617+ {
618+ if ( _suppressValueLoadedEvent )
619+ {
620+ _suppressValueLoadedEvent = false ;
621+ return ;
622+ }
623+
624+ if ( item is not null )
625+ await SelectionAdded . InvokeAsync ( item ) ;
626+ }
627+
609628 /// <summary>
610629 /// Converts an item to its corresponding value using <see cref="ConvertMethod"/> or direct casting.
611630 /// </summary>
0 commit comments