手动移动焦点时Combo不会模糊

Combo doesn't blur when manually shifting focus

本文关键字:模糊 Combo 移动 焦点      更新时间:2023-09-26

我有一个组合框,用于在用户选择值后立即将焦点转移到另一个表单元素,配置如下:

new Ext.form.ComboBox({
    // ...
    listeners: {
        select: function( a, record ) {
            if ( typeof( record ) == 'undefined' ) {
                return;
            }
            if ( !Ext.getCmp('input-name').getValue() ) {
                Ext.getCmp('input-name').focus();
            }
        },
        blur: function() {
            console.log('blurred');
        },
        render: function( field ) {
            if ( !config.activity ) {
                field.onTriggerClick();
            }
        }
    },
    // ...
});

然而,奇怪的事情发生了。"input-name"表单字段受到关注,我可以开始在其中输入,但组合字段永远不会模糊。它仍然具有"x-form-focus"样式,并且"blur"事件永远不会被触发。只有当我用鼠标点击另一个字段时,组合才会模糊。

有谁知道发生了什么事,我该如何规避?

我是这样解决的:

listeners: {
    select: function( a, record ) {
        if ( typeof( record ) == 'undefined' ) {
            return;
        }
        /**
         * There's some weird stuff going on in the combo control, that causes it not to blur when shifting focus,
         * so we have to do it manually. This action has to be deferred, because the control is going to refocus 
         * itself at the end of the function firing this event (onViewClick()).
         */
        this.moveFocus.defer( 100, this );
    },
    render: function( field ) {
        field.moveFocus = function() {
            if ( !Ext.getCmp('input-name').getValue() ) {
                Ext.getCmp('input-name').focus();
                this.triggerBlur();
            }
        };
        if ( !config.activity ) {
            field.onTriggerClick();
        }
    }
},

当任何代码片段作为事件的一部分被执行并且导致触发其他事件时,则不会触发新事件。onBlur应该只在用户在UI中执行某些操作时执行,因为组合框会失去焦点,而不是通过编程方式使字段失去焦点。