@nrk/core-input
Advanced tools
Comparing version 1.4.2 to 1.4.3
@@ -111,3 +111,3 @@ 'use strict'; | ||
input.setAttribute(UUID, ajax || ''); | ||
input.setAttribute((UUID + "-limit"), limit); | ||
input.setAttribute((UUID + "-limit"), limit || 0); | ||
input.setAttribute(IS_IOS ? 'data-role' : 'role', 'combobox'); // iOS does not inform user area is editable if combobox | ||
@@ -196,3 +196,2 @@ input.setAttribute('aria-autocomplete', 'list'); | ||
function onFilter (input, detail) { | ||
var limit = Number(input.getAttribute((UUID + "-limit"))) || Infinity; | ||
if (dispatchEvent(input, 'input.filter', detail) && ajax(input) === false) { | ||
@@ -210,3 +209,3 @@ queryAll('[tabindex="-1"]', input.nextElementSibling).reduce(function (acc, item, index) { | ||
return setupItem.apply(void 0, [ limit ].concat( args )); | ||
return setupItem.apply(void 0, [ input.getAttribute((UUID + "-limit")) ].concat( args )); | ||
}); | ||
@@ -229,2 +228,3 @@ } | ||
item.setAttribute('type', 'button'); | ||
limit = Number(limit) || Infinity; | ||
if (limit && (index >= limit)) { item.parentElement.setAttribute('hidden', ''); } | ||
@@ -231,0 +231,0 @@ } |
@@ -19,3 +19,3 @@ import { name, version } from './package.json' | ||
input.setAttribute(UUID, ajax || '') | ||
input.setAttribute(`${UUID}-limit`, limit) | ||
input.setAttribute(`${UUID}-limit`, limit || 0) | ||
input.setAttribute(IS_IOS ? 'data-role' : 'role', 'combobox') // iOS does not inform user area is editable if combobox | ||
@@ -97,3 +97,2 @@ input.setAttribute('aria-autocomplete', 'list') | ||
function onFilter (input, detail) { | ||
const limit = Number(input.getAttribute(`${UUID}-limit`)) || Infinity | ||
if (dispatchEvent(input, 'input.filter', detail) && ajax(input) === false) { | ||
@@ -107,3 +106,3 @@ queryAll('[tabindex="-1"]', input.nextElementSibling).reduce((acc, item, index) => { | ||
return show ? acc.concat(item) : acc | ||
}, []).forEach((...args) => setupItem(limit, ...args)) | ||
}, []).forEach((...args) => setupItem(input.getAttribute(`${UUID}-limit`), ...args)) | ||
} | ||
@@ -123,2 +122,3 @@ } | ||
item.setAttribute('type', 'button') | ||
limit = Number(limit) || Infinity | ||
if (limit && (index >= limit)) item.parentElement.setAttribute('hidden', '') | ||
@@ -125,0 +125,0 @@ } |
@@ -1,3 +0,3 @@ | ||
/*! @nrk/core-input v1.4.1 - Copyright (c) 2017-2019 NRK */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["react","prop-types"],t):(e=e||self).CoreInput=t(e.React,e.PropTypes)}(this,function(o,e){"use strict";o=o&&o.hasOwnProperty("default")?o.default:o,e=e&&e.hasOwnProperty("default")?e.default:e;var t="undefined"!=typeof window,u=(t&&/(android)/i.test(navigator.userAgent),t&&/iPad|iPhone|iPod/.test(String(navigator.platform))),i=function(e){void 0===e&&(e=!1);try{window.addEventListener("test",null,{get passive(){e=!0}})}catch(e){}return e}();function n(e,t,n,r){(void 0===r&&(r=!1),"undefined"==typeof window||window[e=e+"-"+t])||(i||"object"!=typeof r||(r=Boolean(r.capture)),("resize"===t||"load"===t?window:document).addEventListener(window[e]=t,n,r))}var r={"&":"&","<":"<",">":">",'"':""","/":"/","'":"'"};function a(e){return String(e||"").replace(/[&<>"'/]/g,function(e){return r[e]})}var l="prevent_recursive_dispatch_maximum_callstack";function c(e,t,n){void 0===n&&(n={});var r,i=""+l+t;if(e[i])return!0;e[i]=!0,"function"==typeof window.CustomEvent?r=new window.CustomEvent(t,{bubbles:!0,cancelable:!0,detail:n}):(r=document.createEvent("CustomEvent")).initCustomEvent(t,!0,!0,n);var o=e.dispatchEvent(r);return e[i]=null,o}function p(e){(window.requestAnimationFrame||window.setTimeout)(e)}function s(e,t){if(void 0===t&&(t=document),e){if(e.nodeType)return[e];if("string"==typeof e)return[].slice.call(t.querySelectorAll(e));if(e.length)return[].slice.call(e)}return[]}var d="data-@nrk/core-input-1.4.2".replace(/\W+/g,"-"),f=13,m=27,v=33,h=34,b=35,g=36,x=38,y=40,A=500;function E(e,t){var o="object"==typeof t?t:{content:t},a="string"==typeof o.content;return s(e).map(function(e){var t=e.nextElementSibling,n=void 0===o.ajax?e.getAttribute(d):o.ajax,r=void 0===o.limit?e.getAttribute(d+"-limit"):Math.max(o.limit||0,0),i=void 0===o.open?e===document.activeElement:o.open;return e.setAttribute(d,n||""),e.setAttribute(d+"-limit",r),e.setAttribute(u?"data-role":"role","combobox"),e.setAttribute("aria-autocomplete","list"),e.setAttribute("autocomplete","off"),a&&(t.innerHTML=o.content),s("a,button",t).forEach(function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return C.apply(void 0,[r].concat(e))}),j(e,i),e})}function w(a){a.ctrlKey||a.altKey||a.metaKey||a.defaultPrevented||s("["+d+"]").forEach(function(e){var t,n,r=e.nextElementSibling,i=e===a.target||r.contains(a.target),o="click"===a.type&&i&&s('[tabindex="-1"]',r).filter(function(e){return e.contains(a.target)})[0];o?(t=e,n={relatedTarget:r,currentTarget:o,value:o.value||o.textContent.trim()},c(t,"input.select",n)&&(t.value=n.value,t.focus(),p(function(){return j(t,!1)}))):j(e,i)})}function S(e,t){var n=e.nextElementSibling,r=[e].concat(s('[tabindex="-1"]:not([hidden])',n)),i=t.keyCode===m&&"true"===e.getAttribute("aria-expanded"),o=r.indexOf(document.activeElement),a=!1;t.keyCode===y?a=r[o+1]||r[0]:t.keyCode===x?a=r[o-1]||r.pop():n.contains(t.target)&&(t.keyCode===b||t.keyCode===h?a=r.pop():t.keyCode===g||t.keyCode===v?a=r[1]:t.keyCode!==f&&e.focus()),j(e,t.keyCode!==m),(!1!==a||i)&&t.preventDefault(),a&&a.focus()}function j(e,t){void 0===t&&(t="true"===e.getAttribute("aria-expanded")),p(function(){e.nextElementSibling[t?"removeAttribute":"setAttribute"]("hidden",""),e.setAttribute("aria-expanded",t)})}function C(e,t,n,r){t.setAttribute("aria-label",t.textContent.trim()+", "+(n+1)+" av "+r.length),t.setAttribute("tabindex","-1"),t.setAttribute("type","button"),e&&e<=n&&t.parentElement.setAttribute("hidden","")}function L(e){var t=e.getAttribute(d),n=L.xhr=L.xhr||new window.XMLHttpRequest;if(!t)return!1;clearTimeout(L.timer),n.abort(),n.onload=function(){try{n.responseJSON=JSON.parse(n.responseText)}catch(e){n.responseJSON=!1}c(e,"input.ajax",n)},L.timer=setTimeout(function(){e.value&&c(e,"input.ajax.beforeSend",n)&&(n.open("GET",t.replace("{{value}}",window.encodeURIComponent(e.value)),!0),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.send())},A)}E.escapeHTML=a,E.highlight=function(e,t){var n=t.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&");return a(e).replace(new RegExp(n||".^","gi"),"<mark>$&</mark>")},n(d,"click",w),n(d,"focus",w,!0),n(d,"input",function(e){var a,t,n,r=e.target;r.hasAttribute(d)&&(t={relatedTarget:(a=r).nextElementSibling},n=Number(a.getAttribute(d+"-limit"))||1/0,c(a,"input.filter",t)&&!1===L(a)&&s('[tabindex="-1"]',a.nextElementSibling).reduce(function(e,t,n){var r="LI"===t.parentElement.nodeName&&t.parentElement,i=-1!==t.textContent.toLowerCase().indexOf(a.value.toLowerCase()),o=i?"removeAttribute":"setAttribute";return r&&r[o]("hidden",""),t[o]("hidden",""),i?e.concat(t):e},[]).forEach(function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return C.apply(void 0,[n].concat(e))}))}),n(d,"keydown",function(e){if(!(e.ctrlKey||e.altKey||e.metaKey)){if(e.target.hasAttribute&&e.target.hasAttribute(d))return S(e.target,e);for(var t=e.target,n=void 0;t;t=t.parentElement)if((n=t.previousElementSibling)&&n.hasAttribute(d))return S(n,e)}},!0);var k=function(n){function t(e){var t=this;n.call(this,e),this.onFilter=function(e){return t.props.onFilter&&t.props.onFilter(e)},this.onSelect=function(e){return t.props.onSelect&&t.props.onSelect(e)},this.onAjaxBeforeSend=function(e){return t.props.onAjaxBeforeSend&&t.props.onAjaxBeforeSend(e)},this.onAjax=function(e){return t.props.onAjax&&t.props.onAjax(e)}}n&&(t.__proto__=n),(t.prototype=Object.create(n&&n.prototype)).constructor=t;var e={defaultProps:{configurable:!0}};return e.defaultProps.get=function(){return{open:null,limit:null,ajax:null,onAjax:null,onAjaxBeforeSend:null,onFilter:null,onSelect:null}},t.prototype.componentDidMount=function(){this.el.addEventListener("input.filter",this.onFilter),this.el.addEventListener("input.select",this.onSelect),this.el.addEventListener("input.ajax.beforeSend",this.onAjaxBeforeSend),this.el.addEventListener("input.ajax",this.onAjax),E(this.el.firstElementChild,this.props)},t.prototype.componentDidUpdate=function(){E(this.el.firstElementChild)},t.prototype.componentWillUnmount=function(){this.el.removeEventListener("input.filter",this.onFilter),this.el.removeEventListener("input.select",this.onSelect),this.el.removeEventListener("input.beforeSend",this.onAjaxBeforeSend),this.el.removeEventListener("input.ajax",this.onAjax)},t.prototype.render=function(){var n,r,e,i=this;return o.createElement("div",(n=this.props,r=t.defaultProps,void 0===(e={ref:function(e){return i.el=e}})&&(e={}),Object.keys(n).reduce(function(e,t){return r.hasOwnProperty(t)||(e[t]=n[t]),e},e)),o.Children.map(this.props.children,function(e,t){return 0===t?o.cloneElement(e,{"aria-expanded":String(Boolean(i.props.open))}):1===t?o.cloneElement(e,{hidden:!i.props.open}):e}))},Object.defineProperties(t,e),t}(o.Component);return k.Highlight=function(e){var t=e.text,n=e.query;return void 0===n&&(n=""),o.createElement("span",{dangerouslySetInnerHTML:{__html:E.highlight(t,n)}})},k.propTypes={onFilter:e.func,onSelect:e.func,onAjax:e.func,open:e.bool,limit:e.number,ajax:e.oneOfType([e.string,e.object])},k}); | ||
/*! @nrk/core-input v1.4.2 - Copyright (c) 2017-2019 NRK */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["react","prop-types"],t):(e=e||self).CoreInput=t(e.React,e.PropTypes)}(this,function(o,e){"use strict";o=o&&o.hasOwnProperty("default")?o.default:o,e=e&&e.hasOwnProperty("default")?e.default:e;var t="undefined"!=typeof window,u=(t&&/(android)/i.test(navigator.userAgent),t&&/iPad|iPhone|iPod/.test(String(navigator.platform))),i=function(e){void 0===e&&(e=!1);try{window.addEventListener("test",null,{get passive(){e=!0}})}catch(e){}return e}();function n(e,t,n,r){(void 0===r&&(r=!1),"undefined"==typeof window||window[e=e+"-"+t])||(i||"object"!=typeof r||(r=Boolean(r.capture)),("resize"===t||"load"===t?window:document).addEventListener(window[e]=t,n,r))}var r={"&":"&","<":"<",">":">",'"':""","/":"/","'":"'"};function a(e){return String(e||"").replace(/[&<>"'/]/g,function(e){return r[e]})}var l="prevent_recursive_dispatch_maximum_callstack";function c(e,t,n){void 0===n&&(n={});var r,i=""+l+t;if(e[i])return!0;e[i]=!0,"function"==typeof window.CustomEvent?r=new window.CustomEvent(t,{bubbles:!0,cancelable:!0,detail:n}):(r=document.createEvent("CustomEvent")).initCustomEvent(t,!0,!0,n);var o=e.dispatchEvent(r);return e[i]=null,o}function p(e){(window.requestAnimationFrame||window.setTimeout)(e)}function s(e,t){if(void 0===t&&(t=document),e){if(e.nodeType)return[e];if("string"==typeof e)return[].slice.call(t.querySelectorAll(e));if(e.length)return[].slice.call(e)}return[]}var d="data-@nrk/core-input-1.4.2".replace(/\W+/g,"-"),f=13,m=27,v=33,h=34,b=35,g=36,x=38,y=40,A=500;function E(e,t){var o="object"==typeof t?t:{content:t},a="string"==typeof o.content;return s(e).map(function(e){var t=e.nextElementSibling,n=void 0===o.ajax?e.getAttribute(d):o.ajax,r=void 0===o.limit?e.getAttribute(d+"-limit"):Math.max(o.limit||0,0),i=void 0===o.open?e===document.activeElement:o.open;return e.setAttribute(d,n||""),e.setAttribute(d+"-limit",r||0),e.setAttribute(u?"data-role":"role","combobox"),e.setAttribute("aria-autocomplete","list"),e.setAttribute("autocomplete","off"),a&&(t.innerHTML=o.content),s("a,button",t).forEach(function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return C.apply(void 0,[r].concat(e))}),j(e,i),e})}function w(a){a.ctrlKey||a.altKey||a.metaKey||a.defaultPrevented||s("["+d+"]").forEach(function(e){var t,n,r=e.nextElementSibling,i=e===a.target||r.contains(a.target),o="click"===a.type&&i&&s('[tabindex="-1"]',r).filter(function(e){return e.contains(a.target)})[0];o?(t=e,n={relatedTarget:r,currentTarget:o,value:o.value||o.textContent.trim()},c(t,"input.select",n)&&(t.value=n.value,t.focus(),p(function(){return j(t,!1)}))):j(e,i)})}function S(e,t){var n=e.nextElementSibling,r=[e].concat(s('[tabindex="-1"]:not([hidden])',n)),i=t.keyCode===m&&"true"===e.getAttribute("aria-expanded"),o=r.indexOf(document.activeElement),a=!1;t.keyCode===y?a=r[o+1]||r[0]:t.keyCode===x?a=r[o-1]||r.pop():n.contains(t.target)&&(t.keyCode===b||t.keyCode===h?a=r.pop():t.keyCode===g||t.keyCode===v?a=r[1]:t.keyCode!==f&&e.focus()),j(e,t.keyCode!==m),(!1!==a||i)&&t.preventDefault(),a&&a.focus()}function j(e,t){void 0===t&&(t="true"===e.getAttribute("aria-expanded")),p(function(){e.nextElementSibling[t?"removeAttribute":"setAttribute"]("hidden",""),e.setAttribute("aria-expanded",t)})}function C(e,t,n,r){t.setAttribute("aria-label",t.textContent.trim()+", "+(n+1)+" av "+r.length),t.setAttribute("tabindex","-1"),t.setAttribute("type","button"),(e=Number(e)||1/0)&&e<=n&&t.parentElement.setAttribute("hidden","")}function L(e){var t=e.getAttribute(d),n=L.xhr=L.xhr||new window.XMLHttpRequest;if(!t)return!1;clearTimeout(L.timer),n.abort(),n.onload=function(){try{n.responseJSON=JSON.parse(n.responseText)}catch(e){n.responseJSON=!1}c(e,"input.ajax",n)},L.timer=setTimeout(function(){e.value&&c(e,"input.ajax.beforeSend",n)&&(n.open("GET",t.replace("{{value}}",window.encodeURIComponent(e.value)),!0),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.send())},A)}E.escapeHTML=a,E.highlight=function(e,t){var n=t.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&");return a(e).replace(new RegExp(n||".^","gi"),"<mark>$&</mark>")},n(d,"click",w),n(d,"focus",w,!0),n(d,"input",function(e){var a,t,n=e.target;n.hasAttribute(d)&&(t={relatedTarget:(a=n).nextElementSibling},c(a,"input.filter",t)&&!1===L(a)&&s('[tabindex="-1"]',a.nextElementSibling).reduce(function(e,t,n){var r="LI"===t.parentElement.nodeName&&t.parentElement,i=-1!==t.textContent.toLowerCase().indexOf(a.value.toLowerCase()),o=i?"removeAttribute":"setAttribute";return r&&r[o]("hidden",""),t[o]("hidden",""),i?e.concat(t):e},[]).forEach(function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return C.apply(void 0,[a.getAttribute(d+"-limit")].concat(e))}))}),n(d,"keydown",function(e){if(!(e.ctrlKey||e.altKey||e.metaKey)){if(e.target.hasAttribute&&e.target.hasAttribute(d))return S(e.target,e);for(var t=e.target,n=void 0;t;t=t.parentElement)if((n=t.previousElementSibling)&&n.hasAttribute(d))return S(n,e)}},!0);var k=function(n){function t(e){var t=this;n.call(this,e),this.onFilter=function(e){return t.props.onFilter&&t.props.onFilter(e)},this.onSelect=function(e){return t.props.onSelect&&t.props.onSelect(e)},this.onAjaxBeforeSend=function(e){return t.props.onAjaxBeforeSend&&t.props.onAjaxBeforeSend(e)},this.onAjax=function(e){return t.props.onAjax&&t.props.onAjax(e)}}n&&(t.__proto__=n),(t.prototype=Object.create(n&&n.prototype)).constructor=t;var e={defaultProps:{configurable:!0}};return e.defaultProps.get=function(){return{open:null,limit:null,ajax:null,onAjax:null,onAjaxBeforeSend:null,onFilter:null,onSelect:null}},t.prototype.componentDidMount=function(){this.el.addEventListener("input.filter",this.onFilter),this.el.addEventListener("input.select",this.onSelect),this.el.addEventListener("input.ajax.beforeSend",this.onAjaxBeforeSend),this.el.addEventListener("input.ajax",this.onAjax),E(this.el.firstElementChild,this.props)},t.prototype.componentDidUpdate=function(){E(this.el.firstElementChild)},t.prototype.componentWillUnmount=function(){this.el.removeEventListener("input.filter",this.onFilter),this.el.removeEventListener("input.select",this.onSelect),this.el.removeEventListener("input.beforeSend",this.onAjaxBeforeSend),this.el.removeEventListener("input.ajax",this.onAjax)},t.prototype.render=function(){var n,r,e,i=this;return o.createElement("div",(n=this.props,r=t.defaultProps,void 0===(e={ref:function(e){return i.el=e}})&&(e={}),Object.keys(n).reduce(function(e,t){return r.hasOwnProperty(t)||(e[t]=n[t]),e},e)),o.Children.map(this.props.children,function(e,t){return 0===t?o.cloneElement(e,{"aria-expanded":String(Boolean(i.props.open))}):1===t?o.cloneElement(e,{hidden:!i.props.open}):e}))},Object.defineProperties(t,e),t}(o.Component);return k.Highlight=function(e){var t=e.text,n=e.query;return void 0===n&&(n=""),o.createElement("span",{dangerouslySetInnerHTML:{__html:E.highlight(t,n)}})},k.propTypes={onFilter:e.func,onSelect:e.func,onAjax:e.func,open:e.bool,limit:e.number,ajax:e.oneOfType([e.string,e.object])},k}); | ||
//# sourceMappingURL=core-input.jsx.js.map |
@@ -1,3 +0,3 @@ | ||
/*! @nrk/core-input v1.4.1 - Copyright (c) 2017-2019 NRK */ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).coreInput=e()}(this,function(){"use strict";var t="undefined"!=typeof window,u=(t&&/(android)/i.test(navigator.userAgent),t&&/iPad|iPhone|iPod/.test(String(navigator.platform))),r=function(t){void 0===t&&(t=!1);try{window.addEventListener("test",null,{get passive(){t=!0}})}catch(t){}return t}();function e(t,e,n,i){(void 0===i&&(i=!1),"undefined"==typeof window||window[t=t+"-"+e])||(r||"object"!=typeof i||(i=Boolean(i.capture)),("resize"===e||"load"===e?window:document).addEventListener(window[t]=e,n,i))}var n={"&":"&","<":"<",">":">",'"':""","/":"/","'":"'"};function i(t){return String(t||"").replace(/[&<>"'/]/g,function(t){return n[t]})}var a="prevent_recursive_dispatch_maximum_callstack";function c(t,e,n){void 0===n&&(n={});var i,r=""+a+e;if(t[r])return!0;t[r]=!0,"function"==typeof window.CustomEvent?i=new window.CustomEvent(e,{bubbles:!0,cancelable:!0,detail:n}):(i=document.createEvent("CustomEvent")).initCustomEvent(e,!0,!0,n);var o=t.dispatchEvent(i);return t[r]=null,o}function d(t){(window.requestAnimationFrame||window.setTimeout)(t)}function l(t,e){if(void 0===e&&(e=document),t){if(t.nodeType)return[t];if("string"==typeof t)return[].slice.call(e.querySelectorAll(t));if(t.length)return[].slice.call(t)}return[]}var f="data-@nrk/core-input-1.4.2".replace(/\W+/g,"-"),s=13,p=27,m=33,v=34,b=35,g=36,y=38,x=40,o=500;function w(t,e){var o="object"==typeof e?e:{content:e},a="string"==typeof o.content;return l(t).map(function(t){var e=t.nextElementSibling,n=void 0===o.ajax?t.getAttribute(f):o.ajax,i=void 0===o.limit?t.getAttribute(f+"-limit"):Math.max(o.limit||0,0),r=void 0===o.open?t===document.activeElement:o.open;return t.setAttribute(f,n||""),t.setAttribute(f+"-limit",i),t.setAttribute(u?"data-role":"role","combobox"),t.setAttribute("aria-autocomplete","list"),t.setAttribute("autocomplete","off"),a&&(e.innerHTML=o.content),l("a,button",e).forEach(function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return C.apply(void 0,[i].concat(t))}),E(t,r),t})}function h(a){a.ctrlKey||a.altKey||a.metaKey||a.defaultPrevented||l("["+f+"]").forEach(function(t){var e,n,i=t.nextElementSibling,r=t===a.target||i.contains(a.target),o="click"===a.type&&r&&l('[tabindex="-1"]',i).filter(function(t){return t.contains(a.target)})[0];o?(e=t,n={relatedTarget:i,currentTarget:o,value:o.value||o.textContent.trim()},c(e,"input.select",n)&&(e.value=n.value,e.focus(),d(function(){return E(e,!1)}))):E(t,r)})}function A(t,e){var n=t.nextElementSibling,i=[t].concat(l('[tabindex="-1"]:not([hidden])',n)),r=e.keyCode===p&&"true"===t.getAttribute("aria-expanded"),o=i.indexOf(document.activeElement),a=!1;e.keyCode===x?a=i[o+1]||i[0]:e.keyCode===y?a=i[o-1]||i.pop():n.contains(e.target)&&(e.keyCode===b||e.keyCode===v?a=i.pop():e.keyCode===g||e.keyCode===m?a=i[1]:e.keyCode!==s&&t.focus()),E(t,e.keyCode!==p),(!1!==a||r)&&e.preventDefault(),a&&a.focus()}function E(t,e){void 0===e&&(e="true"===t.getAttribute("aria-expanded")),d(function(){t.nextElementSibling[e?"removeAttribute":"setAttribute"]("hidden",""),t.setAttribute("aria-expanded",e)})}function C(t,e,n,i){e.setAttribute("aria-label",e.textContent.trim()+", "+(n+1)+" av "+i.length),e.setAttribute("tabindex","-1"),e.setAttribute("type","button"),t&&t<=n&&e.parentElement.setAttribute("hidden","")}function k(t){var e=t.getAttribute(f),n=k.xhr=k.xhr||new window.XMLHttpRequest;if(!e)return!1;clearTimeout(k.timer),n.abort(),n.onload=function(){try{n.responseJSON=JSON.parse(n.responseText)}catch(t){n.responseJSON=!1}c(t,"input.ajax",n)},k.timer=setTimeout(function(){t.value&&c(t,"input.ajax.beforeSend",n)&&(n.open("GET",e.replace("{{value}}",window.encodeURIComponent(t.value)),!0),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.send())},o)}return w.escapeHTML=i,w.highlight=function(t,e){var n=e.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&");return i(t).replace(new RegExp(n||".^","gi"),"<mark>$&</mark>")},e(f,"click",h),e(f,"focus",h,!0),e(f,"input",function(t){var a,e,n,i=t.target;i.hasAttribute(f)&&(e={relatedTarget:(a=i).nextElementSibling},n=Number(a.getAttribute(f+"-limit"))||1/0,c(a,"input.filter",e)&&!1===k(a)&&l('[tabindex="-1"]',a.nextElementSibling).reduce(function(t,e,n){var i="LI"===e.parentElement.nodeName&&e.parentElement,r=-1!==e.textContent.toLowerCase().indexOf(a.value.toLowerCase()),o=r?"removeAttribute":"setAttribute";return i&&i[o]("hidden",""),e[o]("hidden",""),r?t.concat(e):t},[]).forEach(function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return C.apply(void 0,[n].concat(t))}))}),e(f,"keydown",function(t){if(!(t.ctrlKey||t.altKey||t.metaKey)){if(t.target.hasAttribute&&t.target.hasAttribute(f))return A(t.target,t);for(var e=t.target,n=void 0;e;e=e.parentElement)if((n=e.previousElementSibling)&&n.hasAttribute(f))return A(n,t)}},!0),w}); | ||
/*! @nrk/core-input v1.4.2 - Copyright (c) 2017-2019 NRK */ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).coreInput=e()}(this,function(){"use strict";var t="undefined"!=typeof window,u=(t&&/(android)/i.test(navigator.userAgent),t&&/iPad|iPhone|iPod/.test(String(navigator.platform))),r=function(t){void 0===t&&(t=!1);try{window.addEventListener("test",null,{get passive(){t=!0}})}catch(t){}return t}();function e(t,e,n,i){(void 0===i&&(i=!1),"undefined"==typeof window||window[t=t+"-"+e])||(r||"object"!=typeof i||(i=Boolean(i.capture)),("resize"===e||"load"===e?window:document).addEventListener(window[t]=e,n,i))}var n={"&":"&","<":"<",">":">",'"':""","/":"/","'":"'"};function i(t){return String(t||"").replace(/[&<>"'/]/g,function(t){return n[t]})}var a="prevent_recursive_dispatch_maximum_callstack";function c(t,e,n){void 0===n&&(n={});var i,r=""+a+e;if(t[r])return!0;t[r]=!0,"function"==typeof window.CustomEvent?i=new window.CustomEvent(e,{bubbles:!0,cancelable:!0,detail:n}):(i=document.createEvent("CustomEvent")).initCustomEvent(e,!0,!0,n);var o=t.dispatchEvent(i);return t[r]=null,o}function d(t){(window.requestAnimationFrame||window.setTimeout)(t)}function l(t,e){if(void 0===e&&(e=document),t){if(t.nodeType)return[t];if("string"==typeof t)return[].slice.call(e.querySelectorAll(t));if(t.length)return[].slice.call(t)}return[]}var f="data-@nrk/core-input-1.4.2".replace(/\W+/g,"-"),s=13,p=27,m=33,v=34,b=35,g=36,y=38,x=40,o=500;function w(t,e){var o="object"==typeof e?e:{content:e},a="string"==typeof o.content;return l(t).map(function(t){var e=t.nextElementSibling,n=void 0===o.ajax?t.getAttribute(f):o.ajax,i=void 0===o.limit?t.getAttribute(f+"-limit"):Math.max(o.limit||0,0),r=void 0===o.open?t===document.activeElement:o.open;return t.setAttribute(f,n||""),t.setAttribute(f+"-limit",i||0),t.setAttribute(u?"data-role":"role","combobox"),t.setAttribute("aria-autocomplete","list"),t.setAttribute("autocomplete","off"),a&&(e.innerHTML=o.content),l("a,button",e).forEach(function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return C.apply(void 0,[i].concat(t))}),E(t,r),t})}function h(a){a.ctrlKey||a.altKey||a.metaKey||a.defaultPrevented||l("["+f+"]").forEach(function(t){var e,n,i=t.nextElementSibling,r=t===a.target||i.contains(a.target),o="click"===a.type&&r&&l('[tabindex="-1"]',i).filter(function(t){return t.contains(a.target)})[0];o?(e=t,n={relatedTarget:i,currentTarget:o,value:o.value||o.textContent.trim()},c(e,"input.select",n)&&(e.value=n.value,e.focus(),d(function(){return E(e,!1)}))):E(t,r)})}function A(t,e){var n=t.nextElementSibling,i=[t].concat(l('[tabindex="-1"]:not([hidden])',n)),r=e.keyCode===p&&"true"===t.getAttribute("aria-expanded"),o=i.indexOf(document.activeElement),a=!1;e.keyCode===x?a=i[o+1]||i[0]:e.keyCode===y?a=i[o-1]||i.pop():n.contains(e.target)&&(e.keyCode===b||e.keyCode===v?a=i.pop():e.keyCode===g||e.keyCode===m?a=i[1]:e.keyCode!==s&&t.focus()),E(t,e.keyCode!==p),(!1!==a||r)&&e.preventDefault(),a&&a.focus()}function E(t,e){void 0===e&&(e="true"===t.getAttribute("aria-expanded")),d(function(){t.nextElementSibling[e?"removeAttribute":"setAttribute"]("hidden",""),t.setAttribute("aria-expanded",e)})}function C(t,e,n,i){e.setAttribute("aria-label",e.textContent.trim()+", "+(n+1)+" av "+i.length),e.setAttribute("tabindex","-1"),e.setAttribute("type","button"),(t=Number(t)||1/0)&&t<=n&&e.parentElement.setAttribute("hidden","")}function k(t){var e=t.getAttribute(f),n=k.xhr=k.xhr||new window.XMLHttpRequest;if(!e)return!1;clearTimeout(k.timer),n.abort(),n.onload=function(){try{n.responseJSON=JSON.parse(n.responseText)}catch(t){n.responseJSON=!1}c(t,"input.ajax",n)},k.timer=setTimeout(function(){t.value&&c(t,"input.ajax.beforeSend",n)&&(n.open("GET",e.replace("{{value}}",window.encodeURIComponent(t.value)),!0),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.send())},o)}return w.escapeHTML=i,w.highlight=function(t,e){var n=e.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&");return i(t).replace(new RegExp(n||".^","gi"),"<mark>$&</mark>")},e(f,"click",h),e(f,"focus",h,!0),e(f,"input",function(t){var a,e,n=t.target;n.hasAttribute(f)&&(e={relatedTarget:(a=n).nextElementSibling},c(a,"input.filter",e)&&!1===k(a)&&l('[tabindex="-1"]',a.nextElementSibling).reduce(function(t,e,n){var i="LI"===e.parentElement.nodeName&&e.parentElement,r=-1!==e.textContent.toLowerCase().indexOf(a.value.toLowerCase()),o=r?"removeAttribute":"setAttribute";return i&&i[o]("hidden",""),e[o]("hidden",""),r?t.concat(e):t},[]).forEach(function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return C.apply(void 0,[a.getAttribute(f+"-limit")].concat(t))}))}),e(f,"keydown",function(t){if(!(t.ctrlKey||t.altKey||t.metaKey)){if(t.target.hasAttribute&&t.target.hasAttribute(f))return A(t.target,t);for(var e=t.target,n=void 0;e;e=e.parentElement)if((n=e.previousElementSibling)&&n.hasAttribute(f))return A(n,t)}},!0),w}); | ||
//# sourceMappingURL=core-input.min.js.map |
@@ -5,4 +5,4 @@ const { name, version } = require('./package.json') | ||
const UUID = `data-${name}-${version}`.replace(/\W+/g, '-') | ||
const standardHTML = ` | ||
<input type="text" class="my-input" placeholder="Type something..."> | ||
const HTML = ` | ||
<input type="text" placeholder="Type something..."> | ||
<ul hidden> | ||
@@ -15,3 +15,3 @@ <li><button>Chrome</button></li> | ||
</ul> | ||
<button id="something-else" type="button"></button> | ||
<button></button> | ||
` | ||
@@ -33,4 +33,4 @@ | ||
it('should initialize input with props', () => { | ||
document.body.innerHTML = standardHTML | ||
const input = document.querySelector('.my-input') | ||
document.body.innerHTML = HTML | ||
const input = document.querySelector('input') | ||
coreInput(input) | ||
@@ -44,5 +44,5 @@ expect(input.getAttribute('role')).toEqual('combobox') | ||
it('should expand suggestions when input is clicked', () => { | ||
document.body.innerHTML = standardHTML | ||
const input = document.querySelector('.my-input') | ||
const suggestions = document.querySelector('.my-input + ul') | ||
document.body.innerHTML = HTML | ||
const input = document.querySelector('input') | ||
const suggestions = document.querySelector('input + ul') | ||
coreInput(input) | ||
@@ -58,5 +58,5 @@ input.click() | ||
it('should set input value to clicked suggestion', () => { | ||
document.body.innerHTML = standardHTML | ||
const input = document.querySelector('.my-input') | ||
const suggestions = document.querySelector('.my-input + ul') | ||
document.body.innerHTML = HTML | ||
const input = document.querySelector('input') | ||
const suggestions = document.querySelector('input + ul') | ||
const firefoxBtn = suggestions.querySelector('li:nth-child(2) button') | ||
@@ -78,9 +78,9 @@ const callback = jest.fn() | ||
it('should close suggestions on focusing outside', () => { | ||
document.body.innerHTML = standardHTML | ||
const input = document.querySelector('.my-input') | ||
const suggestions = document.querySelector('.my-input + ul') | ||
const someOtherBtn = document.querySelector('#something-else') | ||
document.body.innerHTML = HTML | ||
const input = document.querySelector('input') | ||
const suggestions = document.querySelector('input + ul') | ||
const button = document.querySelector('button') | ||
coreInput(input) | ||
input.click() | ||
someOtherBtn.click() | ||
button.click() | ||
expect(input.getAttribute('role')).toEqual('combobox') | ||
@@ -94,4 +94,4 @@ expect(input.getAttribute('aria-autocomplete')).toEqual('list') | ||
it('should filter suggestion from input value', () => { | ||
document.body.innerHTML = standardHTML | ||
const input = document.querySelector('.my-input') | ||
document.body.innerHTML = HTML | ||
const input = document.querySelector('input') | ||
const event = new window.CustomEvent('input', { bubbles: true }) | ||
@@ -105,4 +105,4 @@ coreInput(input) | ||
it('should set type="button" on all buttons in list', () => { | ||
document.body.innerHTML = standardHTML | ||
coreInput(document.querySelector('.my-input')) | ||
document.body.innerHTML = HTML | ||
coreInput(document.querySelector('input')) | ||
document.querySelectorAll('ul button').forEach((button) => { | ||
@@ -114,7 +114,8 @@ expect(button.type).toEqual('button') | ||
it('should remember and ovewrite options', () => { | ||
document.body.innerHTML = standardHTML | ||
const input = document.querySelector('.my-input') | ||
document.body.innerHTML = HTML | ||
const input = document.querySelector('input') | ||
coreInput(input, { limit: 11, ajax: 'https://example.com/{{value}}' }) | ||
expect(input.getAttribute(`${UUID}-limit`)).toEqual('11') | ||
coreInput(input, { limit: 12 }) | ||
coreInput(input) | ||
expect(input.getAttribute(`${UUID}-limit`)).toEqual('12') | ||
@@ -124,9 +125,47 @@ expect(input.getAttribute(UUID)).toEqual('https://example.com/{{value}}') | ||
it('should correctly parse limit option', () => { | ||
document.body.innerHTML = HTML | ||
const input = document.querySelector('input') | ||
coreInput(input) | ||
expect(input.getAttribute(`${UUID}-limit`)).toBe('0') | ||
coreInput(input, { limit: 2 }) | ||
expect(input.getAttribute(`${UUID}-limit`)).toBe('2') | ||
coreInput(input, { limit: 0 }) | ||
expect(input.getAttribute(`${UUID}-limit`)).toBe('0') | ||
coreInput(input, { limit: -2 }) | ||
expect(input.getAttribute(`${UUID}-limit`)).toBe('0') | ||
coreInput(input, { limit: null }) | ||
expect(input.getAttribute(`${UUID}-limit`)).toBe('0') | ||
coreInput(input, { limit: undefined }) | ||
expect(input.getAttribute(`${UUID}-limit`)).toBe('0') | ||
coreInput(input, { limit: 2 }) | ||
coreInput(input, { limit: undefined }) | ||
expect(input.getAttribute(`${UUID}-limit`)).toBe('2') | ||
}) | ||
it('should limit length of suggestions from limit option', () => { | ||
document.body.innerHTML = standardHTML | ||
const input = document.querySelector('.my-input') | ||
const suggestions = document.querySelector('.my-input + ul') | ||
coreInput(input, { limit: 2 }) | ||
document.body.innerHTML = HTML | ||
const input = document.querySelector('input') | ||
const suggestions = document.querySelector('input + ul') | ||
coreInput(input) | ||
expect(suggestions.children[0].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[1].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[2].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[3].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[4].hasAttribute('hidden')).toBe(false) | ||
coreInput(input, { limit: 3 }) | ||
expect(suggestions.children[0].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[1].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[2].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[3].hasAttribute('hidden')).toBe(true) | ||
expect(suggestions.children[4].hasAttribute('hidden')).toBe(true) | ||
coreInput(input, { limit: undefined }) | ||
expect(suggestions.children[0].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[1].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[2].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[3].hasAttribute('hidden')).toBe(true) | ||
expect(suggestions.children[4].hasAttribute('hidden')).toBe(true) | ||
coreInput(input, { limit: 1 }) | ||
expect(suggestions.children[0].hasAttribute('hidden')).toBe(false) | ||
expect(suggestions.children[1].hasAttribute('hidden')).toBe(true) | ||
expect(suggestions.children[2].hasAttribute('hidden')).toBe(true) | ||
@@ -133,0 +172,0 @@ expect(suggestions.children[3].hasAttribute('hidden')).toBe(true) |
@@ -131,3 +131,3 @@ 'use strict'; | ||
input.setAttribute(UUID, ajax || ''); | ||
input.setAttribute((UUID + "-limit"), limit); | ||
input.setAttribute((UUID + "-limit"), limit || 0); | ||
input.setAttribute(IS_IOS ? 'data-role' : 'role', 'combobox'); // iOS does not inform user area is editable if combobox | ||
@@ -216,3 +216,2 @@ input.setAttribute('aria-autocomplete', 'list'); | ||
function onFilter (input, detail) { | ||
var limit = Number(input.getAttribute((UUID + "-limit"))) || Infinity; | ||
if (dispatchEvent(input, 'input.filter', detail) && ajax(input) === false) { | ||
@@ -230,3 +229,3 @@ queryAll('[tabindex="-1"]', input.nextElementSibling).reduce(function (acc, item, index) { | ||
return setupItem.apply(void 0, [ limit ].concat( args )); | ||
return setupItem.apply(void 0, [ input.getAttribute((UUID + "-limit")) ].concat( args )); | ||
}); | ||
@@ -249,2 +248,3 @@ } | ||
item.setAttribute('type', 'button'); | ||
limit = Number(limit) || Infinity; | ||
if (limit && (index >= limit)) { item.parentElement.setAttribute('hidden', ''); } | ||
@@ -251,0 +251,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"author": "NRK <opensource@nrk.no> (https://www.nrk.no/)", | ||
"version": "1.4.2", | ||
"version": "1.4.3", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "main": "core-input.cjs.js", |
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
143124
967