4
votes

I have a numberfield input which I want to lose focus when the user hits the enter key.

Here's the current config:

{
  xtype: 'numberfield',
  minValue: 0,
  maxValue: 99999,
  hideTrigger: true,
  keyNavEnabled: false,
  mouseWheelEnabled: false,
  enableKeyEvents: true,

  listeners: {
    specialkey: function(f, e) {
      if (e.getKey() == e.ENTER)
        f.blur();
    }
  }
}

The specialkey handler works, calling blur() as expected, but the only result is that the caret disappears from the input (indicating that the DOM text input element has been blurred). The field still has all the x-form-focus etc css classes, and examining the object shows that the private hasFocus property is still true.

Any suggestions?

I have tried calling f.onBlur() explicitly after the blur() call, but it made no difference.

(btw the field does not belong to a form so there is no form to submit.)

2
What about focusing another component? Even its parent container... - VoidMain
@VoidMain - cheers, but same result as calling blur() - Rob Agar
If you call f.up().focus(); it doesn't make f lose its focused class? - VoidMain
Quick question: Are you calling f.up().focus() alone or are you calling f.blur() before? local testing shows that, if i call f.blur() the call to f.up().focus() its, somehow, ignored... - VoidMain
@VoidMain focus() alone, but trying both gives the same result - Rob Agar

2 Answers

3
votes

The fix. The issue is caused by a weird behavior in the Ext.form.field.Trigger class where they don't trigger 'triggerBlur()' when the regular 'blur()' function is called. Very interesting:

listeners: {
    specialkey: function(f, e) {
        if (e.getKey() == e.ENTER)
        {
            f.triggerBlur();
            f.blur();
        }
    }
}
1
votes

This works for me :

specialkey: function(field,events) {
    if (events.getKey() == events.ENTER) {
        var nextfld = field.nextSibling(); 
        nextfld.focus(true,100);
    }
}

specialkey - is a listener of component like textfield,textare,datefield etc,

events.getKey - gets the user keyboard input

events.ENTER - i used this for that will be triggered when "ENTER" in keyboard

field.nextSibling - field refers to current component, and nextSibling property of component to tell who will be the next component

lastly is the nextfld.focus to focus the next component