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) • 14.9 kB
JavaScript
import*as e from"react";import t,{FilterService as n}from"primereact/api";import{useMountEffect as r,useUpdateEffect as l}from"primereact/hooks";import{ObjectUtils as i,classNames as o,DomHandler as a,UniqueComponentId as c}from"primereact/utils";import{Button as s}from"primereact/button";import{Ripple as u}from"primereact/ripple";function p(){return p=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},p.apply(this,arguments)}function f(e){if(Array.isArray(e))return e}function m(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,l,i,o,a=[],c=!0,s=!1;try{if(i=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;c=!1}else for(;!(c=(r=i.call(n)).done)&&(a.push(r.value),a.length!==t);c=!0);}catch(e){s=!0,l=e}finally{try{if(!c&&null!=n.return&&(o=n.return(),Object(o)!==o))return}finally{if(s)throw l}}return a}}function g(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 d(e,t){if(e){if("string"==typeof e)return g(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)?g(e,t):void 0}}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.")}function h(e,t){return f(e)||m(e,t)||d(e,t)||v()}var y={defaultProps:{__TYPE:"PickList",id:null,source:null,target:null,sourceHeader:null,targetHeader:null,style:null,className:null,sourceStyle:null,targetStyle:null,sourceSelection:null,targetSelection:null,showSourceControls:!0,showTargetControls:!0,metaKeySelection:!0,filter:!1,filterBy:null,filterMatchMode:"contains",filterLocale:void 0,sourceFilterValue:null,targetFilterValue:null,showSourceFilter:!0,showTargetFilter:!0,sourceFilterPlaceholder:null,targetFilterPlaceholder:null,sourceFilterTemplate:null,targetFilterTemplate:null,tabIndex:0,dataKey:null,breakpoint:"960px",itemTemplate:null,sourceItemTemplate:null,targetItemTemplate:null,onChange:null,onMoveToSource:null,onMoveAllToSource:null,onMoveToTarget:null,onMoveAllToTarget:null,onSourceSelectionChange:null,onTargetSelectionChange:null,onSourceFilterChange:null,onTargetFilterChange:null,children:void 0},getProps:function(e){return i.getMergedProps(e,y.defaultProps)},getOtherProps:function(e){return i.getDiffProps(e,y.defaultProps)}};function b(e){if(Array.isArray(e))return g(e)}function S(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function T(){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 E(e){return b(e)||S(e)||d(e)||T()}var k=e.memo((function(t){var n=!t.selection||!t.selection.length,r=o("p-picklist-buttons",t.className);return e.createElement("div",{className:r},e.createElement(s,{disabled:n,type:"button",icon:"pi pi-angle-up",onClick:function(e){var n=t.selection;if(n&&n.length){for(var r=E(t.list),l=0;l<n.length;l++){var o=i.findIndexInList(n[l],r,t.dataKey);if(0===o)break;var a=r[o-1];r[o-1]=r[o],r[o]=a}t.onReorder&&t.onReorder({originalEvent:e,value:r,direction:"up"})}}}),e.createElement(s,{disabled:n,type:"button",icon:"pi pi-angle-double-up",onClick:function(e){var n=t.selection;if(n&&n.length){for(var r=E(t.list),l=0;l<n.length;l++){var o=i.findIndexInList(n[l],r,t.dataKey);if(0===o)break;var a=r.splice(o,1)[0];r.unshift(a)}t.onReorder&&t.onReorder({originalEvent:e,value:r,direction:"top"})}}}),e.createElement(s,{disabled:n,type:"button",icon:"pi pi-angle-down",onClick:function(e){var n=t.selection;if(n&&n.length){for(var r=E(t.list),l=n.length-1;l>=0;l--){var o=i.findIndexInList(n[l],r,t.dataKey);if(o===r.length-1)break;var a=r[o+1];r[o+1]=r[o],r[o]=a}t.onReorder&&t.onReorder({originalEvent:e,value:r,direction:"down"})}}}),e.createElement(s,{disabled:n,type:"button",icon:"pi pi-angle-double-down",onClick:function(e){var n=t.selection;if(n&&n.length){for(var r=E(t.list),l=n.length-1;l>=0;l--){var o=i.findIndexInList(n[l],r,t.dataKey);if(o===r.length-1)break;var a=r.splice(o,1)[0];r.push(a)}t.onReorder&&t.onReorder({originalEvent:e,value:r,direction:"bottom"})}}}))}));k.displayName="PickListControls";var C=e.memo((function(t){var n=t.template?t.template(t.value):t.value,r=o("p-picklist-item",{"p-highlight":t.selected},t.className);return e.createElement("li",{className:r,onClick:function(e){t.onClick&&t.onClick({originalEvent:e,value:t.value})},onKeyDown:function(e){t.onKeyDown&&t.onKeyDown({originalEvent:e,value:t.value})},tabIndex:t.tabIndex,role:"option","aria-selected":t.selected},n,e.createElement(u,null))}));C.displayName="PickListItem";var I=e.memo(e.forwardRef((function(t,n){var r=e.useRef(null),l=function(e){var n=e.originalEvent,r=e.value,l=E(t.selection),o=i.findIndexInList(r,l,t.dataKey),a=-1!==o;if(t.metaKeySelection){var c=n.metaKey||n.ctrlKey;a&&c?l.splice(o,1):(c||(l.length=0),l.push(r))}else a?l.splice(o,1):l.push(r);t.onSelectionChange&&t.onSelectionChange({event:n,value:l})},c=function(e){var t=e.originalEvent,n=t.currentTarget;switch(t.which){case 40:var r=s(n);r&&r.focus(),t.preventDefault();break;case 38:var i=u(n);i&&i.focus(),t.preventDefault();break;case 13:l(e),t.preventDefault()}},s=function e(t){var n=t.nextElementSibling;return n?a.hasClass(n,"p-picklist-item")?n:e(n):null},u=function e(t){var n=t.previousElementSibling;return n?a.hasClass(n,"p-picklist-item")?n:e(n):null},p=function(e){return-1!==i.findIndexInList(e,t.selection,t.dataKey)},f=function(e){t.onFilter&&t.onFilter({originalEvent:e,value:e.target.value,type:t.type})},m=function(e){13===e.which&&e.preventDefault()};e.useImperativeHandle(n,(function(){return{listElementRef:r}}));var g=o("p-picklist-list-wrapper",t.className),d=t.header?e.createElement("div",{className:"p-picklist-header"},i.getJSXElement(t.header,t)):null,v=function(){if(t.showFilter){var n=e.createElement("div",{className:"p-picklist-filter"},e.createElement("input",{type:"text",value:t.filterValue,onChange:f,onKeyDown:m,placeholder:t.placeholder,className:"p-picklist-filter-input p-inputtext p-component"}),e.createElement("span",{className:"p-picklist-filter-icon pi pi-search"}));if(t.filterTemplate)n=i.getJSXElement(t.filterTemplate,{className:"p-picklist-filter",inputProps:{className:"p-picklist-filter-input p-inputtext p-component",onChange:f,onKeyDown:m},iconClassName:"p-picklist-filter-icon pi pi-search",element:n,props:t});return e.createElement("div",{className:"p-picklist-filter-container"},n)}return null}(),h=function(){var n=t.list?t.list.map((function(n){var r=JSON.stringify(n),i=p(n);return e.createElement(C,{key:r,value:n,template:t.itemTemplate,selected:i,onClick:l,onKeyDown:c,tabIndex:t.tabIndex})})):null,r=o("p-picklist-list",t.listClassName);return e.createElement("ul",{className:r,style:t.style,role:"listbox","aria-multiselectable":!0},n)}();return e.createElement("div",{ref:r,className:g},d,v,h)})));I.displayName="PickListSubList";var w=e.memo((function(t){var n=i.isEmpty(t.sourceSelection)||i.isEmpty(t.visibleSourceList),r=i.isEmpty(t.targetSelection)||i.isEmpty(t.visibleTargetList),l=i.isEmpty(t.visibleSourceList),a=i.isEmpty(t.visibleTargetList),c=o("p-picklist-buttons p-picklist-transfer-buttons",t.className);return e.createElement("div",{className:c},e.createElement(s,{disabled:n,type:"button",icon:"pi pi-angle-right",onClick:function(e){var n=t.sourceSelection;if(i.isNotEmpty(n)){for(var r=E(t.target),l=E(t.source),o=0;o<n.length;o++){var a=n[o];-1===i.findIndexInList(a,r,t.dataKey)&&r.push(l.splice(i.findIndexInList(a,l,t.dataKey),1)[0])}t.onTransfer&&t.onTransfer({originalEvent:e,source:l,target:r,direction:"toTarget"})}}}),e.createElement(s,{disabled:l,type:"button",icon:"pi pi-angle-double-right",onClick:function(e){if(t.source){var n=[].concat(E(t.target),E(t.visibleSourceList)),r=t.source.filter((function(e){return!t.visibleSourceList.some((function(t){return t===e}))}));t.onTransfer&&t.onTransfer({originalEvent:e,source:r,target:n,direction:"allToTarget"})}}}),e.createElement(s,{disabled:r,type:"button",icon:"pi pi-angle-left",onClick:function(e){var n=t.targetSelection;if(i.isNotEmpty(n)){for(var r=E(t.target),l=E(t.source),o=0;o<n.length;o++){var a=n[o];-1===i.findIndexInList(a,l,t.dataKey)&&l.push(r.splice(i.findIndexInList(a,r,t.dataKey),1)[0])}t.onTransfer&&t.onTransfer({originalEvent:e,source:l,target:r,direction:"toSource"})}}}),e.createElement(s,{disabled:a,type:"button",icon:"pi pi-angle-double-left",onClick:function(e){if(t.source){var n=[].concat(E(t.source),E(t.visibleTargetList)),r=t.target.filter((function(e){return!t.visibleTargetList.some((function(t){return t===e}))}));t.onTransfer&&t.onTransfer({originalEvent:e,source:n,target:r,direction:"allToSource"})}}}))}));w.displayName="PickListTransferControls";var N=e.memo(e.forwardRef((function(s,u){var f=y.getProps(s),m=h(e.useState([]),2),g=m[0],d=m[1],v=h(e.useState([]),2),b=v[0],S=v[1],T=h(e.useState(""),2),E=T[0],C=T[1],N=h(e.useState(""),2),L=N[0],K=N[1],x=h(e.useState(null),2),F=x[0],P=x[1],R=e.useRef(null),M=e.useRef(null),A=e.useRef(null),D=e.useRef(null),O=e.useRef(null),j=e.useRef(null),H=f.sourceSelection?f.sourceSelection:g,V=f.targetSelection?f.targetSelection:b,B=f.onSourceFilterChange?f.sourceFilterValue:E,J=f.onTargetFilterChange?f.targetFilterValue:L,X=i.isNotEmpty(f.filterBy),_=X&&f.showSourceFilter,U=X&&f.showTargetFilter,Y=function(e,t){if(e){var n=a.findSingle(e,".p-picklist-list");switch(t){case"up":q(n,-1);break;case"top":n.scrollTop=0;break;case"down":q(n,1);break;case"bottom":setTimeout((function(){return n.scrollTop=n.scrollHeight}),100)}}},$=function(e,t,n){f.onChange&&f.onChange({originalEvent:e.originalEvent,source:t,target:n})},q=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,n=e.getElementsByClassName("p-highlight");i.isNotEmpty(n)&&a.scrollInView(e,-1===t?n[0]:n[n.length-1])},z=function(e,t,n){"sourceSelection"===t?d(e.value):S(e.value),n&&n(e),i.isNotEmpty(H)&&"targetSelection"===t?d([]):i.isNotEmpty(V)&&"sourceSelection"===t&&S([])},G=function(e){var t=e.originalEvent,n=e.value,r=h("source"===e.type?[C,f.onSourceFilterChange]:[K,f.onTargetFilterChange],2),l=r[0],i=r[1];i?i({originalEvent:t,value:n}):l(n)},Q=function(e,t){var n=h("source"===t?[B,W]:[J,Z],2),r=n[0],l=n[1];return X&&i.isNotEmpty(r)?l(r):e},W=function(){var e=(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"").trim().toLocaleLowerCase(f.filterLocale);return ee(f.source,e)},Z=function(){var e=(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"").trim().toLocaleLowerCase(f.filterLocale);return ee(f.target,e)},ee=function(e,t){var r=X?f.filterBy.split(","):[];return n.filter(e,r,t,f.filterMatchMode,f.filterLocale)},te=function(){if(!j.current){j.current=a.createInlineStyle(t.nonce);var e="\n@media screen and (max-width: ".concat(f.breakpoint,") {\n .p-picklist[").concat(F,"] {\n flex-direction: column;\n }\n\n .p-picklist[").concat(F,"] .p-picklist-buttons {\n padding: var(--content-padding);\n flex-direction: row;\n }\n\n .p-picklist[").concat(F,"] .p-picklist-buttons .p-button {\n margin-right: var(--inline-spacing);\n margin-bottom: 0;\n }\n\n .p-picklist[").concat(F,"] .p-picklist-buttons .p-button:last-child {\n margin-right: 0;\n }\n\n .p-picklist[").concat(F,'] .pi-angle-right:before {\n content: "\\e930"\n }\n\n .p-picklist[').concat(F,'] .pi-angle-double-right:before {\n content: "\\e92c"\n }\n\n .p-picklist[').concat(F,'] .pi-angle-left:before {\n content: "\\e933"\n }\n\n .p-picklist[').concat(F,'] .pi-angle-double-left:before {\n content: "\\e92f"\n }\n}\n');j.current.innerHTML=e}};e.useImperativeHandle(u,(function(){return{props:f,getElement:function(){return R.current}}})),r((function(){!F&&P(c())})),l((function(){return F&&(R.current.setAttribute(F,""),te()),function(){j.current=a.removeInlineStyle(j.current)}}),[F,f.breakpoint]),l((function(){D.current&&(Y(D.current,O.current),D.current=null,O.current=null)}));var ne=y.getOtherProps(f),re=o("p-picklist p-component",f.className),le=f.sourceItemTemplate?f.sourceItemTemplate:f.itemTemplate,ie=f.targetItemTemplate?f.targetItemTemplate:f.itemTemplate,oe=Q(f.source,"source"),ae=Q(f.target,"target");return e.createElement("div",p({id:f.id,ref:R,className:re,style:f.style},ne),f.showSourceControls&&e.createElement(k,{list:f.source,selection:H,onReorder:function(e){$(e,e.value,f.target),D.current=M.current.listElementRef.current,O.current=e.direction},className:"p-picklist-source-controls",dataKey:f.dataKey}),e.createElement(I,{ref:M,type:"source",list:oe,selection:H,onSelectionChange:function(e){return z(e,"sourceSelection",f.onSourceSelectionChange)},itemTemplate:le,header:f.sourceHeader,style:f.sourceStyle,className:"p-picklist-source-wrapper",listClassName:"p-picklist-source",metaKeySelection:f.metaKeySelection,tabIndex:f.tabIndex,dataKey:f.dataKey,filterValue:B,onFilter:G,showFilter:_,placeholder:f.sourceFilterPlaceholder,template:f.sourceFilterTemplate}),e.createElement(w,{onTransfer:function(e){var t=e.originalEvent,n=e.source,r=e.target,l=[];switch(e.direction){case"toTarget":l=H,f.onMoveToTarget&&f.onMoveToTarget({originalEvent:t,value:l});break;case"allToTarget":l=f.source,f.onMoveAllToTarget&&f.onMoveAllToTarget({originalEvent:t,value:l});break;case"toSource":l=V,f.onMoveToSource&&f.onMoveToSource({originalEvent:t,value:l});break;case"allToSource":l=f.target,f.onMoveAllToSource&&f.onMoveAllToSource({originalEvent:t,value:l})}z({originalEvent:t,value:l},"sourceSelection",f.onSourceSelectionChange),z({originalEvent:t,value:l},"targetSelection",f.onTargetSelectionChange),$(e,n,r)},source:f.source,visibleSourceList:oe,target:f.target,visibleTargetList:ae,sourceSelection:H,targetSelection:V,dataKey:f.dataKey}),e.createElement(I,{ref:A,type:"target",list:ae,selection:V,onSelectionChange:function(e){return z(e,"targetSelection",f.onTargetSelectionChange)},itemTemplate:ie,header:f.targetHeader,style:f.targetStyle,className:"p-picklist-target-wrapper",listClassName:"p-picklist-target",metaKeySelection:f.metaKeySelection,tabIndex:f.tabIndex,dataKey:f.dataKey,filterValue:J,onFilter:G,showFilter:U,placeholder:f.targetFilterPlaceholder,template:f.targetFilterTemplate}),f.showTargetControls&&e.createElement(k,{list:f.target,selection:V,onReorder:function(e){$(e,f.source,e.value),D.current=A.current.listElementRef.current,O.current=e.direction},className:"p-picklist-target-controls",dataKey:f.dataKey}))})));N.displayName="PickList";export{N as PickList};