Creating an Autogrow Directive for a Textarea
Growing the height of a textarea to fit user input
PROModule Outline
- Source Code & Resources PRO
- Lesson 1: Introduction PUBLIC
- Lesson 2: The Role of Components and Directives PUBLIC
- Lesson 3: When to Use Custom Components & Directives PRO
- Lesson 4: Building a Simple Component PRO
- Lesson 5: Building a Simple Directive PRO
- Lesson 6: Setting up Components and Directives PRO
- Lesson 7: Using @Input and @Output PRO
- Lesson 8: Using ElementRef and Renderer PRO
- Lesson 9: Understanding Content Projection PRO
- Lesson 10: Understanding @ViewChild and @ContentChild PRO
- Lesson 11: Building a Complex Component PRO
- Lesson 12: Listening for Events and Binding to Properties PRO
- Lesson 13: Building a Skeleton Card Component PRO
- Lesson 14: Creating an Autogrow Directive for a Textarea PRO
- Lesson 15: Handling Error Messages with a Custom Component PRO
- Lesson 16: Building a Parallax Header Directive PRO
- Lesson 17: High Performance Accordion Component PRO
- Lesson 18: Conclusion PRO
Lesson Outline
Creating an Autogrow Directive for a Textarea
We've created a simple component, and now we are going to create a reasonably simple directive - this one is going to be a little more interesting. The previous example was a good example of how a component has its own template (which is what makes it a "component"), and this example should be a good demonstration of how a more generic directive is used to modify the behaviour of an existing component and does not provide its own template.
We will be creating a directive that we can attach to an <ion-textarea>
that will cause it to automatically expand to fit the content that the user types into it - by default, the textarea height will remain static and at some point what the user's typing will be cut off.
When you use the <ion-textarea>
component, the resulting output to the DOM looks like this:
<ion-textarea class="sc-ion-textarea-md-h sc-ion-textarea-md-s md hydrated">
<div class="textarea-wrapper sc-ion-textarea-md">
<textarea
class="native-textarea sc-ion-textarea-md"
autocapitalize="none"
name="ion-textarea-0"
placeholder=""
spellcheck="false"
></textarea>
</div>
</ion-textarea>
This creates an interesting issue, in that we can't apply attributes directly to the <textarea>
element. For example, <textarea>
accepts an attribute of autofocus
to determine whether an input should be automatically focused when the page loads. But if we are using <ion-textarea>
in our pages component, we would just have:
<ion-textarea></ion-textarea>
Thanks for checking out the preview of this lesson!
You do not have the appropriate membership to view the full lesson. If you would like full access to this module you can view membership options (or log in if you are already have an appropriate membership).