primefaces / primeng

The Most Complete Angular UI Component Library
https://primeng.org
Other
10.6k stars 4.62k forks source link

Component: Autocomplete. Option Value and FormControl as an Object #16752

Closed tranvo-dev closed 6 days ago

tranvo-dev commented 1 week ago

Describe the bug

Hi, I am using autocomplete components with the snippet below:

HTML

[...]
<p-autoComplete
            #test
            placeholder="Add participants"
            optionLabel="name"
            optionValue="id"
            appendTo="body"
            multiple
            [formControl]="transactionForm.controls.participants"
            [suggestions]="participants()"
            (completeMethod)="search($event)" />
[...]

while transactionForm.controls.participants = new FormControl<string[]>([]); // which will contains only ids participants = signal<{name: string, id: string}[]>([]); // the suggestion list

search($event: AutoCompleteCompleteEvent) {
        const filteredParticipants = [
            ...this._participants
                .filter(member => member.id !== this.hostId())
                .filter(
                    member =>
                        member.name
                            .toLowerCase()
                            .indexOf($event.query.toLowerCase()) === 0 ||
                        member.name.toLowerCase() === $event.query.toLowerCase()
                ),
        ];
        this.participants.set(filteredParticipants);
    }

I expect that whenever I search a value, if it does not have the result the rest selected value should stay the same But actually, the selected one becomes empty on the view (but the form control still keeps the selected id value). Please refer to the image below

Screenshot 2024-11-15 at 09 08 50

I tried to remove the optionValue, It worked but the result that is filled into the formControl does not satify the requirement (It filled in with objects instead of id strings as expected)

Is there any other way to overcome this? Thank you for your effort

Environment

Development

Reproducer

No response

Angular version

18.2.0

PrimeNG version

17.18.11

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

v20.18.0

Browser(s)

Chrome

Steps to reproduce the behavior

  1. Search a value that is existed in the suggestion list
  2. Select one from the result
  3. Continue searching a value that is not existed in the suggestion list

Expected behavior

Expect: If the search value does not exist in the list, the selected value will stay the same on the view Actual: The selected value becomes empty on the view (BUT the form control still keeps the selected id value)

Screenshot 2024-11-15 at 09 08 50
mertsincan commented 6 days ago

Hi,

I think this is not related to PrimeNG core. Please use PrimeNg Discord channel/Discussion for such questions.

Best Regards,