src/input/label.component.ts
* <ibm-label labelState="success">
* <label label>Field with success</label>
* <input type="text" class="input-field">
* </ibm-label>
*
* <ibm-label labelState="warning">
* <label label>Field with warning</label>
* <input type="text" class="input-field">
* </ibm-label>
*
* <ibm-label labelState="error">
* <label label>Field with error</label>
* <input type="text" class="input-field">
* </ibm-label>
*
selector | ibm-label |
template |
|
Properties |
Methods |
|
Inputs |
HostBindings |
constructor()
|
Defined in src/input/label.component.ts:101
|
Creates an instance of Label. |
helperText
|
Optional helper text that appears under the label.
Type : |
Defined in src/input/label.component.ts:87
|
invalid
|
Set to
Default value : |
Defined in src/input/label.component.ts:95
|
invalidText
|
Sets the invalid text.
Type : |
Defined in src/input/label.component.ts:91
|
labelState
|
State of the
Type :
Default value : |
Defined in src/input/label.component.ts:79
|
skeleton
|
Set to
Default value : |
Defined in src/input/label.component.ts:83
|
class.bx--form-item |
class.bx--form-item:
|
Default value : true
|
Defined in src/input/label.component.ts:101
|
Public isTemplate | ||||
isTemplate(value)
|
||||
Defined in src/input/label.component.ts:120
|
||||
Parameters :
Returns :
boolean
|
ngAfterContentInit |
ngAfterContentInit()
|
Defined in src/input/label.component.ts:113
|
Sets the id on the input item associated with the
Returns :
void
|
Static labelCounter |
labelCounter:
|
Type : number
|
Default value : 0
|
Defined in src/input/label.component.ts:65
|
Used to build the id of the input item associated with the |
labelInputID |
labelInputID:
|
Default value : "ibm-label-" + Label.labelCounter
|
Defined in src/input/label.component.ts:70
|
The id of the input item associated with the |
textArea |
textArea:
|
Type : TextArea
|
Decorators :
@ContentChild(TextArea)
|
Defined in src/input/label.component.ts:99
|
wrapper |
wrapper:
|
Type : ElementRef<HTMLDivElement>
|
Decorators :
@ViewChild('wrapper')
|
Defined in src/input/label.component.ts:97
|
wrapperClass |
wrapperClass:
|
Type : string
|
Default value : "bx--text-input__field-wrapper"
|
Defined in src/input/label.component.ts:74
|
The class of the wrapper |
import {
Component,
Input,
AfterContentInit,
ElementRef,
HostBinding,
TemplateRef,
ViewChild,
ContentChild
} from "@angular/core";
import { TextArea } from "./text-area.directive";
/**
* [See demo](../../?path=/story/input--label)
*
* ```html
* <ibm-label labelState="success">
* <label label>Field with success</label>
* <input type="text" class="input-field">
* </ibm-label>
*
* <ibm-label labelState="warning">
* <label label>Field with warning</label>
* <input type="text" class="input-field">
* </ibm-label>
*
* <ibm-label labelState="error">
* <label label>Field with error</label>
* <input type="text" class="input-field">
* </ibm-label>
* ```
*
* <example-url>../../iframe.html?id=input--label</example-url>
*/
@Component({
selector: "ibm-label",
template: `
<label
[for]="labelInputID"
class="bx--label"
[ngClass]="{
'bx--skeleton': skeleton
}">
<ng-content></ng-content>
</label>
<div *ngIf="!skeleton && helperText" class="bx--form__helper-text">{{helperText}}</div>
<div [class]="wrapperClass" [attr.data-invalid]="(invalid ? true : null)" #wrapper>
<ibm-icon-warning-filled16
*ngIf="invalid"
class="bx--text-input__invalid-icon bx--text-area__invalid-icon">
</ibm-icon-warning-filled16>
<ng-content select="input,textarea,div"></ng-content>
</div>
<div *ngIf="invalid" class="bx--form-requirement">
<ng-container *ngIf="!isTemplate(invalidText)">{{invalidText}}</ng-container>
<ng-template *ngIf="isTemplate(invalidText)" [ngTemplateOutlet]="invalidText"></ng-template>
</div>
`
})
export class Label implements AfterContentInit {
/**
* Used to build the id of the input item associated with the `Label`.
*/
static labelCounter = 0;
/**
* The id of the input item associated with the `Label`. This value is also used to associate the `Label` with
* its input counterpart through the 'for' attribute.
*/
labelInputID = "ibm-label-" + Label.labelCounter;
/**
* The class of the wrapper
*/
wrapperClass = "bx--text-input__field-wrapper";
/**
* State of the `Label` will determine the styles applied.
*/
@Input() labelState: "success" | "warning" | "error" | "" = "";
/**
* Set to `true` for a loading label.
*/
@Input() skeleton = false;
/**
* Optional helper text that appears under the label.
*/
@Input() helperText: string;
/**
* Sets the invalid text.
*/
@Input() invalidText: string | TemplateRef<any>;
/**
* Set to `true` for an invalid label component.
*/
@Input() invalid = false;
@ViewChild("wrapper") wrapper: ElementRef<HTMLDivElement>;
@ContentChild(TextArea) textArea: TextArea;
@HostBinding("class.bx--form-item") labelClass = true;
/**
* Creates an instance of Label.
*/
constructor() {
Label.labelCounter++;
}
/**
* Sets the id on the input item associated with the `Label`.
*/
ngAfterContentInit() {
if (this.textArea) {
this.wrapperClass = "bx--text-area__wrapper";
}
this.wrapper.nativeElement.querySelector("input,textarea,div").setAttribute("id", this.labelInputID);
}
public isTemplate(value) {
return value instanceof TemplateRef;
}
}