autocompleter
Advanced tools
Comparing version 9.0.0-alpha.0 to 9.0.0-alpha.1
@@ -18,2 +18,13 @@ /** | ||
} | ||
/** | ||
* Enum for controlling form submission when `ENTER` key is pressed in the autocomplete input field. | ||
*/ | ||
export declare const enum PreventSubmit { | ||
Never = 0, | ||
Always = 1, | ||
/** | ||
* Form submission is prevented only when an item is selected from the autocomplete list. | ||
*/ | ||
OnSelect = 2 | ||
} | ||
export interface AutocompleteItem { | ||
@@ -95,5 +106,5 @@ label?: string; | ||
/** | ||
* Prevents automatic form submit when ENTER is pressed | ||
* Controls form submission when the ENTER key is pressed in a input field. | ||
*/ | ||
preventSubmit?: boolean; | ||
preventSubmit?: PreventSubmit; | ||
/** | ||
@@ -100,0 +111,0 @@ * Prevents the first item in the list from being selected automatically. This option allows you |
@@ -19,6 +19,6 @@ (function (global, factory) { | ||
var container = settings.container || doc.createElement('div'); | ||
var preventSubmit = settings.preventSubmit || 0 /* Never */; | ||
container.id = container.id || 'autocomplete-' + uid(); | ||
var containerStyle = container.style; | ||
var debounceWaitMs = settings.debounceWaitMs || 0; | ||
var preventSubmit = settings.preventSubmit || false; | ||
var disableAutoSelect = settings.disableAutoSelect || false; | ||
@@ -325,2 +325,5 @@ var customContainerParent = container.parentElement; | ||
if (selected) { | ||
if (preventSubmit === 2 /* OnSelect */) { | ||
ev.preventDefault(); | ||
} | ||
suppressAutocomplete = true; | ||
@@ -335,3 +338,3 @@ try { | ||
} | ||
if (preventSubmit) { | ||
if (preventSubmit === 1 /* Always */) { | ||
ev.preventDefault(); | ||
@@ -338,0 +341,0 @@ } |
@@ -9,3 +9,3 @@ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).autocomplete=t()}(this,(function(){"use strict"; | ||
* MIT License | ||
*/return function(e){var t=document,n=e.container||t.createElement("div");n.id=n.id||"autocomplete-"+h();var i,r,o=n.style,a=e.debounceWaitMs||0,s=e.preventSubmit||!1,u=e.disableAutoSelect||!1,c=n.parentElement,d=[],l="",f=2,p=e.showOnFocus,v=0,m=!1,b=!1;if(void 0!==e.minLength&&(f=e.minLength),!e.input)throw new Error("input undefined");var g=e.input;function h(){return Date.now().toString(36)+Math.random().toString(36).substring(2)}function E(){r&&window.clearTimeout(r)}function A(){return!!n.parentNode}function w(){var e;v++,d=[],l="",i=void 0,g.setAttribute("aria-activedescendant",""),g.setAttribute("aria-expanded","false"),(e=n.parentNode)&&e.removeChild(n)}function y(){n.innerHTML="",g.setAttribute("aria-activedescendant","");var r=function(e,n,i){var r=t.createElement("div");return r.textContent=e.label||"",r};e.render&&(r=e.render);var a=function(e,n){var i=t.createElement("div");return i.textContent=e,i};e.renderGroup&&(a=e.renderGroup);var s=t.createDocumentFragment(),u=h();if(d.forEach((function(t,o){if(t.group&&t.group!==u){u=t.group;var c=a(t.group,l);c&&(c.className+=" group",s.appendChild(c))}var d=r(t,l,o);d&&(d.id=n.id+"_"+o,d.setAttribute("role","option"),d.addEventListener("click",(function(n){b=!0;try{e.onSelect(t,g)}finally{b=!1}w(),n.preventDefault(),n.stopPropagation()})),t===i&&(d.className+=" selected",d.setAttribute("aria-selected","true"),g.setAttribute("aria-activedescendant",d.id)),s.appendChild(d))})),n.appendChild(s),d.length<1){if(!e.emptyMsg)return void w();var f=t.createElement("div");f.id=n.id+"_"+h(),f.className="empty",f.textContent=e.emptyMsg,n.appendChild(f),g.setAttribute("aria-activedescendant",f.id)}n.parentNode||(c||t.body).appendChild(n),function(){if(A()){g.setAttribute("aria-expanded","true"),o.height="auto",o.width=g.offsetWidth+"px";var i,r=0;a(),a(),e.customize&&i&&e.customize(g,i,n,r)}function a(){var e=t.documentElement,n=e.clientTop||t.body.clientTop||0,a=e.clientLeft||t.body.clientLeft||0,s=window.pageYOffset||e.scrollTop,u=window.pageXOffset||e.scrollLeft,c=(i=g.getBoundingClientRect()).top+g.offsetHeight+s-n,d=i.left+u-a;o.top=c+"px",o.left=d+"px",(r=window.innerHeight-(i.top+g.offsetHeight))<0&&(r=0),o.top=c+"px",o.bottom="",o.left=d+"px",o.maxHeight=r+"px"}}(),C()}function L(){A()&&y()}function x(){L()}function T(e){e.target!==n?L():e.preventDefault()}function k(){b||O(0)}function C(){var e=n.getElementsByClassName("selected");if(e.length>0){var t=e[0],i=t.previousElementSibling;if(i&&-1!==i.className.indexOf("group")&&!i.previousElementSibling&&(t=i),t.offsetTop<n.scrollTop)n.scrollTop=t.offsetTop;else{var r=t.offsetTop+t.offsetHeight,o=n.scrollTop+n.offsetHeight;r>o&&(n.scrollTop+=r-o)}}}function N(e){d.length>0&&(!function(e){var i=t.getElementById(n.id+"_"+e);i&&(i.classList.remove("selected"),i.removeAttribute("aria-selected"),g.removeAttribute("aria-activedescendant"))}(e),function(e){var i=t.getElementById(n.id+"_"+e);i&&(i.classList.add("selected"),i.setAttribute("aria-selected","true"),g.setAttribute("aria-activedescendant",i.id))}(d.indexOf(i)),C())}function S(e,t){var n,r=A();if("Escape"===t)w();else{if(!r||d.length<1)return;"ArrowUp"===t?(n=d.indexOf(i),i=-1===n?void 0:d[(n+d.length-1)%d.length],N(n)):function(){var e=d.indexOf(i);i=d.length<1?void 0:-1===e?d[0]:d[(e+1)%d.length],N(e)}()}e.preventDefault(),r&&e.stopPropagation()}function D(t){var n=t.key;switch(n){case"ArrowUp":case"ArrowDown":case"Escape":S(t,n);break;case"Enter":!function(t){if(i){b=!0;try{e.onSelect(i,g)}finally{b=!1}w()}s&&t.preventDefault()}(t)}}function H(){p&&O(1)}function O(e){g.value.length>=f||1===e?(E(),r=window.setTimeout((function(){return M(g.value,e,g.selectionStart||0)}),0===e||2===e?a:0)):w()}function M(t,n,r){if(!m){var o=++v;e.fetch(t,(function(e){v===o&&e&&(l=t,i=(d=e).length<1||u?void 0:d[0],y())}),n,r)}}function z(t){e.keyup?e.keyup({event:t,fetch:function(){return O(0)}}):A()||"ArrowDown"!==t.key||O(0)}function B(t){e.click&&e.click({event:t,fetch:function(){return O(2)}})}function _(){setTimeout((function(){t.activeElement!==g&&w()}),200)}return n.className="autocomplete "+(e.className||""),n.setAttribute("role","listbox"),g.setAttribute("role","combobox"),g.setAttribute("aria-expanded","false"),g.setAttribute("aria-autocomplete","list"),g.setAttribute("aria-controls",n.id),g.setAttribute("aria-owns",n.id),g.setAttribute("aria-activedescendant",""),g.setAttribute("aria-haspopup","listbox"),o.position="absolute",n.addEventListener("mousedown",(function(e){e.stopPropagation(),e.preventDefault()})),n.addEventListener("focus",(function(){return g.focus()})),g.addEventListener("keyup",z),g.addEventListener("click",B),g.addEventListener("keydown",D),g.addEventListener("input",k),g.addEventListener("blur",_),g.addEventListener("focus",H),window.addEventListener("resize",x),t.addEventListener("scroll",T,!0),{destroy:function(){g.removeEventListener("focus",H),g.removeEventListener("keyup",z),g.removeEventListener("click",B),g.removeEventListener("keydown",D),g.removeEventListener("input",k),g.removeEventListener("blur",_),window.removeEventListener("resize",x),t.removeEventListener("scroll",T,!0),g.removeAttribute("role"),g.removeAttribute("aria-expanded"),g.removeAttribute("aria-autocomplete"),g.removeAttribute("aria-controls"),g.removeAttribute("aria-activedescendant"),g.removeAttribute("aria-owns"),g.removeAttribute("aria-haspopup"),E(),w(),m=!0},fetch:function(){M(g.value,3,g.selectionStart||0)}}}})); | ||
*/return function(e){var t=document,n=e.container||t.createElement("div"),i=e.preventSubmit||0;n.id=n.id||"autocomplete-"+h();var r,o,a=n.style,s=e.debounceWaitMs||0,u=e.disableAutoSelect||!1,c=n.parentElement,d=[],l="",f=2,p=e.showOnFocus,v=0,m=!1,b=!1;if(void 0!==e.minLength&&(f=e.minLength),!e.input)throw new Error("input undefined");var g=e.input;function h(){return Date.now().toString(36)+Math.random().toString(36).substring(2)}function E(){o&&window.clearTimeout(o)}function A(){return!!n.parentNode}function w(){var e;v++,d=[],l="",r=void 0,g.setAttribute("aria-activedescendant",""),g.setAttribute("aria-expanded","false"),(e=n.parentNode)&&e.removeChild(n)}function y(){n.innerHTML="",g.setAttribute("aria-activedescendant","");var i=function(e,n,i){var r=t.createElement("div");return r.textContent=e.label||"",r};e.render&&(i=e.render);var o=function(e,n){var i=t.createElement("div");return i.textContent=e,i};e.renderGroup&&(o=e.renderGroup);var s=t.createDocumentFragment(),u=h();if(d.forEach((function(t,a){if(t.group&&t.group!==u){u=t.group;var c=o(t.group,l);c&&(c.className+=" group",s.appendChild(c))}var d=i(t,l,a);d&&(d.id=n.id+"_"+a,d.setAttribute("role","option"),d.addEventListener("click",(function(n){b=!0;try{e.onSelect(t,g)}finally{b=!1}w(),n.preventDefault(),n.stopPropagation()})),t===r&&(d.className+=" selected",d.setAttribute("aria-selected","true"),g.setAttribute("aria-activedescendant",d.id)),s.appendChild(d))})),n.appendChild(s),d.length<1){if(!e.emptyMsg)return void w();var f=t.createElement("div");f.id=n.id+"_"+h(),f.className="empty",f.textContent=e.emptyMsg,n.appendChild(f),g.setAttribute("aria-activedescendant",f.id)}n.parentNode||(c||t.body).appendChild(n),function(){if(A()){g.setAttribute("aria-expanded","true"),a.height="auto",a.width=g.offsetWidth+"px";var i,r=0;o(),o(),e.customize&&i&&e.customize(g,i,n,r)}function o(){var e=t.documentElement,n=e.clientTop||t.body.clientTop||0,o=e.clientLeft||t.body.clientLeft||0,s=window.pageYOffset||e.scrollTop,u=window.pageXOffset||e.scrollLeft,c=(i=g.getBoundingClientRect()).top+g.offsetHeight+s-n,d=i.left+u-o;a.top=c+"px",a.left=d+"px",(r=window.innerHeight-(i.top+g.offsetHeight))<0&&(r=0),a.top=c+"px",a.bottom="",a.left=d+"px",a.maxHeight=r+"px"}}(),C()}function L(){A()&&y()}function x(){L()}function T(e){e.target!==n?L():e.preventDefault()}function k(){b||O(0)}function C(){var e=n.getElementsByClassName("selected");if(e.length>0){var t=e[0],i=t.previousElementSibling;if(i&&-1!==i.className.indexOf("group")&&!i.previousElementSibling&&(t=i),t.offsetTop<n.scrollTop)n.scrollTop=t.offsetTop;else{var r=t.offsetTop+t.offsetHeight,o=n.scrollTop+n.offsetHeight;r>o&&(n.scrollTop+=r-o)}}}function D(e){d.length>0&&(!function(e){var i=t.getElementById(n.id+"_"+e);i&&(i.classList.remove("selected"),i.removeAttribute("aria-selected"),g.removeAttribute("aria-activedescendant"))}(e),function(e){var i=t.getElementById(n.id+"_"+e);i&&(i.classList.add("selected"),i.setAttribute("aria-selected","true"),g.setAttribute("aria-activedescendant",i.id))}(d.indexOf(r)),C())}function N(e,t){var n,i=A();if("Escape"===t)w();else{if(!i||d.length<1)return;"ArrowUp"===t?(n=d.indexOf(r),r=-1===n?void 0:d[(n+d.length-1)%d.length],D(n)):function(){var e=d.indexOf(r);r=d.length<1?void 0:-1===e?d[0]:d[(e+1)%d.length],D(e)}()}e.preventDefault(),i&&e.stopPropagation()}function S(t){var n=t.key;switch(n){case"ArrowUp":case"ArrowDown":case"Escape":N(t,n);break;case"Enter":!function(t){if(r){2===i&&t.preventDefault(),b=!0;try{e.onSelect(r,g)}finally{b=!1}w()}1===i&&t.preventDefault()}(t)}}function H(){p&&O(1)}function O(e){g.value.length>=f||1===e?(E(),o=window.setTimeout((function(){return M(g.value,e,g.selectionStart||0)}),0===e||2===e?s:0)):w()}function M(t,n,i){if(!m){var o=++v;e.fetch(t,(function(e){v===o&&e&&(l=t,r=(d=e).length<1||u?void 0:d[0],y())}),n,i)}}function z(t){e.keyup?e.keyup({event:t,fetch:function(){return O(0)}}):A()||"ArrowDown"!==t.key||O(0)}function B(t){e.click&&e.click({event:t,fetch:function(){return O(2)}})}function _(){setTimeout((function(){t.activeElement!==g&&w()}),200)}return n.className="autocomplete "+(e.className||""),n.setAttribute("role","listbox"),g.setAttribute("role","combobox"),g.setAttribute("aria-expanded","false"),g.setAttribute("aria-autocomplete","list"),g.setAttribute("aria-controls",n.id),g.setAttribute("aria-owns",n.id),g.setAttribute("aria-activedescendant",""),g.setAttribute("aria-haspopup","listbox"),a.position="absolute",n.addEventListener("mousedown",(function(e){e.stopPropagation(),e.preventDefault()})),n.addEventListener("focus",(function(){return g.focus()})),g.addEventListener("keyup",z),g.addEventListener("click",B),g.addEventListener("keydown",S),g.addEventListener("input",k),g.addEventListener("blur",_),g.addEventListener("focus",H),window.addEventListener("resize",x),t.addEventListener("scroll",T,!0),{destroy:function(){g.removeEventListener("focus",H),g.removeEventListener("keyup",z),g.removeEventListener("click",B),g.removeEventListener("keydown",S),g.removeEventListener("input",k),g.removeEventListener("blur",_),window.removeEventListener("resize",x),t.removeEventListener("scroll",T,!0),g.removeAttribute("role"),g.removeAttribute("aria-expanded"),g.removeAttribute("aria-autocomplete"),g.removeAttribute("aria-controls"),g.removeAttribute("aria-activedescendant"),g.removeAttribute("aria-owns"),g.removeAttribute("aria-haspopup"),E(),w(),m=!0},fetch:function(){M(g.value,3,g.selectionStart||0)}}}})); | ||
//# sourceMappingURL=autocomplete.min.js.map |
@@ -20,2 +20,14 @@ /** | ||
/** | ||
* Enum for controlling form submission when `ENTER` key is pressed in the autocomplete input field. | ||
*/ | ||
export const enum PreventSubmit { | ||
Never = 0, | ||
Always = 1, | ||
/** | ||
* Form submission is prevented only when an item is selected from the autocomplete list. | ||
*/ | ||
OnSelect = 2 | ||
} | ||
export interface AutocompleteItem { | ||
@@ -112,5 +124,5 @@ label?: string; | ||
/** | ||
* Prevents automatic form submit when ENTER is pressed | ||
* Controls form submission when the ENTER key is pressed in a input field. | ||
*/ | ||
preventSubmit?: boolean; | ||
preventSubmit?: PreventSubmit; | ||
@@ -154,6 +166,7 @@ /** | ||
const container: HTMLDivElement = settings.container || doc.createElement('div'); | ||
const preventSubmit: PreventSubmit = settings.preventSubmit || PreventSubmit.Never; | ||
container.id = container.id || 'autocomplete-' + uid(); | ||
const containerStyle = container.style; | ||
const debounceWaitMs = settings.debounceWaitMs || 0; | ||
const preventSubmit = settings.preventSubmit || false; | ||
const disableAutoSelect = settings.disableAutoSelect || false; | ||
@@ -512,2 +525,5 @@ const customContainerParent = container.parentElement; | ||
if (selected) { | ||
if (preventSubmit === PreventSubmit.OnSelect) { | ||
ev.preventDefault(); | ||
} | ||
suppressAutocomplete = true; | ||
@@ -522,3 +538,3 @@ try { | ||
if (preventSubmit) { | ||
if (preventSubmit === PreventSubmit.Always) { | ||
ev.preventDefault(); | ||
@@ -525,0 +541,0 @@ } |
{ | ||
"name": "autocompleter", | ||
"version": "9.0.0-alpha.0", | ||
"version": "9.0.0-alpha.1", | ||
"description": "Blazing fast and lightweight autocomplete library without dependencies. 1KB gzipped.", | ||
@@ -5,0 +5,0 @@ "main": "autocomplete.js", |
@@ -119,3 +119,3 @@ | ||
|`customize`|Callback for additional autocomplete customization after rendering is finished. Use this function if you want to change autocomplete default position.|`undefined`| | ||
|`preventSubmit`|Prevents automatic form submit when ENTER is pressed.|`false`| | ||
|`preventSubmit`|This option controls form submission when the ENTER key is pressed in a input field. Three settings are available: `Never`, `Always`, and `OnSelect`. Choose the appropriate setting to customize form submission behavior as per your needs.|`Never`| | ||
|`showOnFocus`|Displays suggestions on focus of the input element. Note that if `true`, the minLength property will be ignored and it will always call `fetch`.|`false`| | ||
@@ -122,0 +122,0 @@ |`disableAutoSelect`|Prevents the first item in the list from being selected automatically. This option allows you to submit a custom text by pressing `ENTER` even when autocomplete is displayed.|`false`| |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
136725
1220