Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / source / TextArea.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-form-field-TextArea'>/**
19 </span> * @class Ext.form.field.TextArea
20  * @extends Ext.form.field.Text
21
22 This class creates a multiline text field, which can be used as a direct replacement for traditional 
23 textarea fields. In addition, it supports automatically {@link #grow growing} the height of the textarea to 
24 fit its content.
25
26 All of the configuration options from {@link Ext.form.field.Text} can be used on TextArea.
27 {@img Ext.form.TextArea/Ext.form.TextArea.png Ext.form.TextArea component}
28 Example usage:
29
30     Ext.create('Ext.form.FormPanel', {
31         title      : 'Sample TextArea',
32         width      : 400,
33         bodyPadding: 10,
34         renderTo   : Ext.getBody(),
35         items: [{
36             xtype     : 'textareafield',
37             grow      : true,
38             name      : 'message',
39             fieldLabel: 'Message',
40             anchor    : '100%'
41         }]
42     }); 
43
44 Some other useful configuration options when using {@link #grow} are {@link #growMin} and {@link #growMax}. These 
45 allow you to set the minimum and maximum grow heights for the textarea.
46
47  * @docauthor Robert Dougan &lt;rob@sencha.com&gt;
48  */
49 Ext.define('Ext.form.field.TextArea', {
50     extend:'Ext.form.field.Text',
51     alias: ['widget.textareafield', 'widget.textarea'],
52     alternateClassName: 'Ext.form.TextArea',
53     requires: ['Ext.XTemplate', 'Ext.layout.component.field.TextArea'],
54
55     fieldSubTpl: [
56         '&lt;textarea id=&quot;{id}&quot; ',
57             '&lt;tpl if=&quot;name&quot;&gt;name=&quot;{name}&quot; &lt;/tpl&gt;',
58             '&lt;tpl if=&quot;rows&quot;&gt;rows=&quot;{rows}&quot; &lt;/tpl&gt;',
59             '&lt;tpl if=&quot;cols&quot;&gt;cols=&quot;{cols}&quot; &lt;/tpl&gt;',
60             '&lt;tpl if=&quot;tabIdx&quot;&gt;tabIndex=&quot;{tabIdx}&quot; &lt;/tpl&gt;',
61             'class=&quot;{fieldCls} {typeCls}&quot; ',
62             'autocomplete=&quot;off&quot;&gt;',
63         '&lt;/textarea&gt;',
64         {
65             compiled: true,
66             disableFormats: true
67         }
68     ],
69
70 <span id='Ext-form-field-TextArea-cfg-growMin'>    /**
71 </span>     * @cfg {Number} growMin The minimum height to allow when &lt;tt&gt;{@link Ext.form.field.Text#grow grow}=true&lt;/tt&gt;
72      * (defaults to &lt;tt&gt;60&lt;/tt&gt;)
73      */
74     growMin: 60,
75
76 <span id='Ext-form-field-TextArea-cfg-growMax'>    /**
77 </span>     * @cfg {Number} growMax The maximum height to allow when &lt;tt&gt;{@link Ext.form.field.Text#grow grow}=true&lt;/tt&gt;
78      * (defaults to &lt;tt&gt;1000&lt;/tt&gt;)
79      */
80     growMax: 1000,
81
82 <span id='Ext-form-field-TextArea-cfg-growAppend'>    /**
83 </span>     * @cfg {String} growAppend
84      * A string that will be appended to the field's current value for the purposes of calculating the target
85      * field size. Only used when the {@link #grow} config is &lt;tt&gt;true&lt;/tt&gt;. Defaults to a newline for TextArea
86      * to ensure there is always a space below the current line.
87      */
88     growAppend: '\n-',
89
90 <span id='Ext-form-field-TextArea-cfg-cols'>    /**
91 </span>     * @cfg {Number} cols An initial value for the 'cols' attribute on the textarea element. This is only
92      * used if the component has no configured {@link #width} and is not given a width by its container's
93      * layout. Defaults to &lt;tt&gt;20&lt;/tt&gt;.
94      */
95     cols: 20,
96
97 <span id='Ext-form-field-TextArea-cfg-cols'>    /**
98 </span>     * @cfg {Number} cols An initial value for the 'cols' attribute on the textarea element. This is only
99      * used if the component has no configured {@link #width} and is not given a width by its container's
100      * layout. Defaults to &lt;tt&gt;4&lt;/tt&gt;.
101      */
102     rows: 4,
103
104 <span id='Ext-form-field-TextArea-cfg-enterIsSpecial'>    /**
105 </span>     * @cfg {Boolean} enterIsSpecial
106      * True if you want the enter key to be classed as a &lt;tt&gt;special&lt;/tt&gt; key. Special keys are generally navigation
107      * keys (arrows, space, enter). Setting the config property to &lt;tt&gt;true&lt;/tt&gt; would mean that you could not insert
108      * returns into the textarea.
109      * (defaults to &lt;tt&gt;false&lt;/tt&gt;)
110      */
111     enterIsSpecial: false,
112
113 <span id='Ext-form-field-TextArea-cfg-preventScrollbars'>    /**
114 </span>     * @cfg {Boolean} preventScrollbars &lt;tt&gt;true&lt;/tt&gt; to prevent scrollbars from appearing regardless of how much text is
115      * in the field. This option is only relevant when {@link #grow} is &lt;tt&gt;true&lt;/tt&gt;. Equivalent to setting overflow: hidden, defaults to
116      * &lt;tt&gt;false&lt;/tt&gt;.
117      */
118     preventScrollbars: false,
119
120     // private
121     componentLayout: 'textareafield',
122
123     // private
124     onRender: function(ct, position) {
125         var me = this;
126         Ext.applyIf(me.subTplData, {
127             cols: me.cols,
128             rows: me.rows
129         });
130
131         me.callParent(arguments);
132     },
133
134     // private
135     afterRender: function(){
136         var me = this;
137
138         me.callParent(arguments);
139
140         if (me.grow) {
141             if (me.preventScrollbars) {
142                 me.inputEl.setStyle('overflow', 'hidden');
143             }
144             me.inputEl.setHeight(me.growMin);
145         }
146     },
147
148     // private
149     fireKey: function(e) {
150         if (e.isSpecialKey() &amp;&amp; (this.enterIsSpecial || (e.getKey() !== e.ENTER || e.hasModifier()))) {
151             this.fireEvent('specialkey', this, e);
152         }
153     },
154
155 <span id='Ext-form-field-TextArea-method-autoSize'>    /**
156 </span>     * Automatically grows the field to accomodate the height of the text up to the maximum field height allowed.
157      * This only takes effect if &lt;tt&gt;{@link #grow} = true&lt;/tt&gt;, and fires the {@link #autosize} event if
158      * the height changes.
159      */
160     autoSize: function() {
161         var me = this,
162             height;
163
164         if (me.grow &amp;&amp; me.rendered) {
165             me.doComponentLayout();
166             height = me.inputEl.getHeight();
167             if (height !== me.lastInputHeight) {
168                 me.fireEvent('autosize', height);
169                 me.lastInputHeight = height;
170             }
171         }
172     },
173
174     // private
175     initAria: function() {
176         this.callParent(arguments);
177         this.getActionEl().dom.setAttribute('aria-multiline', true);
178     },
179
180 <span id='Ext-form-field-TextArea-method-getBodyNaturalWidth'>    /**
181 </span>     * @protected override
182      * To get the natural width of the textarea element, we do a simple calculation based on the
183      * 'cols' config. We use hard-coded numbers to approximate what browsers do natively,
184      * to avoid having to read any styles which would hurt performance.
185      */
186     getBodyNaturalWidth: function() {
187         return Math.round(this.cols * 6.5) + 20;
188     }
189
190 });
191
192 </pre>
193 </body>
194 </html>