@@ -15,28 +15,35 @@ import {
15
15
Input ,
16
16
NgZone ,
17
17
OnChanges ,
18
- OnDestroy ,
19
18
OnInit ,
20
19
Renderer2 ,
21
20
SimpleChanges ,
22
21
ViewEncapsulation ,
23
- booleanAttribute
22
+ booleanAttribute ,
23
+ computed ,
24
+ inject ,
25
+ signal
24
26
} from '@angular/core' ;
25
27
import { Subject , fromEvent } from 'rxjs' ;
26
28
import { filter , startWith , takeUntil } from 'rxjs/operators' ;
27
29
28
30
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' ;
29
33
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' ;
30
35
31
36
export type NzButtonType = 'primary' | 'default' | 'dashed' | 'link' | 'text' | null ;
32
37
export type NzButtonShape = 'circle' | 'round' | null ;
33
- export type NzButtonSize = 'large' | 'default' | 'small' ;
38
+ export type NzButtonSize = NzSizeLDSType ;
34
39
35
40
const NZ_CONFIG_MODULE_NAME : NzConfigKey = 'button' ;
36
41
37
42
@Component ( {
38
43
selector : 'button[nz-button], a[nz-button]' ,
39
44
exportAs : 'nzButton' ,
45
+ standalone : true ,
46
+ imports : [ NzIconModule ] ,
40
47
preserveWhitespaces : false ,
41
48
changeDetection : ChangeDetectionStrategy . OnPush ,
42
49
encapsulation : ViewEncapsulation . None ,
@@ -55,8 +62,8 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'button';
55
62
'[class.ant-btn-text]' : `nzType === 'text'` ,
56
63
'[class.ant-btn-circle]' : `nzShape === 'circle'` ,
57
64
'[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'` ,
60
67
'[class.ant-btn-dangerous]' : `nzDanger` ,
61
68
'[class.ant-btn-loading]' : `nzLoading` ,
62
69
'[class.ant-btn-background-ghost]' : `nzGhost` ,
@@ -67,10 +74,10 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'button';
67
74
'[attr.tabindex]' : 'disabled ? -1 : (tabIndex === null ? null : tabIndex)' ,
68
75
'[attr.disabled]' : 'disabled || null'
69
76
} ,
70
- imports : [ NzIconModule ] ,
71
- standalone : true
77
+ hostDirectives : [ NzSpaceCompactItemDirective ] ,
78
+ providers : [ NzDestroyService , { provide : NZ_SPACE_COMPACT_ITEM_TYPE , useValue : 'btn' } ]
72
79
} )
73
- export class NzButtonComponent implements OnDestroy , OnChanges , AfterViewInit , AfterContentInit , OnInit {
80
+ export class NzButtonComponent implements OnChanges , AfterViewInit , AfterContentInit , OnInit {
74
81
readonly _nzModuleName : NzConfigKey = NZ_CONFIG_MODULE_NAME ;
75
82
76
83
@ContentChild ( NzIconDirective , { read : ElementRef } ) nzIconDirectiveElement ! : ElementRef ;
@@ -85,7 +92,17 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
85
92
@Input ( ) nzShape : NzButtonShape = null ;
86
93
@Input ( ) @WithConfig ( ) nzSize : NzButtonSize = 'default' ;
87
94
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 ) ;
89
106
private loading$ = new Subject < boolean > ( ) ;
90
107
91
108
insertSpan ( nodes : NodeList , renderer : Renderer2 ) : void {
@@ -117,16 +134,18 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
117
134
private renderer : Renderer2 ,
118
135
public nzConfigService : NzConfigService ,
119
136
private directionality : Directionality
120
- ) {
137
+ ) { }
138
+
139
+ ngOnInit ( ) : void {
140
+ this . size . set ( this . nzSize ) ;
121
141
this . nzConfigService
122
142
. getConfigChangeEventForComponent ( NZ_CONFIG_MODULE_NAME )
123
143
. pipe ( takeUntil ( this . destroy$ ) )
124
144
. subscribe ( ( ) => {
145
+ this . size . set ( this . nzSize ) ;
125
146
this . cdr . markForCheck ( ) ;
126
147
} ) ;
127
- }
128
148
129
- ngOnInit ( ) : void {
130
149
this . directionality . change ?. pipe ( takeUntil ( this . destroy$ ) ) . subscribe ( ( direction : Direction ) => {
131
150
this . dir = direction ;
132
151
this . cdr . detectChanges ( ) ;
@@ -150,11 +169,13 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
150
169
} ) ;
151
170
}
152
171
153
- ngOnChanges ( changes : SimpleChanges ) : void {
154
- const { nzLoading } = changes ;
172
+ ngOnChanges ( { nzLoading, nzSize } : SimpleChanges ) : void {
155
173
if ( nzLoading ) {
156
174
this . loading$ . next ( this . nzLoading ) ;
157
175
}
176
+ if ( nzSize ) {
177
+ this . size . set ( nzSize . currentValue ) ;
178
+ }
158
179
}
159
180
160
181
ngAfterViewInit ( ) : void {
@@ -177,9 +198,4 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
177
198
}
178
199
} ) ;
179
200
}
180
-
181
- ngOnDestroy ( ) : void {
182
- this . destroy$ . next ( ) ;
183
- this . destroy$ . complete ( ) ;
184
- }
185
201
}
0 commit comments