Skip to content
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

fix(module:tree-select): update nzPlacement demo #7551

Merged
merged 12 commits into from Dec 11, 2022
3 changes: 2 additions & 1 deletion components/tree-select/demo/module
@@ -1,7 +1,8 @@
import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { FormsModule } from '@angular/forms';
import { NzRadioModule } from 'ng-zorro-antd/radio';
import { NzSpaceModule } from 'ng-zorro-antd/space';


export const moduleList = [ FormsModule, NzTreeSelectModule, NzIconModule , NzSpaceModule];
export const moduleList = [ FormsModule, NzTreeSelectModule, NzIconModule , NzRadioModule , NzSpaceModule];
13 changes: 13 additions & 0 deletions components/tree-select/demo/placement.md
@@ -0,0 +1,13 @@
---
order: 7
title:
zh-CN: 位置
en-US: Placement
---

## zh-CN

可以通过 `nzPlacement` 手动指定弹出的位置。
## en-US

You can manually specify the position of the popup via `nzPlacement`.
59 changes: 59 additions & 0 deletions components/tree-select/demo/placement.ts
@@ -0,0 +1,59 @@
import { Component } from '@angular/core';
export type NzPlacementType = 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight' | '';

@Component({
selector: 'nz-demo-tree-select-placement',
template: `
<nz-space nzDirection="vertical" style="width:100%;">
<nz-radio-group *nzSpaceItem [(ngModel)]="placement">
<label *ngFor="let item of list" nz-radio-button [nzValue]="item">{{ item }}</label>
</nz-radio-group>
<nz-tree-select
*nzSpaceItem
style="width: 120px"
nzPlaceHolder="Please select"
[nzPlacement]="placement"
[(ngModel)]="value"
[nzMaxTagCount]="3"
[nzMaxTagPlaceholder]="omittedPlaceHolder"
[nzNodes]="nodes"
[nzDropdownStyle]="{ width: '300px' }"
[nzDefaultExpandAll]="true"
[nzAllowClear]="false"
[nzMultiple]="true"
(ngModelChange)="onChange($event)"
></nz-tree-select>
<ng-template #omittedPlaceHolder let-omittedValues>and {{ omittedValues.length }} more...</ng-template>
</nz-space>
`
})
export class NzDemoTreeSelectPlacementComponent {
list: NzPlacementType[] = ['topLeft', 'topRight', 'bottomLeft', 'bottomRight'];
placement: NzPlacementType = 'topLeft';
value: string[] = [];
nodes = [
{
title: 'parent 1',
key: '100',
children: [
{
title: 'parent 1-0',
key: '1001',
children: [
{ title: 'leaf 1-0-0', key: '10010', isLeaf: true },
{ title: 'leaf 1-0-1', key: '10011', isLeaf: true }
]
},
{
title: 'parent 1-1',
key: '1002',
children: [{ title: 'leaf 1-1-0', key: '10020', isLeaf: true }]
}
]
}
];

onChange($event: string[]): void {
console.log($event);
}
}
1 change: 1 addition & 0 deletions components/tree-select/doc/index.en-US.md
Expand Up @@ -25,6 +25,7 @@ import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
| `[nzId]` | input id attribute inside the component| `string` | - |
| `[nzAllowClear]` | Whether allow clear | `boolean` | `false` |
| `[nzPlaceHolder]` | Placeholder of the select input | `string` | - |
| `[nzPlacement]` | The position where the selection box pops up | `bottomLeft` `bottomRight` `topLeft` `topRight` | bottomLeft |
| `[nzDisabled]` | Disabled or not | `boolean` | `false` |
| `[nzShowIcon]` | Shows the icon before a TreeNode's title. There is no default style | `boolean` | `false` |
| `[nzShowSearch]` | Whether to display a search input in the dropdown menu(valid only in the single mode) | `boolean` | `false` | ✅ |
Expand Down
1 change: 1 addition & 0 deletions components/tree-select/doc/index.zh-CN.md
Expand Up @@ -25,6 +25,7 @@ import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
| `[nzId]` | 组件内部 input 的 id 值 | `string` | - |
| `[nzAllowClear]` | 显示清除按钮 | `boolean` | `false` |
| `[nzPlaceHolder]` | 选择框默认文字 | `string` | - |
| `[nzPlacement]` | 选择框弹出的位置 | `bottomLeft` `bottomRight` `topLeft` `topRight` | bottomLeft |
| `[nzDisabled]` | 禁用选择器 | `boolean` | `false` |
| `[nzShowIcon]` | 是否展示 TreeNode title 前的图标,没有默认样式 | `boolean` | `false` | ✅ |
| `[nzShowSearch]` | 显示搜索框 | `boolean` | `false` |
Expand Down
30 changes: 27 additions & 3 deletions components/tree-select/tree-select.component.ts
Expand Up @@ -6,7 +6,12 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { Direction, Directionality } from '@angular/cdk/bidi';
import { BACKSPACE, ESCAPE, TAB } from '@angular/cdk/keycodes';
import { CdkConnectedOverlay, CdkOverlayOrigin, ConnectedOverlayPositionChange } from '@angular/cdk/overlay';
import {
CdkConnectedOverlay,
CdkOverlayOrigin,
ConnectedOverlayPositionChange,
ConnectionPositionPair
} from '@angular/cdk/overlay';
import {
ChangeDetectorRef,
Component,
Expand Down Expand Up @@ -36,6 +41,7 @@ import { slideMotion } from 'ng-zorro-antd/core/animation';
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { NzFormNoStatusService, NzFormStatusService } from 'ng-zorro-antd/core/form';
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
import { POSITION_MAP } from 'ng-zorro-antd/core/overlay';
import { reqAnimFrame } from 'ng-zorro-antd/core/polyfill';
import {
NzFormatEmitEvent,
Expand Down Expand Up @@ -65,8 +71,15 @@ export function higherOrderServiceFactory(injector: Injector): NzTreeBaseService
return injector.get(NzTreeSelectService);
}

export type NzPlacementType = 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight' | '';
const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'treeSelect';
const TREE_SELECT_DEFAULT_CLASS = 'ant-select-dropdown ant-select-tree-dropdown';
const listOfPositions = [
POSITION_MAP.bottomLeft,
POSITION_MAP.bottomRight,
POSITION_MAP.topRight,
POSITION_MAP.topLeft
];

@Component({
selector: 'nz-tree-select',
Expand All @@ -78,6 +91,7 @@ const TREE_SELECT_DEFAULT_CLASS = 'ant-select-dropdown ant-select-tree-dropdown'
nzConnectedOverlay
[cdkConnectedOverlayHasBackdrop]="nzBackdrop"
[cdkConnectedOverlayOrigin]="cdkOverlayOrigin"
[cdkConnectedOverlayPositions]="nzPlacement ? positions : []"
[cdkConnectedOverlayOpen]="nzOpen"
[cdkConnectedOverlayTransformOriginOn]="'.ant-select-tree-dropdown'"
[cdkConnectedOverlayMinWidth]="$any(nzDropdownMatchSelectWidth ? null : triggerWidth)"
Expand Down Expand Up @@ -273,6 +287,7 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
@Input() nzDropdownClassName?: string;
@Input() @WithConfig() nzBackdrop = false;
@Input() nzStatus: NzStatus = '';
@Input() nzPlacement: NzPlacementType = '';
@Input()
set nzExpandedKeys(value: string[]) {
this.expandedKeys = value;
Expand Down Expand Up @@ -322,6 +337,7 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
expandedKeys: string[] = [];
value: string[] = [];
dir: Direction = 'ltr';
positions: ConnectionPositionPair[] = [];

private destroy$ = new Subject<void>();

Expand Down Expand Up @@ -427,7 +443,7 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
}

ngOnChanges(changes: SimpleChanges): void {
const { nzNodes, nzDropdownClassName, nzStatus } = changes;
const { nzNodes, nzDropdownClassName, nzStatus, nzPlacement } = changes;
if (nzNodes) {
this.updateSelectedNodes(true);
}
Expand All @@ -438,6 +454,12 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
if (nzStatus) {
this.setStatusStyles(this.nzStatus, this.hasFeedback);
}

if (nzPlacement && this.nzPlacement) {
if (POSITION_MAP[this.nzPlacement]) {
this.positions = [POSITION_MAP[this.nzPlacement]];
}
}
}

writeValue(value: string[] | string): void {
Expand Down Expand Up @@ -647,7 +669,9 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
}

updateCdkConnectedOverlayStatus(): void {
this.triggerWidth = this.cdkOverlayOrigin.elementRef.nativeElement.getBoundingClientRect().width;
if (!this.nzPlacement || !listOfPositions.includes(POSITION_MAP[this.nzPlacement])) {
this.triggerWidth = this.cdkOverlayOrigin.elementRef.nativeElement.getBoundingClientRect().width;
}
}

trackValue(_index: number, option: NzTreeNode): string {
Expand Down