@cronocode/react-box 3.1.2 → 3.1.3

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.
@@ -1,7 +1,7 @@
1
1
  import { BoxProps } from '../../../box';
2
2
  import { default as ColumnModel } from '../models/columnModel';
3
3
  interface Props<TRow> extends BoxProps {
4
- children?: React.ReactNode;
4
+ children: React.ReactNode;
5
5
  column: ColumnModel<TRow>;
6
6
  }
7
7
  export default function DataGridCell<TRow>(props: Props<TRow>): import("react/jsx-runtime").JSX.Element;
@@ -34,12 +34,15 @@ export interface GridDefinition<TRow> {
34
34
  columns: ColumnType<TRow>[];
35
35
  showRowNumber?: boolean | {
36
36
  pinned?: boolean;
37
+ width?: number;
37
38
  };
38
39
  rowSelection?: boolean | {
39
40
  pinned?: boolean;
40
41
  };
41
42
  rowHeight?: number;
42
43
  visibleRowsCount?: number;
44
+ topBar?: boolean;
45
+ bottomBar?: boolean;
43
46
  }
44
47
  export interface DataGridProps<TRow> {
45
48
  data: TRow[];
@@ -4,6 +4,7 @@ import { default as GroupRowModel } from './groupRowModel';
4
4
  import { default as RowModel } from './rowModel';
5
5
  export declare const EMPTY_CELL_KEY: Key;
6
6
  export declare const ROW_NUMBER_CELL_KEY: Key;
7
+ export declare const DEFAULT_ROW_NUMBER_COLUMN_WIDTH = 70;
7
8
  export declare const ROW_SELECTION_CELL_KEY: Key;
8
9
  export declare const GROUPING_CELL_KEY: Key;
9
10
  export default class GridModel<TRow> {
@@ -13,6 +13,8 @@ export default class GroupRowModel<TRow> {
13
13
  get key(): Key;
14
14
  parentRow?: GroupRowModel<TRow>;
15
15
  get cells(): GroupRowCellModel<TRow>[];
16
+ get selected(): boolean;
17
+ get indeterminate(): boolean;
16
18
  get expanded(): boolean;
17
19
  get depth(): number;
18
20
  get count(): number;
@@ -1 +1 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("react/jsx-runtime"),h=require("../box.cjs"),f=require("./flex.cjs"),p=require("react"),c=require("../core.cjs"),$=require("./grid.cjs"),W=require("./checkbox.cjs"),w=require("./button.cjs"),L=require("./semantics.cjs"),F=require("./tooltip.cjs"),pe=require("./baseSvg.cjs");function ge(a){const{grid:e}=a;return n.jsxs(f.default,{component:"datagrid.bottomBar",children:[n.jsxs(h.default,{children:["Rows: ",e.props.data.length]}),e.props.def.rowSelection&&n.jsxs(h.default,{children:["Selected: ",e.selectedRows.size]})]})}function U(a){const{cell:e}=a,t=p.useCallback(()=>{e.grid.toggleRowSelection(e.row.key)},[e.grid,e.row.key]);return n.jsx(W.default,{variant:"datagrid",checked:e.row.selected,onChange:t})}U.displayName="DataGridCellRowSelection";const fe="NO_PIN";class D{constructor(e,t,i){this.def=e,this.grid=t,this.parent=i,this.columns=e.columns?.map(s=>new D(e.pin?{...s,pin:e.pin}:s,t,this))??[],this.isLeaf&&(this._inlineWidth=this.key==M?void 0:this.def.width??this.grid.DEFAULT_COLUMN_WIDTH_PX,this._pin=e.pin)}columns=[];get visibleColumns(){return this.columns.filter(e=>e.isVisible)}get key(){return this.def.key}get header(){return this.def.header}get align(){return this.def.align}get isLeaf(){return this.columns.length===0}get Cell(){return this.def.Cell}_pin;get pin(){if(this.isLeaf)return this._pin;const e=[...new Set(this.columns.flatMap(t=>t.pin))];if(e.length===1)return e[0]}get uniqueKey(){return`${this.key}${this.pin??""}`}getPinnedColumn(e){if(this.hasPin(e)){if(this.isLeaf)return this;const t=new D({...this.def,pin:e},this.grid,this.parent);return t.columns=this.columns.filter(i=>i.hasPin(e)).map(i=>{const s=i.getPinnedColumn(e);return s.parent=t,s}).filter(i=>!!i),t}}hasPin(e){return this.isLeaf?this._pin===e:this.columns.some(t=>t.hasPin(e))}get death(){return this.parent?this.parent.death+1:0}get flatColumns(){const e=[this];return e.push(...this.columns.flatMap(t=>t.flatColumns)),e}_inlineWidth;get inlineWidth(){if(this.isLeaf)return this._inlineWidth;const e=this.visibleColumns.map(t=>t.inlineWidth).filter(t=>typeof t=="number");if(e.length!==0)return e.sumBy(t=>t)}get left(){let e=0;if(this.parent){const{visibleColumns:t,left:i}=this.parent,s=t.findIndex(o=>o===this);e+=t.sumBy((o,r)=>r<s?o.inlineWidth??0:0),e+=i}else{const t=this.grid.columns.value.left.filter(s=>s.isVisible),i=t.findIndex(s=>s===this);e+=t.sumBy((s,o)=>o<i?s.inlineWidth??0:0)}return e}get right(){let e=0;if(this.parent){const{visibleColumns:t}=this.parent,i=t.reverse(),s=i.findIndex(o=>o===this);e+=i.sumBy((o,r)=>r<s?o.inlineWidth??0:0),e+=this.parent.right}else{const i=this.grid.columns.value.right.filter(o=>o.isVisible).reverse(),s=i.findIndex(o=>o===this);e+=i.sumBy((o,r)=>r<s?o.inlineWidth??0:0)}return e}get isEdge(){if(!this.pin)return!1;if(this.parent){const{visibleColumns:t}=this.parent;return(this.pin==="LEFT"?t.at(-1):t.at(0))===this&&this.parent.isEdge}return(this.pin==="LEFT"?this.grid.columns.value.left.filter(t=>t.isVisible).at(-1):this.grid.columns.value.right.filter(t=>t.isVisible).at(0))===this}get isVisible(){return this.isLeaf?!this.grid.hiddenColumns.has(this.key):this.leafs.some(e=>e.isVisible)}get leafs(){return this.isLeaf?[this]:this.visibleColumns.flatMap(e=>e.leafs)}get groupColumnWidthVarName(){return`--${this.uniqueKey}-group-width`}get widthVarName(){return`--${this.uniqueKey}-width`}get leftVarName(){return`--${this.uniqueKey}-left`}get rightVarName(){return`--${this.uniqueKey}-right`}get gridRows(){return this.isLeaf?this.grid.columns.value.maxDeath-this.death:1}resizeColumn=e=>{this.grid.isResizeMode=!0;const t=e.pageX,{MIN_COLUMN_WIDTH_PX:i,update:s}=this.grid,o=this.leafs.sumBy(u=>u.inlineWidth)-this.leafs.length*i,r=this.leafs.toRecord(u=>[u.key,u.inlineWidth]),m=c.FnUtils.throttle(u=>{const g=(u.pageX-t)*(this.pin==="RIGHT"?-1:1);this.leafs.forEach(C=>{const v=r[C.key],j=o>0?(v-i)/o*g:g/this.leafs.length,x=Math.round(v+j);C.setWidth(x<i?i:x)}),this.grid.sizes.clear(),s()},40),l=new AbortController,d=u=>{l.abort(),this.grid.isResizeMode=!1,s()};window.addEventListener("mousemove",m,l),window.addEventListener("mouseup",d,l)};pinColumn=e=>{this.isLeaf?this._pin=e:this.columns.forEach(t=>t.pinColumn(e)),this.grid.pinColumn(this.uniqueKey,e)};toggleGrouping=()=>{this.grid.toggleGrouping(this.key)};sortColumn=(...e)=>{this.grid.setSortColumn(this.key,...e)};setWidth=e=>{if(!this.isLeaf)throw new Error("Cannot set width for a parent column.");this._inlineWidth!==e&&(this._inlineWidth=e,this.grid.setWidth(this.key,e))};toggleVisibility=()=>{this.grid.toggleColumnVisibility(this.key)}}class we{constructor(e,t,i){this.grid=e,this.row=t,this.column=i}get value(){return this.column.key===k?this.row.rowIndex+1:this.row.data[this.column.key]}}class A{constructor(e,t,i){this.grid=e,this.data=t,this.rowIndex=i,this.grid=e,this.data=t,this.key=this.grid.getRowKey(t)}key;parentRow;count=1;get cells(){return this.grid.columns.value.visibleLeafs.map(e=>new we(this.grid,this,e))}get selected(){return this.grid.selectedRows.has(this.key)}get flatRows(){return this}get allRows(){return this}}const M="empty-cell",k="row-number-cell",_="row-selection-cell",R="grouping-cell";class Ce{constructor(e,t){this.props=e,this.update=t,console.debug("\x1B[32m%s\x1B[0m","[react-box]: DataGrid GridModel ctor")}sourceColumns=c.memo(()=>{const{def:e}=this.props,t=[];if(this.groupColumns.size>0&&t.push(new D({key:R},this)),t.push(...e.columns.map(i=>new D(i,this))),t.push(new D({key:M,Cell:()=>null},this)),e.rowSelection){const i=typeof e.rowSelection=="object"&&e.rowSelection.pinned?"LEFT":void 0;t.unshift(new D({key:_,pin:i,width:50,align:"center",Cell:U},this))}if(e.showRowNumber){const i=typeof e.showRowNumber=="object"&&e.showRowNumber.pinned?"LEFT":void 0;t.unshift(new D({key:k,pin:i,width:70,align:"right"},this))}return t});columns=c.memo(()=>{console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid columns memo");const e=this.sourceColumns.value.map(l=>l.getPinnedColumn("LEFT")).filter(l=>!!l),t=this.sourceColumns.value.map(l=>l.getPinnedColumn()).filter(l=>!!l),i=this.sourceColumns.value.map(l=>l.getPinnedColumn("RIGHT")).filter(l=>!!l),s=[...e,...t,...i].flatMap(l=>l.flatColumns),o=s.filter(l=>l.isLeaf),r=s.filter(l=>l.isLeaf&&l.isVisible),m=s.maxBy(l=>l.death)+1;return{left:e,middle:t,right:i,flat:s,leafs:o,visibleLeafs:r,maxDeath:m}});headerRows=c.memo(()=>(console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid headerRows memo"),this.columns.value.flat.groupBy(t=>t.death).sortBy(t=>t.key).map(t=>{const i=t.values.groupBy(s=>s.pin??fe).toRecord(s=>[s.key,s.values]);return[...i.LEFT?.filter(s=>s.isVisible)??[],...i.NO_PIN?.filter(s=>s.isVisible)??[],...i.RIGHT?.filter(s=>s.isVisible)??[]]})));gridTemplateColumns=c.memo(()=>{console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid gridTemplateColumns memo");const{visibleLeafs:e}=this.columns.value,t=e.sumBy(r=>r.pin==="RIGHT"?1:0),i=e.length-t-1,s=i>0?`repeat(${i}, max-content)`:"",o=t>0?`repeat(${t}, max-content)`:"";return`${s} auto ${o}`});rows=c.memo(()=>{console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid rows memo");let e=this.props.data;if(this._sortColumn&&(e=e.sortBy(t=>t[this._sortColumn],this._sortDirection)),this.groupColumns.size>0){const t=(i,s,o)=>{const r=s.values().next().value;s.delete(r);const m=this.columns.value.leafs.findOrThrow(l=>l.key===r);return this._sortColumn===R&&(i=i.sortBy(l=>l[r],this._sortDirection)),i.groupBy(l=>l[r]).map(l=>{let d;s.size>0?d=t(l.values,new Set(s),o+1):d=l.values.map((g,C)=>new A(this,g,o+1+C));const u=new q(this,m,d,o,l.key);return o+=1,u.expanded&&(o+=d.length),u})};return t(e,new Set(this.groupColumns),0)}return e.map((t,i)=>new A(this,t,i))});flatRows=c.memo(()=>(console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid flatRows memo"),this.rows.value.flatMap(e=>e.flatRows)));get rowHeight(){return this.props.def.rowHeight??this.DEFAULT_ROW_HEIGHT_PX}sizes=c.memo(()=>{console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid sizes memo");const e=this.columns.value.flat.reduce((s,o)=>{const{inlineWidth:r}=o;return typeof r=="number"&&(s[o.widthVarName]=`${o.inlineWidth}px`),o.pin==="LEFT"&&(s[o.leftVarName]=`${o.left}px`),o.pin==="RIGHT"&&(s[o.rightVarName]=`${o.right}px`),s},{});e[this.rowHeightVarName]=`${this.rowHeight}px`,e[this.leftEdgeVarName]=`${this.leftEdge}px`;const{visibleLeafs:t}=this.columns.value,i=t.find(s=>s.key===R);if(i){const s=t.sumBy(o=>o.pin===i.pin&&o.key!==k&&o.key!==_?o.inlineWidth??0:0);e[i.groupColumnWidthVarName]=`${s}px`}return this.groupColumns.forEach(s=>{const o=this.columns.value.leafs.findOrThrow(r=>r.key===s);e[o.groupColumnWidthVarName]=`${t.sumBy(r=>r.pin===o.pin?r.inlineWidth??0:0)}px`}),e});DEFAULT_ROW_HEIGHT_PX=48;MIN_COLUMN_WIDTH_PX=48;DEFAULT_COLUMN_WIDTH_PX=200;isResizeMode=!1;expandedGroupRow=new Set;selectedRows=new Set;get leftEdge(){return this.columns.value.left.sumBy(e=>e.inlineWidth??0)}get rightEdge(){return this.columns.value.right.sumBy(e=>e.inlineWidth??0)}leftEdgeVarName="--left-edge";rowHeightVarName="--row-height";_idMap=new WeakMap;getRowKey(e){const{rowKey:t}=this.props.def;return t?typeof t=="function"?t(e):e[t]:(this._idMap.has(e)||this._idMap.set(e,crypto.randomUUID()),this._idMap.get(e))}setSortColumn=(e,...t)=>{if(t.length>0)[this._sortDirection]=t,this._sortColumn=this._sortDirection?e:void 0;else{const{_sortColumn:i,_sortDirection:s}=this;this._sortColumn=i===e&&s==="DESC"?void 0:e,this._sortDirection=i===e&&s==="ASC"?"DESC":"ASC"}this.headerRows.clear(),this.rows.clear(),this.flatRows.clear(),this.update()};pinColumn=(e,t)=>{const i=this.columns.value.flat.findOrThrow(s=>s.uniqueKey===e);i.pin!==t&&i.pinColumn(t),this.columns.clear(),this.headerRows.clear(),this.gridTemplateColumns.clear(),this.rows.clear(),this.flatRows.clear(),this.sizes.clear(),this.update()};toggleGrouping=e=>{this.groupColumns=new Set(this.groupColumns),this.hiddenColumns=new Set(this.hiddenColumns),this.groupColumns.has(e)?(this.groupColumns.delete(e),this.hiddenColumns.delete(e)):(this.groupColumns.add(e),this.hiddenColumns.add(e)),this.sourceColumns.clear(),this.columns.clear(),this.headerRows.clear(),this.gridTemplateColumns.clear(),this.rows.clear(),this.flatRows.clear(),this.sizes.clear(),this.update()};unGroupAll=()=>{this.groupColumns=new Set,this.sourceColumns.clear(),this.columns.clear(),this.headerRows.clear(),this.gridTemplateColumns.clear(),this.rows.clear(),this.flatRows.clear(),this.sizes.clear(),this.update()};toggleGroupRow=e=>{this.expandedGroupRow=new Set(this.expandedGroupRow),this.expandedGroupRow.has(e)?this.expandedGroupRow.delete(e):this.expandedGroupRow.add(e),this.rows.clear(),this.flatRows.clear(),this.update()};toggleRowSelection=e=>{this.toggleRowsSelection([e])};toggleRowsSelection=e=>{this.selectedRows=new Set(this.selectedRows);const t=e.every(i=>this.selectedRows.has(i));t?e.forEach(i=>this.selectedRows.delete(i)):e.forEach(i=>this.selectedRows.add(i)),this.flatRows.clear(),this.update(),this.props.onSelectionChange?.({action:t?"deselect":"select",affectedRowKeys:e,selectedRowKeys:Array.from(this.selectedRows),isAllSelected:this.selectedRows.size===this.props.data.length})};toggleSelectAllRows=()=>{this.toggleRowsSelection(this.props.data.map(e=>this.getRowKey(e)))};toggleColumnVisibility=e=>{this.hiddenColumns=new Set(this.hiddenColumns),this.hiddenColumns.has(e)?this.hiddenColumns.delete(e):this.hiddenColumns.add(e),this.columns.clear(),this.headerRows.clear(),this.gridTemplateColumns.clear(),this.rows.clear(),this.flatRows.clear(),this.sizes.clear(),this.update()};setWidth=(e,t)=>{const i=this.columns.value.leafs.find(o=>o.key===e);if(!i)throw new Error("Leaf column not found.");i.setWidth(t),this.sourceColumns.value.flatMap(o=>o.flatColumns).findOrThrow(o=>o.key===e).setWidth(t)};groupColumns=new Set;hiddenColumns=new Set;_sortColumn;get sortColumn(){return this._sortColumn}_sortDirection="ASC";get sortDirection(){return this._sortDirection}}class xe{constructor(e,t,i){this.grid=e,this.row=t,this.column=i}get value(){return this.column.key===k?this.row.rowIndex+1:this.column.key===R?`${this.row.groupValue} (${this.row.count})`:null}}class q{constructor(e,t,i,s,o){this.grid=e,this.groupColumn=t,this.rows=i,this.rowIndex=s,this.groupValue=o,i.forEach(r=>r.parentRow=this)}get key(){return`${this.parentRow?.key??""}${this.groupColumn.key}${this.groupValue}`}parentRow;get cells(){return this.grid.columns.value.visibleLeafs.map(e=>new xe(this.grid,this,e))}get expanded(){return this.grid.expandedGroupRow.has(this.key)}get depth(){return this.parentRow?this.parentRow.depth+1:0}get count(){return this.rows.sumBy(e=>e.count,0)}get flatRows(){return this.expanded?[this,...this.rows.flatMap(e=>e.flatRows)]:[this]}get allRows(){return this.rows.flatMap(e=>e.allRows)}get groupingColumn(){return this.grid.columns.value.leafs.findOrThrow(e=>e.key===R)}get groupingColumnGridColumn(){const{visibleLeafs:e}=this.grid.columns.value,{groupingColumn:t}=this;return e.sumBy(s=>s.pin===t.pin&&s.key!==M&&s.key!==_&&s.key!==k?1:0)}toggleRow(){this.grid.toggleGroupRow(this.key)}}function N(a){const{children:e,column:t,style:i,...s}=a,{key:o,pin:r,left:m,right:l,isEdge:d,align:u,widthVarName:g,leftVarName:C,rightVarName:v}=t;"align"in t.def&&(s.jc=u);const j=o===k,x=o===_,y=r==="LEFT",b=r==="RIGHT",G=y||b,S=y&&m===0,T=y&&d,B=b&&d,I=b&&l===0;return n.jsx(f.default,{component:"datagrid.cell",props:{role:"cell"},variant:{isPinned:G,isFirstLeftPinned:S,isLastLeftPinned:T,isFirstRightPinned:B,isLastRightPinned:I,isRowNumber:j,isRowSelection:x},style:{width:`var(${g})`,height:`var(${t.grid.rowHeightVarName})`,left:y?`var(${C})`:void 0,right:b?`var(${v})`:void 0,...i},...s,children:e})}N.displayName="DataGridCell";function X(a){const{row:e}=a,t=p.useCallback(()=>{e.grid.toggleRowsSelection(e.allRows.map(i=>i.key))},[]);return n.jsx(f.default,{className:"grid-row",display:"contents",props:{role:"rowgroup"},children:e.cells.map(i=>{const{key:s,pin:o,groupColumnWidthVarName:r}=i.column,m=o==="RIGHT";if(s===R)return n.jsx(N,{column:i.column,style:{width:`var(${r})`,right:m?"0":void 0},br:e.groupingColumn.pin==="LEFT"?1:void 0,gridColumn:e.groupingColumnGridColumn,pl:4*e.depth,overflow:"auto",children:n.jsx(h.default,{textWrap:"nowrap",px:4,children:n.jsxs(w.default,{clean:!0,onClick:()=>e.toggleRow(),cursor:"pointer",display:"flex",gap:1,ai:"center",children:[n.jsx(c.ExpandIcon,{fill:"currentColor",width:"14px",height:"14px",rotate:e.expanded?0:-90}),i.value]})})},s);if(s===_){const l=e.allRows,d=l.every(g=>g.selected),u=!d&&l.some(g=>g.selected);return n.jsx(N,{column:i.column,children:n.jsx(W.default,{variant:"datagrid",m:1,checked:d,indeterminate:u,onChange:t})},s)}if(s===M)return n.jsx(h.default,{},s);if(o!==e.groupingColumn.pin||s===k)return n.jsx(N,{column:i.column,px:4,children:i.value},s)})})}X.displayName="DataGridGroupRow";function Y(a){const{cell:e}=a;return n.jsx(f.default,{height:"fit",width:"fit",overflow:"auto",ai:"center",jc:e.column.align,children:n.jsx(h.default,{px:4,textOverflow:"ellipsis",overflow:"hidden",textWrap:"nowrap",children:e.value})})}Y.displayName="DataGridCellText";function K(a){const{row:e}=a;return n.jsx(f.default,{className:"grid-row",display:"contents",props:{role:"row"},children:e.cells.map(t=>n.jsx(N,{column:t.column,children:t.column.Cell?n.jsx(t.column.Cell,{cell:t}):n.jsx(Y,{cell:t})},t.column.key))})}K.displayName="DataGridRow";const ye=10,O=20;function J(a){const{grid:e,scrollTop:t}=a,i=e.flatRows.value.length,s=Math.max(0,Math.floor(t/e.rowHeight)-O),o=e.props.def.visibleRowsCount??ye,r=e.rowHeight*o+e.rowHeight/5,m=p.useMemo(()=>{if(console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid render rows"),e.props.data.length===0)return n.jsx(f.default,{jc:"center",ai:"center",gridColumn:"full-row",style:{height:r},children:e.props.loading?"loading...":"empty"});const l=o+O*2;return e.flatRows.value.take(l,s).map(u=>u instanceof q?n.jsx(X,{row:u},u.key):n.jsx(K,{row:u},u.key))},[e.flatRows.value,e.props.data.length,e.props.loading,s,r,o]);return console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid render DataGridBody"),n.jsx(h.default,{style:{height:r},children:n.jsx(h.default,{style:{height:`${i*e.rowHeight}px`},children:n.jsx($.default,{width:"max-content",minWidth:"fit",transition:"none",style:{transform:`translateY(${s*e.rowHeight}px)`,gridTemplateColumns:e.gridTemplateColumns.value},children:m})})})}J.displayName="DataGridBody";function Q(a){const{column:e}=a,[t,i,s]=c.useVisibility({hideOnScroll:!0,event:"mousedown"}),[o,r]=p.useState({top:0,left:0}),m=p.useMemo(()=>o.left>window.innerWidth/2,[o.left]),l=e.isLeaf&&(e.grid.sortColumn!==e.key||e.grid.sortDirection==="DESC"),d=e.isLeaf&&(e.grid.sortColumn!==e.key||e.grid.sortDirection==="ASC"),u=e.isLeaf&&e.grid.sortColumn===e.key,g=e.pin!=="LEFT",C=e.pin!=="RIGHT",v=!!e.pin,j=e.isLeaf&&e.key!==R,x=e.isLeaf&&e.key===R,y=l||d||u,b=g||C||v,G=e.align==="right"?2:void 0,S=e.align==="right"?void 0:e.pin==="RIGHT"?2.5:4;return n.jsx(f.default,{position:"absolute",left:G,right:S,top:"1/2",translateY:-3,ai:"center",children:n.jsxs(w.default,{component:"datagrid.header.cell.contextMenu",onClick:()=>i(!t),children:[n.jsx(L.Span,{component:"datagrid.header.cell.contextMenu.icon",children:n.jsx(c.DotsIcon,{fill:"currentColor"})}),t&&n.jsxs(F.default,{component:"datagrid.header.cell.contextMenu.tooltip",variant:{openLeft:m},onPositionChange:r,ref:s,children:[l&&n.jsxs(w.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.sortColumn("ASC"),children:[n.jsx(L.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(c.SortIcon,{width:"100%",fill:"currentColor"})}),"Sort Ascending"]}),d&&n.jsxs(w.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.sortColumn("DESC"),children:[n.jsx(L.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(c.SortIcon,{width:"100%",fill:"currentColor",rotate:180})}),"Sort Descending"]}),u&&n.jsxs(w.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.sortColumn(void 0),children:[n.jsx(h.default,{width:4}),"Clear Sort"]}),y&&(b||j||x)&&n.jsx(h.default,{bb:1,my:2,borderColor:"gray-300"}),g&&n.jsxs(w.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.pinColumn("LEFT"),children:[n.jsx(L.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(c.PinIcon,{width:"100%",fill:"currentColor"})}),"Pin Left"]}),C&&n.jsxs(w.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.pinColumn("RIGHT"),children:[n.jsx(L.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(c.PinIcon,{width:"100%",fill:"currentColor",rotate:-90})}),"Pin Right"]}),v&&n.jsxs(w.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.pinColumn(),children:[n.jsx(h.default,{width:4}),"Unpin"]}),y&&b&&(j||x)&&n.jsx(h.default,{bb:1,my:2,borderColor:"gray-300"}),j&&n.jsxs(w.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:e.toggleGrouping,children:[n.jsx(L.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(c.GroupingIcon,{width:"100%",fill:"currentColor"})}),n.jsxs(h.default,{textWrap:"nowrap",children:["Group by ",e.header??e.key]})]}),x&&n.jsxs(w.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:e.grid.unGroupAll,children:[n.jsx(L.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(c.GroupingIcon,{width:"100%",fill:"currentColor"})}),n.jsx(h.default,{textWrap:"nowrap",children:"Un-Group All"})]})]})]})})}Q.displayName="DataGridHeaderCellContextMenu";function Z(a){const{column:e}=a;return n.jsx(f.default,{height:"fit",ai:"center",position:"absolute",right:e.pin==="RIGHT"?void 0:0,left:e.pin!=="RIGHT"?void 0:0,py:3,children:n.jsx(h.default,{cursor:"col-resize",px:.75,className:"resizer",height:"fit",props:{onMouseDown:e.resizeColumn,onTouchStart:e.resizeColumn},children:n.jsx(h.default,{component:"datagrid.header.cell.resizer"})})})}Z.displayName="DataGridHeaderCellResizer";function ee(a){const{column:e}=a,{key:t,pin:i,left:s,right:o,isEdge:r,isLeaf:m,leafs:l,grid:d,header:u,gridRows:g,widthVarName:C,leftVarName:v,rightVarName:j,inlineWidth:x}=e,y=t===M,b=t===R,G=t===k,S=t===_,T=i==="LEFT",B=i==="RIGHT",I=T||i==="RIGHT",le=T&&s===0,ae=T&&r,de=B&&r,ue=B&&o===0,V=m&&!y&&!G&&!S,ce=m?1:l.length,he=!G&&!S,H=!G&&!S,z=p.useCallback(()=>{d.toggleSelectAllRows()},[]),me=p.useMemo(()=>{if(G)return null;if(S){const E=d.selectedRows.size===d.props.data.length,P=!E&&d.selectedRows.size>0;return n.jsx(W.default,{variant:"datagrid",m:1,indeterminate:P,checked:E,onChange:z})}if(b){if(d.groupColumns.size===1){const E=d.columns.value.leafs.findOrThrow(P=>P.key===d.groupColumns.values().next().value);return E.header??E.key}return"Group"}return u??t},[d.groupColumns,d.selectedRows,z]);return n.jsx(f.default,{component:"datagrid.header.cell",variant:{isRowNumber:G,isPinned:I,isFirstLeftPinned:le,isLastLeftPinned:ae,isFirstRightPinned:de,isLastRightPinned:ue,isSortable:V},gridRow:g,gridColumn:ce,style:{width:`var(${C})`,left:T?`var(${v})`:void 0,right:B?`var(${j})`:void 0},children:!y&&n.jsxs(n.Fragment,{children:[n.jsx(f.default,{width:"fit",height:"fit",jc:e.align,props:{onClick:V?()=>e.sortColumn():void 0},children:n.jsxs(f.default,{overflow:"hidden",position:m?void 0:"sticky",ai:"center",transition:"none",pl:e.align==="right"?10:4,style:{left:i?void 0:`var(${d.leftEdgeVarName})`},children:[n.jsx(h.default,{overflow:"hidden",textOverflow:"ellipsis",textWrap:"nowrap",children:me}),t===d.sortColumn&&n.jsx(h.default,{pl:(x??0)<58?0:2,children:n.jsx(c.SortIcon,{width:"16px",rotate:d.sortDirection==="ASC"?0:180,fill:"currentColor"})}),H&&n.jsx(h.default,{minWidth:e.align==="right"?4:10})]})}),he&&n.jsx(Z,{column:e}),H&&n.jsx(Q,{column:e})]})})}ee.displayName="DataGridHeaderCell";function te(a){const{grid:e}=a,{isResizeMode:t}=e;return n.jsx($.default,{component:"datagrid.header",variant:{isResizeMode:t},style:{gridTemplateColumns:e.gridTemplateColumns.value},children:e.headerRows.value.map(i=>i.map(s=>n.jsx(ee,{column:s},s.uniqueKey)))})}te.displayName="DataGridHeader";function ie(a){const{grid:e}=a,[t,i]=p.useState(0),s=p.useCallback(c.FnUtils.throttle(o=>{i(o.target.scrollTop)},100),[]);return n.jsxs(h.default,{overflowX:"scroll",props:{onScroll:s},children:[n.jsx(te,{grid:e}),n.jsx(J,{grid:e,scrollTop:t})]})}ie.displayName="DataGridContent";function se(a){const{grid:e}=a;return e.groupColumns.size===0?null:n.jsxs(f.default,{component:"datagrid.topBar.columnGroups",children:[n.jsx(L.Span,{component:"datagrid.topBar.columnGroups.icon",children:n.jsx(c.GroupingIcon,{width:"100%",fill:"currentColor"})}),Array.from(e.groupColumns,t=>{const i=e.columns.value.leafs.findOrThrow(s=>s.key===t);return n.jsxs(p.Fragment,{children:[n.jsx(c.ExpandIcon,{fill:"currentColor",width:"14px",height:"14px",rotate:-90}),n.jsxs(f.default,{component:"datagrid.topBar.columnGroups.item",children:[i.header??i.key,n.jsx(w.default,{component:"datagrid.topBar.columnGroups.item.icon",onClick:()=>e.toggleGrouping(i.key),children:n.jsx(c.CloseSvg,{fill:"currentColor",width:"100%"})})]})]},t)})]})}se.displayName="DataGridColumnGroups";function ne(a){const{grid:e}=a,[t,i,s]=c.useVisibility({event:"mousedown"}),o=p.useMemo(()=>e.columns.value.leafs.filter(r=>![M,k,_,R].includes(r.key)),[e.columns.value.leafs]);return n.jsxs(w.default,{component:"datagrid.topBar.contextMenu",onClick:()=>i(!t),children:[n.jsx(pe.default,{viewBox:"0 0 24 24",width:"20",fill:"currentColor",...a,children:n.jsx("path",{d:"M5 6h14M5 12h14M5 18h14",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})}),t&&n.jsx(F.default,{component:"datagrid.topBar.contextMenu.tooltip",ref:s,children:o.map(r=>n.jsxs(w.default,{component:"datagrid.topBar.contextMenu.tooltip.item",onClick:m=>{m.stopPropagation(),r.toggleVisibility()},children:[n.jsx(W.default,{variant:"datagrid",checked:r.isVisible,onChange:()=>{},focus:{outline:0}}),r.header??r.key]},r.key))})]})}ne.displayName="DataGridTopBarContextMenu";function oe(a){const{grid:e}=a;return n.jsxs(f.default,{component:"datagrid.topBar",position:"relative",children:[n.jsx(ne,{grid:e}),n.jsx(se,{grid:e})]})}oe.displayName="DataGridTopBar";function Re(a){const[e,t]=p.useState(0),i=p.useRef();return i.current||(i.current=new Ce(a,()=>t(s=>s+1))),p.useEffect(()=>{i.current.props=a,i.current.rows.clear(),i.current.flatRows.clear(),i.current.update()},[a.data]),p.useEffect(()=>{i.current.props=a,i.current.sourceColumns.clear(),i.current.columns.clear(),i.current.headerRows.clear(),i.current.gridTemplateColumns.clear(),i.current.rows.clear(),i.current.flatRows.clear(),i.current.sizes.clear(),i.current.update()},[a.def]),p.useEffect(()=>{i.current.props=a,i.current.update()},[a.loading]),i.current}function re(a){const e=Re(a);return console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid render"),n.jsxs(h.default,{component:"datagrid",style:e.sizes.value,props:{role:"presentation"},children:[n.jsx(oe,{grid:e}),n.jsx(ie,{grid:e}),n.jsx(ge,{grid:e})]})}re.displayName="DataGrid";exports.default=re;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const s=require("react/jsx-runtime"),p=require("../box.cjs"),f=require("./flex.cjs"),g=require("react"),h=require("../core.cjs"),ee=require("./grid.cjs"),U=require("./checkbox.cjs"),C=require("./button.cjs"),_=require("./semantics.cjs"),te=require("./tooltip.cjs"),Re=require("./baseSvg.cjs");function ye(l){const{grid:e}=l;return s.jsxs(f.default,{component:"datagrid.bottomBar",children:[s.jsxs(p.default,{children:["Rows: ",e.props.data.length]}),e.props.def.rowSelection&&s.jsxs(p.default,{children:["Selected: ",e.selectedRows.size]})]})}function ie(l){const{cell:e}=l,t=g.useCallback(()=>{e.grid.toggleRowSelection(e.row.key)},[e.grid,e.row.key]);return s.jsx(U.default,{variant:"datagrid",checked:e.row.selected,onChange:t})}ie.displayName="DataGridCellRowSelection";const ve="NO_PIN";class D{constructor(e,t,i){this.def=e,this.grid=t,this.parent=i,this.columns=e.columns?.map(n=>new D(e.pin?{...n,pin:e.pin}:n,t,this))??[],this.isLeaf&&(this._inlineWidth=this.key==E?void 0:this.def.width??this.grid.DEFAULT_COLUMN_WIDTH_PX,this._pin=e.pin)}columns=[];get visibleColumns(){return this.columns.filter(e=>e.isVisible)}get key(){return this.def.key}get header(){return this.def.header}get align(){return this.def.align}get isLeaf(){return this.columns.length===0}get Cell(){return this.def.Cell}_pin;get pin(){if(this.isLeaf)return this._pin;const e=[...new Set(this.columns.flatMap(t=>t.pin))];if(e.length===1)return e[0]}get uniqueKey(){return`${this.key}${this.pin??""}`}getPinnedColumn(e){if(this.hasPin(e)){if(this.isLeaf)return this;const t=new D({...this.def,pin:e},this.grid,this.parent);return t.columns=this.columns.filter(i=>i.hasPin(e)).map(i=>{const n=i.getPinnedColumn(e);return n.parent=t,n}).filter(i=>!!i),t}}hasPin(e){return this.isLeaf?this._pin===e:this.columns.some(t=>t.hasPin(e))}get death(){return this.parent?this.parent.death+1:0}get flatColumns(){const e=[this];return e.push(...this.columns.flatMap(t=>t.flatColumns)),e}_inlineWidth;get inlineWidth(){if(this.isLeaf)return this._inlineWidth;const e=this.visibleColumns.map(t=>t.inlineWidth).filter(t=>typeof t=="number");if(e.length!==0)return e.sumBy(t=>t)}get left(){let e=0;if(this.parent){const{visibleColumns:t,left:i}=this.parent,n=t.findIndex(o=>o===this);e+=t.sumBy((o,r)=>r<n?o.inlineWidth??0:0),e+=i}else{const t=this.grid.columns.value.left.filter(n=>n.isVisible),i=t.findIndex(n=>n===this);e+=t.sumBy((n,o)=>o<i?n.inlineWidth??0:0)}return e}get right(){let e=0;if(this.parent){const{visibleColumns:t}=this.parent,i=t.reverse(),n=i.findIndex(o=>o===this);e+=i.sumBy((o,r)=>r<n?o.inlineWidth??0:0),e+=this.parent.right}else{const i=this.grid.columns.value.right.filter(o=>o.isVisible).reverse(),n=i.findIndex(o=>o===this);e+=i.sumBy((o,r)=>r<n?o.inlineWidth??0:0)}return e}get isEdge(){if(!this.pin)return!1;if(this.parent){const{visibleColumns:t}=this.parent;return(this.pin==="LEFT"?t.at(-1):t.at(0))===this&&this.parent.isEdge}return(this.pin==="LEFT"?this.grid.columns.value.left.filter(t=>t.isVisible).at(-1):this.grid.columns.value.right.filter(t=>t.isVisible).at(0))===this}get isVisible(){return this.isLeaf?!this.grid.hiddenColumns.has(this.key):this.leafs.some(e=>e.isVisible)}get leafs(){return this.isLeaf?[this]:this.visibleColumns.flatMap(e=>e.leafs)}get groupColumnWidthVarName(){return`--${this.uniqueKey}-group-width`}get widthVarName(){return`--${this.uniqueKey}-width`}get leftVarName(){return`--${this.uniqueKey}-left`}get rightVarName(){return`--${this.uniqueKey}-right`}get gridRows(){return this.isLeaf?this.grid.columns.value.maxDeath-this.death:1}resizeColumn=e=>{this.grid.isResizeMode=!0;const t=e.pageX,{MIN_COLUMN_WIDTH_PX:i,update:n}=this.grid,o=this.leafs.sumBy(d=>d.inlineWidth)-this.leafs.length*i,r=this.leafs.toRecord(d=>[d.key,d.inlineWidth]),c=h.FnUtils.throttle(d=>{const m=(d.pageX-t)*(this.pin==="RIGHT"?-1:1);this.leafs.forEach(w=>{const y=r[w.key],v=o>0?(y-i)/o*m:m/this.leafs.length,L=Math.round(y+v);w.setWidth(L<i?i:L)}),this.grid.sizes.clear(),n()},40),a=new AbortController,u=d=>{a.abort(),this.grid.isResizeMode=!1,n()};window.addEventListener("mousemove",c,a),window.addEventListener("mouseup",u,a)};pinColumn=e=>{this.isLeaf?this._pin=e:this.columns.forEach(t=>t.pinColumn(e)),this.grid.pinColumn(this.uniqueKey,e)};toggleGrouping=()=>{this.grid.toggleGrouping(this.key)};sortColumn=(...e)=>{this.grid.setSortColumn(this.key,...e)};setWidth=e=>{if(!this.isLeaf)throw new Error("Cannot set width for a parent column.");this._inlineWidth!==e&&(this._inlineWidth=e,this.grid.setWidth(this.key,e))};toggleVisibility=()=>{this.grid.toggleColumnVisibility(this.key)}}class be{constructor(e,t,i){this.grid=e,this.row=t,this.column=i}get value(){return this.column.key===R?this.row.rowIndex+1:this.row.data[this.column.key]}}class J{constructor(e,t,i){this.grid=e,this.data=t,this.rowIndex=i,this.grid=e,this.data=t,this.key=this.grid.getRowKey(t)}key;parentRow;count=1;get cells(){return this.grid.columns.value.visibleLeafs.map(e=>new be(this.grid,this,e))}get selected(){return this.grid.selectedRows.has(this.key)}get flatRows(){return this}get allRows(){return this}}const E="empty-cell",R="row-number-cell",Q=70,M="row-selection-cell",j="grouping-cell";class je{constructor(e,t){this.props=e,this.update=t,console.debug("\x1B[32m%s\x1B[0m","[react-box]: DataGrid GridModel ctor")}sourceColumns=h.memo(()=>{const{def:e}=this.props,t=[];if(this.groupColumns.size>0&&t.push(new D({key:j},this)),t.push(...e.columns.map(i=>new D(i,this))),t.push(new D({key:E,Cell:()=>null},this)),e.rowSelection){const i=typeof e.rowSelection=="object"&&e.rowSelection.pinned?"LEFT":void 0;t.unshift(new D({key:M,pin:i,width:50,align:"center",Cell:ie},this))}if(e.showRowNumber){let i,n=Q;typeof e.showRowNumber=="object"&&(e.showRowNumber.pinned&&(i="LEFT"),n=e.showRowNumber.width??Q),t.unshift(new D({key:R,pin:i,width:n,align:"right"},this))}return t});columns=h.memo(()=>{console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid columns memo");const e=this.sourceColumns.value.map(a=>a.getPinnedColumn("LEFT")).filter(a=>!!a),t=this.sourceColumns.value.map(a=>a.getPinnedColumn()).filter(a=>!!a),i=this.sourceColumns.value.map(a=>a.getPinnedColumn("RIGHT")).filter(a=>!!a),n=[...e,...t,...i].flatMap(a=>a.flatColumns),o=n.filter(a=>a.isLeaf),r=n.filter(a=>a.isLeaf&&a.isVisible),c=n.maxBy(a=>a.death)+1;return{left:e,middle:t,right:i,flat:n,leafs:o,visibleLeafs:r,maxDeath:c}});headerRows=h.memo(()=>(console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid headerRows memo"),this.columns.value.flat.groupBy(t=>t.death).sortBy(t=>t.key).map(t=>{const i=t.values.groupBy(n=>n.pin??ve).toRecord(n=>[n.key,n.values]);return[...i.LEFT?.filter(n=>n.isVisible)??[],...i.NO_PIN?.filter(n=>n.isVisible)??[],...i.RIGHT?.filter(n=>n.isVisible)??[]]})));gridTemplateColumns=h.memo(()=>{console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid gridTemplateColumns memo");const{visibleLeafs:e}=this.columns.value,t=e.sumBy(r=>r.pin==="RIGHT"?1:0),i=e.length-t-1,n=i>0?`repeat(${i}, max-content)`:"",o=t>0?`repeat(${t}, max-content)`:"";return`${n} auto ${o}`});rows=h.memo(()=>{console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid rows memo");let e=this.props.data;if(this._sortColumn&&(e=e.sortBy(t=>t[this._sortColumn],this._sortDirection)),this.groupColumns.size>0){const t=(i,n,o)=>{const r=n.values().next().value;n.delete(r);const c=this.columns.value.leafs.findOrThrow(a=>a.key===r);return this._sortColumn===j&&(i=i.sortBy(a=>a[r],this._sortDirection)),i.groupBy(a=>a[r]).map(a=>{let u;n.size>0?u=t(a.values,new Set(n),o+1):u=a.values.map((m,w)=>new J(this,m,o+1+w));const d=new se(this,c,u,o,a.key);return o+=1,d.expanded&&(o+=u.length),d})};return t(e,new Set(this.groupColumns),0)}return e.map((t,i)=>new J(this,t,i))});flatRows=h.memo(()=>(console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid flatRows memo"),this.rows.value.flatMap(e=>e.flatRows)));get rowHeight(){return this.props.def.rowHeight??this.DEFAULT_ROW_HEIGHT_PX}sizes=h.memo(()=>{console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid sizes memo");const e=this.columns.value.flat.reduce((n,o)=>{const{inlineWidth:r}=o;return typeof r=="number"&&(n[o.widthVarName]=`${o.inlineWidth}px`),o.pin==="LEFT"&&(n[o.leftVarName]=`${o.left}px`),o.pin==="RIGHT"&&(n[o.rightVarName]=`${o.right}px`),n},{});e[this.rowHeightVarName]=`${this.rowHeight}px`,e[this.leftEdgeVarName]=`${this.leftEdge}px`;const{visibleLeafs:t}=this.columns.value,i=t.find(n=>n.key===j);if(i){const n=t.sumBy(o=>o.pin===i.pin&&o.key!==R&&o.key!==M?o.inlineWidth??0:0);e[i.groupColumnWidthVarName]=`${n}px`}return this.groupColumns.forEach(n=>{const o=this.columns.value.leafs.findOrThrow(r=>r.key===n);e[o.groupColumnWidthVarName]=`${t.sumBy(r=>r.pin===o.pin?r.inlineWidth??0:0)}px`}),e});DEFAULT_ROW_HEIGHT_PX=48;MIN_COLUMN_WIDTH_PX=48;DEFAULT_COLUMN_WIDTH_PX=200;isResizeMode=!1;expandedGroupRow=new Set;selectedRows=new Set;get leftEdge(){return this.columns.value.left.sumBy(e=>e.inlineWidth??0)}get rightEdge(){return this.columns.value.right.sumBy(e=>e.inlineWidth??0)}leftEdgeVarName="--left-edge";rowHeightVarName="--row-height";_idMap=new WeakMap;getRowKey(e){const{rowKey:t}=this.props.def;return t?typeof t=="function"?t(e):e[t]:(this._idMap.has(e)||this._idMap.set(e,crypto.randomUUID()),this._idMap.get(e))}setSortColumn=(e,...t)=>{if(t.length>0)[this._sortDirection]=t,this._sortColumn=this._sortDirection?e:void 0;else{const{_sortColumn:i,_sortDirection:n}=this;this._sortColumn=i===e&&n==="DESC"?void 0:e,this._sortDirection=i===e&&n==="ASC"?"DESC":"ASC"}this.headerRows.clear(),this.rows.clear(),this.flatRows.clear(),this.update()};pinColumn=(e,t)=>{const i=this.columns.value.flat.findOrThrow(n=>n.uniqueKey===e);i.pin!==t&&i.pinColumn(t),this.columns.clear(),this.headerRows.clear(),this.gridTemplateColumns.clear(),this.rows.clear(),this.flatRows.clear(),this.sizes.clear(),this.update()};toggleGrouping=e=>{this.groupColumns=new Set(this.groupColumns),this.hiddenColumns=new Set(this.hiddenColumns),this.groupColumns.has(e)?(this.groupColumns.delete(e),this.hiddenColumns.delete(e)):(this.groupColumns.add(e),this.hiddenColumns.add(e)),this.sourceColumns.clear(),this.columns.clear(),this.headerRows.clear(),this.gridTemplateColumns.clear(),this.rows.clear(),this.flatRows.clear(),this.sizes.clear(),this.update()};unGroupAll=()=>{this.groupColumns=new Set,this.sourceColumns.clear(),this.columns.clear(),this.headerRows.clear(),this.gridTemplateColumns.clear(),this.rows.clear(),this.flatRows.clear(),this.sizes.clear(),this.update()};toggleGroupRow=e=>{this.expandedGroupRow=new Set(this.expandedGroupRow),this.expandedGroupRow.has(e)?this.expandedGroupRow.delete(e):this.expandedGroupRow.add(e),this.rows.clear(),this.flatRows.clear(),this.update()};toggleRowSelection=e=>{this.toggleRowsSelection([e])};toggleRowsSelection=e=>{this.selectedRows=new Set(this.selectedRows);const t=e.every(i=>this.selectedRows.has(i));t?e.forEach(i=>this.selectedRows.delete(i)):e.forEach(i=>this.selectedRows.add(i)),this.flatRows.clear(),this.update(),this.props.onSelectionChange?.({action:t?"deselect":"select",affectedRowKeys:e,selectedRowKeys:Array.from(this.selectedRows),isAllSelected:this.selectedRows.size===this.props.data.length})};toggleSelectAllRows=()=>{this.toggleRowsSelection(this.props.data.map(e=>this.getRowKey(e)))};toggleColumnVisibility=e=>{this.hiddenColumns=new Set(this.hiddenColumns),this.hiddenColumns.has(e)?this.hiddenColumns.delete(e):this.hiddenColumns.add(e),this.columns.clear(),this.headerRows.clear(),this.gridTemplateColumns.clear(),this.rows.clear(),this.flatRows.clear(),this.sizes.clear(),this.update()};setWidth=(e,t)=>{const i=this.columns.value.leafs.find(o=>o.key===e);if(!i)throw new Error("Leaf column not found.");i.setWidth(t),this.sourceColumns.value.flatMap(o=>o.flatColumns).findOrThrow(o=>o.key===e).setWidth(t)};groupColumns=new Set;hiddenColumns=new Set;_sortColumn;get sortColumn(){return this._sortColumn}_sortDirection="ASC";get sortDirection(){return this._sortDirection}}class Ge{constructor(e,t,i){this.grid=e,this.row=t,this.column=i}get value(){return this.column.key===R?this.row.rowIndex+1:this.column.key===j?`${this.row.groupValue} (${this.row.count})`:null}}class se{constructor(e,t,i,n,o){this.grid=e,this.groupColumn=t,this.rows=i,this.rowIndex=n,this.groupValue=o,i.forEach(r=>r.parentRow=this)}get key(){return`${this.parentRow?.key??""}${this.groupColumn.key}${this.groupValue}`}parentRow;get cells(){return this.grid.columns.value.visibleLeafs.map(e=>new Ge(this.grid,this,e))}get selected(){return this.allRows.every(e=>e.selected)}get indeterminate(){return!this.selected&&this.allRows.some(e=>e.selected)}get expanded(){return this.grid.expandedGroupRow.has(this.key)}get depth(){return this.parentRow?this.parentRow.depth+1:0}get count(){return this.rows.sumBy(e=>e.count,0)}get flatRows(){return this.expanded?[this,...this.rows.flatMap(e=>e.flatRows)]:[this]}get allRows(){return this.rows.flatMap(e=>e.allRows)}get groupingColumn(){return this.grid.columns.value.leafs.findOrThrow(e=>e.key===j)}get groupingColumnGridColumn(){const{visibleLeafs:e}=this.grid.columns.value,{groupingColumn:t}=this;return e.sumBy(n=>n.pin===t.pin&&n.key!==E&&n.key!==M&&n.key!==R?1:0)}toggleRow(){this.grid.toggleGroupRow(this.key)}}function A(l){const{children:e,column:t,style:i,...n}=l,{key:o,pin:r,left:c,right:a,isEdge:u,align:d,widthVarName:m,leftVarName:w,rightVarName:y}=t;"align"in t.def&&(n.jc=d);const v=o===R,L=o===M,G=r==="LEFT",k=r==="RIGHT",b=G||k,x=G&&c===0,S=G&&u,T=k&&u,N=k&&a===0;return s.jsx(f.default,{component:"datagrid.body.cell",props:{role:"cell"},variant:{isPinned:b,isFirstLeftPinned:x,isLastLeftPinned:S,isFirstRightPinned:T,isLastRightPinned:N,isRowSelection:L,isRowNumber:v},style:{width:`var(${m})`,height:`var(${t.grid.rowHeightVarName})`,left:G?`var(${w})`:void 0,right:k?`var(${y})`:void 0,...i},...n,children:e})}A.displayName="DataGridCell";function ne(l){const{row:e}=l,{selected:t,indeterminate:i,cells:n,groupingColumn:o,groupingColumnGridColumn:r,depth:c,expanded:a}=e,u=g.useCallback(()=>{e.grid.toggleRowsSelection(e.allRows.map(d=>d.key))},[]);return s.jsx(f.default,{className:"grid-row",selected:t,display:"contents",props:{role:"rowgroup"},children:n.map(d=>{const{key:m,pin:w,groupColumnWidthVarName:y}=d.column,v=w==="RIGHT";if(m===j)return s.jsx(A,{column:d.column,style:{width:`var(${y})`,right:v?"0":void 0},br:o.pin==="LEFT"?1:void 0,gridColumn:r,pl:4*c,overflow:"auto",children:s.jsx(p.default,{textWrap:"nowrap",px:3,children:s.jsxs(C.default,{clean:!0,onClick:()=>e.toggleRow(),cursor:"pointer",display:"flex",gap:1,ai:"center",children:[s.jsx(h.ExpandIcon,{fill:"currentColor",width:"14px",height:"14px",rotate:a?0:-90}),d.value]})})},m);if(m===M)return s.jsx(A,{column:d.column,children:s.jsx(U.default,{variant:"datagrid",m:1,checked:t,indeterminate:i,onChange:u})},m);if(w!==o.pin||m===R||m===E)return s.jsx(A,{column:d.column,px:m===R?3:void 0,children:d.value},m)})})}ne.displayName="DataGridGroupRow";function oe(l){const{cell:e}=l;return s.jsx(f.default,{height:"fit",width:"fit",overflow:"auto",ai:"center",jc:e.column.align,children:s.jsx(p.default,{px:3,textOverflow:"ellipsis",overflow:"hidden",textWrap:"nowrap",children:e.value})})}oe.displayName="DataGridCellText";function re(l){const{row:e}=l,{selected:t}=e;return s.jsx(f.default,{className:"grid-row",selected:t,display:"contents",props:{role:"row"},children:e.cells.map(i=>s.jsx(A,{column:i.column,children:i.column.Cell?s.jsx(i.column.Cell,{cell:i}):s.jsx(oe,{cell:i})},i.column.key))})}re.displayName="DataGridRow";const ke=10,Z=20;function le(l){const{grid:e,scrollTop:t}=l,i=e.flatRows.value.length,n=Math.max(0,Math.floor(t/e.rowHeight)-Z),o=e.props.def.visibleRowsCount??ke,r=e.rowHeight*o+e.rowHeight/5,c=g.useMemo(()=>{if(console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid render rows"),e.props.data.length===0)return s.jsx(f.default,{jc:"center",ai:"center",gridColumn:"full-row",style:{height:r},children:e.props.loading?"loading...":"empty"});const a=o+Z*2;return e.flatRows.value.take(a,n).map(d=>d instanceof se?s.jsx(ne,{row:d},d.key):s.jsx(re,{row:d},d.key))},[e.flatRows.value,e.props.data.length,e.props.loading,n,r,o]);return console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid render DataGridBody"),s.jsx(p.default,{style:{height:r},children:s.jsx(p.default,{style:{height:`${i*e.rowHeight}px`},children:s.jsx(ee.default,{component:"datagrid.body",width:"max-content",minWidth:"fit",transition:"none",style:{transform:`translateY(${n*e.rowHeight}px)`,gridTemplateColumns:e.gridTemplateColumns.value},children:c})})})}le.displayName="DataGridBody";function ae(l){const{column:e}=l,{key:t,pin:i,left:n,right:o,isEdge:r,isLeaf:c,align:a,header:u,grid:d}=e,[m,w,y]=h.useVisibility({hideOnScroll:!0,event:"mousedown"}),[v,L]=g.useState({top:0,left:0}),G=g.useMemo(()=>v.left>window.innerWidth/2,[v.left]),k=c&&(d.sortColumn!==t||d.sortDirection==="DESC"),b=c&&(d.sortColumn!==t||d.sortDirection==="ASC"),x=c&&d.sortColumn===t,S=i!=="LEFT",T=i!=="RIGHT",N=!!i,P=c&&t!==j,I=c&&t===j,O=k||b||x,$=S||T||N,F=a==="right"?2:void 0,q=a==="right"?void 0:i==="RIGHT"?2.5:4,X=t===E,W=t===R,Y=t===M,H=i==="LEFT",V=i==="RIGHT",K=H||i==="RIGHT",B=H&&n===0,z=H&&r,we=V&&r,Ce=V&&o===0,xe=c&&!X&&!W&&!Y;return s.jsx(f.default,{position:"absolute",left:F,right:q,top:"1/2",translateY:-3,ai:"center",children:s.jsxs(C.default,{component:"datagrid.header.cell.contextMenu",onClick:()=>w(!m),variant:{isPinned:K,isFirstLeftPinned:B,isLastLeftPinned:z,isFirstRightPinned:we,isLastRightPinned:Ce,isSortable:xe,isRowNumber:W},children:[s.jsx(_.Span,{component:"datagrid.header.cell.contextMenu.icon",children:s.jsx(h.DotsIcon,{fill:"currentColor"})}),m&&s.jsxs(te.default,{component:"datagrid.header.cell.contextMenu.tooltip",variant:{openLeft:G},onPositionChange:L,ref:y,children:[k&&s.jsxs(C.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.sortColumn("ASC"),children:[s.jsx(_.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:s.jsx(h.SortIcon,{width:"100%",fill:"currentColor"})}),"Sort Ascending"]}),b&&s.jsxs(C.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.sortColumn("DESC"),children:[s.jsx(_.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:s.jsx(h.SortIcon,{width:"100%",fill:"currentColor",rotate:180})}),"Sort Descending"]}),x&&s.jsxs(C.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.sortColumn(void 0),children:[s.jsx(p.default,{width:4}),"Clear Sort"]}),O&&($||P||I)&&s.jsx(p.default,{bb:1,my:2,borderColor:"gray-300"}),S&&s.jsxs(C.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.pinColumn("LEFT"),children:[s.jsx(_.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:s.jsx(h.PinIcon,{width:"100%",fill:"currentColor"})}),"Pin Left"]}),T&&s.jsxs(C.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.pinColumn("RIGHT"),children:[s.jsx(_.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:s.jsx(h.PinIcon,{width:"100%",fill:"currentColor",rotate:-90})}),"Pin Right"]}),N&&s.jsxs(C.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.pinColumn(),children:[s.jsx(p.default,{width:4}),"Unpin"]}),O&&$&&(P||I)&&s.jsx(p.default,{bb:1,my:2,borderColor:"gray-300"}),P&&s.jsxs(C.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:e.toggleGrouping,children:[s.jsx(_.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:s.jsx(h.GroupingIcon,{width:"100%",fill:"currentColor"})}),s.jsxs(p.default,{textWrap:"nowrap",children:["Group by ",u??t]})]}),I&&s.jsxs(C.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:d.unGroupAll,children:[s.jsx(_.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:s.jsx(h.GroupingIcon,{width:"100%",fill:"currentColor"})}),s.jsx(p.default,{textWrap:"nowrap",children:"Un-Group All"})]})]})]})})}ae.displayName="DataGridHeaderCellContextMenu";function de(l){const{column:e}=l;return s.jsx(f.default,{height:"fit",ai:"center",position:"absolute",right:e.pin==="RIGHT"?void 0:0,left:e.pin!=="RIGHT"?void 0:0,py:3,children:s.jsx(p.default,{cursor:"col-resize",px:.75,className:"resizer",height:"fit",props:{onMouseDown:e.resizeColumn,onTouchStart:e.resizeColumn},children:s.jsx(p.default,{component:"datagrid.header.cell.resizer"})})})}de.displayName="DataGridHeaderCellResizer";function ue(l){const{column:e}=l,{key:t,pin:i,left:n,right:o,isEdge:r,isLeaf:c,leafs:a,grid:u,header:d,gridRows:m,widthVarName:w,leftVarName:y,rightVarName:v,inlineWidth:L}=e,G=t===E,k=t===j,b=t===R,x=t===M,S=i==="LEFT",T=i==="RIGHT",N=S||i==="RIGHT",P=S&&n===0,I=S&&r,O=T&&r,$=T&&o===0,F=c&&!G&&!b&&!x,q=c?1:a.length,X=!b&&!x,W=!b&&!x,Y=x?void 0:e.align==="right"?10:3,H=x?void 0:e.align==="center"?3:void 0,V=g.useCallback(()=>{u.toggleSelectAllRows()},[]),K=g.useMemo(()=>{if(b)return null;if(x){const B=u.selectedRows.size===u.props.data.length,z=!B&&u.selectedRows.size>0;return s.jsx(U.default,{variant:"datagrid",m:1,indeterminate:z,checked:B,onChange:V})}if(k){if(u.groupColumns.size===1){const B=u.columns.value.leafs.findOrThrow(z=>z.key===u.groupColumns.values().next().value);return B.header??B.key}return"Group"}return d??t},[u.groupColumns,u.selectedRows,V]);return s.jsx(f.default,{props:{role:"columnheader"},component:"datagrid.header.cell",variant:{isPinned:N,isFirstLeftPinned:P,isLastLeftPinned:I,isFirstRightPinned:O,isLastRightPinned:$,isSortable:F,isRowNumber:b},gridRow:m,gridColumn:q,style:{width:`var(${w})`,left:S?`var(${y})`:void 0,right:T?`var(${v})`:void 0},children:!G&&s.jsxs(s.Fragment,{children:[s.jsx(f.default,{width:"fit",height:"fit",jc:e.align,props:{onClick:F?()=>e.sortColumn():void 0},children:s.jsxs(f.default,{overflow:"hidden",position:c?void 0:"sticky",ai:"center",transition:"none",pl:Y,pr:H,style:{left:i?void 0:`var(${u.leftEdgeVarName})`},children:[s.jsx(p.default,{overflow:"hidden",textOverflow:"ellipsis",textWrap:"nowrap",children:K}),t===u.sortColumn&&s.jsx(p.default,{pl:(L??0)<58?0:2,children:s.jsx(h.SortIcon,{width:"16px",rotate:u.sortDirection==="ASC"?0:180,fill:"currentColor"})}),W&&s.jsx(p.default,{minWidth:e.align==="right"?4:10})]})}),X&&s.jsx(de,{column:e}),W&&s.jsx(ae,{column:e})]})})}ue.displayName="DataGridHeaderCell";function ce(l){const{grid:e}=l,{isResizeMode:t}=e;return s.jsx(ee.default,{component:"datagrid.header",variant:{isResizeMode:t},style:{gridTemplateColumns:e.gridTemplateColumns.value},children:e.headerRows.value.map(i=>i.map(n=>s.jsx(ue,{column:n},n.uniqueKey)))})}ce.displayName="DataGridHeader";function he(l){const{grid:e}=l,[t,i]=g.useState(0),n=g.useCallback(h.FnUtils.throttle(o=>{i(o.target.scrollTop)},100),[]);return s.jsxs(p.default,{overflowX:"scroll",props:{onScroll:n},children:[s.jsx(ce,{grid:e}),s.jsx(le,{grid:e,scrollTop:t})]})}he.displayName="DataGridContent";function me(l){const{grid:e}=l;return e.groupColumns.size===0?null:s.jsxs(f.default,{component:"datagrid.topBar.columnGroups",children:[s.jsx(_.Span,{component:"datagrid.topBar.columnGroups.icon",children:s.jsx(h.GroupingIcon,{width:"100%",fill:"currentColor"})}),Array.from(e.groupColumns,t=>{const i=e.columns.value.leafs.findOrThrow(n=>n.key===t);return s.jsxs(g.Fragment,{children:[s.jsx(h.ExpandIcon,{fill:"currentColor",width:"14px",height:"14px",rotate:-90}),s.jsxs(f.default,{component:"datagrid.topBar.columnGroups.item",children:[i.header??i.key,s.jsx(C.default,{component:"datagrid.topBar.columnGroups.item.icon",onClick:()=>e.toggleGrouping(i.key),children:s.jsx(h.CloseSvg,{fill:"currentColor",width:"100%"})})]})]},t)})]})}me.displayName="DataGridColumnGroups";function pe(l){const{grid:e}=l,[t,i,n]=h.useVisibility({event:"mousedown"}),o=g.useMemo(()=>e.columns.value.leafs.filter(r=>![E,R,M,j].includes(r.key)),[e.columns.value.leafs]);return s.jsxs(C.default,{component:"datagrid.topBar.contextMenu",onClick:()=>i(!t),children:[s.jsx(Re.default,{viewBox:"0 0 24 24",width:"20",fill:"currentColor",...l,children:s.jsx("path",{d:"M5 6h14M5 12h14M5 18h14",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})}),t&&s.jsx(te.default,{component:"datagrid.topBar.contextMenu.tooltip",ref:n,children:o.map(r=>s.jsxs(C.default,{component:"datagrid.topBar.contextMenu.tooltip.item",onClick:c=>{c.stopPropagation(),r.toggleVisibility()},children:[s.jsx(U.default,{variant:"datagrid",checked:r.isVisible,onChange:()=>{},focus:{outline:0}}),r.header??r.key]},r.key))})]})}pe.displayName="DataGridTopBarContextMenu";function ge(l){const{grid:e}=l;return s.jsxs(f.default,{component:"datagrid.topBar",position:"relative",children:[s.jsx(pe,{grid:e}),s.jsx(me,{grid:e})]})}ge.displayName="DataGridTopBar";function Se(l){const[e,t]=g.useState(0),i=g.useRef();return i.current||(i.current=new je(l,()=>t(n=>n+1))),g.useEffect(()=>{i.current.props=l,i.current.rows.clear(),i.current.flatRows.clear(),i.current.update()},[l.data]),g.useEffect(()=>{i.current.props=l,i.current.sourceColumns.clear(),i.current.columns.clear(),i.current.headerRows.clear(),i.current.gridTemplateColumns.clear(),i.current.rows.clear(),i.current.flatRows.clear(),i.current.sizes.clear(),i.current.update()},[l.def]),g.useEffect(()=>{i.current.props=l,i.current.update()},[l.loading]),i.current}function fe(l){const e=Se(l);return console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid render"),s.jsxs(p.default,{component:"datagrid",style:e.sizes.value,props:{role:"presentation"},children:[e.props.def.topBar&&s.jsx(ge,{grid:e}),s.jsx(he,{grid:e}),e.props.def.bottomBar&&s.jsx(ye,{grid:e})]})}fe.displayName="DataGrid";exports.default=fe;