]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/field/Text.js
2 * The text field is the basis for most of the input fields in Sencha Touch. It provides a baseline of shared
3 * functionality such as input validation, standard events, state management and look and feel. Typically we create
4 * text fields inside a form, like this:
7 * Ext.create('Ext.form.Panel', {
12 * title: 'Enter your name',
16 * label: 'First Name',
29 * This creates two text fields inside a form. Text Fields can also be created outside of a Form, like this:
31 * Ext.create('Ext.field.Text', {
38 * Text field offers several configuration options, including {@link #placeHolder}, {@link #maxLength},
39 * {@link #autoComplete}, {@link #autoCapitalize} and {@link #autoCorrect}. For example, here is how we would configure
40 * a text field to have a maximum length of 10 characters, with placeholder text that disappears when the field is
43 * Ext.create('Ext.field.Text', {
46 * placeHolder: 'Enter your username'
49 * The autoComplete, autoCapitalize and autoCorrect configs simply set those attributes on the text field and allow the
50 * native browser to provide those capabilities. For example, to enable auto complete and auto correct, simply
51 * configure your text field like this:
53 * Ext.create('Ext.field.Text', {
59 * These configurations will be picked up by the native browser, which will enable the options at the OS level.
61 * Text field inherits from {@link Ext.field.Field}, which is the base class for all fields in Sencha Touch and provides
62 * a lot of shared functionality for all fields, including setting values, clearing and basic validation. See the
63 * {@link Ext.field.Field} documentation to see how to leverage its capabilities.
65 * For more information regarding forms and fields, please review [Using Forms in Sencha Touch Guide](../../../components/forms.html)
67 Ext
.define('Ext.field.Text', {
68 extend
: 'Ext.field.Field',
70 alternateClassName
: 'Ext.form.Text',
74 * Fires when this field receives input focus
75 * @param {Ext.field.Text} this This field
76 * @param {Ext.event.Event} e
81 * Fires when this field loses input focus
82 * @param {Ext.field.Text} this This field
83 * @param {Ext.event.Event} e
88 * Fires when this field is pasted.
89 * @param {Ext.field.Text} this This field
90 * @param {Ext.event.Event} e
95 * Fires when this field receives a mousedown
96 * @param {Ext.field.Text} this This field
97 * @param {Ext.event.Event} e
102 * @preventable doKeyUp
103 * Fires when a key is released on the input element
104 * @param {Ext.field.Text} this This field
105 * @param {Ext.event.Event} e
109 * @event clearicontap
110 * @preventable doClearIconTap
111 * Fires when the clear icon is tapped
112 * @param {Ext.field.Text} this This field
113 * @param {Ext.field.Input} input The field's input component.
114 * @param {Ext.event.Event} e
119 * Fires just before the field blurs if the field value has changed
120 * @param {Ext.field.Text} this This field
121 * @param {Mixed} newValue The new value
122 * @param {Mixed} oldValue The original value
127 * @preventable doAction
128 * Fires whenever the return key or go is pressed. FormPanel listeners
129 * for this event, and submits itself whenever it fires. Also note
130 * that this event bubbles up to parent containers.
131 * @param {Ext.field.Text} this This field
132 * @param {Mixed} e The key event object
149 * @cfg {String} placeHolder A string value displayed in the input (if supported) when the control is empty.
155 * @cfg {Number} maxLength The maximum number of permitted input characters.
161 * True to set the field's DOM element autocomplete attribute to "on", false to set to "off".
162 * @cfg {Boolean} autoComplete
168 * True to set the field's DOM element autocapitalize attribute to "on", false to set to "off".
169 * @cfg {Boolean} autoCapitalize
172 autoCapitalize
: null,
175 * True to set the field DOM element autocorrect attribute to "on", false to set to "off".
176 * @cfg {Boolean} autoCorrect
182 * True to set the field DOM element readonly attribute to true.
183 * @cfg {Boolean} readOnly
189 * @cfg {Object} component The inner component for this field, which defaults to an input text.
198 bubbleEvents
: ['action']
202 initialize: function() {
207 me
.getComponent().on({
215 mousedown
: 'onMouseDown',
216 clearicontap
: 'onClearIconTap'
219 // set the originalValue of the textfield, if one exists
220 me
.originalValue
= me
.getValue() || "";
221 me
.getComponent().originalValue
= me
.originalValue
;
226 syncEmptyCls: function() {
227 var empty
= (this._value
) ? this._value
.length
: false,
228 cls
= Ext
.baseCSSPrefix
+ 'empty';
238 updateValue: function(newValue
) {
239 var component
= this.getComponent(),
240 // allows newValue to be zero but not undefined or null (other falsey values)
241 valueValid
= newValue
!== undefined && newValue
!== null && newValue
!== "";
244 component
.setValue(newValue
);
247 this[valueValid
&& this.isDirty() ? 'showClearIcon' : 'hideClearIcon']();
252 getValue: function() {
255 me
._value
= me
.getComponent().getValue();
263 updatePlaceHolder: function(newPlaceHolder
) {
264 this.getComponent().setPlaceHolder(newPlaceHolder
);
268 updateMaxLength: function(newMaxLength
) {
269 this.getComponent().setMaxLength(newMaxLength
);
273 updateAutoComplete: function(newAutoComplete
) {
274 this.getComponent().setAutoComplete(newAutoComplete
);
278 updateAutoCapitalize: function(newAutoCapitalize
) {
279 this.getComponent().setAutoCapitalize(newAutoCapitalize
);
283 updateAutoCorrect: function(newAutoCorrect
) {
284 this.getComponent().setAutoCorrect(newAutoCorrect
);
288 updateReadOnly: function(newReadOnly
) {
290 this.hideClearIcon();
292 this.showClearIcon();
295 this.getComponent().setReadOnly(newReadOnly
);
299 updateInputType: function(newInputType
) {
300 var component
= this.getComponent();
302 component
.setType(newInputType
);
307 updateName: function(newName
) {
308 var component
= this.getComponent();
310 component
.setName(newName
);
315 updateTabIndex: function(newTabIndex
) {
316 var component
= this.getComponent();
318 component
.setTabIndex(newTabIndex
);
323 * Updates the {@link #inputCls} configuration on this fields {@link #component}
326 updateInputCls: function(newInputCls
, oldInputCls
) {
327 var component
= this.getComponent();
329 component
.replaceCls(oldInputCls
, newInputCls
);
333 doSetDisabled: function(disabled
) {
336 me
.callParent(arguments
);
338 var component
= me
.getComponent();
340 component
.setDisabled(disabled
);
351 showClearIcon: function() {
353 value
= me
.getValue(),
354 // allows value to be zero but not undefined or null (other falsey values)
355 valueValid
= value
!== undefined && value
!== null && value
!== "";
357 if (me
.getClearIcon() && !me
.getDisabled() && !me
.getReadOnly() && valueValid
) {
358 me
.element
.addCls(Ext
.baseCSSPrefix
+ 'field-clearable');
365 hideClearIcon: function() {
366 if (this.getClearIcon()) {
367 this.element
.removeCls(Ext
.baseCSSPrefix
+ 'field-clearable');
371 onKeyUp: function(e
) {
372 this.fireAction('keyup', [this, e
], 'doKeyUp');
376 * Called when a key has been pressed in the `<input>`
379 doKeyUp: function(me
, e
) {
380 // getValue to ensure that we are in sync with the dom
381 var value
= me
.getValue(),
382 // allows value to be zero but not undefined or null (other falsey values)
383 valueValid
= value
!== undefined && value
!== null && value
!== "";
385 this[valueValid
? 'showClearIcon' : 'hideClearIcon']();
387 if (e
.browserEvent
.keyCode
=== 13) {
388 me
.fireAction('action', [me
, e
], 'doAction');
392 doAction: function() {
396 onClearIconTap: function(input
, e
) {
397 this.fireAction('clearicontap', [this, input
, e
], 'doClearIconTap');
401 doClearIconTap: function(me
, e
) {
404 //sync with the input
408 onChange: function(me
, value
, startValue
) {
409 me
.fireEvent('change', this, value
, startValue
);
412 onFocus: function(e
) {
413 this.addCls(Ext
.baseCSSPrefix
+ 'field-focused');
414 this.isFocused
= true;
415 this.fireEvent('focus', this, e
);
418 onBlur: function(e
) {
421 this.removeCls(Ext
.baseCSSPrefix
+ 'field-focused');
422 this.isFocused
= false;
424 me
.fireEvent('blur', me
, e
);
426 setTimeout(function() {
427 me
.isFocused
= false;
431 onPaste: function(e
) {
432 this.fireEvent('paste', this, e
);
435 onMouseDown: function(e
) {
436 this.fireEvent('mousedown', this, e
);
440 * Attempts to set the field as the active input focus.
441 * @return {Ext.field.Text} This field
444 this.getComponent().focus();
449 * Attempts to forcefully blur input focus for the field.
450 * @return {Ext.field.Text} This field
453 this.getComponent().blur();
458 * Attempts to forcefully select all the contents of the input field.
459 * @return {Ext.field.Text} this
462 this.getComponent().select();
466 resetOriginalValue: function() {
468 var component
= this.getComponent();
469 if(component
&& component
.hasOwnProperty("originalValue")) {
470 this.getComponent().originalValue
= this.originalValue
;
476 this.getComponent().reset();
478 //we need to call this to sync the input with this field
481 this[this.isDirty() ? 'showClearIcon' : 'hideClearIcon']();
484 isDirty: function() {
485 var component
= this.getComponent();
487 return component
.isDirty();
493 //<deprecated product=touch since=2.0>
495 * @property startValue
496 * @type String/Number
497 * Used to contain the previous value of the field before the edit
499 * @member Ext.field.Text