File

src/input/password-input-label.component.ts

Description

Get started with importing the module:

Example :
import { InputModule } from 'carbon-components-angular';
Example :
<cds-password-label>
    Label
    <input cdsPassword>
</cds-password-label>

See demo

Extends

BaseIconButton

Implements

AfterViewInit

Metadata

Index

Properties
Methods
Inputs
HostBindings
Accessors

Constructor

constructor(changeDetectorRef: ChangeDetectorRef)

Constructor for PasswordInputLabelComponent.

Parameters :
Name Type Optional Description
changeDetectorRef ChangeDetectorRef No
  • Reference to ChangeDetectorRef.

Inputs

ariaLabel
Type : string

Aria label for label.

disabled
Type : boolean
Default value : false

Flag for disabled label.

fluid
Type : boolean
Default value : false

Experimental: enable fluid state

helperText
Type : string | TemplateRef<any>

Optional helper text under the label.

hidePasswordLabel
Type : string
Default value : "Hide password"

Tooltip text for hiding password.

invalid
Type : boolean
Default value : false

Flag for an invalid label component.

invalidText
Type : string | TemplateRef<any>

Sets the invalid text.

labelInputID
Type : string
Default value : "cds-password-input-" + PasswordInputLabelComponent.labelCounter++

ID for the input item associated with the label.

labelTemplate
Type : TemplateRef<any>

Template for label content.

passwordInputTemplate
Type : TemplateRef<any>

Template for password input.

showPasswordLabel
Type : string
Default value : "Show password"

Tooltip text for showing password.

skeleton
Type : boolean
Default value : false

Flag for loading (skeleton) label.

warn
Type : boolean
Default value : false

Flag for showing a warning.

warnText
Type : string | TemplateRef<any>

Warning text.

align
Type : "top" | "top-left" | "top-right" | "bottom" | "bottom-left" | "bottom-right" | "left" | "left-bottom" | "left-top" | "right" | "right-bottom" | "right-top"
Default value : "bottom"
Inherited from BaseIconButton
Defined in BaseIconButton:32

Set popover alignment

autoAlign
Type : boolean
Default value : false
Inherited from BaseIconButton
Defined in BaseIconButton:40

Experimental: Use floating-ui to position the tooltip This is not toggleable - should be assigned once

caret
Type : boolean
Default value : true
Inherited from BaseIconButton
Defined in BaseIconButton:16

Set to false to hide caret

dropShadow
Type : boolean
Default value : true
Inherited from BaseIconButton
Defined in BaseIconButton:20

Set to false to hide shadow

enterDelayMs
Type : number
Default value : 100
Inherited from BaseIconButton
Defined in BaseIconButton:44

Set delay before tooltip is shown

highContrast
Type : boolean
Default value : true
Inherited from BaseIconButton
Defined in BaseIconButton:24

Set to true to enable high contrast

isOpen
Type : boolean
Default value : false
Inherited from BaseIconButton
Defined in BaseIconButton:28

Set to true to have the popover open by default

leaveDelayMs
Type : number
Default value : 300
Inherited from BaseIconButton
Defined in BaseIconButton:48

Set delay when tooltip disappears

HostBindings

class.cds--form-item
Type : boolean
Default value : true

Binding for applying class to host element.

class.cds--password-input-wrapper
Type : boolean
Default value : true
class.cds--text-input--fluid
Type : boolean
class.cds--text-input--fluid__skeleton
Type : boolean
class.cds--text-input-wrapper
Type : boolean
Default value : true
class.cds--text-input-wrapper--readonly
Type : any

Methods

Public handleTogglePasswordVisibility
handleTogglePasswordVisibility()

Handler for toggling password visibility.

Returns : void
Public isTemplate
isTemplate(value)

Function to check if a value is a TemplateRef.

Parameters :
Name Optional Description
value No
  • Value to check.
Returns : boolean

Whether the value is a TemplateRef.

ngAfterViewInit
ngAfterViewInit()

Lifecycle hook called after the view has been initialized to set the ID of the input element

Returns : void

Properties

inputType
Type : "password" | "text"
Default value : "password"

Type for input field, either password or text.

labelClass
Default value : true
Decorators :
@HostBinding('class.cds--form-item')

Binding for applying class to host element.

Static labelCounter
Type : number
Default value : 0

Counter for generating unique labelInputID.

passwordInputWrapper
Default value : true
Decorators :
@HostBinding('class.cds--password-input-wrapper')
passwordIsVisible
Default value : false

Flag for checking if password is visible.

textInput
Type : PasswordInput
Decorators :
@ContentChild(PasswordInput)
textInputWrapper
Default value : true
Decorators :
@HostBinding('class.cds--text-input-wrapper')
wrapper
Type : ElementRef<HTMLDivElement>
Decorators :
@ViewChild('wrapper', {static: true})

Reference to the wrapper element.

Accessors

isReadonly
getisReadonly()
fluidClass
getfluidClass()
fluidSkeletonClass
getfluidSkeletonClass()
import {
	Component,
	Input,
	AfterViewInit,
	ElementRef,
	HostBinding,
	TemplateRef,
	ViewChild,
	ChangeDetectorRef,
	ContentChild
} from "@angular/core";
import { PasswordInput } from "./password.directive";
import { BaseIconButton } from "carbon-components-angular/button";

/**
 * Get started with importing the module:
 *
 * ```typescript
 * import { InputModule } from 'carbon-components-angular';
 * ```
 *
 * ```html
 * <cds-password-label>
 * 	Label
 *	<input cdsPassword>
 * </cds-password-label>
 * ```
 *
 * [See demo](../../?path=/story/components-input--basic)
 */
@Component({
	selector: "cds-password-label, ibm-password-label",
	template: `
		<ng-container *ngIf="skeleton">
			<span class="cds--label cds--skeleton"></span>
			<div class="cds--text-input cds--skeleton"></div>
		</ng-container>
		<label
			*ngIf="!skeleton"
			[for]="labelInputID"
			[attr.aria-label]="ariaLabel"
			class="cds--label"
			[ngClass]="{
				'cds--label--disabled': disabled
			}">
			<ng-template *ngIf="labelTemplate; else labelContent" [ngTemplateOutlet]="labelTemplate"></ng-template>
			<ng-template #labelContent>
				<ng-content></ng-content>
			</ng-template>
		</label>

		<div *ngIf="!skeleton" class="cds--text-input__field-outer-wrapper">
			<div
			class="cds--text-input__field-wrapper"
			[ngClass]="{
				'cds--text-input__field-wrapper--warning': warn
			}"
			[attr.data-invalid]="invalid ? true : null"
			#wrapper>
				<svg
					*ngIf="!warn && 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-content select="[cdsPassword], [ibmPassword]"></ng-content>
				<cds-tooltip
					*ngIf="!skeleton"
					[description]="passwordIsVisible ? hidePasswordLabel : showPasswordLabel"
					[disabled]="disabled"
					[caret]="caret"
					[dropShadow]="dropShadow"
					[highContrast]="highContrast"
					[isOpen]="isOpen"
					[align]="align"
					[autoAlign]="autoAlign"
					[enterDelayMs]="enterDelayMs"
					[leaveDelayMs]="leaveDelayMs"
					class="cds--toggle-password-tooltip">
						<div class="cds--tooltip-trigger__wrapper">
							<button
								class="cds--text-input--password__visibility__toggle cds--btn cds--tooltip__trigger cds--tooltip--a11y"
								[disabled]="disabled"
								type="button"
								(click)="handleTogglePasswordVisibility($event)">
								<svg *ngIf="passwordIsVisible" cdsIcon="view--off" class="cds--icon-visibility-off" size="16"></svg>
								<svg *ngIf="!passwordIsVisible" cdsIcon="view" class="cds--icon-visibility-on" size="16"></svg>
							</button>
						</div>
				</cds-tooltip>

				<ng-container *ngIf="fluid">
					<hr class="cds--text-input__divider" />
					<div *ngIf="!warn && 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-container>
			</div>
			<ng-container *ngIf="!fluid">
				<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="!warn && 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-container>
		</div>
    `
})
/**
 * Represents the Password Input Label Component.
 */
export class PasswordInputLabelComponent extends BaseIconButton implements AfterViewInit {
	/**
	 * Counter for generating unique labelInputID.
	 */
	static labelCounter = 0;

	@ContentChild(PasswordInput) textInput: PasswordInput;

	/**
	 * ID for the input item associated with the label.
	 */
	@Input() labelInputID = "cds-password-input-" + PasswordInputLabelComponent.labelCounter++;

	/**
	 * Type for input field, either password or text.
	 */
	inputType: "password" | "text" = "password";

	/**
	* Flag for checking if password is visible.
	*/
	passwordIsVisible = false;

	/**
	 * Flag for disabled label.
	 */
	@Input() disabled = false;

	/**
	 * Flag for loading (skeleton) label.
	 */
	@Input() skeleton = false;

	/**
	 * Template for label content.
	 */
	@Input() labelTemplate: TemplateRef<any>;

	/**
	 * Template for password input.
	 */
	@Input() passwordInputTemplate: TemplateRef<any>;

	/**
	 * Optional helper text under the label.
	 */
	@Input() helperText: string | TemplateRef<any>;

	/**
	 * Sets the invalid text.
	 */
	@Input() invalidText: string | TemplateRef<any>;

	/**
	 * Flag for an invalid label component.
	 */
	@Input() invalid = false;

	/**
	 * Flag for showing a warning.
	 */
	@Input() warn = false;

	/**
	 * Warning text.
	 */
	@Input() warnText: string | TemplateRef<any>;

	/**
	 * Aria label for label.
	 */
	@Input() ariaLabel: string;

	/**
	 * Tooltip text for hiding password.
	 */
	@Input() hidePasswordLabel = "Hide password";

	/**
	 * Tooltip text for showing password.
	 */
	@Input() showPasswordLabel = "Show password";

	/**
	 * Experimental: enable fluid state
	 */
	@Input() fluid = false;

	/**
	 * Reference to the wrapper element.
	 */
	@ViewChild("wrapper", { static: true }) wrapper: ElementRef<HTMLDivElement>;

	/**
	 * Binding for applying class to host element.
	 */
	@HostBinding("class.cds--form-item") labelClass = true;
	@HostBinding("class.cds--password-input-wrapper") passwordInputWrapper = true;
	@HostBinding("class.cds--text-input-wrapper") textInputWrapper = true;
	@HostBinding("class.cds--text-input-wrapper--readonly") get isReadonly() {
		return this.wrapper?.nativeElement.querySelector("input")?.readOnly ?? false;
	}

	@HostBinding("class.cds--text-input--fluid") get fluidClass() {
		return this.fluid && !this.skeleton;
	}

	@HostBinding("class.cds--text-input--fluid__skeleton") get fluidSkeletonClass() {
		return this.fluid && this.skeleton;
	}

	/**
	 * Constructor for PasswordInputLabelComponent.
	 * @param changeDetectorRef - Reference to ChangeDetectorRef.
	 */
	constructor(protected changeDetectorRef: ChangeDetectorRef) {
		super();
	}

	/**
	 * Lifecycle hook called after the view has been initialized to set the ID of the input element
	 */
	ngAfterViewInit() {
		if (this.wrapper) {
			const inputElement =
				this.wrapper.nativeElement.querySelector("input");
			if (inputElement) {
				if (inputElement.id) {
					this.labelInputID = inputElement.id;
					this.changeDetectorRef.detectChanges();
				}
				inputElement.setAttribute("id", this.labelInputID);
				return;
			}
		}
	}

	/**
	 * Function to check if a value is a TemplateRef.
	 * @param value - Value to check.
	 * @returns Whether the value is a TemplateRef.
	 */
	public isTemplate(value) {
		return value instanceof TemplateRef;
	}

	/**
	 * Handler for toggling password visibility.
	 */
	public handleTogglePasswordVisibility() {
		this.inputType = this.inputType === "password" ? "text" : "password";
		this.textInput.type = this.inputType;
		this.passwordIsVisible = this.inputType === "text";
	}
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""