Skip to content

Commit 8a5713e

Browse files
dgsmith2josephperrott
authored andcommitted
feat(autocomplete): allow panel to have a width value of auto (#11879)
1 parent fc1d1a4 commit 8a5713e

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,13 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
529529
if (this._viewportRuler) {
530530
this._viewportSubscription = this._viewportRuler.change().subscribe(() => {
531531
if (this.panelOpen && this._overlayRef) {
532-
this._overlayRef.updateSize({width: this._getHostWidth()});
532+
this._overlayRef.updateSize({width: this._getPanelWidth()});
533533
}
534534
});
535535
}
536536
} else {
537537
// Update the panel width and direction, in case anything has changed.
538-
this._overlayRef.updateSize({width: this._getHostWidth()});
538+
this._overlayRef.updateSize({width: this._getPanelWidth()});
539539
}
540540

541541
if (this._overlayRef && !this._overlayRef.hasAttached()) {
@@ -559,7 +559,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
559559
return new OverlayConfig({
560560
positionStrategy: this._getOverlayPosition(),
561561
scrollStrategy: this._scrollStrategy(),
562-
width: this._getHostWidth(),
562+
width: this._getPanelWidth(),
563563
direction: this._dir
564564
});
565565
}
@@ -585,6 +585,10 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
585585
return this._formField ? this._formField.getConnectedOverlayOrigin() : this._element;
586586
}
587587

588+
private _getPanelWidth(): number | string {
589+
return this.autocomplete.panelWidth || this._getHostWidth();
590+
}
591+
588592
/** Returns the width of the input element, so the panel width can match it. */
589593
private _getHostWidth(): number {
590594
return this._getConnectedElement().nativeElement.getBoundingClientRect().width;

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,50 @@ describe('MatAutocomplete', () => {
19311931
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(400);
19321932
}));
19331933

1934+
it('should have panel width match host width by default', () => {
1935+
const widthFixture = createComponent(SimpleAutocomplete);
1936+
1937+
widthFixture.componentInstance.width = 300;
1938+
widthFixture.detectChanges();
1939+
1940+
widthFixture.componentInstance.trigger.openPanel();
1941+
widthFixture.detectChanges();
1942+
1943+
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
1944+
1945+
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(300);
1946+
});
1947+
1948+
it('should have panel width set to string value', () => {
1949+
const widthFixture = createComponent(SimpleAutocomplete);
1950+
1951+
widthFixture.componentInstance.width = 300;
1952+
widthFixture.detectChanges();
1953+
1954+
widthFixture.componentInstance.trigger.autocomplete.panelWidth = 'auto';
1955+
widthFixture.componentInstance.trigger.openPanel();
1956+
widthFixture.detectChanges();
1957+
1958+
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
1959+
1960+
expect(overlayPane.style.width).toBe('auto');
1961+
});
1962+
1963+
it('should have panel width set to number value', () => {
1964+
const widthFixture = createComponent(SimpleAutocomplete);
1965+
1966+
widthFixture.componentInstance.width = 300;
1967+
widthFixture.detectChanges();
1968+
1969+
widthFixture.componentInstance.trigger.autocomplete.panelWidth = 400;
1970+
widthFixture.componentInstance.trigger.openPanel();
1971+
widthFixture.detectChanges();
1972+
1973+
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
1974+
1975+
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(400);
1976+
});
1977+
19341978
it('should show the panel when the options are initialized later within a component with ' +
19351979
'OnPush change detection', fakeAsync(() => {
19361980
let fixture = createComponent(AutocompleteWithOnPushDelay);

src/lib/autocomplete/autocomplete.ts

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

129+
/**
130+
* Specify the width of the autocomplete panel. Can be any CSS sizing value, otherwise it will
131+
* match the width of its host.
132+
*/
133+
@Input() panelWidth: string | number;
129134

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

0 commit comments

Comments
 (0)