Search code examples
extjsextreact

Extreact 6 checkbox "checked" value prevent true


Toying with Extreact and I have a checkbox on my page that I want to have control over it's checked state. For example I want to set the checked value inside my state and be able to check or uncheck that input without clicking on it as a user.

The problem is that CheckboxField only has a checked prop that refers to the initial state so it's pretty useless afterwards.


Solution

  • It's not possible about of the box to prevent clicks from changing the state, but it's pretty easy to achieve with an override. Fiddle.

    Ext.define(null, {
        override: 'Ext.field.Checkbox',
    
        updateReadOnly: function (value, oldValue) {
            this.callParent([value, oldValue]);
            this.inputElement.dom.onclick = value ? function () {
                return false;
            } : null;
        }
    })
    
    Ext.application({
        name: 'Fiddle',
    
        launch: function () {
            Ext.Viewport.add({
                xtype: 'container',
                items: [{
                    id: 'foo',
                    xtype: 'checkbox',
                    readOnly: true
                }, {
                    xtype: 'button',
                    text: 'On',
                    handler: function () {
                        Ext.getCmp('foo').check();
                    }
                }, {
                    xtype: 'button',
                    text: 'Off',
                    handler: function () {
                        Ext.getCmp('foo').uncheck();
                    }
                }]
            });
        }
    });