Skip to content

Commit 909e499

Browse files
feat(module:auto-complete): support nzDropdownMatchSelectWidth (#9879)
1 parent c59ade4 commit 909e499

5 files changed

Lines changed: 36 additions & 6 deletions

File tree

components/auto-complete/autocomplete-trigger.directive.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,14 @@ export class NzAutocompleteTriggerDirective implements AfterViewInit, ControlVal
263263
}
264264

265265
if (!this.overlayRef) {
266+
const width = this.nzAutocomplete.nzWidth || this.getHostWidth();
267+
const dropdownMatchSelectWidth = this.nzAutocomplete.nzDropdownMatchSelectWidth;
266268
this.overlayRef = createOverlayRef(this.injector, {
267269
positionStrategy: this.getOverlayPosition(),
268270
disposeOnNavigation: true,
269271
scrollStrategy: createRepositionScrollStrategy(this.injector),
270-
// default host element width
271-
width: this.nzAutocomplete.nzWidth || this.getHostWidth()
272+
minWidth: dropdownMatchSelectWidth ? undefined : width,
273+
width: dropdownMatchSelectWidth ? width : undefined
272274
});
273275
}
274276

@@ -289,7 +291,9 @@ export class NzAutocompleteTriggerDirective implements AfterViewInit, ControlVal
289291

290292
private updateStatus(): void {
291293
if (this.overlayRef) {
292-
this.overlayRef.updateSize({ width: this.nzAutocomplete.nzWidth || this.getHostWidth() });
294+
const dropdownMatchSelectWidth = this.nzAutocomplete.nzDropdownMatchSelectWidth;
295+
const width = this.nzAutocomplete.nzWidth || this.getHostWidth();
296+
this.overlayRef.updateSize({ width: dropdownMatchSelectWidth ? width : undefined });
293297
}
294298
this.nzAutocomplete.setVisibility();
295299
this.resetActiveItem();

components/auto-complete/autocomplete.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export class NzAutocompleteComponent implements AfterContentInit, AfterViewInit,
111111
@Input() nzOverlayStyle: Record<string, string> = {};
112112
@Input({ transform: booleanAttribute }) nzDefaultActiveFirstOption = true;
113113
@Input({ transform: booleanAttribute }) nzBackfill = false;
114+
@Input({ transform: booleanAttribute }) nzDropdownMatchSelectWidth = true;
114115
@Input() compareWith: CompareWith = (o1, o2) => o1 === o2;
115116
@Input() nzDataSource?: AutocompleteDataSource;
116117
@Output()

components/auto-complete/autocomplete.spec.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import {
1212
ElementRef,
1313
NgZone,
1414
QueryList,
15+
signal,
1516
SimpleChanges,
1617
ViewChild,
17-
ViewChildren,
18-
signal
18+
ViewChildren
1919
} from '@angular/core';
20-
import { ComponentFixture, inject as testingInject, TestBed } from '@angular/core/testing';
20+
import { ComponentFixture, TestBed, inject as testingInject } from '@angular/core/testing';
2121
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
2222
import { By } from '@angular/platform-browser';
2323
import { Subject } from 'rxjs';
@@ -302,6 +302,27 @@ describe('auto-complete', () => {
302302
expect(Math.ceil(parseFloat(overlayPane.style.width))).toBe(500);
303303
});
304304

305+
it('should apply dropdown match select width', () => {
306+
component.dropdownMatchSelectWidth.set(true);
307+
component.width.set(500);
308+
fixture.detectChanges();
309+
310+
component.trigger.openPanel();
311+
fixture.detectChanges();
312+
313+
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
314+
expect(Math.ceil(parseFloat(overlayPane.style.width))).toBe(500);
315+
316+
component.dropdownMatchSelectWidth.set(false);
317+
component.trigger.closePanel();
318+
fixture.detectChanges();
319+
320+
component.trigger.openPanel();
321+
fixture.detectChanges();
322+
323+
expect(Math.ceil(parseFloat(overlayPane.style.width))).not.toBe(500);
324+
});
325+
305326
it('should backfill display value when DOWN key is pressed', async () => {
306327
component.trigger.openPanel();
307328
await stabilize(fixture, 0);
@@ -900,6 +921,7 @@ class NzTestSimpleAutocompleteComponent {
900921
[nzOverlayClassName]="overlayClassName()"
901922
[nzOverlayStyle]="overlayStyle()"
902923
[nzDataSource]="options()"
924+
[nzDropdownMatchSelectWidth]="dropdownMatchSelectWidth()"
903925
[nzDefaultActiveFirstOption]="false"
904926
nzBackfill
905927
#auto
@@ -909,6 +931,7 @@ class NzTestSimpleAutocompleteComponent {
909931
class NzTestAutocompletePropertyComponent {
910932
inputValue?: string;
911933
readonly width = signal<number | undefined>(undefined);
934+
readonly dropdownMatchSelectWidth = signal(true);
912935
readonly overlayClassName = signal('');
913936
readonly overlayStyle = signal({});
914937
readonly options = signal(['Burns Bay Road', 'Downing Street', 'Wall Street']);

components/auto-complete/doc/index.en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ When there is a need for autocomplete functionality.
3737
| Property | Description | Type | Default |
3838
| ------------------------------ | ---------------------------------------------------------------------------------------------- | ------------------------------- | ------------------------------- |
3939
| `[nzBackfill]` | backfill selected item the input when using keyboard | `boolean` | `false` |
40+
| `[nzDropdownMatchSelectWidth]` | Whether dropdown's width is same width than select. | `boolean` | `true` |
4041
| `[nzDataSource]` | Data source for autocomplete | `AutocompleteDataSource` | - |
4142
| `[nzDefaultActiveFirstOption]` | Whether active first option by default | `boolean` | `true` |
4243
| `[nzWidth]` | Custom width, unit px | `number` | trigger element width |

components/auto-complete/doc/index.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ description: 输入框自动完成功能。
3838
| 属性 | 说明 | 类型 | 默认值 |
3939
| ------------------------------ | --------------------------------------------------------------------------------------------- | ------------------------------- | ------------------------------- |
4040
| `[nzBackfill]` | 使用键盘选择选项的时候,会把当前高亮项的值即时回填到输入框中 | `boolean` | `false` |
41+
| `[nzDropdownMatchSelectWidth]` | 下拉菜单和选择器同宽 | `boolean` | `true` |
4142
| `[nzDataSource]` | 自动完成的数据源 | `AutocompleteDataSource` | - |
4243
| `[nzDefaultActiveFirstOption]` | 是否默认高亮第一个选项。 | `boolean` | `true` |
4344
| `[nzWidth]` | 自定义宽度单位 px | `number` | 触发元素宽度 |

0 commit comments

Comments
 (0)