简体   繁体   English

垫自动完成宽度不能随输入主机上的动态宽度调整

[英]Mat-autocomplete width doesn't adjust with dynamic width on input host

I have a mat-form-field with a standard input inside of it and a mat-autocomplete attached. 我有一个mat-form-field,其中带有标准输入,并附加了mat-autocomplete。

    <mat-form-field appearance="fill">
    <mat-label>{{ 'INVENTORY.areaOfBusiness' | translate }}</mat-label>
    <input type="text" matInput
    [matAutocomplete]="businessAreaAuto"
    (keyup)="search($event, 'businessArea')"
    [(ngModel)]="businessArea">
    <mat-icon matSuffix>add</mat-icon>
    <mat-autocomplete #businessAreaAuto="matAutocomplete" (optionSelected)="businessSelection($event)">
      <mat-option *ngIf="bizNoResults === true" class="no-click">{{ 'INVENTORY.no-result' | translate }}</mat-option>
      <mat-option *ngIf="isSearchBiz === true">
        <div class="desktop-spinner">
          <span>{{ 'INVENTORY.searching' | translate }}...</span>     
          <mat-progress-spinner
            diameter = 20
            mode = "indeterminate"
            [value]="value">
          </mat-progress-spinner>
        </div>
      </mat-option>
      <mat-option 
        *ngFor="let bizArea of businessAreaResults | async"
        [value]="bizArea"
        matTooltip="{{bizArea}}">
        {{bizArea}}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>

On the mat-form-field I have added a transition width when the mat-focused class is applied: 在垫子形式字段上,当应用以垫子为焦点的类时,我添加了过渡宽度:

    mat-form-field {
    margin-left: 24px;
    font-size: 14px;
    width: 170px;
    transition: width .5s ease;
  }
  .mat-focused {
    width: 270px;
  }

However I am running into issues with the width sizing of the autocomplete panel when the input goes in and out of focus. 但是,当输入变得模糊不清时,我会遇到自动完成面板的宽度调整问题。 The autocomplete panel doesn't seem to truly catch up with the proper width until the user is typing in text. 在用户输入文本之前,自动完成面板似乎并没有真正赶上正确的宽度。 I haven't found a method or input in the angular material docs that has a good way to update the autocomplete with the correct size. 我尚未在有角度的材料文档中找到一种方法或输入法来以正确的尺寸更新自动填充功能。 It seems to be catching the size on the input at different points of the transition process. 在过渡过程的不同点上似乎正在捕获输入的大小。

Any help/suggestions would be much appreciated 任何帮助/建议将不胜感激

stackblitz https://stackblitz.com/edit/angular-yfxr8y stackblitz https://stackblitz.com/edit/angular-yfxr8y

There are two events in this scenario, one event to open the mat-autocomplete , and another event triggered by your first key stroke of the search. 在这种情况下,有两个事件,一个是打开mat-autocomplete事件,另一个是由您的第一次搜索键触发的事件。

The first creates the overlay via _attachOverlay() as if (!overlayRef) equals to true https://github.com/angular/material2/blob/master/src/lib/autocomplete/autocomplete-trigger.ts#L594 ' 第一个通过_attachOverlay()创建覆盖, if (!overlayRef)等于true https://github.com/angular/material2/blob/master/src/lib/autocomplete/autocomplete-trigger.ts#L594 '

and the second triggers the else condition of _attachOverlay() as the overlay already exists. 第二个触发_attachOverlay()else条件,因为该覆盖已经存在。

// Update the trigger, panel width and direction, in case anything has changed. //更新触发器,面板宽度和方向,以防万一发生任何变化。

https://github.com/angular/material2/blob/master/src/lib/autocomplete/autocomplete-trigger.ts#L601 https://github.com/angular/material2/blob/master/src/lib/autocomplete/autocomplete-trigger.ts#L601

Calls _attachOverlay() with each event. 每个事件调用_attachOverlay()

 /** Opens the autocomplete suggestion panel. */
  openPanel(): void {
    this._attachOverlay();
    this._floatLabel();
  }

Neither of which are a continuous "stream" of updates, both conditions are designed to be singular events. 这两个条件都不是连续的更新“流”,这两个条件都被设计为单一事件。


You could leverage panelWidth input on mat-autocomplete and specify your width of 270px 您可以在mat-autocomplete上利用panelWidth输入,并指定270px的宽度

<mat-autocomplete #auto="matAutocomplete" panelWidth="270px">

This will not make the width autosize with your transition however, if you wish to animate it, you will need to explore overriding the mat-autocomplete or write your own solution. 这不会使宽度随着过渡而自动调整大小,但是,如果希望对其进行动画处理,则需要探索覆盖mat-autocomplete或编写自己的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM