Skip to content

Commit

Permalink
debounce onInput. fixes #560
Browse files Browse the repository at this point in the history
  • Loading branch information
oyejorge committed Oct 26, 2023
1 parent 681aadb commit f5218a7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export default {
preload: null,
allowEmptyOption: false,
//closeAfterSelect: false,
refreshThrottle: 300,


loadThrottle: 300,
loadingClass: 'loading',
Expand Down
38 changes: 28 additions & 10 deletions src/tom-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
preventDefault,
addEvent,
loadDebounce,
timeout,
isKeyDown,
getId,
addSlashes,
Expand Down Expand Up @@ -87,6 +88,7 @@ export default class TomSelect extends MicroPlugin(MicroEvent){
public userOptions : {[key:string]:boolean} = {};
public items : string[] = [];

private refreshTimeout : null|ReturnType<typeof setTimeout> = null;


constructor( input_arg: string|TomInput, user_settings:RecursivePartial<TomSettings> ){
Expand Down Expand Up @@ -760,23 +762,39 @@ export default class TomSelect extends MicroPlugin(MicroEvent){
*
*/
onInput(e:MouseEvent|KeyboardEvent):void {
var self = this;

if( this.isLocked ){
return;
}

if( self.isLocked ){
const value = this.inputValue();
if( this.lastValue === value ) return;
this.lastValue = value;

if( value == '' ){
this._onInput();
return;
}

var value = self.inputValue();
if (self.lastValue !== value) {
self.lastValue = value;
if( this.refreshTimeout ){
clearTimeout(this.refreshTimeout);
}

this.refreshTimeout = timeout(()=> {
this.refreshTimeout = null;
this._onInput();
}, this.settings.refreshThrottle);
}

if( self.settings.shouldLoad.call(self,value) ){
self.load(value);
}
_onInput():void {
const value = this.lastValue;

self.refreshOptions();
self.trigger('type', value);
if( this.settings.shouldLoad.call(this,value) ){
this.load(value);
}

this.refreshOptions();
this.trigger('type', value);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type TomSettings = {
preload : boolean|string,
allowEmptyOption : boolean,
closeAfterSelect : boolean,
refreshThrottle : number,

loadThrottle : number,
loadingClass : string,
Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export const escape_html = (str:string):string => {
};


/**
* use setTimeout if timeout > 0
*/
export const timeout = (fn:()=>void,timeout:number) => {
if( timeout > 0 ){
return setTimeout(fn,timeout);
}

fn.call(null);
return null;
}

/**
* Debounce the user provided load function
*
Expand Down
2 changes: 2 additions & 0 deletions test/support/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ window.setup_test = function(html, options, callback) {
}

if( select.nodeName == 'SELECT' || select.nodeName == 'INPUT' ){
options = options || {};
options.refreshThrottle = 0;
instance = tomSelect(select,options);
}

Expand Down

1 comment on commit f5218a7

@mikz
Copy link

@mikz mikz commented on f5218a7 Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For anyone trying to figure out why their automation simulating key input (like #71) broke. This is the reason and the fix.

Please sign in to comment.