Skip to content

Commit 5049b8c

Browse files
committed
feat(autocomplete): accept truthy value for panel width
When value is supplied it will be used as a CSS value for the panel width. Otherwise, width will default to match the host. Fixes #11773
1 parent 9f662af commit 5049b8c

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 5 additions & 5 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._getWidth()});
526+
this._overlayRef.updateSize({width: this._getPanelWidth()});
527527
}
528528
});
529529
}
530530
} else {
531531
// Update the panel width and direction, in case anything has changed.
532-
this._overlayRef.updateSize({width: this._getWidth()});
532+
this._overlayRef.updateSize({width: this._getPanelWidth()});
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._getWidth(),
556+
width: this._getPanelWidth(),
557557
direction: this._dir
558558
});
559559
}
@@ -579,8 +579,8 @@ 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();
582+
private _getPanelWidth(): number | string {
583+
return this.autocomplete.panelWidth || this._getHostWidth();
584584
}
585585

586586
/** Returns the width of the input element, so the panel width can match it. */

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,19 +1922,48 @@ 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);
1925+
it('should have panel width match host width by default', () => {
1926+
const widthFixture = createComponent(SimpleAutocomplete);
1927+
1928+
widthFixture.componentInstance.width = 300;
1929+
widthFixture.detectChanges();
1930+
1931+
widthFixture.componentInstance.trigger.openPanel();
1932+
widthFixture.detectChanges();
1933+
1934+
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
1935+
1936+
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(300);
1937+
});
1938+
1939+
it('should have panel width set to string value', () => {
1940+
const widthFixture = createComponent(SimpleAutocomplete);
1941+
1942+
widthFixture.componentInstance.width = 300;
1943+
widthFixture.detectChanges();
1944+
1945+
widthFixture.componentInstance.trigger.autocomplete.panelWidth = 'auto';
1946+
widthFixture.componentInstance.trigger.openPanel();
1947+
widthFixture.detectChanges();
1948+
1949+
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
1950+
1951+
expect(overlayPane.style.width).toBe('auto');
1952+
});
19271953

1928-
widthFixture.componentInstance.width = 300;
1929-
widthFixture.detectChanges();
1954+
it('should have panel width set to number value', () => {
1955+
const widthFixture = createComponent(SimpleAutocomplete);
1956+
1957+
widthFixture.componentInstance.width = 300;
1958+
widthFixture.detectChanges();
19301959

1931-
widthFixture.componentInstance.trigger.autocomplete.panelWidthAuto = true;
1932-
widthFixture.componentInstance.trigger.openPanel();
1933-
widthFixture.detectChanges();
1960+
widthFixture.componentInstance.trigger.autocomplete.panelWidth = 400;
1961+
widthFixture.componentInstance.trigger.openPanel();
1962+
widthFixture.detectChanges();
19341963

1935-
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
1964+
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
19361965

1937-
expect(overlayPane.style.width).toBe('auto');
1966+
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(400);
19381967
});
19391968

19401969
it('should show the panel when the options are initialized later within a component with ' +

src/lib/autocomplete/autocomplete.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,10 @@ export class MatAutocomplete extends _MatAutocompleteMixinBase implements AfterC
127127
private _autoActiveFirstOption: boolean;
128128

129129
/**
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.
130+
* Specify the width of the autocomplete panel. Can be any CSS sizing value, otherwise it will
131+
* match the width of its host.
132132
*/
133-
@Input()
134-
get panelWidthAuto(): boolean { return this._panelWidthAuto; }
135-
set panelWidthAuto(value: boolean) {
136-
this._panelWidthAuto = coerceBooleanProperty(value);
137-
}
138-
private _panelWidthAuto: boolean;
133+
@Input() panelWidth: string | number;
139134

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

0 commit comments

Comments
 (0)