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

Dropdown | Deprecate autoDisplayFirst property #14426

Closed
cetincakiroglu opened this issue Dec 25, 2023 · 8 comments
Closed

Dropdown | Deprecate autoDisplayFirst property #14426

cetincakiroglu opened this issue Dec 25, 2023 · 8 comments
Assignees
Labels
Core Team Issue or pull request has been *opened* by a member of Core Team Type: Breaking Change Issue contains a breaking change related to a specific component
Milestone

Comments

@cetincakiroglu
Copy link
Contributor

cetincakiroglu commented Dec 25, 2023

  • autoDisplayFirst property will be removed in the future because of the maintenance time cost and model value collisions.

  • Instead of the autoDisplayFirst, users can set the value by the model value as shown below.


// component.html
<p-dropdown [options]="cities" [(ngModel)]="selectedCity" optionLabel="name" placeholder="Select a City"></p-dropdown>


// component.ts
    cities: City[];

    selectedCity: City | undefined;

    ngOnInit() {
        this.cities = [
            { name: 'New York', code: 'NY' },
            { name: 'Rome', code: 'RM' },
            { name: 'London', code: 'LDN' },
            { name: 'Istanbul', code: 'IST' },
            { name: 'Paris', code: 'PRS' }
        ];
        this.selectedCity = this.cities[0] // same output with the autoDisplayFirst property, therefore it's not needed.
    }

@cetincakiroglu cetincakiroglu added Type: Breaking Change Issue contains a breaking change related to a specific component Core Team Issue or pull request has been *opened* by a member of Core Team labels Dec 25, 2023
@cetincakiroglu cetincakiroglu added this to the 17.2.1 milestone Dec 25, 2023
@cetincakiroglu cetincakiroglu self-assigned this Dec 25, 2023
@github-actions github-actions bot added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Dec 25, 2023
@cetincakiroglu cetincakiroglu removed the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Dec 25, 2023
@alexnoise79
Copy link
Contributor

it's a pity cause scenario like this cannot be used anymore, and they were very useful

<p-dropdown [options]="cities" ngModel optionLabel="name" (ngModelChange)="doThis($event)></p-dropdown>

@PeterHewat
Copy link

Documentation needs updating: https://primeng.org/dropdown#api.dropdown.props.autoDisplayFirst

@ArturJarosz
Copy link

ArturJarosz commented Apr 21, 2024

Isn't ngModel deprecated as well here as well?

@AlbaSS18
Copy link

And if I don't want to select any value, what value should I set? Null or undefined?

@majkers
Copy link

majkers commented May 27, 2024

It's a really bad change. What if we want to force user to select an item (so we don't want to select firs item) and option list is taken from service? We have to either spread it (option list) with empty|null value and label or use empty string as placeholder now...

@digitalcraftco
Copy link
Contributor

What about async as options now? #14190 (comment)

@samwightt
Copy link

Unfortunately this broke our app on our Angular 17 upgrade and was quite annoying to hunt down :( Wish this hadn't been deprecated.

@Kolic822
Copy link

Kolic822 commented Nov 5, 2024

You can create a custom directive for p-dropdown that will auto display first. Please copy all the code, not just the code in the box. I don't know why it won't register everything as code...

import { AfterContentInit, Directive } from '@angular/core';
import { NgControl } from '@angular/forms';
import { Dropdown } from 'primeng/dropdown';

@directive({
selector: '[autoSelectFirst]',
standalone: true,
})
export class AutoSelectFirstDirective implements AfterContentInit {
constructor(
private control: NgControl,
private dropdown: Dropdown,
) {}

ngAfterContentInit(): void {
// Ensure the form control exists
const formControl = this.control.control;
if (!formControl) return;

// Auto-select the first option when options are available
const setFirstOption = () => {
  if (this.dropdown.options && this.dropdown.options.length > 0) {
    const firstValue = this.dropdown.options[0].key;
    formControl.patchValue(firstValue);
  }
};

// PrimeNG `p-dropdown` may load options asynchronously, so use `ngAfterViewInit` to wait for them
setFirstOption();

// Listen for option changes on the dropdown (using PrimeNG's update event)
this.dropdown.onShow.subscribe(setFirstOption);

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Team Issue or pull request has been *opened* by a member of Core Team Type: Breaking Change Issue contains a breaking change related to a specific component
Projects
None yet
Development

No branches or pull requests

9 participants