Skip to content

Commit 9f662af

Browse files
committed
feat(autocomplete): allow panel to have a width value of auto
For scenarios when the width of autocomplete panel needs to exceed the width of its input (host) in order to display the full text of each option. Fixes #11773
1 parent 85711aa commit 9f662af

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,13 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
523523
if (this._viewportRuler) {
524524
this._viewportSubscription = this._viewportRuler.change().subscribe(() => {
525525
if (this.panelOpen && this._overlayRef) {
526-
this._overlayRef.updateSize({width: this._getHostWidth()});
526+
this._overlayRef.updateSize({width: this._getWidth()});
527527
}
528528
});
529529
}
530530
} else {
531531
// Update the panel width and direction, in case anything has changed.
532-
this._overlayRef.updateSize({width: this._getHostWidth()});
532+
this._overlayRef.updateSize({width: this._getWidth()});
533533
}
534534

535535
if (this._overlayRef && !this._overlayRef.hasAttached()) {
@@ -553,7 +553,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
553553
return new OverlayConfig({
554554
positionStrategy: this._getOverlayPosition(),
555555
scrollStrategy: this._scrollStrategy(),
556-
width: this._getHostWidth(),
556+
width: this._getWidth(),
557557
direction: this._dir
558558
});
559559
}
@@ -579,6 +579,10 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
579579
return this._formField ? this._formField.getConnectedOverlayOrigin() : this._element;
580580
}
581581

582+
private _getWidth(): number | string {
583+
return this.autocomplete.panelWidthAuto ? 'auto' : this._getHostWidth();
584+
}
585+
582586
/** Returns the width of the input element, so the panel width can match it. */
583587
private _getHostWidth(): number {
584588
return this._getConnectedElement().nativeElement.getBoundingClientRect().width;

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,21 @@ describe('MatAutocomplete', () => {
19221922
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(400);
19231923
}));
19241924

1925+
it('should have panel width set to auto', () => {
1926+
const widthFixture = createComponent(SimpleAutocomplete);
1927+
1928+
widthFixture.componentInstance.width = 300;
1929+
widthFixture.detectChanges();
1930+
1931+
widthFixture.componentInstance.trigger.autocomplete.panelWidthAuto = true;
1932+
widthFixture.componentInstance.trigger.openPanel();
1933+
widthFixture.detectChanges();
1934+
1935+
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
1936+
1937+
expect(overlayPane.style.width).toBe('auto');
1938+
});
1939+
19251940
it('should show the panel when the options are initialized later within a component with ' +
19261941
'OnPush change detection', fakeAsync(() => {
19271942
let fixture = createComponent(AutocompleteWithOnPushDelay);

src/lib/autocomplete/autocomplete.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ export class MatAutocomplete extends _MatAutocompleteMixinBase implements AfterC
126126
}
127127
private _autoActiveFirstOption: boolean;
128128

129+
/**
130+
* Whether the width of the autocomplete panel is set to 'auto'. When 'false', the width of the
131+
* panel will match the width of the host.
132+
*/
133+
@Input()
134+
get panelWidthAuto(): boolean { return this._panelWidthAuto; }
135+
set panelWidthAuto(value: boolean) {
136+
this._panelWidthAuto = coerceBooleanProperty(value);
137+
}
138+
private _panelWidthAuto: boolean;
129139

130140
/** Event that is emitted whenever an option from the list is selected. */
131141
@Output() readonly optionSelected: EventEmitter<MatAutocompleteSelectedEvent> =

0 commit comments

Comments
 (0)