Skip to content

Commit b9c511d

Browse files
feat(module:space): add space compact component (#8755)
1 parent 776fb66 commit b9c511d

33 files changed

+1286
-260
lines changed

components/button/button-group.component.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { takeUntil } from 'rxjs/operators';
1010

1111
export type NzButtonGroupSize = 'large' | 'default' | 'small';
1212

13+
/**
14+
* @deprecated Will be removed in v20. Use `NzSpaceCompactComponent` instead.
15+
*/
1316
@Component({
1417
selector: 'nz-button-group',
1518
exportAs: 'nzButtonGroup',

components/button/button.component.ts

+35-19
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,35 @@ import {
1515
Input,
1616
NgZone,
1717
OnChanges,
18-
OnDestroy,
1918
OnInit,
2019
Renderer2,
2120
SimpleChanges,
2221
ViewEncapsulation,
23-
booleanAttribute
22+
booleanAttribute,
23+
computed,
24+
inject,
25+
signal
2426
} from '@angular/core';
2527
import { Subject, fromEvent } from 'rxjs';
2628
import { filter, startWith, takeUntil } from 'rxjs/operators';
2729

2830
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
31+
import { NzDestroyService } from 'ng-zorro-antd/core/services';
32+
import { NzSizeLDSType } from 'ng-zorro-antd/core/types';
2933
import { NzIconDirective, NzIconModule } from 'ng-zorro-antd/icon';
34+
import { NZ_SPACE_COMPACT_ITEM_TYPE, NZ_SPACE_COMPACT_SIZE, NzSpaceCompactItemDirective } from 'ng-zorro-antd/space';
3035

3136
export type NzButtonType = 'primary' | 'default' | 'dashed' | 'link' | 'text' | null;
3237
export type NzButtonShape = 'circle' | 'round' | null;
33-
export type NzButtonSize = 'large' | 'default' | 'small';
38+
export type NzButtonSize = NzSizeLDSType;
3439

3540
const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'button';
3641

3742
@Component({
3843
selector: 'button[nz-button], a[nz-button]',
3944
exportAs: 'nzButton',
45+
standalone: true,
46+
imports: [NzIconModule],
4047
preserveWhitespaces: false,
4148
changeDetection: ChangeDetectionStrategy.OnPush,
4249
encapsulation: ViewEncapsulation.None,
@@ -55,8 +62,8 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'button';
5562
'[class.ant-btn-text]': `nzType === 'text'`,
5663
'[class.ant-btn-circle]': `nzShape === 'circle'`,
5764
'[class.ant-btn-round]': `nzShape === 'round'`,
58-
'[class.ant-btn-lg]': `nzSize === 'large'`,
59-
'[class.ant-btn-sm]': `nzSize === 'small'`,
65+
'[class.ant-btn-lg]': `finalSize() === 'large'`,
66+
'[class.ant-btn-sm]': `finalSize() === 'small'`,
6067
'[class.ant-btn-dangerous]': `nzDanger`,
6168
'[class.ant-btn-loading]': `nzLoading`,
6269
'[class.ant-btn-background-ghost]': `nzGhost`,
@@ -67,10 +74,10 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'button';
6774
'[attr.tabindex]': 'disabled ? -1 : (tabIndex === null ? null : tabIndex)',
6875
'[attr.disabled]': 'disabled || null'
6976
},
70-
imports: [NzIconModule],
71-
standalone: true
77+
hostDirectives: [NzSpaceCompactItemDirective],
78+
providers: [NzDestroyService, { provide: NZ_SPACE_COMPACT_ITEM_TYPE, useValue: 'btn' }]
7279
})
73-
export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, AfterContentInit, OnInit {
80+
export class NzButtonComponent implements OnChanges, AfterViewInit, AfterContentInit, OnInit {
7481
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
7582

7683
@ContentChild(NzIconDirective, { read: ElementRef }) nzIconDirectiveElement!: ElementRef;
@@ -85,7 +92,17 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
8592
@Input() nzShape: NzButtonShape = null;
8693
@Input() @WithConfig() nzSize: NzButtonSize = 'default';
8794
dir: Direction = 'ltr';
88-
private destroy$ = new Subject<void>();
95+
96+
protected finalSize = computed(() => {
97+
if (this.compactSize) {
98+
return this.compactSize();
99+
}
100+
return this.size();
101+
});
102+
103+
private size = signal<NzSizeLDSType>(this.nzSize);
104+
private compactSize = inject(NZ_SPACE_COMPACT_SIZE, { optional: true });
105+
private destroy$ = inject(NzDestroyService);
89106
private loading$ = new Subject<boolean>();
90107

91108
insertSpan(nodes: NodeList, renderer: Renderer2): void {
@@ -117,16 +134,18 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
117134
private renderer: Renderer2,
118135
public nzConfigService: NzConfigService,
119136
private directionality: Directionality
120-
) {
137+
) {}
138+
139+
ngOnInit(): void {
140+
this.size.set(this.nzSize);
121141
this.nzConfigService
122142
.getConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME)
123143
.pipe(takeUntil(this.destroy$))
124144
.subscribe(() => {
145+
this.size.set(this.nzSize);
125146
this.cdr.markForCheck();
126147
});
127-
}
128148

129-
ngOnInit(): void {
130149
this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
131150
this.dir = direction;
132151
this.cdr.detectChanges();
@@ -150,11 +169,13 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
150169
});
151170
}
152171

153-
ngOnChanges(changes: SimpleChanges): void {
154-
const { nzLoading } = changes;
172+
ngOnChanges({ nzLoading, nzSize }: SimpleChanges): void {
155173
if (nzLoading) {
156174
this.loading$.next(this.nzLoading);
157175
}
176+
if (nzSize) {
177+
this.size.set(nzSize.currentValue);
178+
}
158179
}
159180

160181
ngAfterViewInit(): void {
@@ -177,9 +198,4 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
177198
}
178199
});
179200
}
180-
181-
ngOnDestroy(): void {
182-
this.destroy$.next();
183-
this.destroy$.complete();
184-
}
185201
}

components/button/demo/button-group.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ title:
1111

1212
通过设置 `nzSize``large` `small` 分别把按钮组合设为大、小尺寸。若不设置 `nzSize`,则尺寸为中。
1313

14+
警告:`<nz-button-group>``v19` 中被弃用,请使用 `<nz-space-compact>` 组件替代。
15+
1416
## en-US
1517

1618
Buttons can be grouped by placing multiple `nz-button` components into a `nz-button-group`.
1719

1820
The `nzSize` can be set to `large`, `small` or left unset resulting in a default size.
21+
22+
Warning: `<nz-button-group>` is deprecated in `v19`, please use `<nz-space-compact>` instead.

components/button/style/space-compact.less

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
// Special styles for Primary Button
1515
&-compact-item.@{btn-prefix-cls}-primary {
16-
&:not([disabled]) + &:not([disabled]) {
16+
&:not([disabled]) + &:not([disabled]):not([ant-click-animating-without-extra-node='true']) {
1717
position: relative;
1818

1919
&::after {
@@ -69,7 +69,7 @@
6969
// Special styles for Primary Button
7070
&-compact-vertical-item {
7171
&.@{btn-prefix-cls}-primary {
72-
&:not([disabled]) + &:not([disabled]) {
72+
&:not([disabled]) + &:not([disabled]):not([ant-click-animating-without-extra-node='true']) {
7373
position: relative;
7474

7575
&::after {

0 commit comments

Comments
 (0)