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

Conversation

dgsmith2
Copy link
Contributor

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

@dgsmith2 dgsmith2 requested a review from crisbeto as a code owner June 21, 2018 17:08
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jun 21, 2018
* panel will match the width of the host.
*/
@Input()
get panelWidthAuto(): boolean { return this._panelWidthAuto; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than directly controlling if the panel width is auto, would it make more sense to take in a string which can be auto or any CSS sizing value? This would cover a few more use cases.

We could even have a default of something like 'match' to match the host's width.

@Input() panelWidth: string = 'match';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should avoid having magic strings like these in APIs. We can make it so by default it matches the input width, unless the consumer passed in a truthy width.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a string literal if you don't want a regular string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crisbeto good point. Using match is a magic string. Really, I just wanted to make it a bit more flexible than a boolean would be.

}
});
}
} else {
// Update the panel width and direction, in case anything has changed.
this._overlayRef.updateSize({width: this._getHostWidth()});
this._overlayRef.updateSize({width: this._getWidth()});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while updateSize does any in number | string as its type for width, it ends up just coercing the value provided into {$value}px. So if auto was provided as the value it would end up as autopx.

Copy link
Contributor Author

@dgsmith2 dgsmith2 Jun 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only does that if the value is a number. Per https://github.com/angular/material2/blob/6.3.x/src/cdk/coercion/css-pixel-value.ts

export function coerceCssPixelValue(value: any): string {
  if (value == null) {
    return '';
  }

  return typeof value === 'string' ? value : `${value}px`;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes, I glanced too quickly when I read it.

* panel will match the width of the host.
*/
@Input()
get panelWidthAuto(): boolean { return this._panelWidthAuto; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should avoid having magic strings like these in APIs. We can make it so by default it matches the input width, unless the consumer passed in a truthy width.

@@ -1922,6 +1922,21 @@ describe('MatAutocomplete', () => {
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(400);
}));

it('should have panel width set to auto', () => {
const widthFixture = createComponent(SimpleAutocomplete);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the indentation on the entire function call is too much.

@@ -579,6 +579,10 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
return this._formField ? this._formField.getConnectedOverlayOrigin() : this._element;
}

private _getWidth(): number | string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this one to _getPanelWidth or _getOverlayWidth so it's more obvious what it does. Also consider adding a doc string.

dgsmith2 added 2 commits June 26, 2018 16:07
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 angular#11773
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 angular#11773
@dgsmith2 dgsmith2 force-pushed the autocomplete-width-auto branch from f0a18c7 to 5049b8c Compare June 26, 2018 20:19
Copy link
Member

@josephperrott josephperrott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@josephperrott josephperrott added pr: lgtm action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release labels Jun 26, 2018
@josephperrott josephperrott merged commit 8a5713e into angular:master Jun 29, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Override dropdown/overlay width in autocomplete, select and other components
5 participants