Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight first item in AutoComplete #2040

Closed
davladus opened this issue Feb 10, 2017 · 4 comments
Closed

Highlight first item in AutoComplete #2040

davladus opened this issue Feb 10, 2017 · 4 comments
Assignees
Labels
Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add
Milestone

Comments

@davladus
Copy link

**I'm submitting a usability bug

Plunkr Case (Bug Reports)
http://www.primefaces.org/primeng/#/autocomplete

Current/Expected behavior
Minimal reproduction of the problem with instructions

When I start typing letters (Advanced sample)
And selection dropdown is expanded
Then
Observed: nothing selected
Expected: first filtered item is selected

Please tell us about your environment:
Window 7, Chrome

@Mrtcndkn Mrtcndkn added the Resolution: Wontfix Issue will not be fixed due to technical limitations label Feb 10, 2017
@Mrtcndkn
Copy link
Contributor

We reviewed this and this is expected behaviour.

@GFoley83
Copy link

GFoley83 commented Feb 16, 2017

Is there any way to programmatically default to the first item in the suggestion list, without having to click the down arrow key?

This makes even more sense if there's only one option is the suggestion list. Currently, the user always has to use the down arrow key to select the value, which seem redundant and forces an extra keyboard transaction for every search.

E.g Spain should be preselected here

image

@GFoley83
Copy link

GFoley83 commented Feb 17, 2017

@davladus I was able to get this functionality by dispatching a down arrow key event to the autocomplete component at the end of the completeMethod callback. Less than ideal solution but easy to refactor out later if PrimeNG adds native support (or I get around to adding it myself).
Working code:

import { AutoComplete } from 'primeng/primeng';
@ViewChild(AutoComplete) primeAutoCompleteComponent: AutoComplete;
    // (completeMethod)="search($event)"    
    search(event?: any) {
        // filter you search results array
        . . .    
        this.preSelectFirstOptionInResultsList();
    }
private preSelectFirstOptionInResultsList(): void {
    setTimeout(() => {

        let highlightItemIndex = this.primeAutoCompleteComponent.findOptionIndex(this.primeAutoCompleteComponent.highlightOption);

        if (highlightItemIndex < 0) {
            var arrowDownEvent = document.createEvent('Event');
            (<any>arrowDownEvent).initEvent('keydown', { "bubbles": false, "cancelable": false });
            Object.defineProperty(arrowDownEvent, 'which', { 'value': 40 });

            (<Node>this.primeAutoCompleteComponent.input).dispatchEvent(arrowDownEvent);
        }

    }, 0);
}

@cagataycivici cagataycivici changed the title Autocomplete no default selection after filtering Highlight first item in AutoComplete Mar 27, 2017
@cagataycivici cagataycivici self-assigned this Mar 27, 2017
@cagataycivici cagataycivici added Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add and removed Resolution: Wontfix Issue will not be fixed due to technical limitations labels Mar 27, 2017
@cagataycivici cagataycivici added this to the 4.0.RC2 milestone Mar 27, 2017
@cagataycivici cagataycivici reopened this Mar 27, 2017
@Errabaany
Copy link

Errabaany commented Jan 4, 2024

the feature is already implemented in version 16.9.3lst,
I am using the an older version and I can't update primeng at the moment,
I used @GFoley83 solution I just had to update it to fit my case since I am using multi select


setTimeout(() => {
    const arrowDownEvent = document.createEvent('Event');
    (<any>arrowDownEvent).initEvent('keydown', { "bubbles": false, "cancelable": false });
    Object.defineProperty(arrowDownEvent, 'which', { 'value': 40 });
    (<Node>this.inputElement.multiInputEl?.nativeElement).dispatchEvent(arrowDownEvent);
}, 0)
            

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add
Projects
None yet
Development

No branches or pull requests

5 participants