I am using Ionic 7 and Angular (material) 14.
I want to display a required field indicator (*) in the field labels where applicable.
In Ionic 6, I achieved this by using adding span tag in the ion-label, with a class for styling, ex:
<ion-item>
<ion-label position="fixed">Title<span class="required-field-indicator">*</span></ion-label>
<ion-select formControlName="title">
<ion-select-option *ngFor="let title of titles" value="{{title}}">{{title}}</ion-select-option>
</ion-select>
</ion-item>
I could then change the styling of my indicator using CSS:
.required-field-indicator {
color: var(--ion-color-tertiary);
font-weight: bolder;
padding: 2px;
}
My field would display as follows:
I've now upgraded to Ionic 7, and as per the documentation (https://ionicframework.com/docs/api/select), the ion-label no longer works with ion-input and ion-select. Instead, one should use the label and label-placement properties on the ion-select component as per example:
I've updated my code accordingly:
<ion-item>
<ion-select label="Title" labelPlacement="fixed" formControlName="title" [required]="true">
<ion-select-option *ngFor="let title of titles" value="{{title}}">{{title}}</ion-select-option>
</ion-select>
</ion-item>
However, with this change, I can no longer add (unless I add it as character in the label property) or customize my required field indicator symbol.
How can I add a my indicator and style it in Ionic 7?
I have managed to do it using CSS on the ion-input component:
.required-indicator {
.label-text::after {
content: '*';
transform: translate(-100%, 0);
color: var(--ion-color-tertiary) !important;
font-weight: bolder;
padding: 2px;
}
}
<ion-input label="First Name" labelPlacement="fixed" type="text" formControlName="firstName"
class="required-indicator" [required]="true"></ion-input>
But this does not work on the ion-select. I have also tried using the shadow parts and creating a directive to append the indicator the DOM, but I can't access the label in any case. Please help!