Skip to content
This repository was archived by the owner on Aug 25, 2020. It is now read-only.

Commit 495d4cb

Browse files
committed
feat: add uiText[autoResize] param
1 parent 9fcf063 commit 495d4cb

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

projects/ui/src/lib/ui-text/demo/ui-text-demo.component.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ <h2>Input</h2>
1010
<h2>Textarea</h2>
1111
<textarea uiText [(ngModel)]="textareaModel" rows="6"></textarea>
1212
{{ textareaModel }}
13+
14+
<h3>With auto-resize</h3>
15+
<textarea uiText
16+
[(ngModel)]="textareaModel"
17+
[autoResize]="true"
18+
rows="2"></textarea>
19+
{{ textareaModel }}
1320
</div>

projects/ui/src/lib/ui-text/ui-text/ui-text.component.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import {
22
ChangeDetectionStrategy,
33
ChangeDetectorRef,
44
Component,
5+
ElementRef,
56
HostBinding,
7+
HostListener,
68
Input,
79
OnInit,
810
Optional,
911
} from '@angular/core';
10-
import { KitFormFieldService } from '@ngx-kit/core';
12+
import { NgControl } from '@angular/forms';
13+
import { KitFormFieldService, KitPlatformService } from '@ngx-kit/core';
1114

1215
@Component({
1316
// tslint:disable-next-line
@@ -19,20 +22,54 @@ import { KitFormFieldService } from '@ngx-kit/core';
1922
export class UiTextComponent implements OnInit {
2023
@Input() uiText: void;
2124

25+
@Input() autoResize = false;
26+
2227
@HostBinding('class.-has-errors') hasErrorsClass = false;
2328

2429
constructor(
2530
@Optional() private formFieldService: KitFormFieldService,
31+
@Optional() private ngControl: NgControl,
2632
private cdr: ChangeDetectorRef,
33+
private el: ElementRef,
34+
private platform: KitPlatformService,
2735
) {
2836
}
2937

3038
ngOnInit() {
39+
// Control error state
3140
if (this.formFieldService) {
32-
this.formFieldService.control.errorStateChanges.subscribe(errors => {
33-
this.hasErrorsClass = !errors || errors.length > 0;
34-
this.cdr.detectChanges();
35-
});
41+
this.formFieldService.control
42+
.errorStateChanges
43+
.subscribe(errors => {
44+
this.hasErrorsClass = !errors || errors.length > 0;
45+
this.cdr.detectChanges();
46+
});
47+
}
48+
// Update size on model change
49+
if (this.ngControl) {
50+
this.ngControl
51+
.valueChanges
52+
.subscribe(() => {
53+
this.resize();
54+
});
55+
}
56+
}
57+
58+
@HostListener('keyup') keydownHandler() {
59+
this.resize();
60+
}
61+
62+
@HostListener('input') inputHandler() {
63+
this.resize();
64+
}
65+
66+
private resize() {
67+
if (this.autoResize && this.platform.isBrowser()) {
68+
const el = this.el.nativeElement;
69+
// clean up hack
70+
el.style.height = 'auto';
71+
// set height
72+
el.style.height = el.scrollHeight + el.offsetHeight - el.clientHeight + 'px';
3673
}
3774
}
3875
}

0 commit comments

Comments
 (0)