Skip to content

Commit 1f5e955

Browse files
authored
fix(autocomplete): allow autocomplete drop-down width to be adjustable #6247 (#6658)
1 parent 48967e2 commit 1f5e955

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

projects/igniteui-angular/src/lib/directives/autocomplete/autocomplete.directive.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,13 @@ describe('IgxAutocomplete', () => {
682682
expect(dropDownAny.scrollContainer.getBoundingClientRect().width)
683683
.toEqual(group.element.nativeElement.getBoundingClientRect().width);
684684
});
685+
it('Should apply width to dropdown list if set', () => {
686+
UIInteractions.sendInput(input, 's', fixture);
687+
fixture.componentInstance.ddWidth = '600px';
688+
fixture.detectChanges();
689+
const dropDownAny = dropDown as any;
690+
expect(dropDownAny.scrollContainer.getBoundingClientRect().width).toEqual(600);
691+
});
685692
it('Should render aria attributes properly', fakeAsync(() => {
686693
expect(input.nativeElement.attributes['autocomplete'].value).toEqual('off');
687694
expect(input.nativeElement.attributes['role'].value).toEqual('combobox');
@@ -853,7 +860,7 @@ describe('IgxAutocomplete', () => {
853860
<label igxLabel for="towns">Towns</label>
854861
<igx-suffix igxRipple><igx-icon fontSet="material">clear</igx-icon> </igx-suffix>
855862
</igx-input-group>
856-
<igx-drop-down #townsPanel>
863+
<igx-drop-down #townsPanel [width]="ddWidth">
857864
<igx-drop-down-item *ngFor="let town of towns | startsWith:townSelected" [value]="town">
858865
{{town}}
859866
</igx-drop-down-item>
@@ -866,6 +873,7 @@ class AutocompleteComponent {
866873
@ViewChild(IgxDropDownComponent, { static: true }) public dropDown: IgxDropDownComponent;
867874
townSelected;
868875
public towns;
876+
public ddWidth = null;
869877
settings: AutocompleteOverlaySettings = null;
870878
onItemSelected(args) { }
871879

projects/igniteui-angular/src/lib/directives/autocomplete/autocomplete.directive.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ export class IgxAutocompleteDirective extends IgxDropDownItemNavigationDirective
277277
if (this.disabled || !this.collapsed) {
278278
return;
279279
}
280-
this.target.width = this.parentElement.clientWidth + 'px';
280+
// If no drop-down width is set, the drop-down will be as wide as the autocomplete input;
281+
this.target.width = this.target.width || (this.parentElement.clientWidth + 'px');
281282
this.target.open(this.settings);
282283
this.target.onSelection.pipe(takeUntil(this.dropDownOpened$)).subscribe(this.select);
283284
this.target.onOpened.pipe(first()).subscribe(this.highlightFirstItem);

src/app/autocomplete/autocomplete.sample.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
[igxAutocomplete]='attractionsPanel'/>
2626
<label igxLabel>Attraction</label>
2727
</igx-input-group>
28-
<igx-drop-down #attractionsPanel maxHeight="300px">
28+
<igx-drop-down #attractionsPanel [width]="attractionsWidth" maxHeight="300px">
2929
<igx-drop-down-item *ngFor="let attraction of attractions | contains: travel.value.attraction" [value]="attraction.name">
3030
<igx-icon class="logo" fontSet="material">{{attraction.image}}</igx-icon>
3131
{{attraction.name}}
@@ -37,6 +37,7 @@
3737
<button igxButton (click)="onSearch()" [disabled]="!travel.valid">Search</button>
3838
</div>
3939
</form>
40+
<igx-switch (change)="changeDefaultWidth($event)">Attractions Custom Width</igx-switch>
4041
</article>
4142
</section>
4243
</div>

src/app/autocomplete/autocomplete.sample.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FormBuilder, FormControl, Validators, FormGroup } from '@angular/forms'
33
import { worldInfo, attractions } from './data';
44
import { IgxDialogComponent } from 'igniteui-angular';
55

6+
const ATTRACTIONS_CUSTOM_WIDTH = '300px';
67
@Component({
78
selector: 'app-autocomplete-sample',
89
styleUrls: ['autocomplete.sample.css'],
@@ -13,6 +14,7 @@ export class AutocompleteSampleComponent {
1314
public travel: FormGroup;
1415
worldInfo;
1516
attractions;
17+
public attractionsWidth = '';
1618

1719
constructor(fb: FormBuilder) {
1820
this.worldInfo = worldInfo;
@@ -34,6 +36,10 @@ export class AutocompleteSampleComponent {
3436
}
3537
this.alert.open();
3638
}
39+
40+
changeDefaultWidth(event: any) {
41+
this.attractionsWidth = event.checked ? ATTRACTIONS_CUSTOM_WIDTH : '';
42+
}
3743
}
3844

3945
@Pipe({ name: 'contains' })

0 commit comments

Comments
 (0)