I would like to dynamically add a class based on an input parameter but only if the input is an 'approved' string.
I have a component with an input and class array from which I want to check the input against:
@Input() modalSize?: string;
sizeClassList = ['xs', 'small', 'medium', 'large', 'xl'];
I have tried the following method within the component:
sizingMethod() {
const isValid = this.sizeClassList.indexOf(this.modalSize) >= 0;
if (isValid) {
return 'modal__dialog--' + this.modalSize;
}
}
Within the template:
<div class="modal__dialog" [ngClass]="sizingMethod()"> ... </div>
Essentially I would like to add an additional sizing class based on an input where the user only has to input the size.
If the user inputs [modalSize]="small", the class added will be 'modal__dialog--small' and if the user inputs [modalSize]="derp", no class will be added.
What is a good way to go about this?
*edit: Title edited to be more clear