Skip to content

Commit 7470ed6

Browse files
withOutUndoMaster Yi
andauthored
fix(module:button): fix add class ant-btn-icon-only(#7631) (#7678)
* fix(module:button): fix add class ant-btn-icon-only(#7631) * test(module:button): add class ant-btn-icon-only --------- Co-authored-by: Master Yi <xh765155143@gmail.com>
1 parent ceb29a9 commit 7470ed6

File tree

2 files changed

+85
-22
lines changed

2 files changed

+85
-22
lines changed

components/button/button.component.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'button';
6262
'[class.ant-btn-block]': `nzBlock`,
6363
'[class.ant-input-search-button]': `nzSearch`,
6464
'[class.ant-btn-rtl]': `dir === 'rtl'`,
65+
'[class.ant-btn-icon-only]': `iconOnly`,
6566
'[attr.tabindex]': 'disabled ? -1 : (tabIndex === null ? null : tabIndex)',
6667
'[attr.disabled]': 'disabled || null'
6768
}
@@ -101,28 +102,17 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
101102
});
102103
}
103104

104-
assertIconOnly(element: HTMLButtonElement, renderer: Renderer2): void {
105-
const listOfNode = Array.from(element.childNodes);
106-
const iconCount = listOfNode.filter(node => {
107-
const iconChildNodes = Array.from(node.childNodes || []);
108-
return node.nodeName === 'SPAN' && iconChildNodes.length > 0 && iconChildNodes.every(ic => ic.nodeName === 'svg');
109-
}).length;
105+
public get iconOnly(): boolean {
106+
const listOfNode = Array.from((this.elementRef?.nativeElement as HTMLButtonElement)?.childNodes || []);
110107
const noText = listOfNode.every(node => node.nodeName !== '#text');
111-
// ignore icon
112-
const noSpan = listOfNode
113-
.filter(node => {
114-
const iconChildNodes = Array.from(node.childNodes || []);
115-
return !(
116-
node.nodeName === 'SPAN' &&
117-
iconChildNodes.length > 0 &&
118-
iconChildNodes.every(ic => ic.nodeName === 'svg')
119-
);
120-
})
121-
.every(node => node.nodeName !== 'SPAN');
122-
const isIconOnly = noSpan && noText && iconCount >= 1;
123-
if (isIconOnly) {
124-
renderer.addClass(element, 'ant-btn-icon-only');
125-
}
108+
// ignore icon and comment
109+
const noSpan =
110+
listOfNode.filter(node => {
111+
return !(node.nodeName === '#comment' || !!(node as HTMLElement)?.attributes?.getNamedItem('nz-icon'));
112+
}).length == 0;
113+
const isIconOnly = !!this.nzIconDirectiveElement && noSpan && noText;
114+
115+
return isIconOnly;
126116
}
127117

128118
constructor(
@@ -173,7 +163,6 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
173163
}
174164

175165
ngAfterViewInit(): void {
176-
this.assertIconOnly(this.elementRef.nativeElement, this.renderer);
177166
this.insertSpan(this.elementRef.nativeElement.childNodes, this.renderer);
178167
}
179168

components/button/button.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,42 @@ describe('button', () => {
132132
testBed.fixture.detectChanges();
133133
expect(buttonElement.classList).toContain('ant-btn-icon-only');
134134
}));
135+
it('should icon only works correctly with any tag', fakeAsync(() => {
136+
const testBed = createComponentBed(TestButtonIconOnlyWithAnyTagComponent, {
137+
imports: [NzIconTestModule],
138+
declarations: [NzButtonComponent]
139+
});
140+
const buttonElement = testBed.debugElement.query(By.directive(NzButtonComponent)).nativeElement;
141+
testBed.fixture.detectChanges();
142+
expect(buttonElement.classList).toContain('ant-btn-icon-only');
143+
}));
144+
it('should icon only works correctly with any Comment', fakeAsync(() => {
145+
const testBed = createComponentBed(TestButtonIconOnlyWithCommentComponent, {
146+
imports: [NzIconTestModule],
147+
declarations: [NzButtonComponent]
148+
});
149+
const buttonElement = testBed.debugElement.query(By.directive(NzButtonComponent)).nativeElement;
150+
testBed.fixture.detectChanges();
151+
expect(buttonElement.classList).toContain('ant-btn-icon-only');
152+
}));
153+
it('should icon only works correctly with any text', fakeAsync(() => {
154+
const testBed = createComponentBed(TestButtonIconOnlyWithTextComponent, {
155+
imports: [NzIconTestModule],
156+
declarations: [NzButtonComponent]
157+
});
158+
const buttonElement = testBed.debugElement.query(By.directive(NzButtonComponent)).nativeElement;
159+
testBed.fixture.detectChanges();
160+
expect(buttonElement.classList).not.toContain('ant-btn-icon-only');
161+
}));
162+
it('should icon only works correctly without nz-icon', fakeAsync(() => {
163+
const testBed = createComponentBed(TestButtonIconOnlyWithoutIconComponent, {
164+
imports: [NzIconTestModule],
165+
declarations: [NzButtonComponent]
166+
});
167+
const buttonElement = testBed.debugElement.query(By.directive(NzButtonComponent)).nativeElement;
168+
testBed.fixture.detectChanges();
169+
expect(buttonElement.classList).not.toContain('ant-btn-icon-only');
170+
}));
135171
it('should icon only loading works correctly', () => {
136172
const testBed = createComponentBed(TestButtonIconOnlyLoadingComponent, {
137173
imports: [NzIconTestModule],
@@ -283,6 +319,44 @@ export class TestButtonWithIconComponent implements OnInit {
283319
})
284320
export class TestButtonIconOnlyComponent {}
285321

322+
@Component({
323+
template: `
324+
<button nz-button>
325+
<u nz-icon nzType="up"></u>
326+
</button>
327+
`
328+
})
329+
export class TestButtonIconOnlyWithAnyTagComponent {}
330+
331+
@Component({
332+
template: `
333+
<button nz-button>
334+
<i nz-icon nzType="down"></i>
335+
<!-- Comment -->
336+
</button>
337+
`
338+
})
339+
export class TestButtonIconOnlyWithCommentComponent {}
340+
341+
@Component({
342+
template: `
343+
<button nz-button>
344+
<i nz-icon nzType="down"></i>
345+
text
346+
</button>
347+
`
348+
})
349+
export class TestButtonIconOnlyWithTextComponent {}
350+
351+
@Component({
352+
template: `
353+
<button nz-button>
354+
<span>text</span>
355+
</button>
356+
`
357+
})
358+
export class TestButtonIconOnlyWithoutIconComponent {}
359+
286360
@Component({
287361
template: `
288362
<button nz-button nzLoading>

0 commit comments

Comments
 (0)