1.0.2 • Published 9 months ago

test-ng-plugin-oct-v1 v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Angular Autocomplete

A simple, powerful, lightweight and customizable autocomplete tool programmed for Angular projects.

Recommended Angular version

Angular 14+

Whats better in this package?

  • Lightweight module
  • No 3rd party packages installed
  • Ability to use 3rd party style packages like bootstrap, tailwind or any css libraries. (You can update the classes from your component)
  • No angular material or cdk library
  • Supports large data set
  • Customizable scroll functionality
  • Supports custom events
  • Easy virtual scrolling for large data set without 3rd party
  • Custom classes and ng-styles are allowed
  • 22+ solid test cases to validate the module
  • Eventful module.

What's new in 2.0.0 ?

  • Search algorithm has been redefined without impacting prior versions.
  • Added View More to List dropdown at the end of the list as an alternative to call API when reaching the end of scroll.
  • Now the developers who consume this package can decide how they should trigger an API Call. Using View More, or triggering API Call when reaching end of scroll. Both can be configured as well. Refer below for more information.

How to make best use of this package ?

Read the full documentation without skipping, to know the flexibility of this module and refer the examples provided. Apart from the general use, this package comes handy when

  • Lazy loading is required. A custom function can be executed from the application which may be an API call with offset or paginated params or any other custom function that is suitable for the requirement.
  • Effortless styling of the input field and dropdown using custom css, or with libraries like bootstrap, tailwind and so on.
  • Handling large dataset with or without lazy load configuration
  • When triggerApiLoadEvent is set to true, its developers responsibility to set it as false once the custom function is executed and expected result is achieved.
  • If totalRecords is known during the initialization of the module, the module will act pro-actively based on this input and will not call any custom functions once the totalRecords match the actual dropdownData and when scroll is complete.

Installation

Install ng-autocomplete-plugin with npm

  npm install ng-autocomplete-plugin --save

  or

  npm i ng-autocomplete-plugin

Running Tests

ng-autocomplete-plugin has 20+ solid test cases to make sure components executes without any issues.

To run tests, run the following command

  npm run test
  or
  ng test

Package version

ng-autocomplete-plugin versionDescription
2.0.0Recommended. Major Upgrade in search algorithm with certain bugs fixed. Added View More Button as a new feature.
1.0.3Updated readme file with few changes.
1.0.2Only Read me file updated.
1.0.1First major version

Demo & Examples

Demo DescriptionStackblitz Example
Autocomplete ExampleAutocomplete - Stackblitz
API Call Event ExampleAutocomplete with API call event - Stackblitz
Autocomplete With BootstrapAutocomplete with bootstrap
Autocomplete With TailwindAutocomplete with tailwind
Turn off performance calculation during scrollTurn off performance calculation during scroll
Disabling a list in dropdownDisabling a list in dropdown

API Usage

Input decorators

InputTypeRequiredDescription
dropdownDatastring[] or object[]Yes.Accepts array of strings or numbers or array of objects
objectPropertystringYesRequired only if dropdownData is object[]. Helps to display dropdown value in dropdown list.
placeholderstringNoCustom placeholder for autocomplete input field.
defaultValuestring or objectNoTo pre-select a value from dropdown.
initialVisibleDatanumberNoDisplays 1000 records in dropdown by default. Can be changed as per project requirement.
scrollThresholdnumberNo3 by default. Helps to boost performance. It controls the scroll data and removes top or botton records during user scroll based on the scrollThreshold & initialVisibleData configured. Check below for more details.
totalRecordsnumberNoIf total number of records is known, totalRecords can be provided which will avoid extra events getting executed.
disablePropertystringNoTo disable specific dropdown list item in dropdown. User cannot select the dropdown if disabled. This property can be used for object[] dropdown and disableProperty should be one of the boolean property in object
disableListFnFunctionNoIf disabling a list item should be calculated dynamically using a function and custom code, assign customized function to disableListFn. disableListFn accepts two parameters (index, data)
searchFnFunctionNoCustomized search function. Customized search function accepts one parameter, event. On keyUp, the customized search function will be called to perform custom execution.
isNumberbooleanNoIf the displayed list is number, then sending isNumber as true will help to search the list efficiently
noSearchResultMessagestringNoBy default No results found message will be displayed when search result is 0. It can be changed with this input property.
customTrackByFunctionNoCustom ngFor trackBy Function
isAutoCompleteDisabledbooleanNoDefault is false. When set to true, the input field gets disabled.
isCustomSpinnerbooleanNoDefault is false. When set to true, custom spinner can be implemented with custom class.
customClassobjectNoAllows custom class styling at various dom levels. Check below for more information
customStyleobjectNoAllows custom ng-style. Check below for more information
showdropDownArrowbooleanNoDefault is true. Show or hide dropdown icon in autocomplete field.
showClearOptionbooleanNoDefault is true. Shows clear option to allow the user to reset or clear the selected value.
showLoadingSpinnerbooleanNoDefault is true. Shows the spinner at the botton of the list during lazy loading API call. If set to false, spinner will not be shown.
triggerBlurEventbooleanNoDefault is false. When set to true, it emits an output event emitBlurEvent during focusOut
triggerApiLoadEventbooleanNoDefault is false. If dropdown list is loaded through API via lazy loading, this can be set as true, it emits an event emitApiLoadEvent. When the output event is emitted, user can take care of loading the dropdown data further.
triggerAutoCompleteOpenEventbooleanNoDefault is false. When set to true, it emits an output event emitAutoCompleteOpenEvent when auto-complete dropdown list opens.
triggerSearchEventbooleanNoDefault is false. When set to true, emits an output event emitSearchEvent whenever user types and search.
triggerClearSelectionEventbooleanNoDefault is false. When set to true, emits an output event emitClearSelectedEvent whenever selected field is cleared.
isScrollThresholdRequiredbooleanNoDefault is true. If initialVisibleData and scrollThreshold is performance calculation is not required, set it to false. See below for more information.
inspectAutoCompleteListbooleanNoDefault is false. When set to true, autocomplete dropdown will not be closed or hidden during onBlur or onFocusOut events. This is intended only for debugging and development purposes. For production it should be always false to avoid interruption.
showViewMorebooleanNoDefault is true. View More List will be shown at the end of dropdown if user has enabled lazy loading (triggerApiLoadEvent). View More will appear only when API call is to be executed.
optViewMoreOnlyForApiCallbooleanNoDefault is false. When set to true, API Call will not be executed on reaching the end of the scroll, instead View More button has to be clicked to call the API or any custom function.

Output decorators

OutputRequiredDescription
emitSelectedValueYesEmits the selected dropdown value.
emitApiLoadEventNoTo load next set of dropdownData using API or any other external means. Emits event when end of scroll is reached.triggerApiLoadEvent needs to be set to true to emit this event.
emitAutoCompleteOpenEventNoEmits event when dropdown is opened or displayed. triggerAutoCompleteOpenEvent needs to be set to true to emit this event.
emitClearSelectedEventNoEmits event when selected value is cleared. triggerClearSelectionEvent needs to be set to true to emit this event.
emitBlurEventNoEmits event during focus out. triggerBlurEvent needs to be set to true to emit this event.

Using the module

import NgAutocompletePluginModule into your app module.

import { NgAutocompletePluginModule } from 'ng-autocomplete-plugin';

In your HTML template

For dropdown string array,Eg: "Apple", "Banana", "Kiwi", do as below.

<ng-autocomplete
    [dropdownData]="YOUR_DROPDOWN_DATA"
    (emitSelectedValue)="YOUR_CUSTOM_FUNTION($event)">
</ng-autocomplete>

For dropdown object array, Eg: {"name": "Alex"}, {"name": "John"}, do as below.

<ng-autocomplete
    [dropdownData]="YOUR_DROPDOWN_DATA"
    (emitSelectedValue)="YOUR_CUSTOM_FUNTION($event)"
    [objectProperty]="'name'">
</ng-autocomplete>

To control the width of autocomplete, wrap a div and specify the width. Custom CSS classes can also be used(Scroll below).

<div style="width: 200px">
    <ng-autocomplete
        [dropdownData]="YOUR_DROPDOWN_DATA"
        (emitSelectedValue)="YOUR_CUSTOM_FUNTION($event)"
        [objectProperty]="'name'">
    </ng-autocomplete>
</div>

Setting scrollThreshold & initialVisibleData for maximizng performance

By default, initialVisibleData is set to 1000 and scrollThreshold is set to 3. It can be customized as per requirement.

What is scrollThreshold & initialVisibleData?

Both scrollThreshold & initialVisibleData helps to improve virtual scrolling for better performance.

initialVisibleData is the number of records / data that will be loaded virtually during initial load.

Consider, there are 5000 records to be shown. initialVisibleData is 1000 and scrollThreshold is 3.

During initial load, dropdown list will show 1000 records. When user reaches the end of scroll, the formula will be calculated based on initialVisibleData & scrollThreshold and number of filteredData records.

Math.ceil(filteredData.length /scrollThreshold);

Math.ceil(1000/3);

If calculated result does not exceed initialVisibleData, next set of data will be loaded. And now the dropdown list(filteredData) will hold 2000 records.

Similary same calculation will be performed during the end of scroll and when the result exceeds initialVisibleData, first set of records will be removed from the dropdown list and next set of records will be loaded everytime, to improve performance.

When scrollThreshold is set to 1, the virtual dropdown list will hold the records based on initialVisibleData configuration. When end of scroll is reached, the current set will be replaced by next set.

initialVisibleData & scrollThreshold does not affect or mutate original data and will update only the virtually filtered data.

initialVisibleData & scrollThreshold is customizable based on project needs. If this performance calculation is not required, set isScrollThresholdRequired to false.

CSS Classes used in npm-autocomplete-plugin html template

This is for informational purpose only. Be cautious if you are overriding these CSS as it may affect dropdown style if updated incorrectly.

It is always recommended to add class through customClass or customStyle Input() properties.
ClassesDescriptionComments
auto-complete-textfield-containerClass of parent html div templateAble to override through parentContainerClass or parentContainerStyle
label-containerClass of label tagAble to override through inputLabelContainerClass or inputLabelContainerStyle
label-valueClass of <label> tagAble to override through inputLabelClass or inputLabelStyle
field-containerClass of div that surrounds input field and autocomplete liAble to override through inputFieldContainerClass or inputFieldContainerStyle
auto-complete-textfieldClass of input text fieldAble to override through inputFieldClass or inputFieldStyle
auto-complete-listClass of div that surrounds liAble to override through listContainerClass or listContainerStyle
unorder-listClass of ulAble to override through dropdownUnorderedListClass or dropdownUnorderedListStyle
autocomplete-data-listClass of liAble to override through disableListClass or dropdownListStyle
autocomplete-data-list noSearchResultClass of li if search result not found.Able to override through noResultClass or noResultStyle

Adding Custom CSS Class to autocomplete

Import CustomClassType from the module (Optional).

CustomClassTypesRequiredDescription
parentContainerClassNoAdds class to div surrounding the input field and dropdown list.
inputFieldContainerClassNoAdds class to the parent div of input field
inputFieldClassNoAdds class to input textfield
listContainerClassNoAdds class to div that surrounds ul of dropdown list.
dropdownUnorderedListClassNoAdds class to ul of dropdown list.
dropdownListClassNoAdds class to each li items
noResultClassNoAdds class to separate li item to show no result message.
disableListClassNoAdds class to each li items. Depends on disableListFn function or disableProperty
inputLabelClassNoAdds class to <label> field
inputLabelContainerClassNoAdds class to parent div of <label> field
viewMoreClassNoAdds class to View More li item

import CustomClassType Type into your app, to see the custom class types available. Its optional, but would be good to use. Write your custom class in global css file or use ::ng-deep from specific components.

import { CustomClassType } from 'ng-autocomplete-plugin';

In your template, do the following:

customClassType: CustomClassType = {
    inputFieldClass: 'class1',
    dropdownListClass: 'class1 class2'
}
<ng-autocomplete
    [dropdownData]="YOUR_DROPDOWN_DATA"
    (emitSelectedValue)="YOUR_CUSTOM_FUNTION($event)"
    [objectProperty]="'name'"
    [customClassType]="customClassType">
</ng-autocomplete>

Adding Custom NgStyle to autocomplete

Import CustomNgStyleType from the module (Optional).

CustomNgStyleTypesRequiredDescription
parentContainerStyleNoAdds style to div surrounding the input field and dropdown list.
inputFieldContainerStyleNoAdds style to the parent div of input field
inputFieldStyleNoAdds style to input textfield
listContainerStyleNoAdds style to div that surrounds ul of dropdown list.
dropdownUnorderedListStyleNoAdds style to ul of dropdown list.
dropdownListStyleNoAdds style to each li items
noResultStyleNoAdds style to separate li item to show no result message.
inputLabelStyleNoAdds style to <label> field
inputLabelContainerStyleNoAdds style to parent div of <label> field
viewMoreStyleNoAdds style to View More li item.

import CustomNgStyleType Type into your app, to see the custom class types available. Its optional, but would be good to use.

import { CustomNgStyleType } from 'ng-autocomplete-plugin';

In your template, do the following:

customClassType: CustomNgStyleType = {
    inputFieldStyle: {color: '#FFFFFF' },
    dropdownListStyle: {padding: 2px }
}
<ng-autocomplete
    [dropdownData]="YOUR_DROPDOWN_DATA"
    (emitSelectedValue)="YOUR_CUSTOM_FUNTION($event)"
    [objectProperty]="'name'"
    [customClassType]="customClassType">
</ng-autocomplete>

Adding custom Spinner

To add a custom spinner, do the following. Recommended to use custom spinner to have better control of project requirement.

Update @Input() properties like below

showLoadingSpinner = true; // Turn this ON so that module will show the spinner. By default it is ON (true)
isCustomSpinner = true;
<ng-autocomplete
    [dropdownData]="YOUR_DROPDOWN_DATA"
    (emitSelectedValue)="YOUR_CUSTOM_FUNTION($event)"
    [showLoadingSpinner]="true"
    [isCustomSpinner]="true">
    <span customSpinner class="YOUR_CLASS"> </span>
</ng-autocomplete>
customSpinner in the span represents ng-content select attribute

Using custom content in the input textfield

A flexible option is provided to add custom functionality in the input text field.

<ng-autocomplete
    [dropdownData]="YOUR_DROPDOWN_DATA"
    (emitSelectedValue)="YOUR_CUSTOM_FUNTION($event)"
    [showdropDownArrow]="false"
    [showClearOption]="false">
    <span textField class="YOUR_CLASS">YOUR_CONTENT</span>
</ng-autocomplete>

showdropDownArrow and showClearOption can be set as false for better placement of the customized ng-content.

textField in the span represents ng-content select attribute

Adding Accessible Rich Internet Applications (ARIA)

ARIA provides easy access to the content for people with disabilities with help of screen reader.

AttributesDecoratorRequiredDescription
ariaRole@InputNoAdds ARIA role to input textfield
ariaInputField@InputNoAdds ARIA label to input textfield
ariaNoSearchResult@InputNoAdds ARIA label to no result found li item
ariaULList@InputNoAdds ARIA label to ul list item
ariaListContainer@InputNoAdds ARIA label to list container div.
ariaInputLabel@InputNoAdds ARIA label to label field.

Change Logs and version history

Refer the change history by viewing this link - CHANGELOG

Github link

Github Link - ng-autocomplete-plugin

Support

Please raise an issue in github repository

Github Link - Raise an issue

Roadmap

  • Multiselect dropdown before Q1 2025
  • Extensive search - Ability to search entire object in the list