src/input/label.component.ts
Get started with importing the module:
Example :import { InputModule } from 'carbon-components-angular';
To prevent attribute drilling, use ibm-text-label
or ibm-textarea-label
components
<cds-label>
Label
<input cdsText type="text" class="input-field">
</cds-label>
AfterContentInit
AfterViewInit
selector | cds-label, ibm-label |
template |
|
Properties |
Methods |
Inputs |
HostBindings |
Accessors |
constructor(changeDetectorRef: ChangeDetectorRef)
|
||||||
Defined in src/input/label.component.ts:205
|
||||||
Creates an instance of Label.
Parameters :
|
ariaLabel | |
Type : string
|
|
Defined in src/input/label.component.ts:189
|
|
Set the arialabel for label |
disabled | |
Type : boolean
|
|
Default value : false
|
|
Defined in src/input/label.component.ts:161
|
|
Set to |
helperText | |
Type : string | TemplateRef<any>
|
|
Defined in src/input/label.component.ts:169
|
|
Optional helper text that appears under the label. |
invalid | |
Type : boolean
|
|
Default value : false
|
|
Defined in src/input/label.component.ts:177
|
|
Set to |
invalidText | |
Type : string | TemplateRef<any>
|
|
Defined in src/input/label.component.ts:173
|
|
Sets the invalid text. |
labelInputID | |
Type : string
|
|
Default value : `cds-label-${Label.labelCounter++}`
|
|
Defined in src/input/label.component.ts:157
|
|
The id of the input item associated with the |
skeleton | |
Type : boolean
|
|
Default value : false
|
|
Defined in src/input/label.component.ts:165
|
|
Set to |
warn | |
Type : boolean
|
|
Default value : false
|
|
Defined in src/input/label.component.ts:181
|
|
Set to |
warnText | |
Type : string | TemplateRef<any>
|
|
Defined in src/input/label.component.ts:185
|
|
Sets the warning text |
class.cds--form-item |
Type : boolean
|
Defined in src/input/label.component.ts:201
|
Public isTemplate | ||||
isTemplate(value)
|
||||
Defined in src/input/label.component.ts:254
|
||||
Parameters :
Returns :
boolean
|
ngAfterContentInit |
ngAfterContentInit()
|
Defined in src/input/label.component.ts:215
|
Update wrapper class if a textarea is hosted.
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Defined in src/input/label.component.ts:228
|
Sets the id on the input item associated with the
Returns :
void
|
Static labelCounter |
Type : number
|
Default value : 0
|
Defined in src/input/label.component.ts:152
|
Used to build the id of the input item associated with the |
passwordInput |
Type : PasswordInput
|
Decorators :
@ContentChild(PasswordInput, {static: false})
|
Defined in src/input/label.component.ts:199
|
textArea |
Type : TextArea
|
Decorators :
@ContentChild(TextArea)
|
Defined in src/input/label.component.ts:193
|
textInput |
Type : TextInput
|
Decorators :
@ContentChild(TextInput, {static: false})
|
Defined in src/input/label.component.ts:196
|
type |
Type : "TextArea" | "TextInput" | "PasswordInput"
|
Defined in src/input/label.component.ts:205
|
wrapper |
Type : ElementRef<HTMLDivElement>
|
Decorators :
@ViewChild('wrapper')
|
Defined in src/input/label.component.ts:191
|
labelClass |
getlabelClass()
|
Defined in src/input/label.component.ts:201
|
import {
Component,
Input,
AfterViewInit,
ElementRef,
HostBinding,
TemplateRef,
ViewChild,
ContentChild,
AfterContentInit,
ChangeDetectorRef
} from "@angular/core";
import { TextArea } from "./text-area.directive";
import { TextInput } from "./input.directive";
import { PasswordInput } from "./password.directive";
/**
* Get started with importing the module:
*
* ```typescript
* import { InputModule } from 'carbon-components-angular';
* ```
*
* To prevent attribute drilling, use `ibm-text-label` or `ibm-textarea-label` components
*
* ```html
* <cds-label>
* Label
* <input cdsText type="text" class="input-field">
* </cds-label>
* ```
*
* [See demo](../../?path=/story/components-input--basic)
*/
@Component({
selector: "cds-label, ibm-label",
template: `
<ng-template #inputContentTemplate>
<ng-content select="input,textarea,div"></ng-content>
</ng-template>
<ng-template #labelContentTemplate>
<ng-content></ng-content>
</ng-template>
<ng-container [ngSwitch]="type">
<ng-container *ngSwitchCase="'TextArea'">
<cds-textarea-label
[labelInputID]="labelInputID"
[disabled]="disabled"
[skeleton]="skeleton"
[helperText]="helperText"
[invalid]="invalid"
[invalidText]="invalidText"
[warn]="warn"
[warnText]="warnText"
[ariaLabel]="ariaLabel"
[labelTemplate]="labelContentTemplate"
[textAreaTemplate]="inputContentTemplate">
</cds-textarea-label>
</ng-container>
<ng-container *ngSwitchCase="'TextInput'">
<cds-text-label
[labelInputID]="labelInputID"
[disabled]="disabled"
[skeleton]="skeleton"
[helperText]="helperText"
[invalid]="invalid"
[invalidText]="invalidText"
[warn]="warn"
[warnText]="warnText"
[ariaLabel]="ariaLabel"
[labelTemplate]="labelContentTemplate"
[textInputTemplate]="inputContentTemplate">
</cds-text-label>
</ng-container>
<ng-container *ngSwitchCase="'PasswordInput'">
<cds-password-label
[labelInputID]="labelInputID"
[disabled]="disabled"
[skeleton]="skeleton"
[helperText]="helperText"
[invalid]="invalid"
[invalidText]="invalidText"
[warn]="warn"
[warnText]="warnText"
[ariaLabel]="ariaLabel"
[labelTemplate]="labelContentTemplate"
[passwordInputTemplate]="inputContentTemplate">
</cds-password-label>
</ng-container>
<ng-container *ngSwitchDefault>
<ng-template [ngTemplateOutlet]="default"></ng-template>
</ng-container>
</ng-container>
<ng-template #default>
<label
[for]="labelInputID"
[attr.aria-label]="ariaLabel"
class="cds--label"
[ngClass]="{
'cds--label--disabled': disabled,
'cds--skeleton': skeleton
}">
<ng-template [ngTemplateOutlet]="labelContentTemplate"></ng-template>
</label>
<div
class="cds--text-input__field-wrapper"
[ngClass]="{
'cds--text-input__field-wrapper--warning': warn
}"
[attr.data-invalid]="(invalid ? true : null)"
#wrapper>
<svg
*ngIf="invalid"
cdsIcon="warning--filled"
size="16"
class="cds--text-input__invalid-icon">
</svg>
<svg
*ngIf="!invalid && warn"
cdsIcon="warning--alt--filled"
size="16"
class="cds--text-input__invalid-icon cds--text-input__invalid-icon--warning">
</svg>
<ng-template [ngTemplateOutlet]="inputContentTemplate"></ng-template>
</div>
<div
*ngIf="!skeleton && helperText && !invalid && !warn"
class="cds--form__helper-text"
[ngClass]="{'cds--form__helper-text--disabled': disabled}">
<ng-container *ngIf="!isTemplate(helperText)">{{helperText}}</ng-container>
<ng-template *ngIf="isTemplate(helperText)" [ngTemplateOutlet]="helperText"></ng-template>
</div>
<div *ngIf="invalid" class="cds--form-requirement">
<ng-container *ngIf="!isTemplate(invalidText)">{{invalidText}}</ng-container>
<ng-template *ngIf="isTemplate(invalidText)" [ngTemplateOutlet]="invalidText"></ng-template>
</div>
<div *ngIf="!invalid && warn" class="cds--form-requirement">
<ng-container *ngIf="!isTemplate(warnText)">{{warnText}}</ng-container>
<ng-template *ngIf="isTemplate(warnText)" [ngTemplateOutlet]="warnText"></ng-template>
</div>
</ng-template>
`
})
export class Label implements AfterContentInit, AfterViewInit {
/**
* 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.
*/
@Input() labelInputID = `cds-label-${Label.labelCounter++}`;
/**
* Set to `true` for disabled state.
*/
@Input() disabled = false;
/**
* Set to `true` for a loading label.
*/
@Input() skeleton = false;
/**
* Optional helper text that appears under the label.
*/
@Input() helperText: string | TemplateRef<any>;
/**
* Sets the invalid text.
*/
@Input() invalidText: string | TemplateRef<any>;
/**
* Set to `true` for an invalid label component.
*/
@Input() invalid = false;
/**
* Set to `true` to show a warning (contents set by warningText)
*/
@Input() warn = false;
/**
* Sets the warning text
*/
@Input() warnText: string | TemplateRef<any>;
/**
* Set the arialabel for label
*/
@Input() ariaLabel: string;
@ViewChild("wrapper") wrapper: ElementRef<HTMLDivElement>;
@ContentChild(TextArea) textArea: TextArea;
// @ts-ignore
@ContentChild(TextInput, { static: false }) textInput: TextInput;
@ContentChild(PasswordInput, { static: false })
passwordInput: PasswordInput;
@HostBinding("class.cds--form-item") get labelClass() {
return this.type === undefined;
}
type: "TextArea" | "TextInput" | "PasswordInput";
/**
* Creates an instance of Label.
*/
constructor(protected changeDetectorRef: ChangeDetectorRef) {}
/**
* Update wrapper class if a textarea is hosted.
*/
ngAfterContentInit() {
if (this.textArea) {
this.type = "TextArea";
} else if (this.textInput) {
this.type = "TextInput";
} else if (this.passwordInput) {
this.type = "PasswordInput";
}
}
/**
* Sets the id on the input item associated with the `Label`.
*/
ngAfterViewInit() {
// Will only be called when `default` template is being used
if (this.wrapper) {
// Prioritize setting id to `input` & `textarea` over div
const inputElement = this.wrapper.nativeElement.querySelector("input,textarea");
if (inputElement) {
// avoid overriding ids already set by the user reuse it instead
if (inputElement.id) {
this.labelInputID = inputElement.id;
this.changeDetectorRef.detectChanges();
}
inputElement.setAttribute("id", this.labelInputID);
return;
}
const divElement = this.wrapper.nativeElement.querySelector("div");
if (divElement) {
if (divElement.id) {
this.labelInputID = divElement.id;
this.changeDetectorRef.detectChanges();
}
divElement.setAttribute("id", this.labelInputID);
}
}
}
public isTemplate(value) {
return value instanceof TemplateRef;
}
}