Created
November 28, 2016 17:39
-
-
Save maxt3r/2485356e91a1969bdb6cf54902e61165 to your computer and use it in GitHub Desktop.
Ionic 2 ion-textarea autoresize
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
// An autoresize directive that works with ion-textarea in Ionic 2 | |
// Usage example: <ion-textarea autoresize [(ngModel)]="body"></ion-textarea> | |
// Based on https://www.npmjs.com/package/angular2-autosize | |
import { Directive, HostListener, ElementRef } from "@angular/core"; | |
@Directive({ | |
selector: "ion-textarea[autoresize]" // Attribute selector | |
}) | |
export class Autoresize { | |
@HostListener("input", ["$event.target"]) | |
onInput(textArea: HTMLTextAreaElement): void { | |
this.adjust(); | |
} | |
constructor(public element: ElementRef) { | |
} | |
ngOnInit(): void { | |
this.adjust(); | |
} | |
adjust(): void { | |
let ta = this.element.nativeElement.querySelector("textarea"); | |
ta.style.overflow = "hidden"; | |
ta.style.height = "auto"; | |
ta.style.height = ta.scrollHeight + "px"; | |
} | |
} |
Amazing! Thanks! Working perfectly with Ionic 3
Hi everyone,
I'm using this tricks for months but a user report me a bug. When you put lots of new lines, the apps seems to be moving to the top : my header is going under Status bar and a blank space appears in the bottom.
Has anyone this kind of bug ? If not could you send me your Status bar config. Here is mine :
<plugin name="cordova-plugin-statusbar" spec="^2.4.1" />
<preference name="StatusBarBackgroundColor" value="#000000" />
<preference name="StatusBarOverlaysWebView" value="false" />
<preference name="StatusBarStyle" value="lightcontent" />
<preference name="StatusBarDefaultScrollToTop" value="false" />
Thanks in advance
@kinoli Thaank you sir its working fine in ionic 3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've updated @bruno-campos addition to allow you to optionally add a maximum height for the textarea.
https://gist.github.com/kinoli/da7aee28934ec5728fb28d1ac3548230
<ion-textarea autoresize [(ngModel)]="body"></ion-textarea>
<ion-textarea autoresize="100" [(ngModel)]="body"></ion-textarea>
Enjoy!