3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.form.field.TextArea
17 * @extends Ext.form.field.Text
19 This class creates a multiline text field, which can be used as a direct replacement for traditional
20 textarea fields. In addition, it supports automatically {@link #grow growing} the height of the textarea to
23 All of the configuration options from {@link Ext.form.field.Text} can be used on TextArea.
24 {@img Ext.form.TextArea/Ext.form.TextArea.png Ext.form.TextArea component}
27 Ext.create('Ext.form.FormPanel', {
28 title : 'Sample TextArea',
31 renderTo : Ext.getBody(),
33 xtype : 'textareafield',
36 fieldLabel: 'Message',
41 Some other useful configuration options when using {@link #grow} are {@link #growMin} and {@link #growMax}. These
42 allow you to set the minimum and maximum grow heights for the textarea.
44 * @docauthor Robert Dougan <rob@sencha.com>
46 Ext.define('Ext.form.field.TextArea', {
47 extend:'Ext.form.field.Text',
48 alias: ['widget.textareafield', 'widget.textarea'],
49 alternateClassName: 'Ext.form.TextArea',
50 requires: ['Ext.XTemplate', 'Ext.layout.component.field.TextArea'],
53 '<textarea id="{id}" ',
54 '<tpl if="name">name="{name}" </tpl>',
55 '<tpl if="rows">rows="{rows}" </tpl>',
56 '<tpl if="cols">cols="{cols}" </tpl>',
57 '<tpl if="tabIdx">tabIndex="{tabIdx}" </tpl>',
58 'class="{fieldCls} {typeCls}" ',
59 'autocomplete="off">',
68 * @cfg {Number} growMin The minimum height to allow when <tt>{@link Ext.form.field.Text#grow grow}=true</tt>
69 * (defaults to <tt>60</tt>)
74 * @cfg {Number} growMax The maximum height to allow when <tt>{@link Ext.form.field.Text#grow grow}=true</tt>
75 * (defaults to <tt>1000</tt>)
80 * @cfg {String} growAppend
81 * A string that will be appended to the field's current value for the purposes of calculating the target
82 * field size. Only used when the {@link #grow} config is <tt>true</tt>. Defaults to a newline for TextArea
83 * to ensure there is always a space below the current line.
88 * @cfg {Number} cols An initial value for the 'cols' attribute on the textarea element. This is only
89 * used if the component has no configured {@link #width} and is not given a width by its container's
90 * layout. Defaults to <tt>20</tt>.
95 * @cfg {Number} cols An initial value for the 'cols' attribute on the textarea element. This is only
96 * used if the component has no configured {@link #width} and is not given a width by its container's
97 * layout. Defaults to <tt>4</tt>.
102 * @cfg {Boolean} enterIsSpecial
103 * True if you want the enter key to be classed as a <tt>special</tt> key. Special keys are generally navigation
104 * keys (arrows, space, enter). Setting the config property to <tt>true</tt> would mean that you could not insert
105 * returns into the textarea.
106 * (defaults to <tt>false</tt>)
108 enterIsSpecial: false,
111 * @cfg {Boolean} preventScrollbars <tt>true</tt> to prevent scrollbars from appearing regardless of how much text is
112 * in the field. This option is only relevant when {@link #grow} is <tt>true</tt>. Equivalent to setting overflow: hidden, defaults to
115 preventScrollbars: false,
118 componentLayout: 'textareafield',
121 onRender: function(ct, position) {
123 Ext.applyIf(me.subTplData, {
128 me.callParent(arguments);
132 afterRender: function(){
135 me.callParent(arguments);
138 if (me.preventScrollbars) {
139 me.inputEl.setStyle('overflow', 'hidden');
141 me.inputEl.setHeight(me.growMin);
146 fireKey: function(e) {
147 if (e.isSpecialKey() && (this.enterIsSpecial || (e.getKey() !== e.ENTER || e.hasModifier()))) {
148 this.fireEvent('specialkey', this, e);
153 * Automatically grows the field to accomodate the height of the text up to the maximum field height allowed.
154 * This only takes effect if <tt>{@link #grow} = true</tt>, and fires the {@link #autosize} event if
155 * the height changes.
157 autoSize: function() {
161 if (me.grow && me.rendered) {
162 me.doComponentLayout();
163 height = me.inputEl.getHeight();
164 if (height !== me.lastInputHeight) {
165 me.fireEvent('autosize', height);
166 me.lastInputHeight = height;
172 initAria: function() {
173 this.callParent(arguments);
174 this.getActionEl().dom.setAttribute('aria-multiline', true);
178 * @protected override
179 * To get the natural width of the textarea element, we do a simple calculation based on the
180 * 'cols' config. We use hard-coded numbers to approximate what browsers do natively,
181 * to avoid having to read any styles which would hurt performance.
183 getBodyNaturalWidth: function() {
184 return Math.round(this.cols * 6.5) + 20;