Skip to content

feat(autocomplete): allow panel to have a width value of auto #11879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,13 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
if (this._viewportRuler) {
this._viewportSubscription = this._viewportRuler.change().subscribe(() => {
if (this.panelOpen && this._overlayRef) {
this._overlayRef.updateSize({width: this._getHostWidth()});
this._overlayRef.updateSize({width: this._getPanelWidth()});
}
});
}
} else {
// Update the panel width and direction, in case anything has changed.
this._overlayRef.updateSize({width: this._getHostWidth()});
this._overlayRef.updateSize({width: this._getPanelWidth()});
}

if (this._overlayRef && !this._overlayRef.hasAttached()) {
Expand All @@ -553,7 +553,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
return new OverlayConfig({
positionStrategy: this._getOverlayPosition(),
scrollStrategy: this._scrollStrategy(),
width: this._getHostWidth(),
width: this._getPanelWidth(),
direction: this._dir
});
}
Expand All @@ -579,6 +579,10 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
return this._formField ? this._formField.getConnectedOverlayOrigin() : this._element;
}

private _getPanelWidth(): number | string {
return this.autocomplete.panelWidth || this._getHostWidth();
}

/** Returns the width of the input element, so the panel width can match it. */
private _getHostWidth(): number {
return this._getConnectedElement().nativeElement.getBoundingClientRect().width;
Expand Down
44 changes: 44 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,50 @@ describe('MatAutocomplete', () => {
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(400);
}));

it('should have panel width match host width by default', () => {
const widthFixture = createComponent(SimpleAutocomplete);

widthFixture.componentInstance.width = 300;
widthFixture.detectChanges();

widthFixture.componentInstance.trigger.openPanel();
widthFixture.detectChanges();

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

expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(300);
});

it('should have panel width set to string value', () => {
const widthFixture = createComponent(SimpleAutocomplete);

widthFixture.componentInstance.width = 300;
widthFixture.detectChanges();

widthFixture.componentInstance.trigger.autocomplete.panelWidth = 'auto';
widthFixture.componentInstance.trigger.openPanel();
widthFixture.detectChanges();

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

expect(overlayPane.style.width).toBe('auto');
});

it('should have panel width set to number value', () => {
const widthFixture = createComponent(SimpleAutocomplete);

widthFixture.componentInstance.width = 300;
widthFixture.detectChanges();

widthFixture.componentInstance.trigger.autocomplete.panelWidth = 400;
widthFixture.componentInstance.trigger.openPanel();
widthFixture.detectChanges();

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

expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(400);
});

it('should show the panel when the options are initialized later within a component with ' +
'OnPush change detection', fakeAsync(() => {
let fixture = createComponent(AutocompleteWithOnPushDelay);
Expand Down
5 changes: 5 additions & 0 deletions src/lib/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export class MatAutocomplete extends _MatAutocompleteMixinBase implements AfterC
}
private _autoActiveFirstOption: boolean;

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

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