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) • 11.3 kB
JavaScript
import*as e from"react";import{PrimeReactContext as n}from"primereact/api";import{ComponentBase as t,useHandleStyle as r}from"primereact/componentbase";import{useMergeProps as o,useMountEffect as i}from"primereact/hooks";import{TimesCircleIcon as l}from"primereact/icons/timescircle";import{KeyFilter as a}from"primereact/keyfilter";import{Tooltip as u}from"primereact/tooltip";import{classNames as c,ObjectUtils as p,DomHandler as s,IconUtils as f}from"primereact/utils";function d(){return d=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},d.apply(this,arguments)}function m(e){return m="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},m(e)}function v(e,n){if("object"!==m(e)||null===e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,n||"default");if("object"!==m(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===n?String:Number)(e)}function y(e){var n=v(e,"string");return"symbol"===m(n)?n:String(n)}function h(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t<n;t++)r[t]=e[t];return r}function b(e){if(Array.isArray(e))return h(e)}function g(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function w(e,n){if(e){if("string"==typeof e)return h(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?h(e,n):void 0}}function k(){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 O(e){return b(e)||g(e)||w(e)||k()}function x(e){if(Array.isArray(e))return e}function P(e,n){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var r,o,i,l,a=[],u=!0,c=!1;try{if(i=(t=t.call(e)).next,0===n){if(Object(t)!==t)return;u=!1}else for(;!(u=(r=i.call(t)).done)&&(a.push(r.value),a.length!==n);u=!0);}catch(e){c=!0,o=e}finally{try{if(!u&&null!=t.return&&(l=t.return(),Object(l)!==l))return}finally{if(c)throw o}}return a}}function E(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function S(e,n){return x(e)||P(e,n)||w(e,n)||E()}var j=t.extend({defaultProps:{__TYPE:"Chips",addOnBlur:null,allowDuplicate:!0,ariaLabelledBy:null,autoFocus:!1,className:null,disabled:null,id:null,inputId:null,inputRef:null,invalid:!1,variant:null,itemTemplate:null,keyfilter:null,max:null,name:null,onAdd:null,onBlur:null,onChange:null,onFocus:null,onKeyDown:null,onRemove:null,placeholder:null,readOnly:!1,removable:!0,removeIcon:null,separator:null,style:null,tooltip:null,tooltipOptions:null,value:null,children:void 0},css:{classes:{removeTokenIcon:"p-chips-token-icon",label:"p-chips-token-label",token:function(e){return c("p-chips-token",{"p-focus":e.focusedIndex===e.index})},inputToken:"p-chips-input-token",container:function(e){var n=e.props,t=e.context;return c("p-inputtext p-chips-multiple-container",{"p-variant-filled":n.variant?"filled"===n.variant:t&&"filled"===t.inputStyle})},root:function(e){var n=e.focusedState;return c("p-chips p-component p-inputwrapper",{"p-inputwrapper-filled":e.isFilled,"p-inputwrapper-focus":n,"p-disabled":e.disabled,"p-invalid":e.invalid,"p-focus":n})}},styles:"\n@layer primereact {\n .p-chips {\n display: inline-flex;\n }\n \n .p-chips-multiple-container {\n margin: 0;\n padding: 0;\n list-style-type: none;\n cursor: text;\n overflow: hidden;\n display: flex;\n align-items: center;\n flex-wrap: wrap;\n }\n \n .p-chips-token {\n cursor: default;\n display: inline-flex;\n align-items: center;\n flex: 0 0 auto;\n }\n \n .p-chips-input-token {\n flex: 1 1 auto;\n display: inline-flex;\n }\n \n .p-chips-token-icon {\n cursor: pointer;\n }\n \n .p-chips-input-token input {\n border: 0 none;\n outline: 0 none;\n background-color: transparent;\n margin: 0;\n padding: 0;\n box-shadow: none;\n border-radius: 0;\n width: 100%;\n }\n \n .p-fluid .p-chips {\n display: flex;\n }\n \n .p-chips-icon-left,\n .p-chips-icon-right {\n position: relative;\n display: inline-block;\n }\n \n .p-chips-icon-left > i,\n .p-chips-icon-right > i,\n .p-chips-icon-left > svg,\n .p-chips-icon-right > svg,\n .p-chips-icon-left > .p-chips-prefix,\n .p-chips-icon-right > .p-chips-suffix {\n position: absolute;\n top: 50%;\n margin-top: -0.5rem;\n }\n \n .p-fluid .p-chips-icon-left,\n .p-fluid .p-chips-icon-right {\n display: block;\n width: 100%;\n }\n}\n"}});function D(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n&&(r=r.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),t.push.apply(t,r)}return t}function I(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?D(Object(t),!0).forEach((function(n){var r,o,i;r=e,i=t[n],(o=y(o=n))in r?Object.defineProperty(r,o,{value:i,enumerable:!0,configurable:!0,writable:!0}):r[o]=i})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):D(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}var A=e.memo(e.forwardRef((function(t,m){var v=o(),y=e.useContext(n),h=j.getProps(t,y),b=S(e.useState(!1),2),g=b[0],w=b[1],k=S(e.useState(null),2),x=k[0],P=k[1],E=j.setMetaData({props:h,state:{focused:g}}),D=E.ptm,A=E.cx;r(j.css.styles,E.isUnstyled,{name:"chips"});var R=e.useRef(null),T=e.useRef(null),N=e.useRef(h.inputRef),C=function(e,n){if(!h.disabled||!h.readOnly){var t=O(h.value),r=t.splice(n,1)[0];q(r,n)&&(h.onRemove&&h.onRemove({originalEvent:e,value:r}),h.onChange&&h.onChange({originalEvent:e,value:t,stopPropagation:function(){null==e||e.stopPropagation()},preventDefault:function(){null==e||e.preventDefault()},target:{name:h.name,id:h.id,value:t}}))}},F=function(e,n,t){if(n&&n.trim().length){var r=h.value?O(h.value):[];if(h.allowDuplicate||-1===r.indexOf(n)){var o=!0;h.onAdd&&(o=h.onAdd({originalEvent:e,value:n})),!1!==o&&r.push(n)}z(e,r,t)}},B=function(e){switch(e.code){case"ArrowLeft":_();break;case"ArrowRight":K();break;case"Backspace":L(e)}},_=function(){var e=x;0===N.current.value.length&&h.value&&h.value.length>0&&(e=null===e?h.value.length-1:e-1)<0&&(e=0),P(e)},K=function(){var e=x;0===N.current.value.length&&h.value&&h.value.length>0&&(e===h.value.length-1?(e=null,N.current.focus()):e++),P(e)},L=function(e){null!==x&&C(e,x)},M=function(e){var n=e.target.value,t=h.value||[];if(h.onKeyDown&&h.onKeyDown(e),!e.defaultPrevented)switch(e.key){case"Backspace":0===n.length&&t.length>0&&C(e,t.length-1);break;case"Enter":n&&n.trim().length&&(!h.max||h.max>t.length)&&F(e,n,!0);break;case"ArrowLeft":0===n.length&&t&&t.length>0&&s.focus(T.current);break;case"ArrowRight":e.stopPropagation();break;default:h.keyfilter&&a.onKeyPress(e,h.keyfilter),X()&&e.preventDefault()}},z=function(e,n,t){h.onChange&&h.onChange({originalEvent:e,value:n,stopPropagation:function(){null==e||e.stopPropagation()},preventDefault:function(){null==e||e.preventDefault()},target:{name:h.name,id:h.id,value:n}}),N.current.value="",t&&e.preventDefault()},H=function(e){var n,t=null===(n=e.target.value)||void 0===n?void 0:n.trim();if(t!==h.separator){if(h.separator&&t.endsWith(h.separator)){var r=t.slice(0,-1);F(e,r)}}else N.current.value=""},U=function(e){if(h.separator){var n=h.separator.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t"),t=(e.clipboardData||window.clipboardData).getData("Text");if(h.keyfilter&&a.onPaste(e,h.keyfilter),t){var r=h.value||[],o=t.split(n);o=o.filter((function(e){return(h.allowDuplicate||-1===r.indexOf(e))&&e.trim().length})),r=[].concat(O(r),O(o)),z(e,r,!0)}}},J=function(){w(!0)},V=function(){P(-1),w(!1)},W=function(e){if(h.addOnBlur){var n=e.target.value,t=h.value||[];n&&n.trim().length&&(!h.max||h.max>t.length)&&F(e,n,!0)}w(!1),h.onBlur&&h.onBlur(e)},X=function(){return h.max&&h.value&&h.max===h.value.length},Y=N.current&&N.current.value,$=e.useMemo((function(){return p.isNotEmpty(h.value)||p.isNotEmpty(Y)}),[h.value,Y]),q=function(e,n){return p.getPropValue(h.removable,{value:e,index:n,props:h})};e.useImperativeHandle(m,(function(){return{props:h,focus:function(){return s.focus(N.current)},getElement:function(){return R.current},getInput:function(){return N.current}}})),e.useEffect((function(){p.combinedRefs(N,h.inputRef)}),[N,h.inputRef]),i((function(){h.autoFocus&&s.focus(N.current,h.autoFocus)}));var G,Q,Z,ee,ne,te=function(n,t){if(!h.disabled&&!h.readOnly&&q(n,t)){var r=v({className:A("removeTokenIcon"),onClick:function(e){return C(e,t)},"aria-hidden":"true"},D("removeTokenIcon"));return f.getJSXIcon(h.removeIcon||e.createElement(l,r),I({},r),{props:h})}return null},re=function(n,t){var r=h.itemTemplate?h.itemTemplate(n):n,o=v({className:A("label")},D("label")),i=e.createElement("span",o,r),l=te(n,t),a=v({id:h.inputId+"_chips_item_"+t,role:"option","aria-label":n,className:A("token",{focusedIndex:x,index:t}),"aria-selected":!0,"aria-setsize":h.value.length,"aria-posinset":t+1,"data-p-highlight":!0,"data-p-focused":x===t},D("token"));return e.createElement("li",d({},a,{key:"".concat(t,"_").concat(n)}),i,l)},oe=p.isNotEmpty(h.tooltip),ie=j.getOtherProps(h),le=p.reduceKeys(ie,s.ARIA_PROPS),ae=(Z=h.value?h.value.map(re):null,G=v({className:A("inputToken")},D("inputToken")),Q=v(I({id:h.inputId,ref:N,placeholder:h.placeholder,type:"text",enterKeyHint:"enter",name:h.name,disabled:h.disabled||X(),onKeyDown:function(e){return M(e)},onChange:function(e){return H(e)},onPaste:function(e){return U(e)},onFocus:function(e){return n=e,w(!0),P(null),void(h.onFocus&&h.onFocus(n));var n},onBlur:function(e){return W(e)},readOnly:h.readOnly},le),D("input")),ee=e.createElement("li",G,e.createElement("input",Q)),ne=v({ref:T,className:A("container",{context:y}),onClick:function(e){s.focus(N.current)},onKeyDown:function(e){return B(e)},tabIndex:-1,role:"listbox","aria-orientation":"horizontal","aria-labelledby":h.ariaLabelledby,"aria-label":h.ariaLabel,"aria-activedescendant":g?null!==x?"".concat(h.inputId,"_chips_item_").concat(x):null:void 0,"data-p-disabled":h.disabled,"data-p-focus":g,onFocus:J,onBlur:V},D("container")),e.createElement("ul",ne,Z,ee)),ue=v({id:h.id,ref:R,className:c(h.className,A("root",{isFilled:$,focusedState:g,disabled:h.disabled,invalid:h.invalid})),style:h.style},D("root"));return e.createElement(e.Fragment,null,e.createElement("div",ue,ae),oe&&e.createElement(u,d({target:N,content:h.tooltip,pt:D("tooltip")},h.tooltipOptions)))})));A.displayName="Chips";export{A as Chips};