infinite-autocomplete [CORE]
Pluggable infinite-autocomplete component that can accept any implementations of yours.
This autocomplete is like all these autocomplete out there, except that he is "Infinite" - which means that scrolling will fetch more data
Ease of use, flexable and written with TypeScript following the SOLID principles to guarantee a plug-in implementations so you can customize it's behaviours and inject it in any environment you want.
[Wrappers for AngularJS and React will be implemented in the next months, and for other frameworks]
Install
npm i --save infinite-autocomplete
Developer section
In the package.json scripts we've 4 basic tasks:
build
- Build the code once
build:watch
- Build the code and watch changes
test
- Run tests once
test:watch
- Run tests and watch changes
Basic Usage (Minimum Configuration)
import { InfiniteAutocomplete } from 'infinite-autocomplete';
new InfiniteAutocomplete(document.getElementById('test'), {
data: [
{ text: 'Islam Attrash', value: 1},
{ text: 'Shai Reznik', value: 2},
{ text: 'Uri Shaked', value: 3},
{ text: 'Salsabel Eawissat', value: 4}
]
});
new InfiniteAutocomplete(document.getElementById('test'), {
getDataFromApi: function(text, page, fetchSize) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve([ ..... ]);
}, 500);
})
}
});
Live Demo (Default Style)
Override Implementations
Regards the custom implementations, they're targeting the two main component of this plugin, which is: InputComponent, OptionsComponent (The result)
For that you've two classes exported to help you manage correctly extending with your own classes
The Defaults implements these interfaces:
The IInputComponent interface can be defined as:
export interface IInputComponent {
render():string;
onInputChange?(inputElement:HTMLInputElement, value:string);
}
The IOptionsComponent interface can be defined as:
export interface IOptionsComponent {
listElementSelector:string;
render():string;
renderOption(option:IOption):string;
}
Implementations Override Examples
import { InfiniteAutocomplete, InputComponent } from 'infinite-autocomplete';
class SpecialInput extends InputComponent {
render() {
return `<input class="test-input" style="background: red;" />`;
}
}
new InfiniteAutocomplete(document.getElementById('test'), {
customizedInput: SpecialInput,
data: [
{ text: 'Islam Attrash', value: 1},
{ text: 'Shai Reznik', value: 2},
{ text: 'Uri Shaked', value: 3},
{ text: 'Salsabel Eawissat', value: 4}
]
});
Configurations
The configuration object will be exposed when initializing the infinite-autocomplete component as the second argument in the type-system typings, so it can help you know what configuration to apply.
data?:Array<IOption>;
onSelect?:Function;
maxHeight?:string;
getDataFromApi?(text:string, page:number, fetchSize:number):es6Promise<Array<any>>;
fetchSize?:number,
customizedInput?:IInputCompoenentConstructor;
customizedOptions?:IOptionsComponentConstructor;
ES5 also works
var CustomizedInput = (function () {
function CustomizedInput() {
}
CustomizedInput.prototype.render = function () {
return '<input type="text" />';
};
CustomizedInput.prototype.onInputChange = function (inputDOMElement, value) {
console.log(value);
};
return CustomizedInput;
}());
var OptionsComponent = (function () {
function OptionsComponent() {
this.listElementSelector = ".myList";
}
OptionsComponent.prototype.render = function () {
return "<ul class='myList'></ul>";
};
OptionsComponent.prototype.renderOption = function (option) {
return "<li style='font-weight:bold;'>" + option.text + "</li>";
};
return OptionsComponent;
}());
new InfiniteAutocomplete(document.getElementById('test'), {
customizedInput: CustomizedInput,
customizedOptions: OptionsComponent,
data: [
{ text: 'Islam Attrash', value: 1},
{ text: 'Shai Reznik', value: 2},
{ text: 'Uri Shaked', value: 3},
{ text: 'Salsabel Eawissat', value: 4}
]
});