[Solved] Override 'Enter' choosing first item in AutoComplete-component

2 Answers 48 Views
AutoComplete
Emil
Top achievements
Rank 1
Iron
Emil asked on 26 Oct 2026, 12:07 PM

Hello,

I am using a TelerikAutoComplete-component as a search bar. 
When pressing 'Enter', the component fills the bar with the first item in the suggestions list, if any.

Is there a way to keep the suggestions list, and the option to click on any suggestion, but override control to make 'Enter' do something other than selecting the first item? 

Something like this:
    <div @onkeydown="@OnKeyDown">
        <TelerikAutoComplete Data="@Suggestions"
                             MinLength="3"
                             Rounded="@ThemeConstants.AutoComplete.Rounded.Full"
                             Size="@ThemeConstants.AutoComplete.Size.Large"/>
    </div>

    private void OnKeyDown(KeyboardEventArgs e)
    {
        if (e.Key == "Enter")
        {
            Console.WriteLine("yo");
        }
    }

2 Answers, 1 is accepted

Sort by
0
Emil
Top achievements
Rank 1
Iron
answered on 27 Oct 2026, 01:06 PM | edited on 29 Oct 2026, 08:26 AM

I ended up doing it this way. I would really like to see if there are any other options, as this comes with some drawbacks.
In the component, add a div around the AutoComplete:

<div class="search-container" @ref="_wrapper">
In code-behind of the component, add this:
    private ElementReference _wrapper;
    private IJSObjectReference jsModule { get; set; }
    private DotNetObjectReference<SearchBar> _dotNetRef;

    private async Task OnKeyDown(KeyboardEventArgs e)
    {
        if (e.Key == "Enter")
        {
            await Search();
        }
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            _dotNetRef = DotNetObjectReference.Create(this);
            jsModule = await jsRuntime.InvokeAsync<IJSObjectReference>("import", "./SearchBar.razor.js").AsTask();
            await jsModule.InvokeVoidAsync("attachEnterOverride", _wrapper, _dotNetRef);
        }
    }

    [JSInvokable]
    public Task OnEnter() => Search();

    public async ValueTask DisposeAsync()
    {
        if (jsModule is not null)
        {
            await jsModule.InvokeVoidAsync("detachEnterOverride", _wrapper);
            await jsModule.DisposeAsync();
        }
        _dotNetRef?.Dispose();
    }


And then add a JS-file that matches the naming of your component:

const handlers = new WeakMap();

export function attachEnterOverride(el, dotNetRef) {
    const handler = e => {
        if (e.key !== "Enter") return;

        e.stopPropagation();
        e.preventDefault();
        dotNetRef.invokeMethodAsync("OnEnter");
    };
    handlers.set(el, handler);
    el.addEventListener("keydown", handler, true);
}

export function detachEnterOverride(el) {
    const h = handlers.get(el);
    if (h) el.removeEventListener("keydown", h, true);
}


There is probably a better way to do this, but it works for me.
0
Emil
Top achievements
Rank 1
Iron
answered on 02 Nov 2026, 01:27 PM
Any other suggestions?
Ivan Danchev
Telerik team
commented on 05 Nov 2026, 02:35 PM

Hello Emil,

Тhe AutoComplete does not provide an OnKeyDown/OnKeyPress event or a cancelable “before selection” event for Enter, and it does not support attribute splatting, so adding @onkeydown or @onkeydown:preventDefault directly to <TelerikAutoComplete> is not an option.

Тhe realistic alternative is only variations of the JavaScript interception approach.

  • Your approach: wrapper capture listener intercepts Enter as it travels down to the input. This is the most robust option because it does not depend on Telerik’s internal generated markup.
  • Direct-input listener: after render, use JavaScript to find the AutoComplete’s generated <input> and add a keydown listener directly to it. This is more tightly coupled to our internal DOM/CSS structure, so it is less upgrade-safe than the wrapper approach.
Tags
AutoComplete
Asked by
Emil
Top achievements
Rank 1
Iron
Answers by
Emil
Top achievements
Rank 1
Iron
Share this question
or