1

分机 JS 6.6

经典工具包,一个文本字段有selectOnFocus : true/false 所以当组件获得焦点时,文本字段中的数据全部被选中

items: [{
  xtype: 'textfield',
  selectOnFocus: true
}]

现代工具包没有这个属性。我理解现代都符合 CSS3 并非常适合移动设备之间的需求,以及使用移动设备之间的差异。使用现代工具包,您可以通过

Ext.ComponentQuery.query('#textfield')[0].focus(true);

这在编写代码时很好。但我发现的问题是使用 Sencha Test 和以下代码

it("Checking existing email error notification", function () {
  ST.component('textfield[name=\"createemail\"]').click();
  ST.play([
    { type: "type", target: "textfield[name=\"createemail\"] => input", text: "someemail.email.com", caret:0 }
  .... additional code here
  ]);
});
it("Some test", function() {
  ST.component('textfield[name=\"createemail\"]').click();
  .... additiona code here
});

在第二个测试块中,单击文本字段不会选择所有文本,因此如果我想替换现有文本,则必须将其清除或插入点从随机位置开始。

那么我们如何才能在 Modern 工具包中获得 selectOnFocus: 真正的功能呢?

我可以看到一些实例,在 Modern 中拥有此功能实际上更重要,因为在移动设备上 ctrl 'a' 不是一个选项。

4

1 回答 1

0

您可以使用我的覆盖获得所需的行为:

Ext.define('Ext.field.TextOverride', {
    override: 'Ext.field.Text',
    onFocus: function (e) {
        var me = this,
            focusTarget, focusElDom;

        this.callParent(arguments)
        var me = this,
            focusTarget, focusElDom;

        if (focusTarget = me.getFocusEl()) {

            focusElDom = focusTarget.dom;

            if (focusElDom) {
                focusTarget.focus();
                if (me.selectOnFocus && (me.selectText || focusElDom.select)) {
                    if (me.selectText) {
                        if (Ext.isArray(selectText)) {
                            me.selectText.apply(me, selectText);
                        } else {
                            me.selectText();
                        }
                    } else {
                        focusElDom.select();
                    }
                }
            }
        }
    }
});

小提琴

PS您不能在测试中使用`focus(true)?

于 2020-05-23T15:32:22.810 回答