flexitablesort 1.0.6 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/useDragContextEvents.d.ts +1 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +202 -142
- package/package.json +73 -73
package/dist/index.cjs.js
CHANGED
|
@@ -12,4 +12,4 @@ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.
|
|
|
12
12
|
user-select: none !important;
|
|
13
13
|
-webkit-user-select: none !important;
|
|
14
14
|
}
|
|
15
|
-
`,m=(0,c.createContext)(void 0),h=()=>{let e=(0,c.useContext)(m);if(e===void 0)throw Error(`useTable must be used within a TableProvider`);return e},g=e=>{let t=(0,c.useRef)(!1),n=(0,c.useRef)(!1),r=(0,c.useRef)(0),i=(0,c.useRef)(null),a=(0,c.useCallback)((e,o,s)=>{let c=s===`vertical`,l=c?o.scrollHeight-o.clientHeight:o.scrollWidth-o.clientWidth;c?o.scrollTop+=e:o.scrollLeft+=e,c&&(o.scrollTop>=l||o.scrollTop<=0)||!c&&(o.scrollLeft>=l||o.scrollLeft<=0)?i.current!==null&&cancelAnimationFrame(i.current):n.current||t.current?(i.current=requestAnimationFrame(()=>a(e+r.current,o,s)),r.current+=e/1e3):i.current!==null&&cancelAnimationFrame(i.current)},[]);return{startAutoScroll:(0,c.useCallback)((e,i,o)=>{let s=o===`vertical`;!n.current&&!t.current&&(s?n.current=!0:t.current=!0,r.current=0,a(e,i,o))},[a]),stopAutoScroll:(0,c.useCallback)(()=>{n.current=!1,t.current=!1,i.current!==null&&cancelAnimationFrame(i.current)},[]),isAutoScrollingHorizontal:t,isAutoScrollingVertical:n,BodyScrollHandle:(0,c.useCallback)(t=>{e.headerRef?.current&&t.currentTarget&&(e.headerRef.current.scrollLeft=t.currentTarget.scrollLeft)},[e]),HeaderScrollHandle:(0,c.useCallback)(t=>{e.bodyRef?.current&&t.currentTarget&&(e.bodyRef.current.scrollLeft=t.currentTarget.scrollLeft)},[e])}},_=(e,t)=>{let n=e,r=0,i=t.length-1;for(;r<i;){let e=Math.floor((r+i)/2),a=t[e];if(a.itemTop<=n&&n<=a.itemBottom)return n<a.itemTop+a.height/2?+a.index:+a.index+1;n<a.itemTop?i=e-1:r=e+1}return+t[r].index},v=(e,t)=>{let n=e,r=0,i=t.length-1;for(;r<i;){let e=Math.floor((r+i)/2),a=t[e];if(a.itemLeft<=n&&n<=a.itemRight)return n<a.itemLeft+a.width/2?+a.index:+a.index+1;n<a.itemLeft?i=e-1:r=e+1}return+t[r].index},y=(e,t,n)=>t!==void 0&&e<t||n!==void 0&&e>n,b=`all 450ms cubic-bezier(0.2, 0, 0, 1)`,x=(e,t,n,r,i,a)=>{let{startAutoScroll:o,stopAutoScroll:s,isAutoScrollingHorizontal:l,isAutoScrollingVertical:u}=g(e),d=(0,c.useRef)(null),f=(0,c.useRef)(null),p=(0,c.useRef)(null),m=(0,c.useRef)({x:0,y:0}),h=(0,c.useRef)(null),y=(0,c.useRef)(null),x=(0,c.useRef)({width:0,height:0}),S=(0,c.useCallback)(()=>{let t=e.bodyRef.current;if(!t)return null;let n=t.scrollTop,r=t.getBoundingClientRect().top,a=t.querySelectorAll(`.draggable[data-type="row"]`),o=[];for(let e=0;e<a.length;e++){let t=a[e];if(t.dataset.index===void 0)continue;let i=t.getBoundingClientRect(),s=i.top-r+n;o.push({height:i.height,itemTop:s,itemBottom:s+i.height,index:t.dataset.index})}let s=i.rowDragRange.start,c=i.rowDragRange.end;return(s||c)&&(o=o.filter(e=>(!s||e.index>=s)&&(!c||e.index<c))),o},[e.bodyRef,i.rowDragRange]),C=(0,c.useCallback)(()=>{let t=e.headerRef.current;if(!t||!t.children[0])return null;let n=Array.from(t.children[0].children).map(e=>{let t=e.getBoundingClientRect();return{left:t.left,width:t.width,itemLeft:t.left,itemRight:t.left+t.width,index:e.dataset.index}}).filter(e=>e.index!==void 0),r=i.columnDragRange?.start,a=i.columnDragRange?.end;return(r!==void 0||a!==void 0)&&(n=n.filter(e=>{let t=+e.index;return(r===void 0||t>=r)&&(a===void 0||t<a)})),n},[e.headerRef,i.columnDragRange]),w=(0,c.useCallback)((t,n,r,i)=>{let a=e.placeholderRef?.current;if(!a||!t){a&&(a.style.display=`none`);return}let o=x.current,s=t.getBoundingClientRect();a.style.display=`block`;let c=n<r;if(i===`row`){let e=c?s.top+s.height-o.height:s.top;a.style.top=`${e}px`,a.style.left=`${s.left}px`,a.style.width=`${s.width}px`,a.style.height=`${o.height}px`}else{let t=(e.tableRef?.current)?.getBoundingClientRect(),n=c?s.left+s.width-o.width:s.left;a.style.top=`${t?.top??s.top}px`,a.style.left=`${n}px`,a.style.width=`${o.width}px`,a.style.height=`${t?.height??s.height}px`}},[e.placeholderRef,e.tableRef]),T=(0,c.useCallback)((t,n,r)=>{if(t===null||n===null)return;let i=x.current,a=null;if(r===`row`){let r=e.bodyRef.current;if(!r)return;let o=r.querySelectorAll(`.draggable[data-type="row"]`);for(let e=0;e<o.length;e++){let r=o[e],s=+r.dataset.index,c=r.firstElementChild;if(!c)continue;let l=``;s>t&&s<=n?l=`translateY(-${i.height}px)`:s<t&&s>=n&&(l=`translateY(${i.height}px)`),c.style.transform=l,c.style.transition=s===t?`none`:b,s===n?(r.setAttribute(`data-drop-target`,`true`),a=r):r.removeAttribute(`data-drop-target`)}}else if(r===`column`){let r=e.headerRef.current;if(r){let e=r.querySelectorAll(`.draggable[data-type="column"]`);for(let r=0;r<e.length;r++){let o=e[r],s=+o.dataset.index,c=o.firstElementChild;if(!c)continue;let l=``;s>t&&s<=n?l=`translateX(-${i.width}px)`:s<t&&s>=n&&(l=`translateX(${i.width}px)`),c.style.transform=l,c.style.transition=s===t?`none`:b,s===n?(o.setAttribute(`data-drop-target`,`true`),a=o):o.removeAttribute(`data-drop-target`)}}let o=e.bodyRef.current;if(o){let e=o.querySelectorAll(`.td[data-col-index]`);for(let r=0;r<e.length;r++){let a=e[r],o=+a.dataset.colIndex,s=``;o>t&&o<=n?s=`translateX(-${i.width}px)`:o<t&&o>=n&&(s=`translateX(${i.width}px)`),a.style.transform=s,a.style.transition=b}}}w(a,t,n,r)},[e.bodyRef,e.headerRef,e.placeholderRef,w]),E=(0,c.useCallback)(()=>{let t=e.placeholderRef?.current;t&&(t.style.display=`none`);let n=e.bodyRef.current;if(n){let e=n.querySelectorAll(`.draggable`);for(let t=0;t<e.length;t++){e[t].removeAttribute(`data-drop-target`);let n=e[t].firstElementChild;n&&(n.style.transform=``,n.style.transition=``)}let t=n.querySelectorAll(`.td[data-col-index]`);for(let e=0;e<t.length;e++)t[e].style.transform=``,t[e].style.transition=``}let r=e.headerRef.current;if(r){let e=r.querySelectorAll(`.draggable`);for(let t=0;t<e.length;t++){e[t].removeAttribute(`data-drop-target`);let n=e[t].firstElementChild;n&&(n.style.transform=``,n.style.transition=``)}}},[e.bodyRef,e.headerRef]),D=(0,c.useCallback)(t=>{if(t.target!==t.currentTarget){let r=!1,i=(e=>{for(;e;){if(e.dataset?.dragHandle===`true`&&(r=!0),e.dataset?.contextid||e.dataset?.disabled===`true`)return null;if(e.dataset?.id)return e;e=e.parentNode}return null})(t.target);if(!i||!r&&i.querySelector(`[data-drag-handle]`))return;let a=i.dataset.id,o=+i.dataset.index,s=i.dataset.type;p.current=s,h.current=o,y.current=null;let c=s===`row`?e.bodyRef.current.scrollLeft:0,l=i.getBoundingClientRect();x.current={width:l.width,height:l.height};let u={x:0,y:0};t.type===`touchstart`?(u.x=t.touches[0].clientX,u.y=t.touches[0].clientY):(u.x=t.clientX-l.left-c,u.y=t.clientY-l.top),m.current=u;let g={x:l.left+c,y:l.top};s===`row`?d.current=S():d.current=C();let _=e.bodyRef.current;_&&(f.current=_.getBoundingClientRect());let v=e.cloneRef?.current;v&&(v.style.transform=`translate(${g.x}px, ${g.y}px)`),requestAnimationFrame(()=>{n({type:`dragStart`,value:{rect:{draggedItemHeight:l.height,draggedItemWidth:l.width},dragged:{initial:u,translate:g,draggedID:a,isDragging:!0,sourceIndex:o},dragType:s}}),requestAnimationFrame(()=>{let t=e.cloneRef?.current,n=e.bodyRef.current;if(t&&n){if(s===`row`)t.scrollLeft=n.scrollLeft;else if(s===`column`){let e=t.querySelector(`.clone-body`);e&&(e.scrollTop=n.scrollTop)}}})})}},[n,e,S,C]),O=(0,c.useCallback)(()=>{let t=y.current,r=h.current,i=p.current;d.current=null,f.current=null;let o=e.cloneRef?.current;o&&(o.style.transform=`translate(0px, 0px)`,o.scrollLeft=0),E(),a&&r!==null&&t!==null&&i&&a({sourceIndex:r,targetIndex:t,dragType:i}),n({type:`dragEnd`,value:{targetIndex:t,sourceIndex:r}}),s(),p.current=null,h.current=null,y.current=null},[n,s,e.cloneRef,E,a]),k=(0,c.useCallback)(()=>{d.current=null,f.current=null;let e=h.current,t=y.current,n=p.current;e!==null&&t!==null&&T(e,t,n)},[T]),A=(0,c.useCallback)(t=>{t.stopPropagation();let n=m.current,i,a;t.type===`touchmove`?(i=t.touches[0].clientX-n.x,a=t.touches[0].clientY-n.y):(i=t.clientX-n.x,a=t.clientY-n.y);let c=e.cloneRef?.current;if(c){c.style.transform=`translate(${i}px, ${a}px)`;let t=e.bodyRef.current;if(t){if(p.current===`row`)c.scrollLeft=t.scrollLeft;else if(p.current===`column`){let e=c.querySelector(`.clone-body`);e&&(e.scrollTop=t.scrollTop)}}}u.current=!1,l.current=!1;let g=e.bodyRef.current;if(!g)return;let{clientY:b,clientX:x}=t,w=f.current;w||(w=g.getBoundingClientRect(),f.current=w);let{top:E,bottom:D,left:O,right:k}=w,A=0,j=p.current||r;if(j===`row`){let e=g.scrollTop,t=w.top,n=d.current;n||(n=S(),d.current=n),n&&n.length>0&&(A=_(b-t+e,n)),b<E+30?(o(-5,g,`vertical`),d.current=null):b>D-30?(o(5,g,`vertical`),d.current=null):s()}else{let e=d.current;e||(e=C(),d.current=e),e&&e.length>0&&(A=v(x,e)),x<O+30?(o(-5,g,`horizontal`),d.current=null):x>k-30?(o(5,g,`horizontal`),d.current=null):s()}A!==y.current&&(y.current=A,requestAnimationFrame(()=>{T(h.current,A,j)}))},[r,l,u,e.bodyRef,e.cloneRef,S,C,o,s,T]),j=(0,c.useCallback)(()=>{d.current=null,f.current=null;let t=e.cloneRef?.current;t&&(t.style.transform=`translate(0px, 0px)`,t.scrollLeft=0),E(),n({type:`dragEnd`,value:{targetIndex:null,sourceIndex:null}}),s(),p.current=null,h.current=null,y.current=null},[n,s,e.cloneRef,E]),M=(0,c.useCallback)(e=>{e.key===`Escape`&&j()},[j]);return(0,c.useEffect)(()=>{if(t.isDragging){let t=e.bodyRef.current;return window.addEventListener(`mousemove`,A,{passive:!0}),window.addEventListener(`touchmove`,A,!1),window.addEventListener(`touchend`,O,!1),window.addEventListener(`mouseup`,O,!1),window.addEventListener(`keydown`,M),t?.addEventListener(`scroll`,k,{passive:!0}),()=>{window.removeEventListener(`mousemove`,A),window.removeEventListener(`touchmove`,A,!1),window.removeEventListener(`touchend`,O,!1),window.removeEventListener(`mouseup`,O,!1),window.removeEventListener(`keydown`,M),t?.removeEventListener(`scroll`,k)}}},[t.isDragging,A,O,j,M,k,e.bodyRef]),{dragStart:D}},S={columnDragRange:{start:void 0,end:void 0},rowDragRange:{start:void 0,end:void 0},defaultSizing:50};function C(e,t){switch(t.type){case`setClone`:return{...e,clone:t.value};case`setDragged`:return{...e,dragged:{...e.dragged,...t.value}};case`setDragType`:return{...e,dragType:t.value};case`setRect`:return{...e,rect:t.value};case`setTableDimensions`:return{...e,tableDimensions:t.value};case`setTableRef`:return{...e,refs:{...e.refs,tableRef:t.value}};case`setBodyRef`:return{...e,refs:{...e.refs,bodyRef:t.value}};case`setHeaderRef`:return{...e,refs:{...e.refs,headerRef:t.value}};case`setCloneRef`:return{...e,refs:{...e.refs,cloneRef:t.value}};case`setPlaceholderRef`:return{...e,refs:{...e.refs,placeholderRef:t.value}};case`setBodyScrollBarWidth`:return{...e,bodyScrollBarWidth:t.value};case`setWidths`:return{...e,widths:t.value};case`setColumnIds`:return{...e,columnIds:t.value};case`setOptions`:return{...e,options:t.value??S};case`dragStart`:return{...e,rect:t.value.rect,dragged:{...e.dragged,...t.value.dragged},dragType:t.value.dragType};case`dragEnd`:return{...e,clone:null,dragged:{initial:{x:0,y:0},translate:{x:0,y:0},isDragging:!1,draggedID:null,targetIndex:t.value?.targetIndex??null,sourceIndex:t.value?.sourceIndex??null},dragType:null,rect:{draggedItemWidth:0,draggedItemHeight:0}};default:throw Error(`Unhandled action type: ${t.type}`)}}var w={clone:null,dragged:{initial:{x:0,y:0},translate:{x:0,y:0},isDragging:!1,draggedID:null,targetIndex:null,sourceIndex:null},dragType:null,rect:{draggedItemWidth:0,draggedItemHeight:0},tableDimensions:{height:0,width:0},refs:{tableRef:null,bodyRef:null,headerRef:null,cloneRef:null,placeholderRef:null},bodyScrollBarWidth:0,options:S,widths:[],columnIds:[]},T={position:`relative`,display:`flex`,flexFlow:`column`},E={position:`fixed`,pointerEvents:`none`,zIndex:3,top:0,left:0,display:`none`},D=(0,c.forwardRef)(({children:e,className:t,style:n,options:r,onDragEnd:i,renderPlaceholder:a},o)=>{let s=(0,c.useRef)(null),l=(0,c.useRef)(null),d=(0,c.useRef)(null);(0,c.useImperativeHandle)(o,()=>s.current,[]);let[f,h]=(0,c.useReducer)(C,w),g=(0,c.useMemo)(()=>({state:f,dispatch:h}),[f]);(0,c.useEffect)(()=>{h({type:`setTableRef`,value:s}),h({type:`setCloneRef`,value:l}),h({type:`setPlaceholderRef`,value:d})},[s]),(0,c.useEffect)(()=>{let e=()=>{s.current&&h({type:`setTableDimensions`,value:{height:s.current.offsetHeight,width:s.current.offsetWidth}})};return e(),window.addEventListener(`resize`,e),()=>{window.removeEventListener(`resize`,e)}},[s]),(0,c.useEffect)(()=>{h({type:`setOptions`,value:r})},[r]);let{dragStart:_}=x(f.refs,f.dragged,h,f.dragType,f.options,i),v=(0,c.useMemo)(()=>({position:`fixed`,zIndex:`5`,pointerEvents:`none`,top:0,left:0,display:`flex`,flexDirection:`column`,height:f.dragType===`row`?f.rect.draggedItemHeight:`${f.tableDimensions.height-20}px`,width:f.dragType===`column`?`${f.rect.draggedItemWidth}px`:`${f.tableDimensions.width-20}px`,overflow:`hidden`,boxShadow:f.dragged.isDragging?`0 0 10px 0 rgba(0, 0, 0, 0.1)`:`none`}),[f.dragType,f.dragged.isDragging,f.rect.draggedItemHeight,f.rect.draggedItemWidth,f.tableDimensions.height,f.tableDimensions.width]);return(0,u.jsx)(m.Provider,{value:g,children:(0,u.jsxs)(p,{className:f.dragged.isDragging?`is-dragging`:``,children:[(0,u.jsx)(`div`,{id:`portalroot`,style:v,ref:l,children:f.clone}),a&&(0,u.jsx)(`div`,{ref:d,style:E,children:a()}),(0,u.jsx)(`div`,{"data-contextid":`context`,ref:s,onMouseDown:_,onTouchStart:_,style:{...T,...n},className:`table ${t??``}`,children:e})]})})});D.displayName=`TableProvider`;var O=(0,c.forwardRef)(({children:e,style:t,className:n},r)=>{let i=(0,c.useRef)(null),a=r||i,{state:o,dispatch:s}=h();(0,c.useEffect)(()=>{s({type:`setHeaderRef`,value:a})},[s,a]);let{HeaderScrollHandle:l}=g(o.refs),d={display:`flex`,flex:`1 0 auto`},f=(0,c.useMemo)(()=>({overflow:`hidden`,display:`flex`,paddingRight:`${o.bodyScrollBarWidth}px`,userSelect:o.dragged.isDragging?`none`:`auto`,...t}),[o.bodyScrollBarWidth,o.dragged.isDragging,t]);return(0,c.useEffect)(()=>{a.current&&s({type:`setWidths`,value:Array.from(a.current.querySelectorAll(`.th`)).map(e=>{let t=e.getAttribute(`data-width`);return t?parseInt(t,10):null})})},[e,s,a]),(0,c.useEffect)(()=>{a.current&&s({type:`setColumnIds`,value:Array.from(a.current.querySelectorAll(`.draggable`)).map(e=>e.getAttribute(`data-id`))})},[e,s,a]),(0,u.jsx)(`div`,{className:`header ${n??``}`,children:(0,u.jsx)(`div`,{className:`thead`,style:f,"data-droppableid":`header`,onScroll:l,ref:a,children:(0,u.jsx)(`div`,{style:d,className:`tr`,children:e})})})});O.displayName=`TableHeader`;var k=(0,c.memo)(({children:e,id:t,index:n,type:r,styles:i={}})=>{let{state:a,dispatch:o}=h(),s=(0,c.useMemo)(()=>String(t)===String(a.dragged.draggedID)&&a.dragged.isDragging,[t,a.dragged.draggedID,a.dragged.isDragging]),l=(0,c.useMemo)(()=>r===`row`?y(n,a.options.rowDragRange.start,a.options.rowDragRange.end):y(n,a.options.columnDragRange.start,a.options.columnDragRange.end),[n,a.options.columnDragRange.end,a.options.columnDragRange.start,a.options.rowDragRange.end,a.options.rowDragRange.start,r]),d=(0,c.useRef)(null),f=(0,c.useRef)(!1);(0,c.useEffect)(()=>{d.current&&(f.current=!!d.current.querySelector(`[data-drag-handle]`))});let p=(0,c.useMemo)(()=>({cursor:s?`-webkit-grabbing`:l||f.current?`auto`:`-webkit-grab`,zIndex:s?2:1,opacity:s?0:1,pointerEvents:s?`none`:`auto`,display:`flex`}),[l,s]),m=t=>{l||requestAnimationFrame(()=>{o({type:`setClone`,value:c.default.cloneElement(e)})})};return(0,u.jsx)(`div`,{className:`draggable`,"data-id":t,"data-index":n,"data-type":r,onMouseDown:m,"data-disabled":l?`true`:`false`,style:i,children:(0,u.jsx)(`div`,{ref:d,style:p,children:e})})});k.displayName=`Draggable`;var A=(0,c.memo)(({children:e,width:t,style:n,className:r,...i})=>{let{state:a}=h(),o=(0,c.useMemo)(()=>t??a.options.defaultSizing,[t,a.options.defaultSizing]),s=(0,c.useMemo)(()=>({width:`${o}px`,flex:`${o} 0 auto`}),[o]);return(0,u.jsx)(k,{...i,styles:s,type:`column`,children:(0,u.jsx)(`div`,{className:`th ${r??``}`,"data-width":t,style:{width:`100%`,...n},children:e})})});A.displayName=`ColumnCell`;var j=(0,c.forwardRef)(({children:e,style:t,className:n},r)=>{let i=(0,c.useRef)(null);(0,c.useImperativeHandle)(r,()=>i.current,[]);let{state:a,dispatch:o}=h(),s=(0,c.useMemo)(()=>{if(a.dragged.sourceIndex===null)return null;let t=e=>{let n=[];return c.default.Children.forEach(e,e=>{c.default.isValidElement(e)&&(e.props.id!==void 0&&e.props.index!==void 0&&e.props.children?n.push(e):e.props.children&&n.push(...t(e.props.children)))}),n};return t(e).map(e=>{let t=c.default.Children.toArray(e.props.children).filter(e=>c.default.isValidElement(e)&&String(e.props.index)===String(a.dragged.sourceIndex)).map(e=>c.default.cloneElement(e,{isClone:!0}));return c.default.cloneElement(e,{...e.props,children:t})})},[e,a.dragged.sourceIndex]);(0,c.useEffect)(()=>{o({type:`setBodyRef`,value:i})},[o,i]);let{BodyScrollHandle:l}=g(a.refs),f=(0,c.useMemo)(()=>({overflowX:`auto`,overflowY:`auto`,flex:1,userSelect:a.dragged.isDragging?`none`:`auto`,...t}),[a.dragged.isDragging,t]);(0,c.useEffect)(()=>{if(i.current){let e=i.current.clientWidth;o({type:`setBodyScrollBarWidth`,value:i.current.offsetWidth-e})}},[o,i]);let p=i.current?.scrollHeight??0;return(0,u.jsxs)(c.default.Fragment,{children:[a.dragType===`column`&&a.refs.cloneRef?.current&&(0,d.createPortal)((0,u.jsx)(`div`,{className:`body clone-body`,"data-droppableid":`body`,style:{overflow:`hidden`,flex:1},children:(0,u.jsx)(`div`,{className:`rbody`,style:{height:p,position:`relative`},children:s})}),a.refs.cloneRef.current),(0,u.jsx)(`div`,{className:`body ${n??``}`,style:M,children:(0,u.jsx)(`div`,{className:`ibody`,style:f,"data-droppableid":`body`,onScroll:l,ref:i,children:e})})]})}),M={display:`flex`,overflow:`hidden`,flex:1};j.displayName=`TableBody`;var N={display:`flex`,flex:`1 0 auto`,minHeight:`24px`},P=(0,c.memo)(({children:e,style:t,className:n,...r})=>{let i=(0,c.useMemo)(()=>t?{...N,...t}:N,[t]),a=(0,f.default)(`tr`,n);return(0,u.jsx)(k,{...r,type:`row`,children:(0,u.jsx)(`div`,{className:a,style:i,children:e})})});P.displayName=`BodyRow`;var F=(0,c.memo)(({children:e,style:t,className:n,isClone:r,...i})=>{let{index:a}=i,{state:o}=h(),s=(0,c.useMemo)(()=>o.columnIds[a]??``,[o.columnIds,a]),l=(0,c.useMemo)(()=>o.widths[a]??o.options.defaultSizing,[o.widths,a,o.options.defaultSizing]),d=(0,c.useMemo)(()=>r?!1:s===o.dragged.draggedID,[r,s,o.dragged.draggedID]),f=(0,c.useMemo)(()=>({display:`inline-flex`,opacity:d?0:1,width:`${l}px`,flex:`${l} 0 auto`,...t}),[d,l,t]);return(0,u.jsx)(`div`,{className:`td ${n??``}`,style:f,"data-col-index":a,children:e})});F.displayName=`RowCell`;var I=(0,c.memo)(({children:e,className:t,style:n})=>(0,u.jsx)(`div`,{"data-drag-handle":`true`,className:t,style:{cursor:`-webkit-grab`,display:`inline-flex`,alignItems:`center`,...n},children:e}));I.displayName=`DragHandle`,exports.BodyRow=P,exports.ColumnCell=A,exports.DragHandle=I,exports.RowCell=F,exports.TableBody=j,exports.TableContainer=D,exports.TableHeader=O,exports.useTable=h;
|
|
15
|
+
`,m=(0,c.createContext)(void 0),h=()=>{let e=(0,c.useContext)(m);if(e===void 0)throw Error(`useTable must be used within a TableProvider`);return e},g=e=>{let t=(0,c.useRef)(!1),n=(0,c.useRef)(!1),r=(0,c.useRef)(0),i=(0,c.useRef)(null),a=(0,c.useCallback)((e,o,s)=>{let c=s===`vertical`,l=c?o.scrollHeight-o.clientHeight:o.scrollWidth-o.clientWidth;c?o.scrollTop+=e:o.scrollLeft+=e,c&&(o.scrollTop>=l||o.scrollTop<=0)||!c&&(o.scrollLeft>=l||o.scrollLeft<=0)?i.current!==null&&cancelAnimationFrame(i.current):n.current||t.current?(i.current=requestAnimationFrame(()=>a(e+r.current,o,s)),r.current+=e/1e3):i.current!==null&&cancelAnimationFrame(i.current)},[]);return{startAutoScroll:(0,c.useCallback)((e,i,o)=>{let s=o===`vertical`;!n.current&&!t.current&&(s?n.current=!0:t.current=!0,r.current=0,a(e,i,o))},[a]),stopAutoScroll:(0,c.useCallback)(()=>{n.current=!1,t.current=!1,i.current!==null&&cancelAnimationFrame(i.current)},[]),isAutoScrollingHorizontal:t,isAutoScrollingVertical:n,BodyScrollHandle:(0,c.useCallback)(t=>{e.headerRef?.current&&t.currentTarget&&(e.headerRef.current.scrollLeft=t.currentTarget.scrollLeft)},[e]),HeaderScrollHandle:(0,c.useCallback)(t=>{e.bodyRef?.current&&t.currentTarget&&(e.bodyRef.current.scrollLeft=t.currentTarget.scrollLeft)},[e])}},_=(e,t)=>{let n=e,r=0,i=t.length-1;for(;r<i;){let e=Math.floor((r+i)/2),a=t[e];if(a.itemTop<=n&&n<=a.itemBottom)return n<a.itemTop+a.height/2?+a.index:+a.index+1;n<a.itemTop?i=e-1:r=e+1}return+t[r].index},v=(e,t)=>{let n=e,r=0,i=t.length-1;for(;r<i;){let e=Math.floor((r+i)/2),a=t[e];if(a.itemLeft<=n&&n<=a.itemRight)return n<a.itemLeft+a.width/2?+a.index:+a.index+1;n<a.itemLeft?i=e-1:r=e+1}return+t[r].index},y=(e,t,n)=>t!==void 0&&e<t||n!==void 0&&e>n,b=`all 450ms cubic-bezier(0.2, 0, 0, 1)`,x=300,S=8,C=(e,t,n,r,i,a)=>{let{startAutoScroll:o,stopAutoScroll:s,isAutoScrollingHorizontal:l,isAutoScrollingVertical:u}=g(e),d=(0,c.useRef)(null),f=(0,c.useRef)(null),p=(0,c.useRef)(null),m=(0,c.useRef)({x:0,y:0}),h=(0,c.useRef)(null),y=(0,c.useRef)(null),C=(0,c.useRef)({width:0,height:0}),w=(0,c.useRef)(null),T=(0,c.useRef)({x:0,y:0}),E=(0,c.useRef)(null),D=(0,c.useRef)(!1),O=(0,c.useRef)(null),k=(0,c.useRef)(null),A=(0,c.useCallback)(()=>{let t=e.bodyRef.current;if(!t)return null;let n=t.scrollTop,r=t.getBoundingClientRect().top,a=t.querySelectorAll(`.draggable[data-type="row"]`),o=[];for(let e=0;e<a.length;e++){let t=a[e];if(t.dataset.index===void 0)continue;let i=t.getBoundingClientRect(),s=i.top-r+n;o.push({height:i.height,itemTop:s,itemBottom:s+i.height,index:t.dataset.index})}let s=i.rowDragRange.start,c=i.rowDragRange.end;return(s||c)&&(o=o.filter(e=>(!s||e.index>=s)&&(!c||e.index<c))),o},[e.bodyRef,i.rowDragRange]),j=(0,c.useCallback)(()=>{let t=e.headerRef.current;if(!t||!t.children[0])return null;let n=Array.from(t.children[0].children).map(e=>{let t=e.getBoundingClientRect();return{left:t.left,width:t.width,itemLeft:t.left,itemRight:t.left+t.width,index:e.dataset.index}}).filter(e=>e.index!==void 0),r=i.columnDragRange?.start,a=i.columnDragRange?.end;return(r!==void 0||a!==void 0)&&(n=n.filter(e=>{let t=+e.index;return(r===void 0||t>=r)&&(a===void 0||t<a)})),n},[e.headerRef,i.columnDragRange]),M=(0,c.useCallback)((t,n,r,i)=>{let a=e.placeholderRef?.current;if(!a||!t){a&&(a.style.display=`none`);return}let o=C.current,s=t.getBoundingClientRect();a.style.display=`block`;let c=n<r;if(i===`row`){let e=c?s.top+s.height-o.height:s.top;a.style.top=`${e}px`,a.style.left=`${s.left}px`,a.style.width=`${s.width}px`,a.style.height=`${o.height}px`}else{let t=(e.tableRef?.current)?.getBoundingClientRect(),n=c?s.left+s.width-o.width:s.left;a.style.top=`${t?.top??s.top}px`,a.style.left=`${n}px`,a.style.width=`${o.width}px`,a.style.height=`${t?.height??s.height}px`}},[e.placeholderRef,e.tableRef]),N=(0,c.useCallback)((t,n,r)=>{if(t===null||n===null)return;let i=C.current,a=null;if(r===`row`){let r=e.bodyRef.current;if(!r)return;let o=r.querySelectorAll(`.draggable[data-type="row"]`);for(let e=0;e<o.length;e++){let r=o[e],s=+r.dataset.index,c=r.firstElementChild;if(!c)continue;let l=``;s>t&&s<=n?l=`translateY(-${i.height}px)`:s<t&&s>=n&&(l=`translateY(${i.height}px)`),c.style.transform=l,c.style.transition=s===t?`none`:b,s===n?(r.setAttribute(`data-drop-target`,`true`),a=r):r.removeAttribute(`data-drop-target`)}}else if(r===`column`){let r=e.headerRef.current;if(r){let e=r.querySelectorAll(`.draggable[data-type="column"]`);for(let r=0;r<e.length;r++){let o=e[r],s=+o.dataset.index,c=o.firstElementChild;if(!c)continue;let l=``;s>t&&s<=n?l=`translateX(-${i.width}px)`:s<t&&s>=n&&(l=`translateX(${i.width}px)`),c.style.transform=l,c.style.transition=s===t?`none`:b,s===n?(o.setAttribute(`data-drop-target`,`true`),a=o):o.removeAttribute(`data-drop-target`)}}let o=e.bodyRef.current;if(o){let e=o.querySelectorAll(`.td[data-col-index]`);for(let r=0;r<e.length;r++){let a=e[r],o=+a.dataset.colIndex,s=``;o>t&&o<=n?s=`translateX(-${i.width}px)`:o<t&&o>=n&&(s=`translateX(${i.width}px)`),a.style.transform=s,a.style.transition=b}}}M(a,t,n,r)},[e.bodyRef,e.headerRef,e.placeholderRef,M]),P=(0,c.useCallback)(()=>{let t=e.placeholderRef?.current;t&&(t.style.display=`none`);let n=e.bodyRef.current;if(n){let e=n.querySelectorAll(`.draggable`);for(let t=0;t<e.length;t++){e[t].removeAttribute(`data-drop-target`);let n=e[t].firstElementChild;n&&(n.style.transform=``,n.style.transition=``)}let t=n.querySelectorAll(`.td[data-col-index]`);for(let e=0;e<t.length;e++)t[e].style.transform=``,t[e].style.transition=``}let r=e.headerRef.current;if(r){let e=r.querySelectorAll(`.draggable`);for(let t=0;t<e.length;t++){e[t].removeAttribute(`data-drop-target`);let n=e[t].firstElementChild;n&&(n.style.transform=``,n.style.transition=``)}}},[e.bodyRef,e.headerRef]),F=(0,c.useCallback)((t,r,i)=>{let a=!1,o=(e=>{for(;e;){if(e.dataset?.dragHandle===`true`&&(a=!0),e.dataset?.contextid||e.dataset?.disabled===`true`)return null;if(e.dataset?.id)return e;e=e.parentNode}return null})(t.target);if(!o||!a&&o.querySelector(`[data-drag-handle]`))return;let s=o.dataset.id,c=+o.dataset.index,l=o.dataset.type,u=t.type===`touchstart`;p.current=l,h.current=c,y.current=null,u&&o.dispatchEvent(new PointerEvent(`pointerdown`,{bubbles:!0,pointerType:`mouse`}));let g=l===`row`?e.bodyRef.current.scrollLeft:0,_=o.getBoundingClientRect();C.current={width:_.width,height:_.height};let v={x:r-_.left-g,y:i-_.top};m.current=v;let b={x:_.left+g,y:_.top};l===`row`?d.current=A():d.current=j();let x=e.bodyRef.current;x&&(f.current=x.getBoundingClientRect());let S=e.cloneRef?.current;S&&(S.style.transform=`translate(${b.x}px, ${b.y}px)`);let w=e.tableRef?.current;w&&(w.style.touchAction=`none`),n({type:`dragStart`,value:{rect:{draggedItemHeight:_.height,draggedItemWidth:_.width},dragged:{initial:v,translate:b,draggedID:s,isDragging:!0,sourceIndex:c},dragType:l}}),requestAnimationFrame(()=>{let t=e.cloneRef?.current,n=e.bodyRef.current;if(t&&n){if(l===`row`)t.scrollLeft=n.scrollLeft;else if(l===`column`){let e=t.querySelector(`.clone-body`);e&&(e.scrollTop=n.scrollTop)}}})},[n,e,A,j]),I=(0,c.useCallback)(e=>{e.target!==e.currentTarget&&(D.current||F(e,e.clientX,e.clientY))},[F]),L=(0,c.useCallback)(()=>{w.current&&=(clearTimeout(w.current),null),E.current=null,window.removeEventListener(`touchmove`,O.current),window.removeEventListener(`touchend`,k.current)},[]),R=(0,c.useCallback)(e=>{if(e.target===e.currentTarget)return;L(),D.current=!0;let t=e.touches[0];T.current={x:t.clientX,y:t.clientY},E.current=e;let n=e=>{let t=e.touches[0],n=t.clientX-T.current.x,r=t.clientY-T.current.y;(Math.abs(n)>S||Math.abs(r)>S)&&(L(),setTimeout(()=>{D.current=!1},400))},r=()=>{L(),setTimeout(()=>{D.current=!1},400)};O.current=n,k.current=r,window.addEventListener(`touchmove`,n,{passive:!0}),window.addEventListener(`touchend`,r,!1),w.current=setTimeout(()=>{w.current=null,window.removeEventListener(`touchmove`,n),window.removeEventListener(`touchend`,r);let e=E.current;E.current=null,e&&F(e,t.clientX,t.clientY)},x)},[F,L]),z=(0,c.useCallback)(()=>{L(),setTimeout(()=>{D.current=!1},400);let t=y.current,r=h.current,i=p.current;d.current=null,f.current=null;let o=e.cloneRef?.current;o&&(o.style.transform=`translate(0px, 0px)`,o.scrollLeft=0);let c=e.tableRef?.current;c&&(c.style.touchAction=``),P(),a&&r!==null&&t!==null&&i&&a({sourceIndex:r,targetIndex:t,dragType:i}),n({type:`dragEnd`,value:{targetIndex:t,sourceIndex:r}}),s(),p.current=null,h.current=null,y.current=null},[n,s,e.cloneRef,e.tableRef,P,L,a]),B=(0,c.useCallback)(()=>{d.current=null,f.current=null;let e=h.current,t=y.current,n=p.current;e!==null&&t!==null&&N(e,t,n)},[N]),V=(0,c.useCallback)(t=>{t.stopPropagation();let n=m.current,i,a;t.type===`touchmove`?(i=t.touches[0].clientX-n.x,a=t.touches[0].clientY-n.y):(i=t.clientX-n.x,a=t.clientY-n.y);let c=e.cloneRef?.current;if(c){c.style.transform=`translate(${i}px, ${a}px)`;let t=e.bodyRef.current;if(t){if(p.current===`row`)c.scrollLeft=t.scrollLeft;else if(p.current===`column`){let e=c.querySelector(`.clone-body`);e&&(e.scrollTop=t.scrollTop)}}}u.current=!1,l.current=!1;let g=e.bodyRef.current;if(!g)return;let b=t.type===`touchmove`?t.touches[0].clientX:t.clientX,x=t.type===`touchmove`?t.touches[0].clientY:t.clientY,S=f.current;S||(S=g.getBoundingClientRect(),f.current=S);let{top:C,bottom:w,left:T,right:E}=S,D=0,O=p.current||r;if(O===`row`){let e=g.scrollTop,t=S.top,n=d.current;n||(n=A(),d.current=n),n&&n.length>0&&(D=_(x-t+e,n)),x<C+30?(o(-5,g,`vertical`),d.current=null):x>w-30?(o(5,g,`vertical`),d.current=null):s()}else{let e=d.current;e||(e=j(),d.current=e),e&&e.length>0&&(D=v(b,e)),b<T+30?(o(-5,g,`horizontal`),d.current=null):b>E-30?(o(5,g,`horizontal`),d.current=null):s()}D!==y.current&&(y.current=D,requestAnimationFrame(()=>{N(h.current,D,O)}))},[r,l,u,e.bodyRef,e.cloneRef,A,j,o,s,N]),H=(0,c.useCallback)(()=>{L(),d.current=null,f.current=null;let t=e.cloneRef?.current;t&&(t.style.transform=`translate(0px, 0px)`,t.scrollLeft=0);let r=e.tableRef?.current;r&&(r.style.touchAction=``),P(),n({type:`dragEnd`,value:{targetIndex:null,sourceIndex:null}}),s(),p.current=null,h.current=null,y.current=null},[n,s,e.cloneRef,e.tableRef,P,L]),U=(0,c.useCallback)(e=>{e.key===`Escape`&&H()},[H]),W=(0,c.useCallback)(e=>{e.preventDefault(),V(e)},[V]);return(0,c.useEffect)(()=>{if(t.isDragging){let t=e.bodyRef.current;return window.addEventListener(`mousemove`,V,{passive:!0}),window.addEventListener(`touchmove`,W,{passive:!1}),window.addEventListener(`touchend`,z,!1),window.addEventListener(`mouseup`,z,!1),window.addEventListener(`keydown`,U),t?.addEventListener(`scroll`,B,{passive:!0}),()=>{window.removeEventListener(`mousemove`,V),window.removeEventListener(`touchmove`,W),window.removeEventListener(`touchend`,z,!1),window.removeEventListener(`mouseup`,z,!1),window.removeEventListener(`keydown`,U),t?.removeEventListener(`scroll`,B);let n=e.tableRef?.current;n&&(n.style.touchAction=``)}}},[t.isDragging,V,W,z,H,U,B,e.bodyRef,e.tableRef]),{dragStart:I,touchStart:R}},w={columnDragRange:{start:void 0,end:void 0},rowDragRange:{start:void 0,end:void 0},defaultSizing:50};function T(e,t){switch(t.type){case`setClone`:return{...e,clone:t.value};case`setDragged`:return{...e,dragged:{...e.dragged,...t.value}};case`setDragType`:return{...e,dragType:t.value};case`setRect`:return{...e,rect:t.value};case`setTableDimensions`:return{...e,tableDimensions:t.value};case`setTableRef`:return{...e,refs:{...e.refs,tableRef:t.value}};case`setBodyRef`:return{...e,refs:{...e.refs,bodyRef:t.value}};case`setHeaderRef`:return{...e,refs:{...e.refs,headerRef:t.value}};case`setCloneRef`:return{...e,refs:{...e.refs,cloneRef:t.value}};case`setPlaceholderRef`:return{...e,refs:{...e.refs,placeholderRef:t.value}};case`setBodyScrollBarWidth`:return{...e,bodyScrollBarWidth:t.value};case`setWidths`:return{...e,widths:t.value};case`setColumnIds`:return{...e,columnIds:t.value};case`setOptions`:return{...e,options:t.value??w};case`dragStart`:return{...e,rect:t.value.rect,dragged:{...e.dragged,...t.value.dragged},dragType:t.value.dragType};case`dragEnd`:return{...e,clone:null,dragged:{initial:{x:0,y:0},translate:{x:0,y:0},isDragging:!1,draggedID:null,targetIndex:t.value?.targetIndex??null,sourceIndex:t.value?.sourceIndex??null},dragType:null,rect:{draggedItemWidth:0,draggedItemHeight:0}};default:throw Error(`Unhandled action type: ${t.type}`)}}var E={clone:null,dragged:{initial:{x:0,y:0},translate:{x:0,y:0},isDragging:!1,draggedID:null,targetIndex:null,sourceIndex:null},dragType:null,rect:{draggedItemWidth:0,draggedItemHeight:0},tableDimensions:{height:0,width:0},refs:{tableRef:null,bodyRef:null,headerRef:null,cloneRef:null,placeholderRef:null},bodyScrollBarWidth:0,options:w,widths:[],columnIds:[]},D={position:`relative`,display:`flex`,flexFlow:`column`},O={position:`fixed`,pointerEvents:`none`,zIndex:3,top:0,left:0,display:`none`},k=(0,c.forwardRef)(({children:e,className:t,style:n,options:r,onDragEnd:i,renderPlaceholder:a},o)=>{let s=(0,c.useRef)(null),l=(0,c.useRef)(null),d=(0,c.useRef)(null);(0,c.useImperativeHandle)(o,()=>s.current,[]);let[f,h]=(0,c.useReducer)(T,E),g=(0,c.useMemo)(()=>({state:f,dispatch:h}),[f]);(0,c.useEffect)(()=>{h({type:`setTableRef`,value:s}),h({type:`setCloneRef`,value:l}),h({type:`setPlaceholderRef`,value:d})},[s]),(0,c.useEffect)(()=>{let e=()=>{s.current&&h({type:`setTableDimensions`,value:{height:s.current.offsetHeight,width:s.current.offsetWidth}})};return e(),window.addEventListener(`resize`,e),()=>{window.removeEventListener(`resize`,e)}},[s]),(0,c.useEffect)(()=>{h({type:`setOptions`,value:r})},[r]);let{dragStart:_,touchStart:v}=C(f.refs,f.dragged,h,f.dragType,f.options,i),y=(0,c.useMemo)(()=>({position:`fixed`,zIndex:`5`,pointerEvents:`none`,top:0,left:0,display:`flex`,flexDirection:`column`,height:f.dragType===`row`?f.rect.draggedItemHeight:`${f.tableDimensions.height-20}px`,width:f.dragType===`column`?`${f.rect.draggedItemWidth}px`:`${f.tableDimensions.width-20}px`,overflow:`hidden`,boxShadow:f.dragged.isDragging?`0 0 10px 0 rgba(0, 0, 0, 0.1)`:`none`}),[f.dragType,f.dragged.isDragging,f.rect.draggedItemHeight,f.rect.draggedItemWidth,f.tableDimensions.height,f.tableDimensions.width]);return(0,u.jsx)(m.Provider,{value:g,children:(0,u.jsxs)(p,{className:f.dragged.isDragging?`is-dragging`:``,children:[(0,u.jsx)(`div`,{id:`portalroot`,style:{...y,visibility:f.dragged.isDragging?`visible`:`hidden`},ref:l,children:(0,u.jsx)(`div`,{style:{flexShrink:0,order:-1},children:f.clone})}),a&&(0,u.jsx)(`div`,{ref:d,style:O,children:a()}),(0,u.jsx)(`div`,{"data-contextid":`context`,ref:s,onMouseDown:_,onTouchStart:v,style:{...D,...n},className:`table ${t??``}`,children:e})]})})});k.displayName=`TableProvider`;var A=(0,c.forwardRef)(({children:e,style:t,className:n},r)=>{let i=(0,c.useRef)(null),a=r||i,{state:o,dispatch:s}=h();(0,c.useEffect)(()=>{s({type:`setHeaderRef`,value:a})},[s,a]);let{HeaderScrollHandle:l}=g(o.refs),d={display:`flex`,flex:`1 0 auto`},f=(0,c.useMemo)(()=>({overflow:`hidden`,display:`flex`,paddingRight:`${o.bodyScrollBarWidth}px`,userSelect:o.dragged.isDragging?`none`:`auto`,...t}),[o.bodyScrollBarWidth,o.dragged.isDragging,t]);return(0,c.useEffect)(()=>{a.current&&s({type:`setWidths`,value:Array.from(a.current.querySelectorAll(`.th`)).map(e=>{let t=e.getAttribute(`data-width`);return t?parseInt(t,10):null})})},[e,s,a]),(0,c.useEffect)(()=>{a.current&&s({type:`setColumnIds`,value:Array.from(a.current.querySelectorAll(`.draggable`)).map(e=>e.getAttribute(`data-id`))})},[e,s,a]),(0,u.jsx)(`div`,{className:`header ${n??``}`,children:(0,u.jsx)(`div`,{className:`thead`,style:f,"data-droppableid":`header`,onScroll:l,ref:a,children:(0,u.jsx)(`div`,{style:d,className:`tr`,children:e})})})});A.displayName=`TableHeader`;var j=(0,c.memo)(({children:e,id:t,index:n,type:r,styles:i={}})=>{let{state:a,dispatch:o}=h(),s=(0,c.useMemo)(()=>String(t)===String(a.dragged.draggedID)&&a.dragged.isDragging,[t,a.dragged.draggedID,a.dragged.isDragging]),l=(0,c.useMemo)(()=>r===`row`?y(n,a.options.rowDragRange.start,a.options.rowDragRange.end):y(n,a.options.columnDragRange.start,a.options.columnDragRange.end),[n,a.options.columnDragRange.end,a.options.columnDragRange.start,a.options.rowDragRange.end,a.options.rowDragRange.start,r]),d=(0,c.useRef)(null),f=(0,c.useRef)(!1);(0,c.useEffect)(()=>{d.current&&(f.current=!!d.current.querySelector(`[data-drag-handle]`))});let p=(0,c.useMemo)(()=>({cursor:s?`-webkit-grabbing`:l||f.current?`auto`:`-webkit-grab`,zIndex:s?2:1,opacity:s?0:1,pointerEvents:s?`none`:`auto`,display:`flex`}),[l,s]),m=t=>{t.pointerType!==`touch`&&(l||requestAnimationFrame(()=>{o({type:`setClone`,value:c.default.cloneElement(e)})}))};return(0,u.jsx)(`div`,{className:`draggable`,"data-id":t,"data-index":n,"data-type":r,onPointerDown:m,"data-disabled":l?`true`:`false`,style:i,children:(0,u.jsx)(`div`,{ref:d,style:p,children:e})})});j.displayName=`Draggable`;var M=(0,c.memo)(({children:e,width:t,style:n,className:r,...i})=>{let{state:a}=h(),o=(0,c.useMemo)(()=>t??a.options.defaultSizing,[t,a.options.defaultSizing]),s=(0,c.useMemo)(()=>({width:`${o}px`,flex:`${o} 0 auto`}),[o]);return(0,u.jsx)(j,{...i,styles:s,type:`column`,children:(0,u.jsx)(`div`,{className:`th ${r??``}`,"data-width":t,style:{width:`100%`,...n},children:e})})});M.displayName=`ColumnCell`;var N=(0,c.forwardRef)(({children:e,style:t,className:n},r)=>{let i=(0,c.useRef)(null);(0,c.useImperativeHandle)(r,()=>i.current,[]);let{state:a,dispatch:o}=h(),s=(0,c.useMemo)(()=>{if(a.dragged.sourceIndex===null)return null;let t=e=>{let n=[];return c.default.Children.forEach(e,e=>{c.default.isValidElement(e)&&(e.props.id!==void 0&&e.props.index!==void 0&&e.props.children?n.push(e):e.props.children&&n.push(...t(e.props.children)))}),n};return t(e).map(e=>{let t=c.default.Children.toArray(e.props.children).filter(e=>c.default.isValidElement(e)&&String(e.props.index)===String(a.dragged.sourceIndex)).map(e=>c.default.cloneElement(e,{isClone:!0}));return c.default.cloneElement(e,{...e.props,children:t})})},[e,a.dragged.sourceIndex]);(0,c.useEffect)(()=>{o({type:`setBodyRef`,value:i})},[o,i]);let{BodyScrollHandle:l}=g(a.refs),f=(0,c.useMemo)(()=>({overflowX:`auto`,overflowY:`auto`,flex:1,userSelect:a.dragged.isDragging?`none`:`auto`,...t}),[a.dragged.isDragging,t]);(0,c.useEffect)(()=>{if(i.current){let e=i.current.clientWidth;o({type:`setBodyScrollBarWidth`,value:i.current.offsetWidth-e})}},[o,i]);let p=i.current?.scrollHeight??0;return(0,u.jsxs)(c.default.Fragment,{children:[a.dragType===`column`&&a.refs.cloneRef?.current&&(0,d.createPortal)((0,u.jsx)(`div`,{className:`body clone-body`,"data-droppableid":`body`,style:{overflow:`hidden`,flex:1},children:(0,u.jsx)(`div`,{className:`rbody`,style:{height:p,position:`relative`},children:s})}),a.refs.cloneRef.current),(0,u.jsx)(`div`,{className:`body ${n??``}`,style:P,children:(0,u.jsx)(`div`,{className:`ibody`,style:f,"data-droppableid":`body`,onScroll:l,ref:i,children:e})})]})}),P={display:`flex`,overflow:`hidden`,flex:1};N.displayName=`TableBody`;var F={display:`flex`,flex:`1 0 auto`,minHeight:`24px`},I=(0,c.memo)(({children:e,style:t,className:n,...r})=>{let i=(0,c.useMemo)(()=>t?{...F,...t}:F,[t]),a=(0,f.default)(`tr`,n);return(0,u.jsx)(j,{...r,type:`row`,children:(0,u.jsx)(`div`,{className:a,style:i,children:e})})});I.displayName=`BodyRow`;var L=(0,c.memo)(({children:e,style:t,className:n,isClone:r,...i})=>{let{index:a}=i,{state:o}=h(),s=(0,c.useMemo)(()=>o.columnIds[a]??``,[o.columnIds,a]),l=(0,c.useMemo)(()=>o.widths[a]??o.options.defaultSizing,[o.widths,a,o.options.defaultSizing]),d=(0,c.useMemo)(()=>r?!1:s===o.dragged.draggedID,[r,s,o.dragged.draggedID]),f=(0,c.useMemo)(()=>({display:`inline-flex`,opacity:d?0:1,width:`${l}px`,flex:`${l} 0 auto`,...t}),[d,l,t]);return(0,u.jsx)(`div`,{className:`td ${n??``}`,style:f,"data-col-index":a,children:e})});L.displayName=`RowCell`;var R=(0,c.memo)(({children:e,className:t,style:n})=>(0,u.jsx)(`div`,{"data-drag-handle":`true`,className:t,style:{cursor:`-webkit-grab`,display:`inline-flex`,alignItems:`center`,...n},children:e}));R.displayName=`DragHandle`,exports.BodyRow=I,exports.ColumnCell=M,exports.DragHandle=R,exports.RowCell=L,exports.TableBody=N,exports.TableContainer=k,exports.TableHeader=A,exports.useTable=h;
|
package/dist/index.es.js
CHANGED
|
@@ -60,14 +60,17 @@ var g = d.div`
|
|
|
60
60
|
n < a.itemLeft ? i = e - 1 : r = e + 1;
|
|
61
61
|
}
|
|
62
62
|
return +t[r].index;
|
|
63
|
-
}, S = (e, t, n) => t !== void 0 && e < t || n !== void 0 && e > n, C = "all 450ms cubic-bezier(0.2, 0, 0, 1)", w = (e, t, n, r, a, s) => {
|
|
63
|
+
}, S = (e, t, n) => t !== void 0 && e < t || n !== void 0 && e > n, C = "all 450ms cubic-bezier(0.2, 0, 0, 1)", w = 300, T = 8, E = (e, t, n, r, a, s) => {
|
|
64
64
|
let { startAutoScroll: c, stopAutoScroll: l, isAutoScrollingHorizontal: d, isAutoScrollingVertical: f } = y(e), p = u(null), m = u(null), h = u(null), g = u({
|
|
65
65
|
x: 0,
|
|
66
66
|
y: 0
|
|
67
67
|
}), _ = u(null), v = u(null), S = u({
|
|
68
68
|
width: 0,
|
|
69
69
|
height: 0
|
|
70
|
-
}),
|
|
70
|
+
}), E = u(null), D = u({
|
|
71
|
+
x: 0,
|
|
72
|
+
y: 0
|
|
73
|
+
}), O = u(null), k = u(!1), A = u(null), j = u(null), M = i(() => {
|
|
71
74
|
let t = e.bodyRef.current;
|
|
72
75
|
if (!t) return null;
|
|
73
76
|
let n = t.scrollTop, r = t.getBoundingClientRect().top, i = t.querySelectorAll(".draggable[data-type=\"row\"]"), o = [];
|
|
@@ -84,7 +87,7 @@ var g = d.div`
|
|
|
84
87
|
}
|
|
85
88
|
let s = a.rowDragRange.start, c = a.rowDragRange.end;
|
|
86
89
|
return (s || c) && (o = o.filter((e) => (!s || e.index >= s) && (!c || e.index < c))), o;
|
|
87
|
-
}, [e.bodyRef, a.rowDragRange]),
|
|
90
|
+
}, [e.bodyRef, a.rowDragRange]), N = i(() => {
|
|
88
91
|
let t = e.headerRef.current;
|
|
89
92
|
if (!t || !t.children[0]) return null;
|
|
90
93
|
let n = Array.from(t.children[0].children).map((e) => {
|
|
@@ -101,7 +104,7 @@ var g = d.div`
|
|
|
101
104
|
let t = +e.index;
|
|
102
105
|
return (r === void 0 || t >= r) && (i === void 0 || t < i);
|
|
103
106
|
})), n;
|
|
104
|
-
}, [e.headerRef, a.columnDragRange]),
|
|
107
|
+
}, [e.headerRef, a.columnDragRange]), P = i((t, n, r, i) => {
|
|
105
108
|
let a = e.placeholderRef?.current;
|
|
106
109
|
if (!a || !t) {
|
|
107
110
|
a && (a.style.display = "none");
|
|
@@ -117,7 +120,7 @@ var g = d.div`
|
|
|
117
120
|
let t = (e.tableRef?.current)?.getBoundingClientRect(), n = c ? s.left + s.width - o.width : s.left;
|
|
118
121
|
a.style.top = `${t?.top ?? s.top}px`, a.style.left = `${n}px`, a.style.width = `${o.width}px`, a.style.height = `${t?.height ?? s.height}px`;
|
|
119
122
|
}
|
|
120
|
-
}, [e.placeholderRef, e.tableRef]),
|
|
123
|
+
}, [e.placeholderRef, e.tableRef]), F = i((t, n, r) => {
|
|
121
124
|
if (t === null || n === null) return;
|
|
122
125
|
let i = S.current, a = null;
|
|
123
126
|
if (r === "row") {
|
|
@@ -150,13 +153,13 @@ var g = d.div`
|
|
|
150
153
|
}
|
|
151
154
|
}
|
|
152
155
|
}
|
|
153
|
-
|
|
156
|
+
P(a, t, n, r);
|
|
154
157
|
}, [
|
|
155
158
|
e.bodyRef,
|
|
156
159
|
e.headerRef,
|
|
157
160
|
e.placeholderRef,
|
|
158
|
-
|
|
159
|
-
]),
|
|
161
|
+
P
|
|
162
|
+
]), I = i(() => {
|
|
160
163
|
let t = e.placeholderRef?.current;
|
|
161
164
|
t && (t.style.display = "none");
|
|
162
165
|
let n = e.bodyRef.current;
|
|
@@ -179,76 +182,109 @@ var g = d.div`
|
|
|
179
182
|
n && (n.style.transform = "", n.style.transition = "");
|
|
180
183
|
}
|
|
181
184
|
}
|
|
182
|
-
}, [e.bodyRef, e.headerRef]),
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
185
|
+
}, [e.bodyRef, e.headerRef]), L = i((t, r, i) => {
|
|
186
|
+
let a = !1, o = ((e) => {
|
|
187
|
+
for (; e;) {
|
|
188
|
+
if (e.dataset?.dragHandle === "true" && (a = !0), e.dataset?.contextid || e.dataset?.disabled === "true") return null;
|
|
189
|
+
if (e.dataset?.id) return e;
|
|
190
|
+
e = e.parentNode;
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
})(t.target);
|
|
194
|
+
if (!o || !a && o.querySelector("[data-drag-handle]")) return;
|
|
195
|
+
let s = o.dataset.id, c = +o.dataset.index, l = o.dataset.type, u = t.type === "touchstart";
|
|
196
|
+
h.current = l, _.current = c, v.current = null, u && o.dispatchEvent(new PointerEvent("pointerdown", {
|
|
197
|
+
bubbles: !0,
|
|
198
|
+
pointerType: "mouse"
|
|
199
|
+
}));
|
|
200
|
+
let d = l === "row" ? e.bodyRef.current.scrollLeft : 0, f = o.getBoundingClientRect();
|
|
201
|
+
S.current = {
|
|
202
|
+
width: f.width,
|
|
203
|
+
height: f.height
|
|
204
|
+
};
|
|
205
|
+
let y = {
|
|
206
|
+
x: r - f.left - d,
|
|
207
|
+
y: i - f.top
|
|
208
|
+
};
|
|
209
|
+
g.current = y;
|
|
210
|
+
let b = {
|
|
211
|
+
x: f.left + d,
|
|
212
|
+
y: f.top
|
|
213
|
+
};
|
|
214
|
+
l === "row" ? p.current = M() : p.current = N();
|
|
215
|
+
let x = e.bodyRef.current;
|
|
216
|
+
x && (m.current = x.getBoundingClientRect());
|
|
217
|
+
let C = e.cloneRef?.current;
|
|
218
|
+
C && (C.style.transform = `translate(${b.x}px, ${b.y}px)`);
|
|
219
|
+
let w = e.tableRef?.current;
|
|
220
|
+
w && (w.style.touchAction = "none"), n({
|
|
221
|
+
type: "dragStart",
|
|
222
|
+
value: {
|
|
223
|
+
rect: {
|
|
224
|
+
draggedItemHeight: f.height,
|
|
225
|
+
draggedItemWidth: f.width
|
|
226
|
+
},
|
|
227
|
+
dragged: {
|
|
228
|
+
initial: y,
|
|
229
|
+
translate: b,
|
|
230
|
+
draggedID: s,
|
|
231
|
+
isDragging: !0,
|
|
232
|
+
sourceIndex: c
|
|
233
|
+
},
|
|
234
|
+
dragType: l
|
|
235
|
+
}
|
|
236
|
+
}), requestAnimationFrame(() => {
|
|
237
|
+
let t = e.cloneRef?.current, n = e.bodyRef.current;
|
|
238
|
+
if (t && n) {
|
|
239
|
+
if (l === "row") t.scrollLeft = n.scrollLeft;
|
|
240
|
+
else if (l === "column") {
|
|
241
|
+
let e = t.querySelector(".clone-body");
|
|
242
|
+
e && (e.scrollTop = n.scrollTop);
|
|
189
243
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
if (!i || !r && i.querySelector("[data-drag-handle]")) return;
|
|
193
|
-
let a = i.dataset.id, o = +i.dataset.index, s = i.dataset.type;
|
|
194
|
-
h.current = s, _.current = o, v.current = null;
|
|
195
|
-
let c = s === "row" ? e.bodyRef.current.scrollLeft : 0, l = i.getBoundingClientRect();
|
|
196
|
-
S.current = {
|
|
197
|
-
width: l.width,
|
|
198
|
-
height: l.height
|
|
199
|
-
};
|
|
200
|
-
let u = {
|
|
201
|
-
x: 0,
|
|
202
|
-
y: 0
|
|
203
|
-
};
|
|
204
|
-
t.type === "touchstart" ? (u.x = t.touches[0].clientX, u.y = t.touches[0].clientY) : (u.x = t.clientX - l.left - c, u.y = t.clientY - l.top), g.current = u;
|
|
205
|
-
let d = {
|
|
206
|
-
x: l.left + c,
|
|
207
|
-
y: l.top
|
|
208
|
-
};
|
|
209
|
-
s === "row" ? p.current = w() : p.current = T();
|
|
210
|
-
let f = e.bodyRef.current;
|
|
211
|
-
f && (m.current = f.getBoundingClientRect());
|
|
212
|
-
let y = e.cloneRef?.current;
|
|
213
|
-
y && (y.style.transform = `translate(${d.x}px, ${d.y}px)`), requestAnimationFrame(() => {
|
|
214
|
-
n({
|
|
215
|
-
type: "dragStart",
|
|
216
|
-
value: {
|
|
217
|
-
rect: {
|
|
218
|
-
draggedItemHeight: l.height,
|
|
219
|
-
draggedItemWidth: l.width
|
|
220
|
-
},
|
|
221
|
-
dragged: {
|
|
222
|
-
initial: u,
|
|
223
|
-
translate: d,
|
|
224
|
-
draggedID: a,
|
|
225
|
-
isDragging: !0,
|
|
226
|
-
sourceIndex: o
|
|
227
|
-
},
|
|
228
|
-
dragType: s
|
|
229
|
-
}
|
|
230
|
-
}), requestAnimationFrame(() => {
|
|
231
|
-
let t = e.cloneRef?.current, n = e.bodyRef.current;
|
|
232
|
-
if (t && n) {
|
|
233
|
-
if (s === "row") t.scrollLeft = n.scrollLeft;
|
|
234
|
-
else if (s === "column") {
|
|
235
|
-
let e = t.querySelector(".clone-body");
|
|
236
|
-
e && (e.scrollTop = n.scrollTop);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
}
|
|
244
|
+
}
|
|
245
|
+
});
|
|
242
246
|
}, [
|
|
243
247
|
n,
|
|
244
248
|
e,
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
]),
|
|
249
|
+
M,
|
|
250
|
+
N
|
|
251
|
+
]), R = i((e) => {
|
|
252
|
+
e.target !== e.currentTarget && (k.current || L(e, e.clientX, e.clientY));
|
|
253
|
+
}, [L]), z = i(() => {
|
|
254
|
+
E.current &&= (clearTimeout(E.current), null), O.current = null, window.removeEventListener("touchmove", A.current), window.removeEventListener("touchend", j.current);
|
|
255
|
+
}, []), B = i((e) => {
|
|
256
|
+
if (e.target === e.currentTarget) return;
|
|
257
|
+
z(), k.current = !0;
|
|
258
|
+
let t = e.touches[0];
|
|
259
|
+
D.current = {
|
|
260
|
+
x: t.clientX,
|
|
261
|
+
y: t.clientY
|
|
262
|
+
}, O.current = e;
|
|
263
|
+
let n = (e) => {
|
|
264
|
+
let t = e.touches[0], n = t.clientX - D.current.x, r = t.clientY - D.current.y;
|
|
265
|
+
(Math.abs(n) > T || Math.abs(r) > T) && (z(), setTimeout(() => {
|
|
266
|
+
k.current = !1;
|
|
267
|
+
}, 400));
|
|
268
|
+
}, r = () => {
|
|
269
|
+
z(), setTimeout(() => {
|
|
270
|
+
k.current = !1;
|
|
271
|
+
}, 400);
|
|
272
|
+
};
|
|
273
|
+
A.current = n, j.current = r, window.addEventListener("touchmove", n, { passive: !0 }), window.addEventListener("touchend", r, !1), E.current = setTimeout(() => {
|
|
274
|
+
E.current = null, window.removeEventListener("touchmove", n), window.removeEventListener("touchend", r);
|
|
275
|
+
let e = O.current;
|
|
276
|
+
O.current = null, e && L(e, t.clientX, t.clientY);
|
|
277
|
+
}, w);
|
|
278
|
+
}, [L, z]), V = i(() => {
|
|
279
|
+
z(), setTimeout(() => {
|
|
280
|
+
k.current = !1;
|
|
281
|
+
}, 400);
|
|
248
282
|
let t = v.current, r = _.current, i = h.current;
|
|
249
283
|
p.current = null, m.current = null;
|
|
250
284
|
let a = e.cloneRef?.current;
|
|
251
|
-
a && (a.style.transform = "translate(0px, 0px)", a.scrollLeft = 0)
|
|
285
|
+
a && (a.style.transform = "translate(0px, 0px)", a.scrollLeft = 0);
|
|
286
|
+
let o = e.tableRef?.current;
|
|
287
|
+
o && (o.style.touchAction = ""), I(), s && r !== null && t !== null && i && s({
|
|
252
288
|
sourceIndex: r,
|
|
253
289
|
targetIndex: t,
|
|
254
290
|
dragType: i
|
|
@@ -263,13 +299,15 @@ var g = d.div`
|
|
|
263
299
|
n,
|
|
264
300
|
l,
|
|
265
301
|
e.cloneRef,
|
|
266
|
-
|
|
302
|
+
e.tableRef,
|
|
303
|
+
I,
|
|
304
|
+
z,
|
|
267
305
|
s
|
|
268
|
-
]),
|
|
306
|
+
]), H = i(() => {
|
|
269
307
|
p.current = null, m.current = null;
|
|
270
308
|
let e = _.current, t = v.current, n = h.current;
|
|
271
|
-
e !== null && t !== null &&
|
|
272
|
-
}, [
|
|
309
|
+
e !== null && t !== null && F(e, t, n);
|
|
310
|
+
}, [F]), U = i((t) => {
|
|
273
311
|
t.stopPropagation();
|
|
274
312
|
let n = g.current, i, a;
|
|
275
313
|
t.type === "touchmove" ? (i = t.touches[0].clientX - n.x, a = t.touches[0].clientY - n.y) : (i = t.clientX - n.x, a = t.clientY - n.y);
|
|
@@ -288,18 +326,18 @@ var g = d.div`
|
|
|
288
326
|
f.current = !1, d.current = !1;
|
|
289
327
|
let s = e.bodyRef.current;
|
|
290
328
|
if (!s) return;
|
|
291
|
-
let
|
|
329
|
+
let u = t.type === "touchmove" ? t.touches[0].clientX : t.clientX, y = t.type === "touchmove" ? t.touches[0].clientY : t.clientY, S = m.current;
|
|
292
330
|
S || (S = s.getBoundingClientRect(), m.current = S);
|
|
293
|
-
let { top: C, bottom:
|
|
294
|
-
if (
|
|
331
|
+
let { top: C, bottom: w, left: T, right: E } = S, D = 0, O = h.current || r;
|
|
332
|
+
if (O === "row") {
|
|
295
333
|
let e = s.scrollTop, t = S.top, n = p.current;
|
|
296
|
-
n || (n =
|
|
334
|
+
n || (n = M(), p.current = n), n && n.length > 0 && (D = b(y - t + e, n)), y < C + 30 ? (c(-5, s, "vertical"), p.current = null) : y > w - 30 ? (c(5, s, "vertical"), p.current = null) : l();
|
|
297
335
|
} else {
|
|
298
336
|
let e = p.current;
|
|
299
|
-
e || (e =
|
|
337
|
+
e || (e = N(), p.current = e), e && e.length > 0 && (D = x(u, e)), u < T + 30 ? (c(-5, s, "horizontal"), p.current = null) : u > E - 30 ? (c(5, s, "horizontal"), p.current = null) : l();
|
|
300
338
|
}
|
|
301
|
-
|
|
302
|
-
|
|
339
|
+
D !== v.current && (v.current = D, requestAnimationFrame(() => {
|
|
340
|
+
F(_.current, D, O);
|
|
303
341
|
}));
|
|
304
342
|
}, [
|
|
305
343
|
r,
|
|
@@ -307,15 +345,17 @@ var g = d.div`
|
|
|
307
345
|
f,
|
|
308
346
|
e.bodyRef,
|
|
309
347
|
e.cloneRef,
|
|
310
|
-
|
|
311
|
-
|
|
348
|
+
M,
|
|
349
|
+
N,
|
|
312
350
|
c,
|
|
313
351
|
l,
|
|
314
|
-
|
|
315
|
-
]),
|
|
316
|
-
p.current = null, m.current = null;
|
|
352
|
+
F
|
|
353
|
+
]), W = i(() => {
|
|
354
|
+
z(), p.current = null, m.current = null;
|
|
317
355
|
let t = e.cloneRef?.current;
|
|
318
|
-
t && (t.style.transform = "translate(0px, 0px)", t.scrollLeft = 0)
|
|
356
|
+
t && (t.style.transform = "translate(0px, 0px)", t.scrollLeft = 0);
|
|
357
|
+
let r = e.tableRef?.current;
|
|
358
|
+
r && (r.style.touchAction = ""), I(), n({
|
|
319
359
|
type: "dragEnd",
|
|
320
360
|
value: {
|
|
321
361
|
targetIndex: null,
|
|
@@ -326,27 +366,38 @@ var g = d.div`
|
|
|
326
366
|
n,
|
|
327
367
|
l,
|
|
328
368
|
e.cloneRef,
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
369
|
+
e.tableRef,
|
|
370
|
+
I,
|
|
371
|
+
z
|
|
372
|
+
]), G = i((e) => {
|
|
373
|
+
e.key === "Escape" && W();
|
|
374
|
+
}, [W]), K = i((e) => {
|
|
375
|
+
e.preventDefault(), U(e);
|
|
376
|
+
}, [U]);
|
|
333
377
|
return o(() => {
|
|
334
378
|
if (t.isDragging) {
|
|
335
379
|
let t = e.bodyRef.current;
|
|
336
|
-
return window.addEventListener("mousemove",
|
|
337
|
-
window.removeEventListener("mousemove",
|
|
380
|
+
return window.addEventListener("mousemove", U, { passive: !0 }), window.addEventListener("touchmove", K, { passive: !1 }), window.addEventListener("touchend", V, !1), window.addEventListener("mouseup", V, !1), window.addEventListener("keydown", G), t?.addEventListener("scroll", H, { passive: !0 }), () => {
|
|
381
|
+
window.removeEventListener("mousemove", U), window.removeEventListener("touchmove", K), window.removeEventListener("touchend", V, !1), window.removeEventListener("mouseup", V, !1), window.removeEventListener("keydown", G), t?.removeEventListener("scroll", H);
|
|
382
|
+
let n = e.tableRef?.current;
|
|
383
|
+
n && (n.style.touchAction = "");
|
|
338
384
|
};
|
|
339
385
|
}
|
|
340
386
|
}, [
|
|
341
387
|
t.isDragging,
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
388
|
+
U,
|
|
389
|
+
K,
|
|
390
|
+
V,
|
|
391
|
+
W,
|
|
392
|
+
G,
|
|
393
|
+
H,
|
|
394
|
+
e.bodyRef,
|
|
395
|
+
e.tableRef
|
|
396
|
+
]), {
|
|
397
|
+
dragStart: R,
|
|
398
|
+
touchStart: B
|
|
399
|
+
};
|
|
400
|
+
}, D = {
|
|
350
401
|
columnDragRange: {
|
|
351
402
|
start: void 0,
|
|
352
403
|
end: void 0
|
|
@@ -357,7 +408,7 @@ var g = d.div`
|
|
|
357
408
|
},
|
|
358
409
|
defaultSizing: 50
|
|
359
410
|
};
|
|
360
|
-
function
|
|
411
|
+
function O(e, t) {
|
|
361
412
|
switch (t.type) {
|
|
362
413
|
case "setClone": return {
|
|
363
414
|
...e,
|
|
@@ -431,7 +482,7 @@ function E(e, t) {
|
|
|
431
482
|
};
|
|
432
483
|
case "setOptions": return {
|
|
433
484
|
...e,
|
|
434
|
-
options: t.value ??
|
|
485
|
+
options: t.value ?? D
|
|
435
486
|
};
|
|
436
487
|
case "dragStart": return {
|
|
437
488
|
...e,
|
|
@@ -468,7 +519,7 @@ function E(e, t) {
|
|
|
468
519
|
default: throw Error(`Unhandled action type: ${t.type}`);
|
|
469
520
|
}
|
|
470
521
|
}
|
|
471
|
-
var
|
|
522
|
+
var k = {
|
|
472
523
|
clone: null,
|
|
473
524
|
dragged: {
|
|
474
525
|
initial: {
|
|
@@ -501,24 +552,24 @@ var D = {
|
|
|
501
552
|
placeholderRef: null
|
|
502
553
|
},
|
|
503
554
|
bodyScrollBarWidth: 0,
|
|
504
|
-
options:
|
|
555
|
+
options: D,
|
|
505
556
|
widths: [],
|
|
506
557
|
columnIds: []
|
|
507
|
-
},
|
|
558
|
+
}, A = {
|
|
508
559
|
position: "relative",
|
|
509
560
|
display: "flex",
|
|
510
561
|
flexFlow: "column"
|
|
511
|
-
},
|
|
562
|
+
}, j = {
|
|
512
563
|
position: "fixed",
|
|
513
564
|
pointerEvents: "none",
|
|
514
565
|
zIndex: 3,
|
|
515
566
|
top: 0,
|
|
516
567
|
left: 0,
|
|
517
568
|
display: "none"
|
|
518
|
-
},
|
|
569
|
+
}, M = n(({ children: e, className: t, style: n, options: r, onDragEnd: i, renderPlaceholder: a }, d) => {
|
|
519
570
|
let m = u(null), h = u(null), v = u(null);
|
|
520
571
|
s(d, () => m.current, []);
|
|
521
|
-
let [y, b] = l(
|
|
572
|
+
let [y, b] = l(O, k), x = c(() => ({
|
|
522
573
|
state: y,
|
|
523
574
|
dispatch: b
|
|
524
575
|
}), [y]);
|
|
@@ -552,7 +603,7 @@ var D = {
|
|
|
552
603
|
value: r
|
|
553
604
|
});
|
|
554
605
|
}, [r]);
|
|
555
|
-
let { dragStart: S } =
|
|
606
|
+
let { dragStart: S, touchStart: C } = E(y.refs, y.dragged, b, y.dragType, y.options, i), w = c(() => ({
|
|
556
607
|
position: "fixed",
|
|
557
608
|
zIndex: "5",
|
|
558
609
|
pointerEvents: "none",
|
|
@@ -579,22 +630,31 @@ var D = {
|
|
|
579
630
|
children: [
|
|
580
631
|
/* @__PURE__ */ f("div", {
|
|
581
632
|
id: "portalroot",
|
|
582
|
-
style:
|
|
633
|
+
style: {
|
|
634
|
+
...w,
|
|
635
|
+
visibility: y.dragged.isDragging ? "visible" : "hidden"
|
|
636
|
+
},
|
|
583
637
|
ref: h,
|
|
584
|
-
children:
|
|
638
|
+
children: /* @__PURE__ */ f("div", {
|
|
639
|
+
style: {
|
|
640
|
+
flexShrink: 0,
|
|
641
|
+
order: -1
|
|
642
|
+
},
|
|
643
|
+
children: y.clone
|
|
644
|
+
})
|
|
585
645
|
}),
|
|
586
646
|
a && /* @__PURE__ */ f("div", {
|
|
587
647
|
ref: v,
|
|
588
|
-
style:
|
|
648
|
+
style: j,
|
|
589
649
|
children: a()
|
|
590
650
|
}),
|
|
591
651
|
/* @__PURE__ */ f("div", {
|
|
592
652
|
"data-contextid": "context",
|
|
593
653
|
ref: m,
|
|
594
654
|
onMouseDown: S,
|
|
595
|
-
onTouchStart:
|
|
655
|
+
onTouchStart: C,
|
|
596
656
|
style: {
|
|
597
|
-
...
|
|
657
|
+
...A,
|
|
598
658
|
...n
|
|
599
659
|
},
|
|
600
660
|
className: `table ${t ?? ""}`,
|
|
@@ -604,10 +664,10 @@ var D = {
|
|
|
604
664
|
})
|
|
605
665
|
});
|
|
606
666
|
});
|
|
607
|
-
|
|
667
|
+
M.displayName = "TableProvider";
|
|
608
668
|
//#endregion
|
|
609
669
|
//#region src/Components/TableHeader.tsx
|
|
610
|
-
var
|
|
670
|
+
var N = n(({ children: e, style: t, className: n }, r) => {
|
|
611
671
|
let i = u(null), a = r || i, { state: s, dispatch: l } = v();
|
|
612
672
|
o(() => {
|
|
613
673
|
l({
|
|
@@ -666,10 +726,10 @@ var j = n(({ children: e, style: t, className: n }, r) => {
|
|
|
666
726
|
})
|
|
667
727
|
});
|
|
668
728
|
});
|
|
669
|
-
|
|
729
|
+
N.displayName = "TableHeader";
|
|
670
730
|
//#endregion
|
|
671
731
|
//#region src/Components/Draggable.tsx
|
|
672
|
-
var
|
|
732
|
+
var P = r(({ children: t, id: n, index: r, type: i, styles: a = {} }) => {
|
|
673
733
|
let { state: s, dispatch: l } = v(), d = c(() => String(n) === String(s.dragged.draggedID) && s.dragged.isDragging, [
|
|
674
734
|
n,
|
|
675
735
|
s.dragged.draggedID,
|
|
@@ -692,19 +752,19 @@ var M = r(({ children: t, id: n, index: r, type: i, styles: a = {} }) => {
|
|
|
692
752
|
pointerEvents: d ? "none" : "auto",
|
|
693
753
|
display: "flex"
|
|
694
754
|
}), [p, d]), _ = (n) => {
|
|
695
|
-
p || requestAnimationFrame(() => {
|
|
755
|
+
n.pointerType !== "touch" && (p || requestAnimationFrame(() => {
|
|
696
756
|
l({
|
|
697
757
|
type: "setClone",
|
|
698
758
|
value: e.cloneElement(t)
|
|
699
759
|
});
|
|
700
|
-
});
|
|
760
|
+
}));
|
|
701
761
|
};
|
|
702
762
|
return /* @__PURE__ */ f("div", {
|
|
703
763
|
className: "draggable",
|
|
704
764
|
"data-id": n,
|
|
705
765
|
"data-index": r,
|
|
706
766
|
"data-type": i,
|
|
707
|
-
|
|
767
|
+
onPointerDown: _,
|
|
708
768
|
"data-disabled": p ? "true" : "false",
|
|
709
769
|
style: a,
|
|
710
770
|
children: /* @__PURE__ */ f("div", {
|
|
@@ -714,15 +774,15 @@ var M = r(({ children: t, id: n, index: r, type: i, styles: a = {} }) => {
|
|
|
714
774
|
})
|
|
715
775
|
});
|
|
716
776
|
});
|
|
717
|
-
|
|
777
|
+
P.displayName = "Draggable";
|
|
718
778
|
//#endregion
|
|
719
779
|
//#region src/Components/ColumnCell.tsx
|
|
720
|
-
var
|
|
780
|
+
var F = r(({ children: e, width: t, style: n, className: r, ...i }) => {
|
|
721
781
|
let { state: a } = v(), o = c(() => t ?? a.options.defaultSizing, [t, a.options.defaultSizing]), s = c(() => ({
|
|
722
782
|
width: `${o}px`,
|
|
723
783
|
flex: `${o} 0 auto`
|
|
724
784
|
}), [o]);
|
|
725
|
-
return /* @__PURE__ */ f(
|
|
785
|
+
return /* @__PURE__ */ f(P, {
|
|
726
786
|
...i,
|
|
727
787
|
styles: s,
|
|
728
788
|
type: "column",
|
|
@@ -737,10 +797,10 @@ var N = r(({ children: e, width: t, style: n, className: r, ...i }) => {
|
|
|
737
797
|
})
|
|
738
798
|
});
|
|
739
799
|
});
|
|
740
|
-
|
|
800
|
+
F.displayName = "ColumnCell";
|
|
741
801
|
//#endregion
|
|
742
802
|
//#region src/Components/TableBody.tsx
|
|
743
|
-
var
|
|
803
|
+
var I = n(({ children: t, style: n, className: r }, i) => {
|
|
744
804
|
let a = u(null);
|
|
745
805
|
s(i, () => a.current, []);
|
|
746
806
|
let { state: l, dispatch: d } = v(), h = c(() => {
|
|
@@ -799,7 +859,7 @@ var P = n(({ children: t, style: n, className: r }, i) => {
|
|
|
799
859
|
})
|
|
800
860
|
}), l.refs.cloneRef.current), /* @__PURE__ */ f("div", {
|
|
801
861
|
className: `body ${r ?? ""}`,
|
|
802
|
-
style:
|
|
862
|
+
style: L,
|
|
803
863
|
children: /* @__PURE__ */ f("div", {
|
|
804
864
|
className: "ibody",
|
|
805
865
|
style: _,
|
|
@@ -809,24 +869,24 @@ var P = n(({ children: t, style: n, className: r }, i) => {
|
|
|
809
869
|
children: t
|
|
810
870
|
})
|
|
811
871
|
})] });
|
|
812
|
-
}),
|
|
872
|
+
}), L = {
|
|
813
873
|
display: "flex",
|
|
814
874
|
overflow: "hidden",
|
|
815
875
|
flex: 1
|
|
816
876
|
};
|
|
817
|
-
|
|
877
|
+
I.displayName = "TableBody";
|
|
818
878
|
//#endregion
|
|
819
879
|
//#region src/Components/BodyRow.tsx
|
|
820
|
-
var
|
|
880
|
+
var R = {
|
|
821
881
|
display: "flex",
|
|
822
882
|
flex: "1 0 auto",
|
|
823
883
|
minHeight: "24px"
|
|
824
|
-
},
|
|
884
|
+
}, z = r(({ children: e, style: t, className: n, ...r }) => {
|
|
825
885
|
let i = c(() => t ? {
|
|
826
|
-
...
|
|
886
|
+
...R,
|
|
827
887
|
...t
|
|
828
|
-
} :
|
|
829
|
-
return /* @__PURE__ */ f(
|
|
888
|
+
} : R, [t]), a = h("tr", n);
|
|
889
|
+
return /* @__PURE__ */ f(P, {
|
|
830
890
|
...r,
|
|
831
891
|
type: "row",
|
|
832
892
|
children: /* @__PURE__ */ f("div", {
|
|
@@ -836,10 +896,10 @@ var I = {
|
|
|
836
896
|
})
|
|
837
897
|
});
|
|
838
898
|
});
|
|
839
|
-
|
|
899
|
+
z.displayName = "BodyRow";
|
|
840
900
|
//#endregion
|
|
841
901
|
//#region src/Components/RowCell.tsx
|
|
842
|
-
var
|
|
902
|
+
var B = r(({ children: e, style: t, className: n, isClone: r, ...i }) => {
|
|
843
903
|
let { index: a } = i, { state: o } = v(), s = c(() => o.columnIds[a] ?? "", [o.columnIds, a]), l = c(() => o.widths[a] ?? o.options.defaultSizing, [
|
|
844
904
|
o.widths,
|
|
845
905
|
a,
|
|
@@ -866,10 +926,10 @@ var R = r(({ children: e, style: t, className: n, isClone: r, ...i }) => {
|
|
|
866
926
|
children: e
|
|
867
927
|
});
|
|
868
928
|
});
|
|
869
|
-
|
|
929
|
+
B.displayName = "RowCell";
|
|
870
930
|
//#endregion
|
|
871
931
|
//#region src/Components/DragHandle.tsx
|
|
872
|
-
var
|
|
932
|
+
var V = r(({ children: e, className: t, style: n }) => /* @__PURE__ */ f("div", {
|
|
873
933
|
"data-drag-handle": "true",
|
|
874
934
|
className: t,
|
|
875
935
|
style: {
|
|
@@ -880,6 +940,6 @@ var z = r(({ children: e, className: t, style: n }) => /* @__PURE__ */ f("div",
|
|
|
880
940
|
},
|
|
881
941
|
children: e
|
|
882
942
|
}));
|
|
883
|
-
|
|
943
|
+
V.displayName = "DragHandle";
|
|
884
944
|
//#endregion
|
|
885
|
-
export {
|
|
945
|
+
export { z as BodyRow, F as ColumnCell, V as DragHandle, B as RowCell, I as TableBody, M as TableContainer, N as TableHeader, v as useTable };
|
package/package.json
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "flexitablesort",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "Sortable table with draggable rows and columns",
|
|
6
|
-
"main": "dist/index.cjs.js",
|
|
7
|
-
"module": "dist/index.es.js",
|
|
8
|
-
"types": "dist/index.d.ts",
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"import": "./dist/index.es.js",
|
|
12
|
-
"require": "./dist/index.cjs.js",
|
|
13
|
-
"types": "./dist/index.d.ts"
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"sideEffects": false,
|
|
17
|
-
"files": [
|
|
18
|
-
"dist"
|
|
19
|
-
],
|
|
20
|
-
"scripts": {
|
|
21
|
-
"build": "tsc -b && vite build",
|
|
22
|
-
"dev": "vite --config vite.config.docs.ts",
|
|
23
|
-
"build:docs": "vite build --config vite.config.docs.ts",
|
|
24
|
-
"lint": "eslint ."
|
|
25
|
-
},
|
|
26
|
-
"keywords": [
|
|
27
|
-
"react",
|
|
28
|
-
"table",
|
|
29
|
-
"sortable",
|
|
30
|
-
"draggable",
|
|
31
|
-
"draggable table",
|
|
32
|
-
"draggable rows",
|
|
33
|
-
"draggable columns",
|
|
34
|
-
"react table",
|
|
35
|
-
"data grid",
|
|
36
|
-
"row reordering",
|
|
37
|
-
"column reordering"
|
|
38
|
-
],
|
|
39
|
-
"author": "Sami Odeh",
|
|
40
|
-
"license": "MIT",
|
|
41
|
-
"repository": {
|
|
42
|
-
"type": "git",
|
|
43
|
-
"url": "git+https://github.com/samiodeh1337/sortable-table.git"
|
|
44
|
-
},
|
|
45
|
-
"homepage": "https://github.com/samiodeh1337/sortable-table.git#readme",
|
|
46
|
-
"peerDependencies": {
|
|
47
|
-
"react": ">=17.0.0",
|
|
48
|
-
"react-dom": ">=17.0.0"
|
|
49
|
-
},
|
|
50
|
-
"dependencies": {
|
|
51
|
-
"classnames": "^2.5.1",
|
|
52
|
-
"styled-components": "^6.3.11"
|
|
53
|
-
},
|
|
54
|
-
"devDependencies": {
|
|
55
|
-
"@tanstack/react-virtual": "^3.13.22",
|
|
56
|
-
"highlight.js": "^11.11.1",
|
|
57
|
-
"@eslint/js": "^9.39.4",
|
|
58
|
-
"@types/node": "^24.12.0",
|
|
59
|
-
"@types/react": "^19.2.14",
|
|
60
|
-
"@types/react-dom": "^19.2.3",
|
|
61
|
-
"@vitejs/plugin-react": "^6.0.0",
|
|
62
|
-
"eslint": "^9.39.4",
|
|
63
|
-
"eslint-plugin-react-hooks": "^7.0.1",
|
|
64
|
-
"eslint-plugin-react-refresh": "^0.5.2",
|
|
65
|
-
"globals": "^17.4.0",
|
|
66
|
-
"react": "^19.2.4",
|
|
67
|
-
"react-dom": "^19.2.4",
|
|
68
|
-
"typescript": "~5.9.3",
|
|
69
|
-
"typescript-eslint": "^8.56.1",
|
|
70
|
-
"vite": "^8.0.0",
|
|
71
|
-
"vite-plugin-dts": "^4.5.4"
|
|
72
|
-
}
|
|
73
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "flexitablesort",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Sortable table with draggable rows and columns",
|
|
6
|
+
"main": "dist/index.cjs.js",
|
|
7
|
+
"module": "dist/index.es.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.es.js",
|
|
12
|
+
"require": "./dist/index.cjs.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc -b && vite build",
|
|
22
|
+
"dev": "vite --config vite.config.docs.ts",
|
|
23
|
+
"build:docs": "vite build --config vite.config.docs.ts",
|
|
24
|
+
"lint": "eslint ."
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"react",
|
|
28
|
+
"table",
|
|
29
|
+
"sortable",
|
|
30
|
+
"draggable",
|
|
31
|
+
"draggable table",
|
|
32
|
+
"draggable rows",
|
|
33
|
+
"draggable columns",
|
|
34
|
+
"react table",
|
|
35
|
+
"data grid",
|
|
36
|
+
"row reordering",
|
|
37
|
+
"column reordering"
|
|
38
|
+
],
|
|
39
|
+
"author": "Sami Odeh",
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/samiodeh1337/sortable-table.git"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/samiodeh1337/sortable-table.git#readme",
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"react": ">=17.0.0",
|
|
48
|
+
"react-dom": ">=17.0.0"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"classnames": "^2.5.1",
|
|
52
|
+
"styled-components": "^6.3.11"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@tanstack/react-virtual": "^3.13.22",
|
|
56
|
+
"highlight.js": "^11.11.1",
|
|
57
|
+
"@eslint/js": "^9.39.4",
|
|
58
|
+
"@types/node": "^24.12.0",
|
|
59
|
+
"@types/react": "^19.2.14",
|
|
60
|
+
"@types/react-dom": "^19.2.3",
|
|
61
|
+
"@vitejs/plugin-react": "^6.0.0",
|
|
62
|
+
"eslint": "^9.39.4",
|
|
63
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
64
|
+
"eslint-plugin-react-refresh": "^0.5.2",
|
|
65
|
+
"globals": "^17.4.0",
|
|
66
|
+
"react": "^19.2.4",
|
|
67
|
+
"react-dom": "^19.2.4",
|
|
68
|
+
"typescript": "~5.9.3",
|
|
69
|
+
"typescript-eslint": "^8.56.1",
|
|
70
|
+
"vite": "^8.0.0",
|
|
71
|
+
"vite-plugin-dts": "^4.5.4"
|
|
72
|
+
}
|
|
73
|
+
}
|