@@ -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';
1922export 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