Skip to content

Commit

Permalink
resolves #12637 -- uses proper group children property in dropdown au…
Browse files Browse the repository at this point in the history
…toselect
  • Loading branch information
jbastyr committed Feb 16, 2023
1 parent 085a4e6 commit 83f6402
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/app/components/dropdown/dropdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { OverlayModule } from 'primeng/overlay';
</p-dropdown>
<p-dropdown [(ngModel)]="selectedCity"></p-dropdown>
<button (click)="setValue()"></button>
<p-dropdown [(ngModel)]="selectedCity" [options]="groupedCarsAlternate" optionGroupChildren="children" [group]="true"></p-dropdown>
`
})
class TestDropdownComponent {
Expand Down Expand Up @@ -52,6 +53,12 @@ class TestDropdownComponent {
}
];

groupedCarsAlternate = this.groupedCars.map(city => ({
label: city.label,
value: city.value,
children: city.items
}));

disabled: boolean;

editable: boolean;
Expand All @@ -66,6 +73,7 @@ describe('Dropdown', () => {
let dropdown: Dropdown;
let testDropdown: Dropdown;
let groupDropdown: Dropdown;
let alternateGroupDropdown: Dropdown;
let fixture: ComponentFixture<Dropdown>;
let groupFixture: ComponentFixture<TestDropdownComponent>;

Expand All @@ -79,6 +87,7 @@ describe('Dropdown', () => {
groupFixture = TestBed.createComponent(TestDropdownComponent);
groupDropdown = groupFixture.debugElement.children[0].componentInstance;
testDropdown = groupFixture.debugElement.children[1].componentInstance;
alternateGroupDropdown = groupFixture.debugElement.children[3].componentInstance;
dropdown = fixture.componentInstance;
});

Expand Down Expand Up @@ -538,6 +547,12 @@ describe('Dropdown', () => {
expect(groupDropdown.selectedOption.label).toEqual('Mercedes');
});

it('should alternateGroup auto select with alternate children field', () => {
groupFixture.detectChanges();

expect(alternateGroupDropdown.selectedOption.label).toEqual('Audi')
});

[null, undefined, ''].map((value) =>
it('should return filled false when value is not provided', () => {
dropdown.value = value;
Expand All @@ -546,4 +561,4 @@ describe('Dropdown', () => {
expect(dropdown.filled).toBeFalsy();
})
);
});
});
2 changes: 1 addition & 1 deletion src/app/components/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV

if (this.autoDisplayFirst && !this.placeholder && !this.selectedOption && this.optionsToDisplay && this.optionsToDisplay.length && !this.editable) {
if (this.group) {
this.selectedOption = this.optionsToDisplay[0].items[0];
this.selectedOption = this.getOptionGroupChildren(this.optionsToDisplay[0])[0];
} else {
this.selectedOption = this.optionsToDisplay[0];
}
Expand Down

0 comments on commit 83f6402

Please sign in to comment.