openOnFocus
I saw the "openOnFocus" and expected the full suggestion list to be used, unfiltered, when openOnFocus is set to true. That's not the case.
In the Dropdown#open() function:
open: function open() {
if (!this.isOpen) {
this.isOpen = true;
if (!this.isEmpty) {
this._show();
}
this.trigger('opened');
}
},
When the property this.isEmpty is true (empty query string) the _show() doesn't get called and the popup is in fact not opened but still closed. However, 'opened' is fired anyway and the property this.isOpen is set to true. Seems like this is not what you want because now this.isOpen is backwards and listeners have been notified that the popup is in fact open. Doh!
Be that as it may, we have a scenario where we show the customer some data, for example a list of recent searches, when the input is empty but focused. In other words, we show a default dataset to render when the query string is empty. How would you go about that scenario without hacking the library :)
Hey @markkra,
I think we're on the same page regarding what it should do, but you got confused because you didn't configure the minLength option:
_onFocused: function onFocused() {
this.isActivated = true;
if (this.openOnFocus) {
var query = this.input.getQuery();
if (query.length >= this.minLength) {
this.dropdown.update(query);
} else {
this.dropdown.empty();
}
this.dropdown.open();
}
},
Basically, if you don't set it to 0 it will not trigger any search setting that underlying isEmpty attribute.
Could you try to put minLength: 0? Maybe that's something we should automatically do if openOnFocus is set.
Yup, was just trying that this morning and got the full list with minLength = 0. Still sort of a gotcha in the open() function. Wouldn't this be better?
open: function open() {
if (!this.isOpen) {
if (!this.isEmpty) {
// only toggle the isOpen flag and fire "opened" when actually showing the popup.
this.isOpen = true;
this._show();
this.trigger('opened');
}
}
}
Cool :)
And yes, I think your snippet would be better; that code has been ported by the original typeahead.js library so I don't have the whole history of it; but I think it would make it much more clearer.
Do you mind sending a pull-request? We may have some tests to update as well.
Doh! Yup, 9 tests not happy. I'll take a look at those as well.
I'm not having much luck debugging the unit tests on a Mac. I'm a long time Windows Visual Studio user (at MSFT for 18yrs before moving on) and just started on the Mac a few months ago. Missing my toolchain... Any hints on stepping through the tests on the Mac? On the PC I used a test runner called Chutzpah that was a big help.
~sorry for the necromancy, but minLength: 0 does not work for me and I just can't get the thing to openOnFocus~
ignore me, I just found out what might be wrong.
This seems to be working for me @ayghor: https://codesandbox.io/s/n5061zp06m
Autocomplete v0 has been in low maintenance mode for a few years and we just released Autocomplete v1 so I'm closing this.
Resources