simple-table-core 0.2.6 → 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/README.md CHANGED
@@ -48,6 +48,7 @@ The Simple Table component accepts the following props:
48
48
  - **label**: A string representing the display name of the column header.
49
49
  - **accessor**: A string used to access the corresponding data in each row.
50
50
  - **width**: A number specifying the width of the column.
51
+ - **isEditable**: An optional boolean indicating if the column is editable.
51
52
  - **cellRenderer**: An optional function that takes a row object and returns a `ReactNode` for custom cell rendering.
52
53
 
53
54
  - **enableColumnResizing**: A boolean to enable or disable column resizing. Default is `true`.
@@ -58,6 +59,7 @@ The Simple Table component accepts the following props:
58
59
  - **rows**: An array of data rows to be displayed in the table.
59
60
  - **rowsPerPage**: The number of rows to display per page. Default is `10`.
60
61
  - **shouldPaginate**: A boolean to enable or disable pagination. Default is `true`.
62
+ - **onCellChange**: A function that is called when a cell value changes.
61
63
 
62
64
  ## Customizable Styles
63
65
 
Binary file
@@ -0,0 +1,8 @@
1
+ import React from "react";
2
+ interface BooleanSelectProps {
3
+ value: boolean;
4
+ onBlur: () => void;
5
+ onChange: (e: React.ChangeEvent<HTMLSelectElement>) => void;
6
+ }
7
+ declare const BooleanSelect: ({ value, onBlur, onChange }: BooleanSelectProps) => import("react/jsx-runtime").JSX.Element;
8
+ export default BooleanSelect;
@@ -0,0 +1,8 @@
1
+ import CellValue from "../../../types/CellValue";
2
+ interface EditableCellProps {
3
+ onChange: (newValue: CellValue) => void;
4
+ setIsEditing: (isEditing: boolean) => void;
5
+ value: CellValue;
6
+ }
7
+ declare const EditableCell: ({ onChange, setIsEditing, value }: EditableCellProps) => import("react/jsx-runtime").JSX.Element;
8
+ export default EditableCell;
@@ -0,0 +1,8 @@
1
+ import React from "react";
2
+ interface TextInputProps {
3
+ defaultValue: string;
4
+ onBlur: () => void;
5
+ onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
6
+ }
7
+ declare const TextEdit: ({ defaultValue, onBlur, onChange }: TextInputProps) => import("react/jsx-runtime").JSX.Element;
8
+ export default TextEdit;
@@ -1,18 +1,21 @@
1
1
  import { ReactNode } from "react";
2
2
  import HeaderObject from "../../types/HeaderObject";
3
3
  import "../../styles/simple-table.css";
4
+ import CellValue from "../../types/CellValue";
5
+ import CellChangeProps from "../../types/CellChangeProps";
4
6
  export interface SpreadsheetProps {
5
7
  defaultHeaders: HeaderObject[];
6
8
  enableColumnResizing?: boolean;
7
9
  height?: string;
8
10
  hideFooter?: boolean;
9
11
  nextIcon?: ReactNode;
12
+ onCellChange?: ({ accessor, newValue, originalRowIndex, row, }: CellChangeProps) => void;
10
13
  prevIcon?: ReactNode;
11
14
  rows: {
12
- [key: string]: string | number | boolean | undefined | null;
15
+ [key: string]: CellValue;
13
16
  }[];
14
17
  rowsPerPage?: number;
15
18
  shouldPaginate?: boolean;
16
19
  }
17
- declare const SimpleTable: ({ defaultHeaders, enableColumnResizing, height, hideFooter, nextIcon, prevIcon, rows, rowsPerPage, shouldPaginate, }: SpreadsheetProps) => import("react/jsx-runtime").JSX.Element;
20
+ declare const SimpleTable: ({ defaultHeaders, enableColumnResizing, height, hideFooter, nextIcon, onCellChange, prevIcon, rows, rowsPerPage, shouldPaginate, }: SpreadsheetProps) => import("react/jsx-runtime").JSX.Element;
18
21
  export default SimpleTable;
@@ -1,4 +1,5 @@
1
1
  import HeaderObject from "../../types/HeaderObject";
2
+ import CellChangeProps from "../../types/CellChangeProps";
2
3
  interface TableBodyProps {
3
4
  getBorderClass: (rowIndex: number, columnIndex: number) => string;
4
5
  handleMouseDown: (rowIndex: number, columnIndex: number) => void;
@@ -12,8 +13,9 @@ interface TableBodyProps {
12
13
  sortedRows: {
13
14
  [key: string]: any;
14
15
  }[];
16
+ onCellChange?: (props: CellChangeProps) => void;
15
17
  }
16
- declare const TableBody: ({ getBorderClass, handleMouseDown, handleMouseOver, headers, isSelected, isTopLeftCell, isWidthDragging, shouldDisplayLastColumnCell, shouldPaginate, sortedRows, }: TableBodyProps & {
18
+ declare const TableBody: ({ getBorderClass, handleMouseDown, handleMouseOver, headers, isSelected, isTopLeftCell, isWidthDragging, onCellChange, shouldDisplayLastColumnCell, shouldPaginate, sortedRows, }: TableBodyProps & {
17
19
  shouldDisplayLastColumnCell: boolean;
18
20
  }) => import("react/jsx-runtime").JSX.Element;
19
21
  export default TableBody;
@@ -1,12 +1,20 @@
1
1
  /// <reference types="react" />
2
+ import HeaderObject from "../../types/HeaderObject";
3
+ import CellChangeProps from "../../types/CellChangeProps";
4
+ import CellValue from "../../types/CellValue";
2
5
  interface TableCellProps {
3
6
  borderClass: string;
4
7
  colIndex: number;
5
- content: any;
8
+ content: CellValue;
9
+ header: HeaderObject;
6
10
  isSelected: boolean;
7
11
  isTopLeftCell: boolean;
12
+ onCellChange?: (props: CellChangeProps) => void;
8
13
  onMouseDown: (rowIndex: number, colIndex: number) => void;
9
14
  onMouseOver: (rowIndex: number, colIndex: number) => void;
15
+ row: {
16
+ [key: string]: CellValue;
17
+ };
10
18
  rowIndex: number;
11
19
  }
12
20
  declare const TableCell: import("react").ForwardRefExoticComponent<TableCellProps & import("react").RefAttributes<HTMLTableCellElement>>;
@@ -1,19 +1,3 @@
1
1
  import HeaderObject from "../types/HeaderObject";
2
2
  export declare const SAMPLE_HEADERS: HeaderObject[];
3
- export declare const inventoryData: {
4
- id: string;
5
- productName: string;
6
- category: string;
7
- quantity: number;
8
- price: string;
9
- supplier: string;
10
- location: string;
11
- reorderLevel: number;
12
- sku: string;
13
- description: string;
14
- weight: string;
15
- dimensions: string;
16
- barcode: string;
17
- expirationDate: string;
18
- manufacturer: string;
19
- }[];
3
+ export declare const inventoryData: any[];
@@ -0,0 +1,11 @@
1
+ /// <reference types="react" />
2
+ import CellValue from "../types/CellValue";
3
+ declare const TableContext: import("react").Context<{
4
+ rows: {
5
+ [key: string]: CellValue;
6
+ }[];
7
+ tableRows: {
8
+ [key: string]: CellValue;
9
+ }[];
10
+ }>;
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 c,useEffect as i,Children as s,useLayoutEffect as u,forwardRef as l,createRef as d,Fragment as f,useReducer as h,useMemo as v}from"react";var g=function(){return g=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},g.apply(this,arguments)};function p(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 m=function(e){var n={};return s.forEach(e,(function(e,r){if(e.ref&&e.ref.current){var t=e.ref.current.getBoundingClientRect();n[e.key]=t}})),n},w=function(e){var n,r,c=e.allowHorizontalAnimate,s=void 0===c||c,l=e.children,d=e.pauseAnimation,f=o({}),h=f[0],v=f[1],g=o({}),p=g[0],w=g[1],b=(n=l,r=a(),i((function(){r.current=n}),[n]),r.current);return u((function(){var e=m(l);v(e)}),[l]),u((function(){var e=m(b);w(e)}),[b]),i((function(){d||Object.keys(p).length&&t.Children.forEach(l,(function(e){var n=e.ref.current,r=p[e.key],t=h[e.key],o=r.left-t.left,a=s?0:r.top-t.top,c=Math.abs(o),i=Math.abs(a);(c>10||i>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"}))}))}))}),[s,h,l,d,p]),l},b=!1,C=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(!b&&(t.current=e,e.accessor!==(null===(a=n.current)||void 0===a?void 0:a.accessor)&&null!==n.current&&!b)){if(b=!0,!r.current)return;var c=p([],r.current,!0),i=c.findIndex((function(e){var r;return e.accessor===(null===(r=n.current)||void 0===r?void 0:r.accessor)})),s=c.findIndex((function(n){return n.accessor===e.accessor}));if(void 0===i||void 0===s)return;var u=c.splice(i,1)[0];c.splice(s,0,u),JSON.stringify(c)!==JSON.stringify(r.current)&&setTimeout((function(){o(c),setTimeout((function(){b=!1}),500)}),50)}}(e)},handleDragEnd:function(){n.current=null,t.current=null}}},D=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))}},y=l((function(r,t){var c,i=r.draggedHeaderRef,s=r.enableColumnResizing,u=r.forceUpdate,l=r.headersRef,d=r.hoveredHeaderRef,f=r.index,h=r.onSort,v=r.onTableHeaderDragEnd,p=r.setIsWidthDragging,m=a({pageX:0,pageY:0}),w=o(!1),b=w[0],y=w[1],M=null===(c=l.current)||void 0===c?void 0:c[f],x=C({draggedHeaderRef:i,headersRef:l,hoveredHeaderRef:d,onTableHeaderDragEnd:v}),R=x.handleDragStart,L=x.handleDragOver,E=x.handleDragEnd,S=a(D((function(e){L(e)}),100)).current;return M?e("div",g({className:"st-header-cell ".concat(M===d.current?"st-hovered":""," ").concat(b?"st-dragging":""),ref:t,style:{width:M.width}},{children:[n("div",g({className:"st-header-label",draggable:!0,onClick:function(){return h(f)},onDragStart:function(){return function(e){y(!0),R(e)}(M)},onDragOver:function(e){var n=e.pageX,r=e.pageY;n===m.current.pageX&&r===m.current.pageY||(m.current={pageX:n,pageY:r},e.preventDefault(),S(M,e))},onDragEnd:function(){y(!1),E()}},{children:null==M?void 0:M.label})),s&&n("div",{className:"st-header-resize-handle",onMouseDown:function(e){p(!0),e.preventDefault();var n=e.clientX;if(M){var r=M.width,t=D((function(e){var t=Math.max(r+(e.clientX-n),10);M&&(l.current[f].width=t,u())}),10),o=function(){document.removeEventListener("mousemove",t),document.removeEventListener("mouseup",o),p(!1)};document.addEventListener("mousemove",t),document.addEventListener("mouseup",o)}}})]})):null})),M=l((function(e,r){return e.visible?n("div",{className:"st-cell",ref:r}):n("div",{ref:r})})),x=function(){return n("div",{className:"st-row-separator"})},R=function(t){var o,c=t.enableColumnResizing,i=t.forceUpdate,s=t.headersRef,u=t.isWidthDragging,l=t.onSort,f=t.onTableHeaderDragEnd,h=t.setIsWidthDragging,v=t.shouldDisplayLastColumnCell,p=a(null),m=a(null);return e(r,{children:[e(w,g({pauseAnimation:u},{children:[null===(o=s.current)||void 0===o?void 0:o.map((function(e,r){return n(y,{draggedHeaderRef:p,enableColumnResizing:c,forceUpdate:i,headersRef:s,hoveredHeaderRef:m,index:r,onSort:l,onTableHeaderDragEnd:f,ref:d(),setIsWidthDragging:h},e.accessor)})),n(M,{ref:d(),visible:v})]})),n(x,{})]})},L=l((function(e,r){var t=e.rowIndex,o=e.colIndex,a=e.content,c=e.isSelected,i=e.isTopLeftCell,s=e.borderClass,u=e.onMouseDown,l=e.onMouseOver,d=t%2==0;return n("div",g({onMouseDown:function(){return u(t,o)},onMouseOver:function(){return l(t,o)},ref:r,className:"st-cell ".concat(c?i?"st-cell-selected-first-cell ".concat(s):"st-cell-selected ".concat(s):""," ").concat(d?"st-cell-odd-row":"")},{children:a}))})),E=function(t){var o=t.getBorderClass,a=t.handleMouseDown,c=t.handleMouseOver,i=t.headers,s=t.isSelected,u=t.isTopLeftCell,l=t.isWidthDragging,h=t.shouldDisplayLastColumnCell,v=t.shouldPaginate,p=t.sortedRows;return n(r,{children:p.map((function(r,t){return e(f,{children:[e(w,g({allowHorizontalAnimate:v,pauseAnimation:l},{children:[i.map((function(e,i){var l=r[e.accessor];return e.cellRenderer&&(l=e.cellRenderer(r)),n(L,{borderClass:o(t,i),colIndex:i,content:l,isSelected:s(t,i),isTopLeftCell:u(t,i),onMouseDown:function(){return a(t,i)},onMouseOver:function(){return c(t,i)},ref:d(),rowIndex:t},e.accessor)})),n(M,{ref:d(),visible:h})]})),t!==p.length-1&&n(x,{})]},r.id)}))})},S=function(r){var t=r.currentPage,o=r.hideFooter,a=r.nextIcon,c=r.onPageChange,i=r.prevIcon,s=r.rowsPerPage,u=r.totalRows,l=Math.ceil(u/s),d=function(e){e>=1&&e<=l&&c(e)};return o?null:e("div",g({className:"st-footer"},{children:[n("button",g({className:"st-next-prev-btn",onClick:function(){return d(t-1)},disabled:1===t},{children:i})),n("button",g({className:"st-next-prev-btn",onClick:function(){return d(t+1)},disabled:t===l},{children:a})),Array.from({length:l},(function(e,r){return n("button",g({onClick:function(){return d(r+1)},className:"st-page-btn ".concat(t===r+1?"active":"")},{children:r+1}),r)}))]}))},O=function(){return n("svg",g({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"})}))},T=function(){return n("svg",g({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"})}))},H=function(r){var t,s=r.defaultHeaders,u=r.enableColumnResizing,l=void 0===u||u,d=r.height,f=r.hideFooter,m=void 0!==f&&f,w=r.nextIcon,b=void 0===w?n(T,{}):w,C=r.prevIcon,D=void 0===C?n(O,{}):C,y=r.rows,M=r.rowsPerPage,x=void 0===M?10:M,L=r.shouldPaginate,H=void 0===L||L,I=o(!1),N=I[0],P=I[1],k=a(s),j=o(y),z=j[0],A=j[1],W=o(null),B=W[0],U=W[1],F=h((function(e){return e+1}),0)[1],X=o(1),Y=X[0],q=X[1],J=a(null),K=v((function(){return!!J.current&&k.current.reduce((function(e,n){return e+n.width}),0)<J.current.clientWidth}),[]),G=function(e,n){var r=o([]),t=r[0],s=r[1],u=a(!1),l=a(null),d=c((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]);i((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,l.current={row:e,col:n},s([{row:e,col:n}])},handleMouseOver:function(e,n){if(u.current&&l.current){for(var r=[],t=Math.min(l.current.row,e),o=Math.max(l.current.row,e),a=Math.min(l.current.col,n),c=Math.max(l.current.col,n),i=t;i<=o;i++)for(var d=a;d<=c;d++)r.push({row:i,col:d});s(r)}},handleMouseUp:function(){u.current=!1,l.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:s}}(z,k.current),Q=G.handleMouseDown,V=G.handleMouseOver,Z=G.handleMouseUp,$=G.isSelected,_=G.getBorderClass,ee=G.isTopLeftCell,ne=G.setSelectedCells;i((function(){var e=function(e){e.target.closest(".st-cell")||ne([])};return document.addEventListener("mousedown",e),function(){document.removeEventListener("mousedown",e)}}),[ne]);var re=H?z.slice((Y-1)*x,Y*x):z;return e("div",g({ref:J,className:"st-wrapper",style:d?{height:d}:{}},{children:[e("div",g({className:"st-table",onMouseUp:Z,onMouseLeave:Z,style:{gridTemplateColumns:"".concat(null===(t=k.current)||void 0===t?void 0:t.map((function(e){return"".concat(e.width,"px")})).join(" ")," 1fr")}},{children:[n(R,{enableColumnResizing:l,forceUpdate:F,headersRef:k,isWidthDragging:N,onSort:function(e){var n=function(e,n,r,t){var o=e[t],a="ascending";return r&&r.key.accessor===o.accessor&&"ascending"===r.direction&&(a="descending"),{sortedData:p([],n,!0).sort((function(e,n){return e[o.accessor]<n[o.accessor]?"ascending"===a?-1:1:e[o.accessor]>n[o.accessor]?"ascending"===a?1:-1:0})),newSortConfig:{key:o,direction:a}}}(k.current,z,B,e),r=n.sortedData,t=n.newSortConfig;A(r),U(t)},onTableHeaderDragEnd:function(e){k.current=e,F()},setIsWidthDragging:P,shouldDisplayLastColumnCell:K}),n(E,{getBorderClass:_,handleMouseDown:Q,handleMouseOver:V,headers:k.current,isSelected:$,isTopLeftCell:ee,isWidthDragging:N,shouldDisplayLastColumnCell:K,shouldPaginate:H,sortedRows:re})]})),H&&n(S,{currentPage:Y,hideFooter:m,onPageChange:q,rowsPerPage:x,totalRows:z.length,nextIcon:b,prevIcon:D})]}))};export{H 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/dist/style.css CHANGED
@@ -1 +1 @@
1
- @import url("https://fonts.googleapis.com/css2?family=Nunito:wght@400;700&display=swap");:root{--slate-50:#f8fafc;--slate-100:#f1f5f9;--slate-200:#e2e8f0;--slate-300:#cbd5e1;--slate-400:#94a3b8;--slate-500:#64748b;--slate-600:#475569;--slate-700:#334155;--slate-800:#1e293b;--slate-900:#0f172a;--blue-50:#eff6ff;--blue-100:#dbeafe;--blue-200:#bfdbfe;--blue-300:#93c5fd;--blue-400:#60a5fa;--blue-500:#3b82f6;--blue-600:#2563eb;--blue-700:#1d4ed8;--blue-800:#1e40af;--blue-900:#1e3a8a;--orange-50:#fff7ed;--orange-100:#ffedd5;--orange-200:#fed7aa;--orange-300:#fdba74;--orange-400:#fb923c;--orange-500:#f97316;--orange-600:#ea580c;--orange-700:#c2410c;--orange-800:#9a3412;--orange-900:#7c2d12;--amber-50:#fffbeb;--amber-100:#fef3c7;--amber-200:#fde68a;--amber-300:#fcd34d;--amber-400:#fbbf24;--amber-500:#f59e0b;--amber-600:#d97706;--amber-700:#b45309;--amber-800:#92400e;--amber-900:#78350f;--st-border-radius:4px;--st-border-color:var(--slate-300);--st-border-width:1px;--st-resize-handle-color:var(--slate-300);--st-separator-border-color:var(--slate-300);--st-odd-row-background-color:var(--slate-100);--st-dragging-background-color:var(--blue-100);--st-selected-cell-background-color:var(--blue-200);--st-selected-first-cell-background-color:var(--amber-100);--st-border-top-color:var(--blue-500);--st-border-bottom-color:var(--blue-500);--st-border-left-color:var(--blue-500);--st-border-right-color:var(--blue-500);--st-border-top-white-color:#fff;--st-footer-background-color:#fff}*{box-sizing:border-box;margin:0;padding:0}body{font-family:Nunito}.st-wrapper{border:var(--st-border-width) solid var(--st-border-color);border-radius:var(--st-border-radius);height:auto;max-height:100dvh;overflow:auto}.st-table{border-collapse:collapse;display:grid;table-layout:auto;white-space:nowrap;width:100%}.st-header-cell{background-color:#fff;position:sticky;top:0;z-index:1}.st-cell,.st-header-cell{border:var(--st-border-width) solid #0000;color:var(--slate-800);cursor:pointer;height:40px;overflow:hidden}.st-cell,.st-header-label{overflow:hidden;padding:8px;text-align:left;text-overflow:ellipsis;user-select:none;white-space:nowrap}.st-header-resize-handle{background-color:var(--st-resize-handle-color);border-radius:.25rem;bottom:0;cursor:col-resize;margin-bottom:.25rem;margin-top:.25rem;position:absolute;right:0;top:0;width:5px}.st-row-separator{background-color:var(--st-separator-border-color);grid-column:1/-1;height:1px}.st-cell-odd-row{background-color:var(--st-odd-row-background-color)}.st-dragging{background-color:var(--st-dragging-background-color)}.st-cell-selected{background-color:var(--st-selected-cell-background-color)}.st-cell-selected-first{background-color:var(--st-selected-first-cell-background-color)}.st-selected-top-border{border-top:var(--st-border-width) solid var(--st-border-top-color)}.st-selected-bottom-border{border-bottom:var(--st-border-width) solid var(--st-border-bottom-color)}.st-selected-left-border{border-left:var(--st-border-width) solid var(--st-border-left-color)}.st-selected-right-border{border-right:var(--st-border-width) solid var(--st-border-right-color)}.st-selected-top-border-white{border-top:var(--st-border-width) solid var(--st-border-top-white-color)}.st-footer{background-color:var(--st-footer-background-color);border-top:var(--st-border-width) solid var(--st-border-color);bottom:0;left:0;padding:8px;position:sticky}.st-footer,.st-next-prev-btn{align-items:center;display:flex}.st-next-prev-btn{background-color:initial;border:none;border-radius:4px;color:var(--slate-600);cursor:pointer;justify-content:center;padding:4px;transition:background-color .3s ease}.st-next-prev-btn:hover{background-color:var(--slate-100)}.st-page-btn{background-color:initial;border:none;border-radius:var(--st-border-radius);color:var(--slate-600);cursor:pointer;margin-left:4px;padding:4px;transition:background-color .3s ease}.st-page-btn:hover{background-color:var(--slate-100)}.st-page-btn.active{background-color:var(--blue-500);color:#fff}
1
+ @import url("https://fonts.googleapis.com/css2?family=Nunito:wght@400;700&display=swap");:root{--slate-50:#f8fafc;--slate-100:#f1f5f9;--slate-200:#e2e8f0;--slate-300:#cbd5e1;--slate-400:#94a3b8;--slate-500:#64748b;--slate-600:#475569;--slate-700:#334155;--slate-800:#1e293b;--slate-900:#0f172a;--blue-50:#eff6ff;--blue-100:#dbeafe;--blue-200:#bfdbfe;--blue-300:#93c5fd;--blue-400:#60a5fa;--blue-500:#3b82f6;--blue-600:#2563eb;--blue-700:#1d4ed8;--blue-800:#1e40af;--blue-900:#1e3a8a;--orange-50:#fff7ed;--orange-100:#ffedd5;--orange-200:#fed7aa;--orange-300:#fdba74;--orange-400:#fb923c;--orange-500:#f97316;--orange-600:#ea580c;--orange-700:#c2410c;--orange-800:#9a3412;--orange-900:#7c2d12;--amber-50:#fffbeb;--amber-100:#fef3c7;--amber-200:#fde68a;--amber-300:#fcd34d;--amber-400:#fbbf24;--amber-500:#f59e0b;--amber-600:#d97706;--amber-700:#b45309;--amber-800:#92400e;--amber-900:#78350f;--st-border-radius:4px;--st-border-color:var(--slate-300);--st-border-width:1px;--st-resize-handle-color:var(--slate-300);--st-separator-border-color:var(--slate-300);--st-odd-row-background-color:var(--slate-100);--st-dragging-background-color:var(--blue-100);--st-selected-cell-background-color:var(--blue-200);--st-selected-first-cell-background-color:var(--amber-100);--st-border-top-color:var(--blue-500);--st-border-bottom-color:var(--blue-500);--st-border-left-color:var(--blue-500);--st-border-right-color:var(--blue-500);--st-border-top-white-color:#fff;--st-footer-background-color:#fff;--st-shadow:0 1px 3px 0 #0000001a,0 1px 2px -1px #0000001a;--st-cell-padding:8px}*{box-sizing:border-box;margin:0;padding:0}body{font-family:Nunito}.st-wrapper{border:var(--st-border-width) solid var(--st-border-color);border-radius:var(--st-border-radius);height:auto;max-height:100dvh;overflow:auto}.st-table{border-collapse:collapse;display:grid;table-layout:auto;white-space:nowrap;width:100%}.st-header-cell{background-color:#fff;font-weight:600;position:sticky;top:0;z-index:1}.st-cell,.st-header-cell{border:var(--st-border-width) solid #0000;color:var(--slate-800);cursor:pointer;overflow:hidden}.st-cell,.st-cell-editing,.st-header-cell{font-family:Nunito;height:100%;height:40px;position:relative;width:100%}.st-cell,.st-header-label{overflow:hidden;padding:var(--st-cell-padding);text-align:left;text-overflow:ellipsis;user-select:none;white-space:nowrap}.st-header-resize-handle{background-color:var(--st-resize-handle-color);border-radius:.25rem;bottom:0;cursor:col-resize;margin-bottom:.5rem;margin-top:.5rem;position:absolute;right:0;top:0;width:5px}.st-row-separator{background-color:var(--st-separator-border-color);grid-column:1/-1;height:1px}.st-cell-odd-row{background-color:var(--st-odd-row-background-color)}.st-dragging{background-color:var(--st-dragging-background-color)}.st-cell-selected{background-color:var(--st-selected-cell-background-color)}.st-cell-selected-first{background-color:var(--st-selected-first-cell-background-color)}.st-selected-top-border{border-top:var(--st-border-width) solid var(--st-border-top-color)}.st-selected-bottom-border{border-bottom:var(--st-border-width) solid var(--st-border-bottom-color)}.st-selected-left-border{border-left:var(--st-border-width) solid var(--st-border-left-color)}.st-selected-right-border{border-right:var(--st-border-width) solid var(--st-border-right-color)}.st-selected-top-border-white{border-top:var(--st-border-width) solid var(--st-border-top-white-color)}.st-footer{background-color:var(--st-footer-background-color);border-top:var(--st-border-width) solid var(--st-border-color);bottom:0;left:0;padding:8px;position:sticky}.st-footer,.st-next-prev-btn{align-items:center;display:flex}.st-next-prev-btn{background-color:initial;border:none;border-radius:4px;color:var(--slate-600);cursor:pointer;justify-content:center;padding:4px;transition:background-color .3s ease}.st-next-prev-btn:hover{background-color:var(--slate-100)}.st-page-btn{background-color:initial;border:none;border-radius:var(--st-border-radius);color:var(--slate-600);cursor:pointer;margin-left:4px;padding:4px;transition:background-color .3s ease}.st-page-btn:hover{background-color:var(--slate-100)}.st-page-btn.active{background-color:var(--blue-500);color:#fff}.editable-cell-input{border:var(--st-border-width) solid var(--st-border-color);border-radius:var(--st-border-radius);box-shadow:var(--st-shadow);font-family:Nunito;font-size:1rem;height:100%;left:0;outline:none;padding:var(--st-cell-padding);position:absolute;top:0;width:100%;z-index:1}.editable-cell-input:focus{border:var(--st-border-width) solid var(--blue-500)}
@@ -0,0 +1,11 @@
1
+ import CellValue from "./CellValue";
2
+ type CellChangeProps = {
3
+ accessor: any;
4
+ newRowIndex: number;
5
+ newValue: CellValue;
6
+ originalRowIndex: number;
7
+ row: {
8
+ [key: string]: CellValue;
9
+ };
10
+ };
11
+ export default CellChangeProps;
@@ -0,0 +1,2 @@
1
+ type CellValue = string | number | boolean | undefined | null;
2
+ export default CellValue;
@@ -3,6 +3,7 @@ type HeaderObject = {
3
3
  label: string;
4
4
  accessor: string;
5
5
  width: number;
6
+ isEditable?: boolean;
6
7
  cellRenderer?: (row: {
7
8
  [key: string]: any;
8
9
  }) => ReactNode;
@@ -0,0 +1,6 @@
1
+ import HeaderObject from "./HeaderObject";
2
+ type SortConfig = {
3
+ key: HeaderObject;
4
+ direction: string;
5
+ };
6
+ export default SortConfig;
@@ -1,10 +1,8 @@
1
1
  import HeaderObject from "../types/HeaderObject";
2
- export declare const onSort: (headers: HeaderObject[], rows: {
2
+ import SortConfig from "../types/SortConfig";
3
+ export declare const handleSort: (headers: HeaderObject[], rows: {
3
4
  [key: string]: any;
4
- }[], sortConfig: {
5
- key: HeaderObject;
6
- direction: string;
7
- } | null, columnIndex: number) => {
5
+ }[], sortConfig: SortConfig) => {
8
6
  sortedData: {
9
7
  [key: string]: any;
10
8
  }[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simple-table-core",
3
- "version": "0.2.6",
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",
@@ -8,6 +8,18 @@
8
8
  "files": [
9
9
  "dist"
10
10
  ],
11
+ "keywords": [
12
+ "table",
13
+ "grid",
14
+ "react",
15
+ "simple",
16
+ "simple-table",
17
+ "simple-table-core",
18
+ "react-table",
19
+ "react-table-core",
20
+ "spreadsheet",
21
+ "spreadsheet-react"
22
+ ],
11
23
  "dependencies": {
12
24
  "react": "^18.3.1"
13
25
  },
@@ -39,7 +51,7 @@
39
51
  "webpack": "^5.95.0"
40
52
  },
41
53
  "scripts": {
42
- "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",
43
55
  "build": "rollup -c",
44
56
  "start": "storybook dev -p 6006",
45
57
  "build-storybook": "storybook build"