Skip to content

Commit c9b43d2

Browse files
authored
Merge pull request #5578 from IgniteUI/rkolev/fix-5404-8.1.x
Fix IgxButton has incorrect CSS class when setting new type - 8.1.x
2 parents 9e03ece + 3523652 commit c9b43d2

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

projects/igniteui-angular/src/lib/directives/button/button.directive.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ const FAB_BUTTON_COSY = 'igx-button--fab-cosy';
1717

1818
describe('IgxButton', () => {
1919
configureTestSuite();
20+
21+
const baseClass = 'igx-button';
22+
const classes = {
23+
flat: `${baseClass}--flat`,
24+
raised: `${baseClass}--raised`,
25+
outlined: `${baseClass}--outlined`,
26+
fab: `${baseClass}--fab`,
27+
icon: `${baseClass}--icon`
28+
};
29+
2030
beforeEach(async(() => {
2131
TestBed.configureTestingModule({
2232
declarations: [
@@ -108,6 +118,40 @@ describe('IgxButton', () => {
108118
verifyDisplayDensity(fabButton, fabButtonDOM, 'fab', DisplayDensity.cosy);
109119
verifyDisplayDensity(iconButton, iconButtonDOM, 'icon', DisplayDensity.cosy);
110120
});
121+
122+
it('Should set the correct CSS class on the element using the "type" input', () => {
123+
const fixture = TestBed.createComponent(InitButtonComponent);
124+
fixture.detectChanges();
125+
const theButton = fixture.componentInstance.button;
126+
const theButtonNativeEl = theButton.nativeElement;
127+
expect(theButtonNativeEl.classList.length).toEqual(1);
128+
expect(theButtonNativeEl.classList).toContain(classes.flat);
129+
130+
theButton.type = 'raised';
131+
fixture.detectChanges();
132+
expect(theButtonNativeEl.classList.length).toEqual(1);
133+
expect(theButtonNativeEl.classList).toContain(classes.raised);
134+
135+
theButton.type = 'outlined';
136+
fixture.detectChanges();
137+
expect(theButtonNativeEl.classList.length).toEqual(1);
138+
expect(theButtonNativeEl.classList).toContain(classes.outlined);
139+
140+
theButton.type = 'fab';
141+
fixture.detectChanges();
142+
expect(theButtonNativeEl.classList.length).toEqual(1);
143+
expect(theButtonNativeEl.classList).toContain(classes.fab);
144+
145+
theButton.type = 'icon';
146+
fixture.detectChanges();
147+
expect(theButtonNativeEl.classList.length).toEqual(1);
148+
expect(theButtonNativeEl.classList).toContain(classes.icon);
149+
150+
theButton.type = 'flat';
151+
fixture.detectChanges();
152+
expect(theButtonNativeEl.classList.length).toEqual(1);
153+
expect(theButtonNativeEl.classList).toContain(classes.flat);
154+
});
111155
});
112156

113157
@Component({
@@ -117,6 +161,8 @@ describe('IgxButton', () => {
117161
</span>`
118162
})
119163
class InitButtonComponent {
164+
@ViewChild(IgxButtonDirective, { read: IgxButtonDirective, static: true })
165+
button: IgxButtonDirective;
120166
}
121167

122168
@Component({

projects/igniteui-angular/src/lib/directives/button/button.directive.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,41 @@ import { DisplayDensityBase, DisplayDensityToken, IDisplayDensityOptions, Displa
1717
selector: '[igxButton]'
1818
})
1919
export class IgxButtonDirective extends DisplayDensityBase {
20+
2021
/**
2122
*@hidden
2223
*/
23-
private _type = 'flat';
24+
private _type: string;
25+
2426
/**
2527
*@hidden
2628
*/
27-
private _cssClass = 'igx-button';
29+
private _defaultType = 'flat';
30+
31+
/**
32+
*@hidden
33+
*/
34+
private _cssClassPrefix = 'igx-button';
35+
2836
/**
2937
*@hidden
3038
*/
3139
private _color: string;
40+
3241
/**
3342
*@hidden
3443
*/
3544
private _label: string;
45+
3646
/**
3747
*@hidden
3848
*/
3949
private _backgroundColor: string;
4050

4151
constructor(public element: ElementRef, private _renderer: Renderer2,
4252
@Optional() @Inject(DisplayDensityToken) protected _displayDensityOptions: IDisplayDensityOptions) {
43-
super(_displayDensityOptions);
44-
}
53+
super(_displayDensityOptions);
54+
}
4555

4656
/**
4757
* Returns the underlying DOM element
@@ -66,18 +76,26 @@ export class IgxButtonDirective extends DisplayDensityBase {
6676
* ```
6777
* @memberof IgxButtonDirective
6878
*/
69-
@HostBinding('attr.role') public role = 'button';
79+
@HostBinding('attr.role')
80+
public role = 'button';
81+
7082
/**
7183
* Sets the type of the button.
7284
* ```html
7385
* <button igxButton= "icon"></button>
7486
* ```
7587
* @memberof IgxButtonDirective
7688
*/
77-
@Input('igxButton') set type(value: string) {
78-
this._type = value || this._type;
79-
this._renderer.addClass(this.nativeElement, `${this._cssClass}--${this._type}`);
89+
@Input('igxButton')
90+
set type(value: string) {
91+
const newValue = value ? value : this._defaultType;
92+
if (this._type !== newValue) {
93+
this._renderer.removeClass(this.nativeElement, `${this._cssClassPrefix}--${this._type}`);
94+
this._type = newValue;
95+
this._renderer.addClass(this.nativeElement, `${this._cssClassPrefix}--${this._type}`);
96+
}
8097
}
98+
8199
/**
82100
* Sets the button text color.
83101
* ```html
@@ -89,6 +107,7 @@ export class IgxButtonDirective extends DisplayDensityBase {
89107
this._color = value || this.nativeElement.style.color;
90108
this._renderer.setStyle(this.nativeElement, 'color', this._color);
91109
}
110+
92111
/**
93112
* Sets the background color of the button.
94113
* ```html
@@ -100,6 +119,7 @@ export class IgxButtonDirective extends DisplayDensityBase {
100119
this._backgroundColor = value || this._backgroundColor;
101120
this._renderer.setStyle(this.nativeElement, 'background', this._backgroundColor);
102121
}
122+
103123
/**
104124
* Sets the `aria-label` attribute.
105125
* ```html
@@ -111,6 +131,7 @@ export class IgxButtonDirective extends DisplayDensityBase {
111131
this._label = value || this._label;
112132
this._renderer.setAttribute(this.nativeElement, `aria-label`, this._label);
113133
}
134+
114135
/**
115136
* Enables/disables the button.
116137
* ```html
@@ -121,9 +142,9 @@ export class IgxButtonDirective extends DisplayDensityBase {
121142
@Input() set disabled(val) {
122143
val = !!val;
123144
if (val) {
124-
this._renderer.addClass(this.nativeElement, `${this._cssClass}--disabled`);
145+
this._renderer.addClass(this.nativeElement, `${this._cssClassPrefix}--disabled`);
125146
} else {
126-
this._renderer.removeClass(this.nativeElement, `${this._cssClass}--disabled`);
147+
this._renderer.removeClass(this.nativeElement, `${this._cssClassPrefix}--disabled`);
127148
}
128149
}
129150

0 commit comments

Comments
 (0)