@@ -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' ;
2527import { Subject , fromEvent } from 'rxjs' ;
2628import { filter , startWith , takeUntil } from 'rxjs/operators' ;
2729
2830import { 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' ;
2933import { 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
3136export type NzButtonType = 'primary' | 'default' | 'dashed' | 'link' | 'text' | null ;
3237export type NzButtonShape = 'circle' | 'round' | null ;
33- export type NzButtonSize = 'large' | 'default' | 'small' ;
38+ export type NzButtonSize = NzSizeLDSType ;
3439
3540const 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}
0 commit comments