Skip to content

Commit 97af7f7

Browse files
authored
Merge pull request #2606 from Akshat55/ng16-type
fix: Add empty string option for props that match directive name
2 parents ffebb4e + 684c002 commit 97af7f7

File tree

3 files changed

+22
-46
lines changed

3 files changed

+22
-46
lines changed

src/button/button.directive.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {
22
Directive,
33
HostBinding,
4-
Input,
5-
OnInit
4+
Input
65
} from "@angular/core";
76
import { ButtonSize, ButtonType } from "./button.types";
87

@@ -23,17 +22,19 @@ import { ButtonSize, ButtonType } from "./button.types";
2322
@Directive({
2423
selector: "[cdsButton], [ibmButton]"
2524
})
26-
export class Button implements OnInit {
25+
export class Button {
2726
/**
2827
* @deprecated as of v5 - Use `cdsButton` input property instead
2928
*/
3029
@Input() set ibmButton(type: ButtonType) {
3130
this.cdsButton = type;
3231
}
3332
/**
34-
* sets the button type
33+
* Sets the button type
34+
* Accepts `ButtonType` or nothing (empty string which is equivalent to "primary")
35+
* Empty string has been added as an option for Angular 16+ to resolve type errors
3536
*/
36-
@Input() cdsButton: ButtonType = "primary";
37+
@Input() cdsButton: ButtonType | "" = "primary";
3738
/**
3839
* Specify the size of the button
3940
*/
@@ -57,7 +58,7 @@ export class Button implements OnInit {
5758
// a whole lot of HostBindings ... this way we don't have to touch the elementRef directly
5859
@HostBinding("class.cds--btn") baseClass = true;
5960
@HostBinding("class.cds--btn--primary") get primaryButton() {
60-
return this.cdsButton === "primary";
61+
return this.cdsButton === "primary" || !this.cdsButton;
6162
}
6263
@HostBinding("class.cds--btn--secondary") get secondaryButton() {
6364
return this.cdsButton === "secondary";
@@ -92,14 +93,4 @@ export class Button implements OnInit {
9293
@HostBinding("class.cds--btn--2xl") get twoExtraLargeSize() {
9394
return this.size === "2xl";
9495
}
95-
96-
/**
97-
* We need to make sure cdsButton is not an empty string since
98-
* input name matches selector name.
99-
*/
100-
ngOnInit() {
101-
if (!this.cdsButton) {
102-
this.cdsButton = "primary";
103-
}
104-
}
10596
}

src/layout/stack.directive.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ import {
33
ElementRef,
44
HostBinding,
55
Input,
6-
OnInit,
76
Renderer2
87
} from "@angular/core";
98

109
@Directive({
1110
selector: "[cdsStack], [ibmStack]"
1211
})
13-
export class StackDirective implements OnInit {
12+
export class StackDirective {
1413
@HostBinding("class.cds--stack-horizontal") get isHorizontal() {
1514
return this.cdsStack === "horizontal";
1615
}
1716

1817
@HostBinding("class.cds--stack-vertical") get isVertical() {
19-
return this.cdsStack === "vertical";
18+
return this.cdsStack === "vertical" || !this.cdsStack;
2019
}
2120

2221
/**
@@ -28,8 +27,11 @@ export class StackDirective implements OnInit {
2827

2928
/**
3029
* Orientation of the items in the stack, defaults to `vertical`
30+
* Empty string is equivalent to "vertical"
31+
*
32+
* Empty string has been added as an option for Angular 16+ to resolve type errors
3133
*/
32-
@Input() cdsStack: "vertical" | "horizontal" = "vertical";
34+
@Input() cdsStack: "vertical" | "horizontal" | "" = "vertical";
3335

3436
/**
3537
* Gap in the layout, provide a custom value (string) or a step from the spacing scale (number)
@@ -44,15 +46,5 @@ export class StackDirective implements OnInit {
4446
// Used to track previous value of gap so we can dynamically remove class
4547
private _gap;
4648

47-
constructor(private render: Renderer2, private hostElement: ElementRef) { }
48-
49-
/**
50-
* We need to make sure cdsStack is not an empty string since
51-
* input name matches selector name.
52-
*/
53-
ngOnInit(): void {
54-
if (!this.cdsStack) {
55-
this.cdsStack = "vertical";
56-
}
57-
}
49+
constructor(private render: Renderer2, private hostElement: ElementRef) {}
5850
}

src/theme/theme.directive.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import {
44
Directive,
55
HostBinding,
66
Input,
7-
OnInit,
87
QueryList
98
} from "@angular/core";
109
import { LayerDirective } from "carbon-components-angular/layer";
1110

11+
export type ThemeType = "white" | "g10" | "g90" | "g100";
12+
1213
/**
1314
* Applies theme styles to the div container it is applied to.
1415
*
@@ -18,18 +19,20 @@ import { LayerDirective } from "carbon-components-angular/layer";
1819
selector: "[cdsTheme], [ibmTheme]",
1920
exportAs: "theme"
2021
})
21-
export class ThemeDirective implements OnInit, AfterContentChecked {
22+
export class ThemeDirective implements AfterContentChecked {
2223
/**
2324
* @deprecated as of v5 - Use `cdsTheme` input property instead
2425
*/
25-
@Input() set ibmTheme(type: "white" | "g10" | "g90" | "g100") {
26+
@Input() set ibmTheme(type: ThemeType | "") {
2627
this.cdsTheme = type;
2728
}
2829

2930
/**
3031
* Sets the theme for the content
32+
* Accepts `ThemeType` or nothing (empty string which is equivalent to "white")
33+
* Empty string has been added as an option for Angular 16+ to resolve type errors
3134
*/
32-
@Input() cdsTheme: "white" | "g10" | "g90" | "g100" = "white";
35+
@Input() cdsTheme: ThemeType | "" = "white";
3336

3437
@ContentChildren(LayerDirective, { descendants: false }) layerChildren: QueryList<LayerDirective>;
3538

@@ -38,7 +41,7 @@ export class ThemeDirective implements OnInit, AfterContentChecked {
3841
* overwrite user added classes
3942
*/
4043
@HostBinding("class.cds--white") get whiteThemeClass() {
41-
return this.cdsTheme === "white";
44+
return this.cdsTheme === "white" || !this.cdsTheme;
4245
}
4346

4447
@HostBinding("class.cds--g10") get g10ThemeClass() {
@@ -66,14 +69,4 @@ export class ThemeDirective implements OnInit, AfterContentChecked {
6669
}
6770
});
6871
}
69-
70-
/**
71-
* We need to make sure cdsTheme is not an empty string because
72-
* input name matches selector name.
73-
*/
74-
ngOnInit(): void {
75-
if (!this.cdsTheme) {
76-
this.cdsTheme = "white";
77-
}
78-
}
7972
}

0 commit comments

Comments
 (0)