Skip to content

Commit

Permalink
fix(button): fix auto align classes when contains html children (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeluzhenko authored and pimenovoleg committed Oct 23, 2019
1 parent c5ce58c commit d917eda
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
42 changes: 42 additions & 0 deletions packages/mosaic/button/button.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ describe('Button with icon', () => {
imports: [McButtonModule, McIconModule],
declarations: [
McButtonCommentCaseTestApp,
McButtonHtmlIconLeftCaseTestApp,
McButtonHtmlIconRightCaseTestApp,
McButtonTextIconCaseTestApp,
McButtonTwoIconsCaseTestApp
]
Expand Down Expand Up @@ -184,6 +186,24 @@ describe('Button with icon', () => {
expect(fixture.debugElement.query(By.css('.mc-icon-button_left'))).toBeNull();
expect(fixture.debugElement.query(By.css('.mc-icon-button_right'))).toBeNull();
});

it('should add right css class when the sibling in an html element', () => {
const fixture = TestBed.createComponent(McButtonHtmlIconRightCaseTestApp);

fixture.detectChanges();

expect(fixture.debugElement.query(By.css('.mc-icon-button_right')))
.toBeTruthy('Expected the element has right css class');
});

it('should add left css class when the sibling in an html element', () => {
const fixture = TestBed.createComponent(McButtonHtmlIconLeftCaseTestApp);

fixture.detectChanges();

expect(fixture.debugElement.query(By.css('.mc-icon-button_left')))
.toBeTruthy('Expected the element has left css class');
});
});


Expand Down Expand Up @@ -224,6 +244,28 @@ class TestApp {
})
class McButtonCommentCaseTestApp {}

@Component({
selector: 'mc-button-two-icons-case-test-app',
template: `
<button mc-button type="button">
<span>Some text</span>
<i mc-icon="mc-angle-down-L_16"></i>
</button>
`
})
class McButtonHtmlIconRightCaseTestApp {}

@Component({
selector: 'mc-button-two-icons-case-test-app',
template: `
<button mc-button type="button">
<i mc-icon="mc-angle-down-L_16"></i>
<span>Some text</span>
</button>
`
})
class McButtonHtmlIconLeftCaseTestApp {}

@Component({
selector: 'mc-button-text-icon-case-test-app',
template: `
Expand Down
33 changes: 16 additions & 17 deletions packages/mosaic/button/button.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Directive,
ElementRef,
OnDestroy,
ViewEncapsulation
ViewEncapsulation,
Renderer2
} from '@angular/core';
import { FocusMonitor } from '@ptsecurity/cdk/a11y';
import { mixinColor, mixinDisabled, CanColor, CanDisable, CanDisableCtor, CanColorCtor } from '@ptsecurity/mosaic/core';
Expand All @@ -26,7 +27,10 @@ export class McButtonCssStyler {

private icons: HTMLElement[] = [];

constructor(elementRef: ElementRef) {
constructor(
elementRef: ElementRef,
private renderer: Renderer2
) {
this.nativeElement = elementRef.nativeElement;
}

Expand All @@ -43,28 +47,23 @@ export class McButtonCssStyler {

private addClassModificatorForIcons() {
const twoIcons = 2;
const [firstIconElement, secondIconElement] = this.icons;

if (this.icons.length === 1) {
const iconElement = this.icons[0];
const COMMENT_NODE = 8;

if (!iconElement.previousElementSibling && !iconElement.nextElementSibling) {
if (iconElement.nextSibling && iconElement.nextSibling.nodeType !== COMMENT_NODE) {
iconElement.classList.add('mc-icon_left');
this.nativeElement.classList.add('mc-icon-button_left');
}
if (firstIconElement.nextSibling && firstIconElement.nextSibling.nodeType !== COMMENT_NODE) {
this.renderer.addClass(firstIconElement, 'mc-icon_left');
this.renderer.addClass(this.nativeElement, 'mc-icon-button_left');
}

if (iconElement.previousSibling && iconElement.previousSibling.nodeType !== COMMENT_NODE) {
iconElement.classList.add('mc-icon_right');
this.nativeElement.classList.add('mc-icon-button_right');
}
if (firstIconElement.previousSibling && firstIconElement.previousSibling.nodeType !== COMMENT_NODE) {
this.renderer.addClass(firstIconElement, 'mc-icon_right');
this.renderer.addClass(this.nativeElement, 'mc-icon-button_right');
}
} else if (this.icons.length === twoIcons) {
const firstIconElement = this.icons[0];
const secondIconElement = this.icons[1];

firstIconElement.classList.add('mc-icon_left');
secondIconElement.classList.add('mc-icon_right');
this.renderer.addClass(firstIconElement, 'mc-icon_left');
this.renderer.addClass(secondIconElement, 'mc-icon_right');
}
}
}
Expand Down

0 comments on commit d917eda

Please sign in to comment.