2 * @class Ext.form.field.Checkbox
3 * @extends Ext.form.field.Base
5 Single checkbox field. Can be used as a direct replacement for traditional checkbox fields. Also serves as a
6 parent class for {@link Ext.form.field.Radio radio buttons}.
8 __Labeling:__ In addition to the {@link Ext.form.Labelable standard field labeling options}, checkboxes
9 may be given an optional {@link #boxLabel} which will be displayed immediately after checkbox. Also see
10 {@link Ext.form.CheckboxGroup} for a convenient method of grouping related checkboxes.
13 The main value of a checkbox is a boolean, indicating whether or not the checkbox is checked.
14 The following values will check the checkbox:
20 Any other value will uncheck the checkbox.
22 In addition to the main boolean value, you may also specify a separate {@link #inputValue}. This will be
23 sent as the parameter value when the form is {@link Ext.form.Basic#submit submitted}. You will want to set
24 this value if you have multiple checkboxes with the same {@link #name}. If not specified, the value `on`
26 {@img Ext.form.Checkbox/Ext.form.Checkbox.png Ext.form.Checkbox Checkbox component}
29 Ext.create('Ext.form.Panel', {
32 title : 'Pizza Order',
35 xtype : 'fieldcontainer',
36 fieldLabel : 'Toppings',
37 defaultType: 'checkboxfield',
40 boxLabel : 'Anchovies',
45 boxLabel : 'Artichoke Hearts',
63 var checkbox = Ext.getCmp('checkbox3');
64 checkbox.setValue(true);
71 var checkbox1 = Ext.getCmp('checkbox1'),
72 checkbox2 = Ext.getCmp('checkbox2'),
73 checkbox3 = Ext.getCmp('checkbox3');
75 checkbox1.setValue(true);
76 checkbox2.setValue(true);
77 checkbox3.setValue(true);
83 var checkbox1 = Ext.getCmp('checkbox1'),
84 checkbox2 = Ext.getCmp('checkbox2'),
85 checkbox3 = Ext.getCmp('checkbox3');
87 checkbox1.setValue(false);
88 checkbox2.setValue(false);
89 checkbox3.setValue(false);
93 renderTo: Ext.getBody()
97 * Creates a new Checkbox
98 * @param {Object} config Configuration options
99 * @xtype checkboxfield
100 * @docauthor Robert Dougan <rob@sencha.com>
103 Ext.define('Ext.form.field.Checkbox', {
104 extend: 'Ext.form.field.Base',
105 alias: ['widget.checkboxfield', 'widget.checkbox'],
106 alternateClassName: 'Ext.form.Checkbox',
107 requires: ['Ext.XTemplate', 'Ext.form.CheckboxManager'],
110 '<tpl if="boxLabel && boxLabelAlign == \'before\'">',
111 '<label class="{boxLabelCls} {boxLabelCls}-{boxLabelAlign}" for="{id}">{boxLabel}</label>',
113 // Creates not an actual checkbox, but a button which is given aria role="checkbox" and
114 // styled with a custom checkbox image. This allows greater control and consistency in
115 // styling, and using a button allows it to gain focus and handle keyboard nav properly.
116 '<input type="button" id="{id}" ',
117 '<tpl if="tabIdx">tabIndex="{tabIdx}" </tpl>',
118 'class="{fieldCls} {typeCls}" autocomplete="off" hidefocus="true" />',
119 '<tpl if="boxLabel && boxLabelAlign == \'after\'">',
120 '<label class="{boxLabelCls} {boxLabelCls}-{boxLabelAlign}" for="{id}">{boxLabel}</label>',
123 disableFormats: true,
131 * @cfg {String} focusCls The CSS class to use when the checkbox receives focus
132 * (defaults to <tt>'x-form-cb-focus'</tt>)
134 focusCls: Ext.baseCSSPrefix + 'form-cb-focus',
137 * @cfg {String} fieldCls The default CSS class for the checkbox (defaults to <tt>'x-form-field'</tt>)
141 * @cfg {String} fieldBodyCls
142 * An extra CSS class to be applied to the body content element in addition to {@link #fieldBodyCls}.
143 * Defaults to 'x-form-cb-wrap.
145 fieldBodyCls: Ext.baseCSSPrefix + 'form-cb-wrap',
148 * @cfg {Boolean} checked <tt>true</tt> if the checkbox should render initially checked (defaults to <tt>false</tt>)
153 * @cfg {String} checkedCls The CSS class added to the component's main element when it is in the checked state.
155 checkedCls: Ext.baseCSSPrefix + 'form-cb-checked',
158 * @cfg {String} boxLabel An optional text label that will appear next to the checkbox. Whether it appears before
159 * or after the checkbox is determined by the {@link #boxLabelAlign} config (defaults to after).
163 * @cfg {String} boxLabelCls The CSS class to be applied to the {@link #boxLabel} element
165 boxLabelCls: Ext.baseCSSPrefix + 'form-cb-label',
168 * @cfg {String} boxLabelAlign The position relative to the checkbox where the {@link #boxLabel} should
169 * appear. Recognized values are <tt>'before'</tt> and <tt>'after'</tt>. Defaults to <tt>'after'</tt>.
171 boxLabelAlign: 'after',
174 * @cfg {String} inputValue The value that should go into the generated input element's value attribute and
175 * should be used as the parameter value when submitting as part of a form. Defaults to <tt>"on"</tt>.
180 * @cfg {String} uncheckedValue If configured, this will be submitted as the checkbox's value during form
181 * submit if the checkbox is unchecked. By default this is undefined, which results in nothing being
182 * submitted for the checkbox field when the form is submitted (the default behavior of HTML checkboxes).
186 * @cfg {Function} handler A function called when the {@link #checked} value changes (can be used instead of
187 * handling the {@link #change change event}). The handler is passed the following parameters:
188 * <div class="mdetail-params"><ul>
189 * <li><b>checkbox</b> : Ext.form.field.Checkbox<div class="sub-desc">The Checkbox being toggled.</div></li>
190 * <li><b>checked</b> : Boolean<div class="sub-desc">The new checked state of the checkbox.</div></li>
195 * @cfg {Object} scope An object to use as the scope ('this' reference) of the {@link #handler} function
196 * (defaults to this Checkbox).
200 checkChangeEvents: [],
201 inputType: 'checkbox',
202 ariaRole: 'checkbox',
207 initComponent: function(){
208 this.callParent(arguments);
209 this.getManager().add(this);
212 initValue: function() {
214 checked = !!me.checked;
217 * The original value of the field as configured in the {@link #checked} configuration, or
218 * as loaded by the last form load operation if the form's {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad}
219 * setting is <code>true</code>.
221 * @property originalValue
223 me.originalValue = me.lastValue = checked;
225 // Set the initial checked state
226 me.setValue(checked);
230 onRender : function(ct, position) {
232 Ext.applyIf(me.renderSelectors, {
234 * @property boxLabelEl
235 * @type Ext.core.Element
236 * A reference to the label element created for the {@link #boxLabel}. Only present if the
237 * component has been rendered and has a boxLabel configured.
239 boxLabelEl: 'label.' + me.boxLabelCls
241 Ext.applyIf(me.subTplData, {
242 boxLabel: me.boxLabel,
243 boxLabelCls: me.boxLabelCls,
244 boxLabelAlign: me.boxLabelAlign
247 me.callParent(arguments);
250 initEvents: function() {
253 me.mon(me.inputEl, 'click', me.onBoxClick, me);
257 * @private Handle click on the checkbox button
259 onBoxClick: function(e) {
261 if (!me.disabled && !me.readOnly) {
262 this.setValue(!this.checked);
267 * Returns the checked state of the checkbox.
268 * @return {Boolean} True if checked, else false
270 getRawValue: function() {
275 * Returns the checked state of the checkbox.
276 * @return {Boolean} True if checked, else false
278 getValue: function() {
283 * Returns the submit value for the checkbox which can be used when submitting forms.
284 * @return {Boolean/null} True if checked; otherwise either the {@link #uncheckedValue} or null.
286 getSubmitValue: function() {
287 var unchecked = this.uncheckedValue,
288 uncheckedVal = Ext.isDefined(unchecked) ? unchecked : null;
289 return this.checked ? this.inputValue : uncheckedVal;
293 * Sets the checked state of the checkbox.
294 * @param {Boolean/String} value The following values will check the checkbox:
295 * <code>true, 'true', '1', or 'on'</code>, as well as a String that matches the {@link #inputValue}.
296 * Any other value will uncheck the checkbox.
297 * @return {Boolean} the new checked state of the checkbox
299 setRawValue: function(value) {
301 inputEl = me.inputEl,
302 inputValue = me.inputValue,
303 checked = (value === true || value === 'true' || value === '1' ||
304 ((Ext.isString(value) && inputValue) ? value == inputValue : me.onRe.test(value)));
307 inputEl.dom.setAttribute('aria-checked', checked);
308 me[checked ? 'addCls' : 'removeCls'](me.checkedCls);
311 me.checked = me.rawValue = checked;
316 * Sets the checked state of the checkbox, and invokes change detection.
317 * @param {Boolean/String} checked The following values will check the checkbox:
318 * <code>true, 'true', '1', or 'on'</code>, as well as a String that matches the {@link #inputValue}.
319 * Any other value will uncheck the checkbox.
320 * @return {Ext.form.field.Checkbox} this
322 setValue: function(checked) {
325 // If an array of strings is passed, find all checkboxes in the group with the same name as this
326 // one and check all those whose inputValue is in the array, unchecking all the others. This is to
327 // facilitate setting values from Ext.form.Basic#setValues, but is not publicly documented as we
328 // don't want users depending on this behavior.
329 if (Ext.isArray(checked)) {
330 me.getManager().getByName(me.name).each(function(cb) {
331 cb.setValue(Ext.Array.contains(checked, cb.inputValue));
334 me.callParent(arguments);
341 valueToRaw: function(value) {
342 // No extra conversion for checkboxes
348 * Called when the checkbox's checked state changes. Invokes the {@link #handler} callback
349 * function if specified.
351 onChange: function(newVal, oldVal) {
353 handler = me.handler;
355 handler.call(me.scope || me, me, newVal);
357 me.callParent(arguments);
361 getManager: function() {
362 return Ext.form.CheckboxManager;
365 onEnable: function() {
367 inputEl = me.inputEl;
370 // Can still be disabled if the field is readOnly
371 inputEl.dom.disabled = me.readOnly;
375 setReadOnly: function(readOnly) {
377 inputEl = me.inputEl;
379 // Set the button to disabled when readonly
380 inputEl.dom.disabled = readOnly || me.disabled;
382 me.readOnly = readOnly;
386 * @protected Calculate and return the natural width of the bodyEl. It's possible that the initial
387 * rendering will cause the boxLabel to wrap and give us a bad width, so we must prevent wrapping
390 getBodyNaturalWidth: function() {
395 bodyEl.setStyle(ws, 'nowrap');
396 width = bodyEl.getWidth();
397 bodyEl.setStyle(ws, '');