UNPKG

primereact

Version:

PrimeReact is an open source UI library for React featuring a rich set of 90+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with Prime

2 lines (1 loc) 5.91 kB
import*as e from"react";import{KeyFilter as t}from"primereact/keyfilter";import{Tooltip as n}from"primereact/tooltip";import{ObjectUtils as r,DomHandler as l,classNames as a}from"primereact/utils";function o(){return o=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},o.apply(this,arguments)}function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function u(e){if(Array.isArray(e))return i(e)}function c(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function p(e,t){if(e){if("string"==typeof e)return i(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?i(e,t):void 0}}function s(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function f(e){return u(e)||c(e)||p(e)||s()}function m(e){if(Array.isArray(e))return e}function d(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,l,a,o,i=[],u=!0,c=!1;try{if(a=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;u=!1}else for(;!(u=(r=a.call(n)).done)&&(i.push(r.value),i.length!==t);u=!0);}catch(e){c=!0,l=e}finally{try{if(!u&&null!=n.return&&(o=n.return(),Object(o)!==o))return}finally{if(c)throw l}}return i}}function v(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var y={defaultProps:{__TYPE:"Chips",id:null,inputRef:null,inputId:null,name:null,placeholder:null,value:null,max:null,disabled:null,readOnly:!1,removable:!0,style:null,className:null,tooltip:null,tooltipOptions:null,ariaLabelledBy:null,separator:null,allowDuplicate:!0,itemTemplate:null,keyfilter:null,addOnBlur:null,onAdd:null,onRemove:null,onChange:null,onFocus:null,onBlur:null,onKeyDown:null,children:void 0},getProps:function(e){return r.getMergedProps(e,y.defaultProps)},getOtherProps:function(e){return r.getDiffProps(e,y.defaultProps)}},h=e.memo(e.forwardRef((function(i,u){var c,s,h=y.getProps(i),g=e.useState(!1),b=(s=2,m(c=g)||d(c,s)||p(c,s)||v()),w=b[0],E=b[1],O=e.useRef(null),P=e.useRef(null),k=e.useRef(h.inputRef),x=function(e,t){if(!h.disabled||!h.readOnly){var n=f(h.value),r=n.splice(t,1);K(r,t)&&(h.onRemove&&h.onRemove({originalEvent:e,value:r}),h.onChange&&h.onChange({originalEvent:e,value:n,stopPropagation:function(){},preventDefault:function(){},target:{name:h.name,id:h.id,value:n}}))}},A=function(e,t,n){if(t&&t.trim().length){var r=h.value?f(h.value):[];if(h.allowDuplicate||-1===r.indexOf(t)){var l=!0;h.onAdd&&(l=h.onAdd({originalEvent:e,value:t})),!1!==l&&r.push(t)}N(e,r,n)}},D=function(){l.focus(k.current)},R=function(e){var n=e.target.value,r=h.value||[];if(h.onKeyDown&&h.onKeyDown(e),!e.defaultPrevented)switch(e.key){case"Backspace":0===k.current.value.length&&r.length>0&&x(e,r.length-1);break;case"Enter":n&&n.trim().length&&(!h.max||h.max>r.length)&&A(e,n,!0);break;default:h.keyfilter&&t.onKeyPress(e,h.keyfilter),j()?e.preventDefault():","===h.separator&&(e.key===h.separator||l.isAndroid()&&229===e.which)&&A(e,n,!0)}},N=function(e,t,n){h.onChange&&h.onChange({originalEvent:e,value:t,stopPropagation:function(){},preventDefault:function(){},target:{name:h.name,id:h.id,value:t}}),k.current.value="",n&&e.preventDefault()},C=function(e){if(h.separator){var n=(e.clipboardData||window.clipboardData).getData("Text");if(h.keyfilter&&t.onPaste(e,h.keyfilter),n){var r=h.value||[],l=n.split(h.separator);l=l.filter((function(e){return(h.allowDuplicate||-1===r.indexOf(e))&&e.trim().length})),r=[].concat(f(r),f(l)),N(e,r,!0)}}},I=function(e){E(!0),h.onFocus&&h.onFocus(e)},S=function(e){if(h.addOnBlur){var t=e.target.value,n=h.value||[];t&&t.trim().length&&(!h.max||h.max>n.length)&&A(e,t,!0)}E(!1),h.onBlur&&h.onBlur(e)},j=function(){return h.max&&h.value&&h.max===h.value.length},B=k.current&&k.current.value,T=e.useMemo((function(){return r.isNotEmpty(h.value)||r.isNotEmpty(B)}),[h.value,B]),K=function(e,t){return r.getPropValue(h.removable,{value:e,index:t,props:h})};e.useImperativeHandle(u,(function(){return{props:h,focus:function(){return l.focus(k.current)},getElement:function(){return O.current},getInput:function(){return k.current}}})),e.useEffect((function(){r.combinedRefs(k,h.inputRef)}),[k,h.inputRef]);var F=function(t,n){return h.disabled||h.readOnly||!K(t,n)?null:e.createElement("span",{className:"p-chips-token-icon pi pi-times-circle",onClick:function(e){return x(e,n)}})},M=function(t,n){var r=h.itemTemplate?h.itemTemplate(t):t,l=e.createElement("span",{className:"p-chips-token-label"},r),a=F(t,n);return e.createElement("li",{key:n,className:"p-chips-token p-highlight"},l,a)},_=r.isNotEmpty(h.tooltip),H=y.getOtherProps(h),L=r.reduceKeys(H,l.ARIA_PROPS),U=a("p-chips p-component p-inputwrapper",{"p-inputwrapper-filled":T,"p-inputwrapper-focus":w},h.className),V=function(){var t=a("p-inputtext p-chips-multiple-container",{"p-disabled":h.disabled,"p-focus":w}),n=h.value?h.value.map(M):null,r=e.createElement("li",{className:"p-chips-input-token"},e.createElement("input",o({ref:k,id:h.inputId,placeholder:h.placeholder,type:"text",name:h.name,disabled:h.disabled||j(),onKeyDown:R,onPaste:C,onFocus:I,onBlur:S,readOnly:h.readOnly},L)));return e.createElement("ul",{ref:P,className:t,onClick:D},n,r)}();return e.createElement(e.Fragment,null,e.createElement("div",o({ref:O,id:h.id,className:U,style:h.style},H),V),_&&e.createElement(n,o({target:k,content:h.tooltip},h.tooltipOptions)))})));h.displayName="Chips";export{h as Chips};