diff --git a/src/input/text-area.directive.ts b/src/input/text-area.directive.ts index 06b473cabb..aa1e3fa04e 100644 --- a/src/input/text-area.directive.ts +++ b/src/input/text-area.directive.ts @@ -1,4 +1,9 @@ -import { Directive, HostBinding, Input } from "@angular/core"; +import { + Directive, + ElementRef, + HostBinding, + Input +} from "@angular/core"; /** * A directive for applying styling to a textarea element. @@ -28,7 +33,14 @@ export class TextArea { return this.theme === "light"; } + /** + * Binding maxlength to textarea + */ + @Input() @HostBinding("attr.maxlength") maxlength: number; + @HostBinding("attr.data-invalid") get getInvalidAttr() { return this.invalid ? true : undefined; } + + constructor(public elementRef: ElementRef) {} } diff --git a/src/input/textarea-label.component.ts b/src/input/textarea-label.component.ts index 3221fe07c3..791da13aab 100644 --- a/src/input/textarea-label.component.ts +++ b/src/input/textarea-label.component.ts @@ -49,6 +49,11 @@ import { TextArea } from "./text-area.directive"; +
+ {{characterCounter}} / {{textArea.maxlength}} +
+ #wrapper + (keyup)="keyDownEvent($event)"> ; - // @ts-ignore - @ContentChild(TextArea, { static: false }) textArea: TextArea; + @ContentChild(TextArea) textArea: TextArea; @HostBinding("class.cds--form-item") labelClass = true; @@ -211,6 +220,7 @@ export class TextareaLabelComponent implements AfterViewInit { this.changeDetectorRef.detectChanges(); } inputElement.setAttribute("id", this.labelInputID); + this.characterCounter = this.textArea.elementRef?.nativeElement?.value?.length; return; } @@ -228,4 +238,8 @@ export class TextareaLabelComponent implements AfterViewInit { public isTemplate(value) { return value instanceof TemplateRef; } + + keyDownEvent(event) { + this.characterCounter = event.target.textLength; + } } diff --git a/src/input/textarea.stories.ts b/src/input/textarea.stories.ts index a90ebf2762..28a0c47eb9 100644 --- a/src/input/textarea.stories.ts +++ b/src/input/textarea.stories.ts @@ -25,7 +25,9 @@ export default { theme: "dark", readonly: false, fluid: false, - skeleton: false + skeleton: false, + maxlength: undefined, + enableCounter: false }, argTypes: { autocomplete: { @@ -35,6 +37,9 @@ export default { theme: { options: ["light", "dark"], control: "radio" + }, + maxlength: { + control: "number" } }, component: TextareaLabelComponent @@ -44,25 +49,29 @@ const Template = (args) => ({ props: args, template: ` - {{label}} - + [invalidText]="invalidText" + [fluid]="fluid" + [skeleton]="skeleton" + [warn]="warn" + [warnText]="warnText" + [enableCounter]="enableCounter"> + {{label}} + ` });