simple-table-core 0.2.7 → 0.2.8
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/context/TableContext.d.ts +7 -2
- package/dist/index.js +1 -1
- package/package.json +2 -2
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import CellValue from "../types/CellValue";
|
|
3
3
|
declare const TableContext: import("react").Context<{
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
rows: {
|
|
5
|
+
[key: string]: CellValue;
|
|
6
|
+
}[];
|
|
7
|
+
tableRows: {
|
|
8
|
+
[key: string]: CellValue;
|
|
9
|
+
}[];
|
|
10
|
+
}>;
|
|
6
11
|
export default TableContext;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsxs as e,jsx as n,Fragment as r}from"react/jsx-runtime";import t,{useState as o,useRef as a,useCallback as i,useEffect as c,Children as l,useLayoutEffect as u,forwardRef as s,createRef as d,createContext as f,useContext as g,Fragment as h,useMemo as v,useReducer as p}from"react";var m=function(){return m=Object.assign||function(e){for(var n,r=1,t=arguments.length;r<t;r++)for(var o in n=arguments[r])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},m.apply(this,arguments)};function w(e,n,r){if(r||2===arguments.length)for(var t,o=0,a=n.length;o<a;o++)!t&&o in n||(t||(t=Array.prototype.slice.call(n,0,o)),t[o]=n[o]);return e.concat(t||Array.prototype.slice.call(n))}"function"==typeof SuppressedError&&SuppressedError;var C=function(e){var n={};return l.forEach(e,(function(e,r){if(e.ref&&e.ref.current){var t=e.ref.current.getBoundingClientRect();n[e.key]=t}})),n},b=function(e){var n,r,i=e.allowHorizontalAnimate,l=void 0===i||i,s=e.children,d=e.pauseAnimation,f=o({}),g=f[0],h=f[1],v=o({}),p=v[0],m=v[1],w=(n=s,r=a(),c((function(){r.current=n}),[n]),r.current);return u((function(){var e=C(s);h(e)}),[s]),u((function(){var e=C(w);m(e)}),[w]),c((function(){d||Object.keys(p).length&&t.Children.forEach(s,(function(e){var n=e.ref.current,r=p[e.key],t=g[e.key];if(r&&t){var o=r.left-t.left,a=l?0:r.top-t.top,i=Math.abs(o),c=Math.abs(a);(i>10||c>10)&&requestAnimationFrame((function(){n.style.transform="translate(".concat(o,"px, ").concat(a,"px)"),n.style.transition="transform 0s",requestAnimationFrame((function(){n.style.transform="",n.style.transition="transform 500ms"}))}))}}))}),[l,g,s,d,p]),s},y=!1,D=function(e){var n=e.draggedHeaderRef,r=e.headersRef,t=e.hoveredHeaderRef,o=e.onTableHeaderDragEnd;return{handleDragStart:function(e){n.current=e},handleDragOver:function(e){!function(e){var a;if(!y&&(t.current=e,e.accessor!==(null===(a=n.current)||void 0===a?void 0:a.accessor)&&null!==n.current&&!y)){if(y=!0,!r.current)return;var i=w([],r.current,!0),c=i.findIndex((function(e){var r;return e.accessor===(null===(r=n.current)||void 0===r?void 0:r.accessor)})),l=i.findIndex((function(n){return n.accessor===e.accessor}));if(void 0===c||void 0===l)return;var u=i.splice(c,1)[0];i.splice(l,0,u),JSON.stringify(i)!==JSON.stringify(r.current)&&setTimeout((function(){o(i),setTimeout((function(){y=!1}),500)}),50)}}(e)},handleDragEnd:function(){n.current=null,t.current=null}}},x=function(e,n){var r=!0,t=!0;return function(){for(var o=[],a=0;a<arguments.length;a++)o[a]=arguments[a];if(r)return r=!1,void setTimeout((function(){return t=!1}),n);t||(e.apply(this,o),t=!0,setTimeout((function(){return t=!1}),n))}},R=s((function(r,t){var i,c=r.draggedHeaderRef,l=r.enableColumnResizing,u=r.forceUpdate,s=r.headersRef,d=r.hoveredHeaderRef,f=r.index,g=r.onSort,h=r.onTableHeaderDragEnd,v=r.setIsWidthDragging,p=a({pageX:0,pageY:0}),w=o(!1),C=w[0],b=w[1],y=null===(i=s.current)||void 0===i?void 0:i[f],R=D({draggedHeaderRef:c,headersRef:s,hoveredHeaderRef:d,onTableHeaderDragEnd:h}),M=R.handleDragStart,I=R.handleDragOver,E=R.handleDragEnd,L=a(x((function(e){I(e)}),100)).current;return y?e("div",m({className:"st-header-cell ".concat(y===d.current?"st-hovered":""," ").concat(C?"st-dragging":""),ref:t,style:{width:y.width}},{children:[n("div",m({className:"st-header-label",draggable:!0,onClick:function(){return g(f)},onDragStart:function(){return function(e){b(!0),M(e)}(y)},onDragOver:function(e){var n=e.pageX,r=e.pageY;n===p.current.pageX&&r===p.current.pageY||(p.current={pageX:n,pageY:r},e.preventDefault(),L(y,e))},onDragEnd:function(){b(!1),E()}},{children:null==y?void 0:y.label})),l&&n("div",{className:"st-header-resize-handle",onMouseDown:function(e){v(!0),e.preventDefault();var n=e.clientX;if(y){var r=y.width,t=x((function(e){var t=Math.max(r+(e.clientX-n),10);y&&(s.current[f].width=t,u())}),10),o=function(){document.removeEventListener("mousemove",t),document.removeEventListener("mouseup",o),v(!1)};document.addEventListener("mousemove",t),document.addEventListener("mouseup",o)}}})]})):null})),M=s((function(e,r){return e.visible?n("div",{className:"st-cell",ref:r}):n("div",{ref:r})})),I=function(){return n("div",{className:"st-row-separator"})},E=function(t){var o,i=t.enableColumnResizing,c=t.forceUpdate,l=t.headersRef,u=t.isWidthDragging,s=t.onSort,f=t.onTableHeaderDragEnd,g=t.setIsWidthDragging,h=t.shouldDisplayLastColumnCell,v=a(null),p=a(null);return e(r,{children:[e(b,m({pauseAnimation:u},{children:[null===(o=l.current)||void 0===o?void 0:o.map((function(e,r){return n(R,{draggedHeaderRef:v,enableColumnResizing:i,forceUpdate:c,headersRef:l,hoveredHeaderRef:p,index:r,onSort:s,onTableHeaderDragEnd:f,ref:d(),setIsWidthDragging:g},e.accessor)})),n(M,{ref:d(),visible:h})]})),n(I,{})]})},L=function(r){var t=r.value,o=r.onBlur,a=r.onChange;return e("select",m({value:t.toString(),onBlur:o,onChange:a},{children:[n("option",m({value:"true"},{children:"True"})),n("option",m({value:"false"},{children:"False"}))]}))},S=function(e){var r=e.defaultValue,t=e.onBlur,o=e.onChange,i=a(null);return n("input",{className:"editable-cell-input",ref:i,autoFocus:!0,type:"text",defaultValue:r,onBlur:t,onChange:o})},T=function(e){var t=e.onChange,o=e.setIsEditing,a=e.value,i=function(e){var n=e.target.value;null==t||t(n)},c=function(){o(!1)};return n(r,{children:"string"==typeof a?n(S,{defaultValue:a,onBlur:c,onChange:i}):"boolean"==typeof a?n(L,{value:a,onBlur:c,onChange:i}):null})},N=f([]),O=s((function(e,r){var t=e.borderClass,a=e.colIndex,i=e.content,l=e.header,u=e.isSelected,s=e.isTopLeftCell,d=e.onCellChange,f=e.onMouseDown,h=e.onMouseOver,v=e.row,p=e.rowIndex,w=g(N),C=o(i),b=C[0],y=C[1],D=o(0===p&&0===a),x=D[0],R=D[1],M=p%2==0,I="st-cell ".concat(u?s?"st-cell-selected-first-cell ".concat(t):"st-cell-selected ".concat(t):""," ").concat(M?"st-cell-odd-row":"");c((function(){y(i)}),[i]),c((function(){if(void 0!==v.originalRowIndex&&"number"==typeof v.originalRowIndex){var e=w[v.originalRowIndex];e[l.accessor]!==b&&y(e[l.accessor])}}),[l.accessor,b,w]);return n("div",x?m({className:"st-cell-editing ".concat(M?"st-cell-odd-row":"")},{children:n(T,{onChange:function(e){y(e),null==d||d({accessor:l.accessor,newValue:e,newRowIndex:p,originalRowIndex:v.originalRowIndex,row:v})},setIsEditing:R,value:b})}):m({className:I,onDoubleClick:function(){return l.isEditable&&R(!0)},onMouseDown:function(){return f(p,a)},onMouseOver:function(){return h(p,a)},ref:r},{children:b}))})),H=function(t){var o=t.getBorderClass,a=t.handleMouseDown,i=t.handleMouseOver,c=t.headers,l=t.isSelected,u=t.isTopLeftCell,s=t.isWidthDragging,f=t.onCellChange,g=t.shouldDisplayLastColumnCell,v=t.shouldPaginate,p=t.sortedRows;return n(r,{children:p.map((function(r,t){return e(h,{children:[e(b,m({allowHorizontalAnimate:v,pauseAnimation:s},{children:[c.map((function(e,c){var s=r[e.accessor];return e.cellRenderer&&(s=e.cellRenderer(r)),n(O,{borderClass:o(t,c),colIndex:c,content:s,header:e,isSelected:l(t,c),isTopLeftCell:u(t,c),onCellChange:f,onMouseDown:function(){return a(t,c)},onMouseOver:function(){return i(t,c)},ref:d(),row:r,rowIndex:t},e.accessor)})),n(M,{ref:d(),visible:g})]})),t!==p.length-1&&n(I,{})]},r.originalRowIndex)}))})},k=function(r){var t=r.currentPage,o=r.hideFooter,a=r.nextIcon,i=r.onPageChange,c=r.prevIcon,l=r.rowsPerPage,u=r.totalRows,s=Math.ceil(u/l),d=function(e){e>=1&&e<=s&&i(e)};return o?null:e("div",m({className:"st-footer"},{children:[n("button",m({className:"st-next-prev-btn",onClick:function(){return d(t-1)},disabled:1===t},{children:c})),n("button",m({className:"st-next-prev-btn",onClick:function(){return d(t+1)},disabled:t===s},{children:a})),Array.from({length:s},(function(e,r){return n("button",m({onClick:function(){return d(r+1)},className:"st-page-btn ".concat(t===r+1?"active":"")},{children:r+1}),r)}))]}))},P=function(){return n("svg",m({className:"angle-icon",viewBox:"0 0 24 24",width:"24",height:"24",xmlns:"http://www.w3.org/2000/svg"},{children:n("path",{d:"M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z"})}))},B=function(){return n("svg",m({className:"angle-icon",viewBox:"0 0 24 24",width:"24",height:"24",xmlns:"http://www.w3.org/2000/svg"},{children:n("path",{d:"M8.59 16.59L10 18l6-6-6-6-1.41 1.41L13.17 12z"})}))},j=function(r){var t,l=r.defaultHeaders,u=r.enableColumnResizing,s=void 0===u||u,d=r.height,f=r.hideFooter,g=void 0!==f&&f,h=r.nextIcon,C=void 0===h?n(B,{}):h,b=r.onCellChange,y=r.prevIcon,D=void 0===y?n(P,{}):y,x=r.rows,R=r.rowsPerPage,M=void 0===R?10:R,I=r.shouldPaginate,L=void 0===I||I,S=v((function(){return x.map((function(e,n){return m(m({},e),{originalRowIndex:n})}))}),[x]),T=a(null),O=o(!1),j=O[0],z=O[1],A=a(l),W=o(null),F=W[0],U=W[1],X=o(1),V=X[0],Y=X[1],q=v((function(){if(!F)return S;var e=function(e,n,r){var t=r?r.key:e[0],o="ascending";return r&&r.key.accessor===t.accessor&&"ascending"===r.direction&&(o="descending"),{sortedData:w([],n,!0).sort((function(e,n){return e[t.accessor]<n[t.accessor]?"ascending"===o?-1:1:e[t.accessor]>n[t.accessor]?"ascending"===o?1:-1:0})),newSortConfig:{key:t,direction:o}}}(A.current,S,F).sortedData;return e}),[S,F]),J=p((function(e){return e+1}),0)[1],K=function(e,n){var r=o([]),t=r[0],l=r[1],u=a(!1),s=a(null),d=i((function(){var r=t.reduce((function(r,t){var o=t.row,a=t.col;return r[o]||(r[o]=[]),r[o][a]=e[o][n[a].accessor],r}),{}),o=Object.values(r).map((function(e){return Object.values(e).join("\t")})).join("\n");navigator.clipboard.writeText(o)}),[t,e,n]);c((function(){var e=function(e){(e.ctrlKey||e.metaKey)&&"c"===e.key&&d()};return document.addEventListener("keydown",e),function(){document.removeEventListener("keydown",e)}}),[d,t]);var f=function(e,n){return t.some((function(r){return r.row===e&&r.col===n}))};return{selectedCells:t,handleMouseDown:function(e,n){u.current=!0,s.current={row:e,col:n},l([{row:e,col:n}])},handleMouseOver:function(e,n){if(u.current&&s.current){for(var r=[],t=Math.min(s.current.row,e),o=Math.max(s.current.row,e),a=Math.min(s.current.col,n),i=Math.max(s.current.col,n),c=t;c<=o;c++)for(var d=a;d<=i;d++)r.push({row:c,col:d});l(r)}},handleMouseUp:function(){u.current=!1,s.current=null},isSelected:f,getBorderClass:function(e,n){var r=[];return f(e-1,n)||r.push("st-selected-top-border"),f(e+1,n)||r.push("st-selected-bottom-border"),f(e,n-1)||r.push("st-selected-left-border"),f(e,n+1)||r.push("st-selected-right-border"),r.join(" ")},isTopLeftCell:function(e,n){return e===Math.min.apply(Math,t.map((function(e){return e.row})))&&n===Math.min.apply(Math,t.map((function(e){return e.col})))},setSelectedCells:l}}(q,A.current),G=K.handleMouseDown,Q=K.handleMouseOver,Z=K.handleMouseUp,$=K.isSelected,_=K.getBorderClass,ee=K.isTopLeftCell,ne=K.setSelectedCells,re=v((function(){return!!T.current&&A.current.reduce((function(e,n){return e+n.width}),0)<T.current.clientWidth}),[]),te=L?q.slice((V-1)*M,V*M):q;return c((function(){var e=function(e){e.target.closest(".st-cell")||ne([])};return document.addEventListener("mousedown",e),function(){document.removeEventListener("mousedown",e)}}),[ne]),n(N.Provider,m({value:x},{children:e("div",m({ref:T,className:"st-wrapper",style:d?{height:d}:{}},{children:[e("div",m({className:"st-table",onMouseUp:Z,onMouseLeave:Z,style:{gridTemplateColumns:"".concat(null===(t=A.current)||void 0===t?void 0:t.map((function(e){return"".concat(e.width,"px")})).join(" ")," 1fr")}},{children:[n(E,{enableColumnResizing:s,forceUpdate:J,headersRef:A,isWidthDragging:j,onSort:function(e){U((function(n){return{key:A.current[e],direction:"ascending"===(null==n?void 0:n.direction)?"descending":"ascending"}}))},onTableHeaderDragEnd:function(e){A.current=e,J()},setIsWidthDragging:z,shouldDisplayLastColumnCell:re}),n(H,{getBorderClass:_,handleMouseDown:G,handleMouseOver:Q,headers:A.current,isSelected:$,isTopLeftCell:ee,isWidthDragging:j,onCellChange:b,shouldDisplayLastColumnCell:re,shouldPaginate:L,sortedRows:te})]})),L&&n(k,{currentPage:V,hideFooter:g,onPageChange:Y,rowsPerPage:M,totalRows:q.length,nextIcon:C,prevIcon:D})]}))}))};export{j as SimpleTable};
|
|
1
|
+
import{jsxs as e,jsx as n,Fragment as r}from"react/jsx-runtime";import t,{useState as o,useRef as a,useCallback as i,useEffect as c,Children as l,useLayoutEffect as u,forwardRef as s,createRef as d,createContext as f,useContext as g,Fragment as h,useMemo as v,useReducer as p}from"react";var m=function(){return m=Object.assign||function(e){for(var n,r=1,t=arguments.length;r<t;r++)for(var o in n=arguments[r])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},m.apply(this,arguments)};function w(e,n,r){if(r||2===arguments.length)for(var t,o=0,a=n.length;o<a;o++)!t&&o in n||(t||(t=Array.prototype.slice.call(n,0,o)),t[o]=n[o]);return e.concat(t||Array.prototype.slice.call(n))}"function"==typeof SuppressedError&&SuppressedError;var C=function(e){var n={};return l.forEach(e,(function(e,r){if(e.ref&&e.ref.current){var t=e.ref.current.getBoundingClientRect();n[e.key]=t}})),n},b=function(e){var n,r,i=e.allowHorizontalAnimate,l=void 0===i||i,s=e.children,d=e.pauseAnimation,f=o({}),g=f[0],h=f[1],v=o({}),p=v[0],m=v[1],w=(n=s,r=a(),c((function(){r.current=n}),[n]),r.current);return u((function(){var e=C(s);h(e)}),[s]),u((function(){var e=C(w);m(e)}),[w]),c((function(){d||Object.keys(p).length&&t.Children.forEach(s,(function(e){var n=e.ref.current,r=p[e.key],t=g[e.key];if(r&&t){var o=r.left-t.left,a=l?0:r.top-t.top,i=Math.abs(o),c=Math.abs(a);(i>10||c>10)&&requestAnimationFrame((function(){n.style.transform="translate(".concat(o,"px, ").concat(a,"px)"),n.style.transition="transform 0s",requestAnimationFrame((function(){n.style.transform="",n.style.transition="transform 500ms"}))}))}}))}),[l,g,s,d,p]),s},y=!1,D=function(e){var n=e.draggedHeaderRef,r=e.headersRef,t=e.hoveredHeaderRef,o=e.onTableHeaderDragEnd;return{handleDragStart:function(e){n.current=e},handleDragOver:function(e){!function(e){var a;if(!y&&(t.current=e,e.accessor!==(null===(a=n.current)||void 0===a?void 0:a.accessor)&&null!==n.current&&!y)){if(y=!0,!r.current)return;var i=w([],r.current,!0),c=i.findIndex((function(e){var r;return e.accessor===(null===(r=n.current)||void 0===r?void 0:r.accessor)})),l=i.findIndex((function(n){return n.accessor===e.accessor}));if(void 0===c||void 0===l)return;var u=i.splice(c,1)[0];i.splice(l,0,u),JSON.stringify(i)!==JSON.stringify(r.current)&&setTimeout((function(){o(i),setTimeout((function(){y=!1}),500)}),50)}}(e)},handleDragEnd:function(){n.current=null,t.current=null}}},R=function(e,n){var r=!0,t=!0;return function(){for(var o=[],a=0;a<arguments.length;a++)o[a]=arguments[a];if(r)return r=!1,void setTimeout((function(){return t=!1}),n);t||(e.apply(this,o),t=!0,setTimeout((function(){return t=!1}),n))}},x=s((function(r,t){var i,c=r.draggedHeaderRef,l=r.enableColumnResizing,u=r.forceUpdate,s=r.headersRef,d=r.hoveredHeaderRef,f=r.index,g=r.onSort,h=r.onTableHeaderDragEnd,v=r.setIsWidthDragging,p=a({pageX:0,pageY:0}),w=o(!1),C=w[0],b=w[1],y=null===(i=s.current)||void 0===i?void 0:i[f],x=D({draggedHeaderRef:c,headersRef:s,hoveredHeaderRef:d,onTableHeaderDragEnd:h}),M=x.handleDragStart,I=x.handleDragOver,E=x.handleDragEnd,L=a(R((function(e){I(e)}),100)).current;return y?e("div",m({className:"st-header-cell ".concat(y===d.current?"st-hovered":""," ").concat(C?"st-dragging":""),ref:t,style:{width:y.width}},{children:[n("div",m({className:"st-header-label",draggable:!0,onClick:function(){return g(f)},onDragStart:function(){return function(e){b(!0),M(e)}(y)},onDragOver:function(e){var n=e.pageX,r=e.pageY;n===p.current.pageX&&r===p.current.pageY||(p.current={pageX:n,pageY:r},e.preventDefault(),L(y,e))},onDragEnd:function(){b(!1),E()}},{children:null==y?void 0:y.label})),l&&n("div",{className:"st-header-resize-handle",onMouseDown:function(e){v(!0),e.preventDefault();var n=e.clientX;if(y){var r=y.width,t=R((function(e){var t=Math.max(r+(e.clientX-n),10);y&&(s.current[f].width=t,u())}),10),o=function(){document.removeEventListener("mousemove",t),document.removeEventListener("mouseup",o),v(!1)};document.addEventListener("mousemove",t),document.addEventListener("mouseup",o)}}})]})):null})),M=s((function(e,r){return e.visible?n("div",{className:"st-cell",ref:r}):n("div",{ref:r})})),I=function(){return n("div",{className:"st-row-separator"})},E=function(t){var o,i=t.enableColumnResizing,c=t.forceUpdate,l=t.headersRef,u=t.isWidthDragging,s=t.onSort,f=t.onTableHeaderDragEnd,g=t.setIsWidthDragging,h=t.shouldDisplayLastColumnCell,v=a(null),p=a(null);return e(r,{children:[e(b,m({pauseAnimation:u},{children:[null===(o=l.current)||void 0===o?void 0:o.map((function(e,r){return n(x,{draggedHeaderRef:v,enableColumnResizing:i,forceUpdate:c,headersRef:l,hoveredHeaderRef:p,index:r,onSort:s,onTableHeaderDragEnd:f,ref:d(),setIsWidthDragging:g},e.accessor)})),n(M,{ref:d(),visible:h})]})),n(I,{})]})},L=function(r){var t=r.value,o=r.onBlur,a=r.onChange;return e("select",m({value:t.toString(),onBlur:o,onChange:a},{children:[n("option",m({value:"true"},{children:"True"})),n("option",m({value:"false"},{children:"False"}))]}))},S=function(e){var r=e.defaultValue,t=e.onBlur,o=e.onChange,i=a(null);return n("input",{className:"editable-cell-input",ref:i,autoFocus:!0,type:"text",defaultValue:r,onBlur:t,onChange:o})},T=function(e){var t=e.onChange,o=e.setIsEditing,a=e.value,i=function(e){var n=e.target.value;null==t||t(n)},c=function(){o(!1)};return n(r,{children:"string"==typeof a?n(S,{defaultValue:a,onBlur:c,onChange:i}):"boolean"==typeof a?n(L,{value:a,onBlur:c,onChange:i}):null})},N=f({rows:[],tableRows:[]}),O=s((function(e,r){var t=e.borderClass,a=e.colIndex,i=e.content,l=e.header,u=e.isSelected,s=e.isTopLeftCell,d=e.onCellChange,f=e.onMouseDown,h=e.onMouseOver,v=e.row,p=e.rowIndex,w=g(N),C=w.rows,b=w.tableRows,y=o(i),D=y[0],R=y[1],x=o(0===p&&0===a),M=x[0],I=x[1],E=p%2==0,L="st-cell ".concat(u?s?"st-cell-selected-first-cell ".concat(t):"st-cell-selected ".concat(t):""," ").concat(E?"st-cell-odd-row":"");c((function(){R(i)}),[i]),c((function(){if(void 0!==v.originalRowIndex&&"number"==typeof v.originalRowIndex){var e=C[v.originalRowIndex];e[l.accessor]!==D?R(e[l.accessor]):b[v.originalRowIndex][l.accessor]=D}}),[l.accessor,D,C,v.originalRowIndex,b]);return n("div",M?m({className:"st-cell-editing ".concat(E?"st-cell-odd-row":"")},{children:n(T,{onChange:function(e){R(e),null==d||d({accessor:l.accessor,newValue:e,newRowIndex:p,originalRowIndex:v.originalRowIndex,row:v})},setIsEditing:I,value:D})}):m({className:L,onDoubleClick:function(){return l.isEditable&&I(!0)},onMouseDown:function(){return f(p,a)},onMouseOver:function(){return h(p,a)},ref:r},{children:D}))})),H=function(t){var o=t.getBorderClass,a=t.handleMouseDown,i=t.handleMouseOver,c=t.headers,l=t.isSelected,u=t.isTopLeftCell,s=t.isWidthDragging,f=t.onCellChange,g=t.shouldDisplayLastColumnCell,v=t.shouldPaginate,p=t.sortedRows;return n(r,{children:p.map((function(r,t){return e(h,{children:[e(b,m({allowHorizontalAnimate:v,pauseAnimation:s},{children:[c.map((function(e,c){var s=r[e.accessor];return e.cellRenderer&&(s=e.cellRenderer(r)),n(O,{borderClass:o(t,c),colIndex:c,content:s,header:e,isSelected:l(t,c),isTopLeftCell:u(t,c),onCellChange:f,onMouseDown:function(){return a(t,c)},onMouseOver:function(){return i(t,c)},ref:d(),row:r,rowIndex:t},e.accessor)})),n(M,{ref:d(),visible:g})]})),t!==p.length-1&&n(I,{})]},r.originalRowIndex)}))})},k=function(r){var t=r.currentPage,o=r.hideFooter,a=r.nextIcon,i=r.onPageChange,c=r.prevIcon,l=r.rowsPerPage,u=r.totalRows,s=Math.ceil(u/l),d=function(e){e>=1&&e<=s&&i(e)};return o?null:e("div",m({className:"st-footer"},{children:[n("button",m({className:"st-next-prev-btn",onClick:function(){return d(t-1)},disabled:1===t},{children:c})),n("button",m({className:"st-next-prev-btn",onClick:function(){return d(t+1)},disabled:t===s},{children:a})),Array.from({length:s},(function(e,r){return n("button",m({onClick:function(){return d(r+1)},className:"st-page-btn ".concat(t===r+1?"active":"")},{children:r+1}),r)}))]}))},P=function(){return n("svg",m({className:"angle-icon",viewBox:"0 0 24 24",width:"24",height:"24",xmlns:"http://www.w3.org/2000/svg"},{children:n("path",{d:"M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z"})}))},B=function(){return n("svg",m({className:"angle-icon",viewBox:"0 0 24 24",width:"24",height:"24",xmlns:"http://www.w3.org/2000/svg"},{children:n("path",{d:"M8.59 16.59L10 18l6-6-6-6-1.41 1.41L13.17 12z"})}))},j=function(r){var t,l=r.defaultHeaders,u=r.enableColumnResizing,s=void 0===u||u,d=r.height,f=r.hideFooter,g=void 0!==f&&f,h=r.nextIcon,C=void 0===h?n(B,{}):h,b=r.onCellChange,y=r.prevIcon,D=void 0===y?n(P,{}):y,R=r.rows,x=r.rowsPerPage,M=void 0===x?10:x,I=r.shouldPaginate,L=void 0===I||I,S=v((function(){return R.map((function(e,n){return m(m({},e),{originalRowIndex:n})}))}),[R]),T=a(null),O=o(!1),j=O[0],z=O[1],A=a(l),W=o(null),F=W[0],U=W[1],X=o(1),V=X[0],Y=X[1],q=v((function(){if(!F)return S;var e=function(e,n,r){var t=r?r.key:e[0],o="ascending";return r&&r.key.accessor===t.accessor&&"ascending"===r.direction&&(o="descending"),{sortedData:w([],n,!0).sort((function(e,n){return e[t.accessor]<n[t.accessor]?"ascending"===o?-1:1:e[t.accessor]>n[t.accessor]?"ascending"===o?1:-1:0})),newSortConfig:{key:t,direction:o}}}(A.current,S,F).sortedData;return e}),[S,F]),J=p((function(e){return e+1}),0)[1],K=function(e,n){var r=o([]),t=r[0],l=r[1],u=a(!1),s=a(null),d=i((function(){var r=t.reduce((function(r,t){var o=t.row,a=t.col;return r[o]||(r[o]=[]),r[o][a]=e[o][n[a].accessor],r}),{}),o=Object.values(r).map((function(e){return Object.values(e).join("\t")})).join("\n");navigator.clipboard.writeText(o)}),[t,e,n]);c((function(){var e=function(e){(e.ctrlKey||e.metaKey)&&"c"===e.key&&d()};return document.addEventListener("keydown",e),function(){document.removeEventListener("keydown",e)}}),[d,t]);var f=function(e,n){return t.some((function(r){return r.row===e&&r.col===n}))};return{selectedCells:t,handleMouseDown:function(e,n){u.current=!0,s.current={row:e,col:n},l([{row:e,col:n}])},handleMouseOver:function(e,n){if(u.current&&s.current){for(var r=[],t=Math.min(s.current.row,e),o=Math.max(s.current.row,e),a=Math.min(s.current.col,n),i=Math.max(s.current.col,n),c=t;c<=o;c++)for(var d=a;d<=i;d++)r.push({row:c,col:d});l(r)}},handleMouseUp:function(){u.current=!1,s.current=null},isSelected:f,getBorderClass:function(e,n){var r=[];return f(e-1,n)||r.push("st-selected-top-border"),f(e+1,n)||r.push("st-selected-bottom-border"),f(e,n-1)||r.push("st-selected-left-border"),f(e,n+1)||r.push("st-selected-right-border"),r.join(" ")},isTopLeftCell:function(e,n){return e===Math.min.apply(Math,t.map((function(e){return e.row})))&&n===Math.min.apply(Math,t.map((function(e){return e.col})))},setSelectedCells:l}}(q,A.current),G=K.handleMouseDown,Q=K.handleMouseOver,Z=K.handleMouseUp,$=K.isSelected,_=K.getBorderClass,ee=K.isTopLeftCell,ne=K.setSelectedCells,re=v((function(){return!!T.current&&A.current.reduce((function(e,n){return e+n.width}),0)<T.current.clientWidth}),[]),te=L?q.slice((V-1)*M,V*M):q;return c((function(){var e=function(e){e.target.closest(".st-cell")||ne([])};return document.addEventListener("mousedown",e),function(){document.removeEventListener("mousedown",e)}}),[ne]),n(N.Provider,m({value:{rows:R,tableRows:S}},{children:e("div",m({ref:T,className:"st-wrapper",style:d?{height:d}:{}},{children:[e("div",m({className:"st-table",onMouseUp:Z,onMouseLeave:Z,style:{gridTemplateColumns:"".concat(null===(t=A.current)||void 0===t?void 0:t.map((function(e){return"".concat(e.width,"px")})).join(" ")," 1fr")}},{children:[n(E,{enableColumnResizing:s,forceUpdate:J,headersRef:A,isWidthDragging:j,onSort:function(e){U((function(n){return{key:A.current[e],direction:"ascending"===(null==n?void 0:n.direction)?"descending":"ascending"}}))},onTableHeaderDragEnd:function(e){A.current=e,J()},setIsWidthDragging:z,shouldDisplayLastColumnCell:re}),n(H,{getBorderClass:_,handleMouseDown:G,handleMouseOver:Q,headers:A.current,isSelected:$,isTopLeftCell:ee,isWidthDragging:j,onCellChange:b,shouldDisplayLastColumnCell:re,shouldPaginate:L,sortedRows:te})]})),L&&n(k,{currentPage:V,hideFooter:g,onPageChange:Y,rowsPerPage:M,totalRows:q.length,nextIcon:C,prevIcon:D})]}))}))};export{j as SimpleTable};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simple-table-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.es.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"webpack": "^5.95.0"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
|
-
"publish": "npm run build && git add . && git commit -m \"$npm_config_message\" && npm publish && git push",
|
|
54
|
+
"publish-fast": "npm run build && git add . && git commit -m \"$npm_config_message\" && npm publish && git push",
|
|
55
55
|
"build": "rollup -c",
|
|
56
56
|
"start": "storybook dev -p 6006",
|
|
57
57
|
"build-storybook": "storybook build"
|