Upgrade to ExtJS 3.1.1 - Released 02/08/2010
[extjs.git] / src / widgets / form / Radio.js
1 /*!
2  * Ext JS Library 3.1.1
3  * Copyright(c) 2006-2010 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.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     // private
42     onClick : function(){
43         if(this.el.dom.checked != this.checked){
44                         var els = this.getCheckEl().select('input[name=' + this.el.dom.name + ']');
45                         els.each(function(el){
46                                 if(el.dom.id == this.id){
47                                         this.setValue(true);
48                                 }else{
49                                         Ext.getCmp(el.dom.id).setValue(false);
50                                 }
51                         }, this);
52                 }
53     },
54
55     /**
56      * Sets either the checked/unchecked status of this Radio, or, if a string value
57      * is passed, checks a sibling Radio of the same name whose value is the value specified.
58      * @param value {String/Boolean} Checked value, or the value of the sibling radio button to check.
59      * @return {Ext.form.Field} this
60      */
61     setValue : function(v){
62         if (typeof v == 'boolean') {
63             Ext.form.Radio.superclass.setValue.call(this, v);
64         } else if (this.rendered) {
65             var r = this.getCheckEl().child('input[name=' + this.el.dom.name + '][value=' + v + ']', true);
66             if(r){
67                 Ext.getCmp(r.id).setValue(true);
68             }
69         }
70         return this;
71     },
72
73     // private
74     getCheckEl: function(){
75         if(this.inGroup){
76             return this.el.up('.x-form-radio-group')
77         }
78         return this.el.up('form') || Ext.getBody();
79     }
80 });
81 Ext.reg('radio', Ext.form.Radio);