Jeremy Thomas
Jeremy Thomas

Reputation: 6674

Ionic 2: Style ion-select with validation

I'm trying to style the label of an <ion-select> input and can't get it to work. It seems like ionic automatically adds the ng-invalid class to the ion-select DOM element, but the <label> for the input is upstream in the DOM so I cannot style it.

My html:

<ion-item>
    <ion-label>Type of Weigh In</ion-label>
    <ion-select [(ngModel)]="check_in.type_of_weighin" name="type_of_weighin" interface="action-sheet" required #type_of_weighin="ngModel">
        <ion-option value="standard">Standard</ion-option>
        <ion-option value="LM">Limited Maintenence</ion-option>
        <ion-option value="FM">Full Maintenence</ion-option>
    </ion-select>
</ion-item>

Renderd DOM:

<ion-item class="item item-block item-ios item-select input-has-value">
    <div class="item-inner">
        <div class="input-wrapper">
            <ion-label class="label label-ios" id="lbl-103">Type of Weigh In</ion-label>
            <ion-select interface="action-sheet" name="type_of_weighin" required="" class="select select-ios ng-untouched ng-pristine ng-invalid">
                <div class="select-placeholder select-text"></div>
                <div class="select-icon">
                    <div class="select-icon-inner"></div>
                </div>
                <button aria-haspopup="true" class="item-cover" ion-button="item-cover" id="sel-103-0" aria-labelledby="lbl-103" aria-disabled="false" style="transition: none;"></button>
            </ion-select>
        </div>
    </div>
    <div class="button-effect"></div>
</ion-item>

Is there another way to style the <label>?

Upvotes: 0

Views: 2281

Answers (1)

SimplyComplexable
SimplyComplexable

Reputation: 1114

Given the reformatting done by ionic, I don't know a way to do it directly with css, but you could attach some eventListeners and update based on the change.

HTML

<ion-item>
    <ion-label [class.is-invalid]="inputsInvalid.type_of_weighin">Type of Weigh In</ion-label>
    <ion-select [(ngModel)]="check_in.type_of_weighin" name="type_of_weighin" interface="action-sheet" required #type_of_weighin="ngModel" (change)="checkValid($event)">
        <ion-option value="standard">Standard</ion-option>
        <ion-option value="LM">Limited Maintenence</ion-option>
        <ion-option value="FM">Full Maintenence</ion-option>
    </ion-select>
</ion-item>

TS

inputsInvalid = {};

checkValid(e) {
    const el = e.target;
    inputsInvalid[el.id] = el.classList.contains('ng-invalid');
}

SCSS/CSS

ion-label.is-invalid {
    ...
}

Hopefully that makes sense.

Upvotes: 1

Related Questions