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