Created
December 26, 2020 19:30
-
-
Save razvanstatescu/6fc6fb889b8a54461de04a86067673a1 to your computer and use it in GitHub Desktop.
An Angular directive that adjust input width based on the content size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Directive, ElementRef, HostListener } from '@angular/core'; | |
@Directive({ | |
selector: '[dynamic-input]', | |
}) | |
export class DynamicInputDirective { | |
constructor(private el: ElementRef) { | |
this.resize(); | |
} | |
@HostListener('keyup') onKeyUp() { | |
this.resize(); | |
} | |
private resize() { | |
this.el.nativeElement.setAttribute( | |
'size', | |
this.el.nativeElement.value.length || 2 | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment