12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- import { Directive , ElementRef , Output , EventEmitter , AfterViewInit , Input , OnChanges , inject } from '@angular/core' ;
15
+ import { Directive , ElementRef , Output , EventEmitter , inject , input , effect , output } from '@angular/core' ;
16
16
17
17
/**
18
18
* Directive to adapt a textarea rows depending on the input text. It's based on Moodle's data-auto-rows.
@@ -24,45 +24,28 @@ import { Directive, ElementRef, Output, EventEmitter, AfterViewInit, Input, OnCh
24
24
@Directive ( {
25
25
selector : 'textarea[core-auto-rows], ion-textarea[core-auto-rows]' ,
26
26
} )
27
- export class CoreAutoRowsDirective implements AfterViewInit , OnChanges {
27
+ export class CoreAutoRowsDirective {
28
+
29
+ readonly value = input < string > ( undefined , { alias : 'core-auto-rows' } ) ;
30
+ readonly onResize = output < void > ( ) ; // Emit when resizing the textarea.
28
31
29
32
protected height = 0 ;
30
33
protected element : HTMLElement = inject ( ElementRef ) . nativeElement ;
31
34
32
- @Input ( 'core-auto-rows' ) value ?: string ;
33
- @Output ( ) onResize : EventEmitter < void > ; // Emit when resizing the textarea.
34
-
35
35
constructor ( ) {
36
- this . onResize = new EventEmitter ( ) ;
37
- }
36
+ effect ( ( ) => {
37
+ this . value ( ) ;
38
38
39
- /**
40
- * Resize after initialized.
41
- */
42
- ngAfterViewInit ( ) : void {
43
- // Wait for rendering of child views.
44
- setTimeout ( ( ) => {
39
+ // Resize the textarea when the value changes.
45
40
this . resize ( ) ;
46
- } , 300 ) ;
47
- }
48
-
49
- /**
50
- * Resize when content changes.
51
- */
52
- ngOnChanges ( ) : void {
53
- this . resize ( ) ;
54
-
55
- if ( this . value === '' ) {
56
- // Maybe the form was resetted. In that case it takes a bit to update the height.
57
- setTimeout ( ( ) => this . resize ( ) , 300 ) ;
58
- }
41
+ } ) ;
59
42
}
60
43
61
44
/**
62
45
* Resize the textarea.
63
46
*/
64
47
protected resize ( ) : void {
65
- if ( this . element . tagName == 'ION-TEXTAREA' ) {
48
+ if ( this . element . tagName === 'ION-TEXTAREA' ) {
66
49
// Search the actual textarea.
67
50
const textarea = this . element . querySelector ( 'textarea' ) ;
68
51
if ( ! textarea ) {
@@ -77,7 +60,7 @@ export class CoreAutoRowsDirective implements AfterViewInit, OnChanges {
77
60
this . element . style . height = `${ this . element . scrollHeight } px` ;
78
61
79
62
// Emit event when resizing.
80
- if ( this . height != this . element . scrollHeight ) {
63
+ if ( this . height !== this . element . scrollHeight ) {
81
64
this . height = this . element . scrollHeight ;
82
65
this . onResize . emit ( ) ;
83
66
}
0 commit comments