Skip to content

Commit 119c1b5

Browse files
authored
fix(autocomplete): allow autocomplete drop-down width to be adjustable #6247 (#6658)
1 parent 57017ef commit 119c1b5

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
@@ -674,6 +674,13 @@ describe('IgxAutocomplete', () => {
674674
expect(dropDownAny.scrollContainer.getBoundingClientRect().width)
675675
.toEqual(group.element.nativeElement.getBoundingClientRect().width);
676676
});
677+
it('Should apply width to dropdown list if set', () => {
678+
UIInteractions.sendInput(input, 's', fixture);
679+
fixture.componentInstance.ddWidth = '600px';
680+
fixture.detectChanges();
681+
const dropDownAny = dropDown as any;
682+
expect(dropDownAny.scrollContainer.getBoundingClientRect().width).toEqual(600);
683+
});
677684
it('Should render aria attributes properly', fakeAsync(() => {
678685
expect(input.nativeElement.attributes['autocomplete'].value).toEqual('off');
679686
expect(input.nativeElement.attributes['role'].value).toEqual('combobox');
@@ -845,7 +852,7 @@ describe('IgxAutocomplete', () => {
845852
<label igxLabel for="towns">Towns</label>
846853
<igx-suffix igxRipple><igx-icon fontSet="material">clear</igx-icon> </igx-suffix>
847854
</igx-input-group>
848-
<igx-drop-down #townsPanel>
855+
<igx-drop-down #townsPanel [width]="ddWidth">
849856
<igx-drop-down-item *ngFor="let town of towns | startsWith:townSelected" [value]="town">
850857
{{town}}
851858
</igx-drop-down-item>
@@ -858,6 +865,7 @@ class AutocompleteComponent {
858865
@ViewChild(IgxDropDownComponent, { static: true }) public dropDown: IgxDropDownComponent;
859866
townSelected;
860867
public towns;
868+
public ddWidth = null;
861869
settings: AutocompleteOverlaySettings = null;
862870
onItemSelected(args) { }
863871

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)