UNPKG

primereact

Version:

PrimeReact is an open source UI library for React featuring a rich set of 80+ 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) 9.16 kB
import*as e from"react";import{useUpdateEffect as t}from"primereact/hooks";import{FilterService as r}from"primereact/api";import{ObjectUtils as n,DomHandler as l,classNames as i}from"primereact/utils";import{Button as o}from"primereact/button";import{Ripple as a}from"primereact/ripple";function u(){return u=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},u.apply(this,arguments)}function c(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function s(e){if(Array.isArray(e))return c(e)}function f(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 c(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?c(e,t):void 0}}function d(){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 m(e){return s(e)||f(e)||p(e)||d()}function v(e){if(Array.isArray(e))return e}function g(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,l,i=[],o=!0,a=!1;try{for(r=r.call(e);!(o=(n=r.next()).done)&&(i.push(n.value),!t||i.length!==t);o=!0);}catch(e){a=!0,l=e}finally{try{o||null==r.return||r.return()}finally{if(a)throw l}}return i}}function h(){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 y(e,t){return v(e)||g(e,t)||p(e,t)||h()}var b=e.memo((function(t){return e.createElement("div",{className:"p-orderlist-controls"},e.createElement(o,{type:"button",icon:"pi pi-angle-up",onClick:function(e){if(t.selection){for(var r=m(t.value),l=0;l<t.selection.length;l++){var i=n.findIndexInList(t.selection[l],r,t.dataKey);if(0===i)break;var o=r[i-1];r[i-1]=r[i],r[i]=o}t.onReorder&&t.onReorder({originalEvent:e,value:r,direction:"up"})}}}),e.createElement(o,{type:"button",icon:"pi pi-angle-double-up",onClick:function(e){if(t.selection){for(var r=m(t.value),l=0;l<t.selection.length;l++){var i=n.findIndexInList(t.selection[l],r,t.dataKey);if(0===i)break;var o=r.splice(i,1)[0];r.unshift(o)}t.onReorder&&t.onReorder({originalEvent:e,value:r,direction:"top"})}}}),e.createElement(o,{type:"button",icon:"pi pi-angle-down",onClick:function(e){if(t.selection){for(var r=m(t.value),l=t.selection.length-1;l>=0;l--){var i=n.findIndexInList(t.selection[l],r,t.dataKey);if(i===r.length-1)break;var o=r[i+1];r[i+1]=r[i],r[i]=o}t.onReorder&&t.onReorder({originalEvent:e,value:r,direction:"down"})}}}),e.createElement(o,{type:"button",icon:"pi pi-angle-double-down",onClick:function(e){if(t.selection){for(var r=m(t.value),l=t.selection.length-1;l>=0;l--){var i=n.findIndexInList(t.selection[l],r,t.dataKey);if(i===r.length-1)break;var o=r.splice(i,1)[0];r.push(o)}t.onReorder&&t.onReorder({originalEvent:e,value:r,direction:"bottom"})}}}))}));b.displayName="OrderListControls";var E=e.memo((function(t){var r,o=e.useRef(null),u=e.useRef(null),c=e.useRef(null),s=e.useRef(null),f={filter:function(e){return t.onFilterInputChange(e)},reset:function(){return t.resetFilter()}},p=function(e){return-1!==n.findIndexInList(e,t.selection,t.dataKey)},d=function(e,t){u.current!==t&&u.current+1!==t&&(c.current=t,l.addClass(e.target,"p-orderlist-droppoint-highlight"),e.preventDefault())},v=function(e){c.current=null,l.removeClass(e.target,"p-orderlist-droppoint-highlight")},g=function(e){var r=u.current>c.current?c.current:0===c.current?0:c.current-1,i=m(t.value);n.reorderArray(i,u.current,r),c.current=null,l.removeClass(e.target,"p-orderlist-droppoint-highlight"),t.onChange&&t.onChange({originalEvent:e,value:i})},h=function(e){o.current=!1},y=function(e){if(o.current){var t=s.current.getBoundingClientRect().top+l.getWindowScrollTop(),r=t+s.current.clientHeight-e.pageY,n=e.pageY-t;r<25&&r>0?s.current.scrollTop+=15:n<25&&n>0&&(s.current.scrollTop-=15)}},b=function(e){13===e.which&&e.preventDefault()},E=function(t,r){return e.createElement("li",{key:r,className:"p-orderlist-droppoint",onDragOver:function(e){return d(e,t+1)},onDragLeave:v,onDrop:g})},I=t.header?e.createElement("div",{className:"p-orderlist-header"},t.header):null,C=function(){if(t.filter){var r=e.createElement("div",{className:"p-orderlist-filter"},e.createElement("input",{type:"text",value:t.filterValue,onChange:t.onFilter,onKeyDown:b,placeholder:t.placeholder,className:"p-orderlist-filter-input p-inputtext p-component"}),e.createElement("span",{className:"p-orderlist-filter-icon pi pi-search"}));if(t.filterTemplate)r=n.getJSXElement(t.filterTemplate,{className:"p-orderlist-filter",inputProps:{inputClassName:"p-orderlist-filter-input p-inputtext p-component",onChange:t.onFilter,onKeyDown:b},filterOptions:f,iconClassName:"p-orderlist-filter-icon pi pi-search",element:r,props:t});return e.createElement("div",{className:"p-orderlist-filter-container"},r)}return null}(),w=(r=t.value?t.value.map((function(r,n){var l=t.itemTemplate?t.itemTemplate(r):r,c=i("p-orderlist-item",{"p-highlight":p(r)},t.className),s=JSON.stringify(r);if(t.dragdrop){var f=[E(n,s+"_droppoint"),e.createElement("li",{key:s,className:c,onClick:function(e){return t.onItemClick({originalEvent:e,value:r,index:n})},onKeyDown:function(e){return t.onItemKeyDown({originalEvent:e,value:r,index:n})},role:"option","aria-selected":p(r),draggable:"true",onDragStart:function(e){return r=e,l=n,o.current=!0,u.current=l,void(t.dragdropScope&&r.dataTransfer.setData("text","orderlist"));var r,l},onDragEnd:h,tabIndex:t.tabIndex},l,e.createElement(a,null))];return n===t.value.length-1&&f.push(E(r,n)),f}return e.createElement("li",{key:s,className:c,role:"option","aria-selected":p(r),onClick:function(e){return t.onItemClick({originalEvent:e,value:r,index:n})},onKeyDown:function(e){return t.onItemKeyDown({originalEvent:e,value:r,index:n})},tabIndex:t.tabIndex},l)})):null,e.createElement("ul",{ref:s,className:"p-orderlist-list",style:t.listStyle,onDragOver:y,role:"listbox","aria-multiselectable":!0},r));return e.createElement("div",{className:"p-orderlist-list-container"},I,C,w)}));E.displayName="OrderListSubList";var I=e.memo(e.forwardRef((function(o,a){var c=y(e.useState([]),2),s=c[0],f=c[1],p=y(e.useState(""),2),d=p[0],v=p[1],g=n.isNotEmpty(d),h=e.useRef(null),C=e.useRef(null),w=function(e){var t,r=e.originalEvent.metaKey||e.originalEvent.ctrlKey,l=n.findIndexInList(e.value,s,o.dataKey);t=-1!==l?r?s.filter((function(e,t){return t!==l})):[e.value]:r?[].concat(m(s),[e.value]):[e.value],f(t)},x=function e(t){var r=t.nextElementSibling;return r?l.hasClass(r,"p-orderlist-item")?r:e(r):null},N=function e(t){var r=t.previousElementSibling;return r?l.hasClass(r,"p-orderlist-item")?r:e(r):null},S=function(){var e=l.findSingle(h.current,".p-orderlist-list"),t=l.find(e,".p-orderlist-item.p-highlight");if(t&&t.length)switch(C.current){case"up":l.scrollInView(e,t[0]);break;case"top":e.scrollTop=0;break;case"down":l.scrollInView(e,t[t.length-1]);break;case"bottom":setTimeout((function(){return e.scrollTop=e.scrollHeight}),100)}};e.useImperativeHandle(a,(function(){return{props:o,getElement:function(){return h.current}}})),t((function(){C.current&&(S(),C.current=null)}));var k=n.findDiffKeys(o,I.defaultProps),K=i("p-orderlist p-component",o.className),T=function(){if(g){var e=d.trim().toLocaleLowerCase(o.filterLocale),t=o.filterBy?o.filterBy.split(","):[];return r.filter(o.value,t,e,o.filterMatchMode,o.filterLocale)}return o.value}();return e.createElement("div",u({ref:h,id:o.id,className:K,style:o.style},k),e.createElement(b,{value:T,selection:s,onReorder:function(e){o.onChange&&o.onChange({event:e.originalEvent,value:e.value}),C.current=e.direction},dataKey:o.dataKey}),e.createElement(E,{value:T,selection:s,onItemClick:w,onItemKeyDown:function(e){var t=e.originalEvent,r=t.currentTarget;switch(t.which){case 40:var n=x(r);n&&n.focus(),t.preventDefault();break;case 38:var l=N(r);l&&l.focus(),t.preventDefault();break;case 13:w(e),t.preventDefault()}},onFilterInputChange:function(e){var t=e.target.value;v(t),o.onFilter&&o.onFilter({originalEvent:e,filter:t})},itemTemplate:o.itemTemplate,filter:o.filter,onFilter:function(e){var t=e.target.value;v(t),o.onFilter&&o.onFilter({originalEvent:e,value:t})},resetFilter:function(){v(""),o.onFilter&&o.onFilter({filter:""})},filterTemplate:o.filterTemplate,header:o.header,listStyle:o.listStyle,dataKey:o.dataKey,dragdrop:o.dragdrop,onChange:o.onChange,tabIndex:o.tabIndex}))})));I.displayName="OrderList",I.defaultProps={__TYPE:"OrderList",id:null,value:null,header:null,style:null,className:null,listStyle:null,dragdrop:!1,tabIndex:0,dataKey:null,onChange:null,itemTemplate:null,filter:!1,filterBy:null,filterMatchMode:"contains",filterLocale:void 0,filterPlaceholder:null,filterTemplate:null,onFilter:null};export{I as OrderList};