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.Radio
17 * @extends Ext.form.field.Checkbox
19 Single radio field. Similar to checkbox, but automatically handles making sure only one radio is checked
20 at a time within a group of radios with the same name.
23 In addition to the {@link Ext.form.Labelable standard field labeling options}, radio buttons
24 may be given an optional {@link #boxLabel} which will be displayed immediately to the right of the input. Also
25 see {@link Ext.form.RadioGroup} for a convenient method of grouping related radio buttons.
28 The main value of a Radio field is a boolean, indicating whether or not the radio is checked.
30 The following values will check the radio:
36 Any other value will uncheck it.
38 In addition to the main boolean value, you may also specify a separate {@link #inputValue}. This will be sent
39 as the parameter value when the form is {@link Ext.form.Basic#submit submitted}. You will want to set this
40 value if you have multiple radio buttons with the same {@link #name}, as is almost always the case.
41 {@img Ext.form.Radio/Ext.form.Radio.png Ext.form.Radio component}
44 Ext.create('Ext.form.Panel', {
48 renderTo : Ext.getBody(),
51 xtype : 'fieldcontainer',
53 defaultType: 'radiofield',
78 xtype : 'fieldcontainer',
80 defaultType: 'radiofield',
107 text: 'Smaller Size',
108 handler: function() {
109 var radio1 = Ext.getCmp('radio1'),
110 radio2 = Ext.getCmp('radio2'),
111 radio3 = Ext.getCmp('radio3');
113 //if L is selected, change to M
114 if (radio2.getValue()) {
115 radio1.setValue(true);
119 //if XL is selected, change to L
120 if (radio3.getValue()) {
121 radio2.setValue(true);
125 //if nothing is set, set size to S
126 radio1.setValue(true);
131 handler: function() {
132 var radio1 = Ext.getCmp('radio1'),
133 radio2 = Ext.getCmp('radio2'),
134 radio3 = Ext.getCmp('radio3');
136 //if M is selected, change to L
137 if (radio1.getValue()) {
138 radio2.setValue(true);
142 //if L is selected, change to XL
143 if (radio2.getValue()) {
144 radio3.setValue(true);
148 //if nothing is set, set size to XL
149 radio3.setValue(true);
154 text: 'Select color',
160 handler: function() {
161 var radio = Ext.getCmp('radio4');
162 radio.setValue(true);
167 handler: function() {
168 var radio = Ext.getCmp('radio5');
169 radio.setValue(true);
174 handler: function() {
175 var radio = Ext.getCmp('radio6');
176 radio.setValue(true);
186 * @docauthor Robert Dougan <rob@sencha.com>
189 Ext.define('Ext.form.field.Radio', {
190 extend:'Ext.form.field.Checkbox',
191 alias: ['widget.radiofield', 'widget.radio'],
192 alternateClassName: 'Ext.form.Radio',
193 requires: ['Ext.form.RadioManager'],
198 * @cfg {String} uncheckedValue @hide
206 * If this radio is part of a group, it will return the selected value
209 getGroupValue: function() {
210 var selected = this.getManager().getChecked(this.name);
211 return selected ? selected.inputValue : null;
215 * @private Handle click on the radio button
217 onBoxClick: function(e) {
219 if (!me.disabled && !me.readOnly) {
225 * Sets either the checked/unchecked status of this Radio, or, if a string value
226 * is passed, checks a sibling Radio of the same name whose value is the value specified.
227 * @param value {String/Boolean} Checked value, or the value of the sibling radio button to check.
228 * @return {Ext.form.field.Radio} this
230 setValue: function(v) {
234 if (Ext.isBoolean(v)) {
235 me.callParent(arguments);
237 active = me.getManager().getWithValue(me.name, v).getAt(0);
239 active.setValue(true);
246 * Returns the submit value for the checkbox which can be used when submitting forms.
247 * @return {Boolean/null} True if checked, null if not.
249 getSubmitValue: function() {
250 return this.checked ? this.inputValue : null;
253 getModelData: function() {
254 return this.getSubmitData();
258 onChange: function(newVal, oldVal) {
260 me.callParent(arguments);
263 this.getManager().getByName(me.name).each(function(item){
265 item.setValue(false);
272 beforeDestroy: function(){
274 this.getManager().removeAtKey(this.id);
278 getManager: function() {
279 return Ext.form.RadioManager;