Skip to content

Commit 2c3b0a3

Browse files
fix(module:button): improve icon only logic in zoneless mode (#9540)
1 parent 0096622 commit 2c3b0a3

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

components/button/button.component.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,36 +122,20 @@ export class NzButtonComponent implements OnChanges, AfterViewInit, AfterContent
122122

123123
readonly iconOnly = computed(() => this.elementOnly() && (!!this.iconDir() || !!this.loadingIconDir()));
124124

125-
insertSpan(nodes: NodeList, renderer: Renderer2): void {
126-
nodes.forEach(node => {
127-
if (node.nodeType === Node.TEXT_NODE && node.textContent!.trim().length > 0) {
128-
const span = renderer.createElement('span');
129-
const parent = renderer.parentNode(node);
130-
renderer.insertBefore(parent, span, node);
131-
renderer.appendChild(span, node);
132-
}
133-
});
134-
}
135-
136125
constructor() {
137126
onConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME, () => {
138127
this.size.set(this.nzSize);
139128
this.cdr.markForCheck();
140129
});
141130

142-
let elementOnly = false;
143131
afterEveryRender({
144132
read: () => {
145-
const { childNodes, children } = this.elementRef.nativeElement;
146-
const hasText = Array.from(childNodes).some(
147-
node => node.nodeType === Node.TEXT_NODE && node.textContent!.trim().length > 0
148-
);
149-
const visibleElementCount = Array.from(children).filter(
133+
const { children } = this.elementRef.nativeElement;
134+
const visibleElement = Array.from(children).filter(
150135
element => (element as HTMLElement).style.display !== 'none'
151-
).length;
152-
elementOnly = !hasText && visibleElementCount === 1;
153-
},
154-
write: () => this.elementOnly.set(elementOnly)
136+
);
137+
this.elementOnly.set(visibleElement.length === 1);
138+
}
155139
});
156140
}
157141

@@ -189,7 +173,7 @@ export class NzButtonComponent implements OnChanges, AfterViewInit, AfterContent
189173
}
190174

191175
ngAfterViewInit(): void {
192-
this.insertSpan(this.elementRef.nativeElement.childNodes, this.renderer);
176+
this.insertSpan();
193177
}
194178

195179
ngAfterContentInit(): void {
@@ -208,4 +192,15 @@ export class NzButtonComponent implements OnChanges, AfterViewInit, AfterContent
208192
}
209193
});
210194
}
195+
196+
insertSpan(): void {
197+
this.elementRef.nativeElement.childNodes.forEach(node => {
198+
if (node.nodeType === Node.TEXT_NODE && node.textContent!.trim().length > 0) {
199+
const span = this.renderer.createElement('span');
200+
const parent = this.renderer.parentNode(node);
201+
this.renderer.insertBefore(parent, span, node);
202+
this.renderer.appendChild(span, node);
203+
}
204+
});
205+
}
211206
}

0 commit comments

Comments
 (0)