Upgrade to ExtJS 3.3.1 - Released 11/30/2010
[extjs.git] / src / widgets / form / Radio.js
1 /*!
2  * Ext JS Library 3.3.1
3  * Copyright(c) 2006-2010 Sencha Inc.
4  * licensing@sencha.com
5  * http://www.sencha.com/license
6  */
7 /**
8  * @class Ext.form.Radio
9  * @extends Ext.form.Checkbox
10  * Single radio field.  Same as Checkbox, but provided as a convenience for automatically setting the input type.
11  * Radio grouping is handled automatically by the browser if you give each radio in a group the same name.
12  * @constructor
13  * Creates a new Radio
14  * @param {Object} config Configuration options
15  * @xtype radio
16  */
17 Ext.form.Radio = Ext.extend(Ext.form.Checkbox, {
18     inputType: 'radio',
19
20     /**
21      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
22      * @method
23      */
24     markInvalid : Ext.emptyFn,
25     /**
26      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
27      * @method
28      */
29     clearInvalid : Ext.emptyFn,
30
31     /**
32      * If this radio is part of a group, it will return the selected value
33      * @return {String}
34      */
35     getGroupValue : function(){
36         var p = this.el.up('form') || Ext.getBody();
37         var c = p.child('input[name='+this.el.dom.name+']:checked', true);
38         return c ? c.value : null;
39     },
40
41     /**
42      * Sets either the checked/unchecked status of this Radio, or, if a string value
43      * is passed, checks a sibling Radio of the same name whose value is the value specified.
44      * @param value {String/Boolean} Checked value, or the value of the sibling radio button to check.
45      * @return {Ext.form.Field} this
46      */
47     setValue : function(v){
48         var checkEl,
49             els,
50             radio;
51         if (typeof v == 'boolean') {
52             Ext.form.Radio.superclass.setValue.call(this, v);
53         } else if (this.rendered) {
54             checkEl = this.getCheckEl();
55             radio = checkEl.child('input[name=' + this.el.dom.name + '][value=' + v + ']', true);
56             if(radio){
57                 Ext.getCmp(radio.id).setValue(true);
58             }
59         }
60         if(this.rendered && this.checked){
61             checkEl = checkEl || this.getCheckEl();
62             els = this.getCheckEl().select('input[name=' + this.el.dom.name + ']');
63                         els.each(function(el){
64                                 if(el.dom.id != this.id){
65                                         Ext.getCmp(el.dom.id).setValue(false);
66                                 }
67                         }, this);
68         }
69         return this;
70     },
71
72     // private
73     getCheckEl: function(){
74         if(this.inGroup){
75             return this.el.up('.x-form-radio-group');
76         }
77         return this.el.up('form') || Ext.getBody();
78     }
79 });
80 Ext.reg('radio', Ext.form.Radio);