@cronocode/react-box 3.1.1 → 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;
@@ -0,0 +1,6 @@
1
+ import { default as CellModel } from '../models/cellModel';
2
+ interface Props<TRow> {
3
+ cell: CellModel<TRow>;
4
+ }
5
+ export default function DataGridCellRowSelection<TRow>(props: Props<TRow>): import("react/jsx-runtime").JSX.Element;
6
+ export {};
@@ -0,0 +1,6 @@
1
+ import { default as CellModel } from '../models/cellModel';
2
+ interface Props<TRow> {
3
+ cell: CellModel<TRow>;
4
+ }
5
+ export default function DataGridCellText<TRow>(props: Props<TRow>): import("react/jsx-runtime").JSX.Element;
6
+ export {};
@@ -1,3 +1,4 @@
1
+ import { default as CellModel } from '../models/cellModel';
1
2
  export type Key = string | number;
2
3
  export type PinPosition = 'LEFT' | 'RIGHT';
3
4
  export declare const NO_PIN = "NO_PIN";
@@ -23,23 +24,30 @@ export interface ColumnType<TRow> {
23
24
  pin?: PinPosition;
24
25
  width?: number;
25
26
  columns?: ColumnType<TRow>[];
26
- align?: 'right' | 'center';
27
+ align?: 'left' | 'right' | 'center';
28
+ Cell?: React.ComponentType<{
29
+ cell: CellModel<TRow>;
30
+ }>;
27
31
  }
28
32
  export interface GridDefinition<TRow> {
29
33
  rowKey?: KeysMatching<TRow, Key> | ((rowData: TRow) => Key);
30
34
  columns: ColumnType<TRow>[];
31
35
  showRowNumber?: boolean | {
32
36
  pinned?: boolean;
37
+ width?: number;
33
38
  };
34
39
  rowSelection?: boolean | {
35
40
  pinned?: boolean;
36
41
  };
37
42
  rowHeight?: number;
38
43
  visibleRowsCount?: number;
44
+ topBar?: boolean;
45
+ bottomBar?: boolean;
39
46
  }
40
47
  export interface DataGridProps<TRow> {
41
48
  data: TRow[];
42
49
  def: GridDefinition<TRow>;
50
+ loading?: boolean;
43
51
  onSelectionChange?: (event: SelectionChangeEvent<TRow>) => void;
44
52
  }
45
53
  interface SelectionChangeEvent<TRow, TKey = TRow[keyof TRow] | number | string> {
@@ -6,5 +6,5 @@ export default class CellModel<TRow> {
6
6
  readonly row: RowModel<TRow>;
7
7
  readonly column: ColumnModel<TRow>;
8
8
  constructor(grid: GridModel<TRow>, row: RowModel<TRow>, column: ColumnModel<TRow>);
9
- get value(): React.ReactNode;
9
+ get value(): number | TRow[keyof TRow];
10
10
  }
@@ -1,7 +1,7 @@
1
- import { default as GridModel } from './gridModel';
2
1
  import { ColumnType, PinPosition } from '../contracts/dataGridContract';
2
+ import { default as GridModel } from './gridModel';
3
3
  export default class ColumnModel<TRow> {
4
- private readonly def;
4
+ readonly def: ColumnType<TRow>;
5
5
  readonly grid: GridModel<TRow>;
6
6
  private parent?;
7
7
  constructor(def: ColumnType<TRow>, grid: GridModel<TRow>, parent?: ColumnModel<TRow> | undefined);
@@ -9,8 +9,11 @@ export default class ColumnModel<TRow> {
9
9
  get visibleColumns(): ColumnModel<TRow>[];
10
10
  get key(): import('../contracts/dataGridContract').Key;
11
11
  get header(): string | undefined;
12
- get align(): "center" | "right" | undefined;
12
+ get align(): "center" | "right" | "left" | undefined;
13
13
  get isLeaf(): boolean;
14
+ get Cell(): import('react').ComponentType<{
15
+ cell: import('./cellModel').default<TRow>;
16
+ }> | undefined;
14
17
  private _pin?;
15
18
  get pin(): PinPosition | undefined;
16
19
  get uniqueKey(): string;
@@ -1,16 +1,17 @@
1
+ import { DataGridProps, Key, PinPosition } from '../contracts/dataGridContract';
1
2
  import { default as ColumnModel } from './columnModel';
2
3
  import { default as GroupRowModel } from './groupRowModel';
3
4
  import { default as RowModel } from './rowModel';
4
- import { DataGridProps, Key, PinPosition } from '../contracts/dataGridContract';
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> {
10
- readonly props: DataGridProps<TRow>;
11
+ props: DataGridProps<TRow>;
11
12
  readonly update: () => void;
12
13
  constructor(props: DataGridProps<TRow>, update: () => void);
13
- private _sourceColumns;
14
+ readonly sourceColumns: import('../../../utils/memo').Memo<ColumnModel<TRow>[]>;
14
15
  readonly columns: import('../../../utils/memo').Memo<{
15
16
  left: ColumnModel<TRow>[];
16
17
  middle: ColumnModel<TRow>[];
@@ -1,8 +1,8 @@
1
+ import { Key } from '../contracts/dataGridContract';
1
2
  import { default as ColumnModel } from './columnModel';
2
3
  import { default as GridModel } from './gridModel';
3
4
  import { default as GroupRowCellModel } from './groupRowCellModel';
4
5
  import { default as RowModel } from './rowModel';
5
- import { Key } from '../contracts/dataGridContract';
6
6
  export default class GroupRowModel<TRow> {
7
7
  readonly grid: GridModel<TRow>;
8
8
  readonly groupColumn: ColumnModel<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,7 +1,7 @@
1
+ import { Key } from '../contracts/dataGridContract';
1
2
  import { default as CellModel } from './cellModel';
2
3
  import { default as GridModel } from './gridModel';
3
4
  import { default as GroupRowModel } from './groupRowModel';
4
- import { Key } from '../contracts/dataGridContract';
5
5
  export default class RowModel<TRow> {
6
6
  readonly grid: GridModel<TRow>;
7
7
  readonly data: TRow;
@@ -1 +1 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("react/jsx-runtime"),m=require("../box.cjs"),C=require("./flex.cjs"),w=require("react"),u=require("../core.cjs"),g=require("./button.cjs"),I=require("./checkbox.cjs"),U=require("./grid.cjs"),S=require("./semantics.cjs"),q=require("./tooltip.cjs"),he=require("./baseSvg.cjs");function me(a){const{grid:e}=a;return n.jsxs(C.default,{component:"datagrid.bottomBar",children:[n.jsxs(m.default,{children:["Rows: ",e.props.data.length]}),e.props.def.rowSelection&&n.jsxs(m.default,{children:["Selected: ",e.selectedRows.size]})]})}class L{constructor(e,t,i){this.def=e,this.grid=t,this.parent=i,this.columns=e.columns?.map(s=>new L(e.pin?{...s,pin:e.pin}:s,t,this))??[],this.isLeaf&&(this._inlineWidth=this.key==T?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}_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 L({...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(h=>h.inlineWidth)-this.leafs.length*i,r=this.leafs.toRecord(h=>[h.key,h.inlineWidth]),c=u.FnUtils.throttle(h=>{const p=(h.pageX-t)*(this.pin==="RIGHT"?-1:1);this.leafs.forEach(x=>{const k=r[x.key],b=o>0?(k-i)/o*p:p/this.leafs.length,y=Math.round(k+b);x.setWidth(y<i?i:y)}),this.grid.sizes.clear(),s()},40),l=new AbortController,d=h=>{l.abort(),this.grid.isResizeMode=!1,s()};window.addEventListener("mousemove",c,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 pe{constructor(e,t,i){this.grid=e,this.row=t,this.column=i}get value(){return this.column.key===v?this.row.rowIndex+1:this.column.key===f?`${this.row.groupValue} (${this.row.count})`:null}}class X{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 pe(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===f)}get groupingColumnGridColumn(){const{visibleLeafs:e}=this.grid.columns.value,{groupingColumn:t}=this;return e.sumBy(s=>s.pin===t.pin&&s.key!==T&&s.key!==_&&s.key!==v?1:0)}toggleRow(){this.grid.toggleGroupRow(this.key)}}class ge{constructor(e,t,i){this.grid=e,this.row=t,this.column=i}get value(){return this.column.key===T?null:this.column.key===v?this.row.rowIndex+1:this.row.data[this.column.key]}}class ${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 ge(this.grid,this,e))}get selected(){return this.grid.selectedRows.has(this.key)}get flatRows(){return this}get allRows(){return this}}const fe="NO_PIN",T="empty-cell",v="row-number-cell",_="row-selection-cell",f="grouping-cell";class we{constructor(e,t){if(this.props=e,this.update=t,console.debug("\x1B[32m%s\x1B[0m","[react-box]: DataGrid GridModel ctor"),this._sourceColumns=e.def.columns.map(i=>new L(i,this)),this._sourceColumns.push(new L({key:T},this)),e.def.rowSelection){const i=typeof e.def.rowSelection=="object"&&e.def.rowSelection.pinned?"LEFT":void 0;this._sourceColumns.unshift(new L({key:_,pin:i,width:50,align:"center"},this))}if(e.def.showRowNumber){const i=typeof e.def.showRowNumber=="object"&&e.def.showRowNumber.pinned?"LEFT":void 0;this._sourceColumns.unshift(new L({key:v,pin:i,width:70,align:"right"},this))}}_sourceColumns=[];columns=u.memo(()=>{console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid columns memo");const e=this._sourceColumns.map(l=>l.getPinnedColumn("LEFT")).filter(l=>!!l),t=this._sourceColumns.map(l=>l.getPinnedColumn()).filter(l=>!!l),i=this._sourceColumns.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),c=s.maxBy(l=>l.death)+1;return{left:e,middle:t,right:i,flat:s,leafs:o,visibleLeafs:r,maxDeath:c}});headerRows=u.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=u.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=u.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 c=this.columns.value.leafs.findOrThrow(l=>l.key===r);return this._sortColumn===f&&(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((p,x)=>new $(this,p,o+1+x));const h=new X(this,c,d,o,l.key);return o+=1,h.expanded&&(o+=d.length),h})};return t(e,new Set(this.groupColumns),0)}return e.map((t,i)=>new $(this,t,i))});flatRows=u.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=u.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===f);if(i){const s=t.sumBy(o=>o.pin===i.pin&&o.key!==v&&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));const t=this._sourceColumns.find(i=>i.key===f);if(this.groupColumns.size>0&&!t){const i=this._sourceColumns.sumBy(s=>s.key===v||s.key===_?1:0);this._sourceColumns.splice(i,0,new L({key:f},this))}else this.groupColumns.size===0&&t&&(this._sourceColumns=this._sourceColumns.removeBy(i=>i.key===f));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=this._sourceColumns.removeBy(e=>e.key===f),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.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}}function N(a){const{children:e,column:t,style:i,...s}=a,{key:o,pin:r,left:c,right:l,isEdge:d,align:h,widthVarName:p,leftVarName:x,rightVarName:k}=t,b=o===T,y=o===v,j=o===_,G=r==="LEFT",R=r==="RIGHT",D=G||R,M=G&&c===0,B=G&&d,P=R&&d,W=R&&l===0,V=!j&&!b;return n.jsxs(C.default,{component:"datagrid.cell",props:{role:"cell"},variant:{isPinned:D,isFirstLeftPinned:M,isLastLeftPinned:B,isFirstRightPinned:P,isLastRightPinned:W,isRowNumber:y,isRowSelection:j},jc:h,style:{width:`var(${p})`,height:`var(${t.grid.rowHeightVarName})`,left:G?`var(${x})`:void 0,right:R?`var(${k})`:void 0,...i},...s,children:[V&&n.jsx(m.default,{px:4,textOverflow:"ellipsis",overflow:"hidden",textWrap:"nowrap",children:e}),j&&e]})}N.displayName="DataGridCell";function Y(a){const{row:e}=a,t=w.useCallback(()=>{e.grid.toggleRowsSelection(e.allRows.map(i=>i.key))},[]);return n.jsx(C.default,{className:"grid-row",display:"contents",props:{role:"rowgroup"},children:e.cells.map(i=>{const{key:s,pin:o,groupColumnWidthVarName:r}=i.column,c=o==="RIGHT";if(s===f)return n.jsx(N,{column:i.column,style:{width:`var(${r})`,right:c?"0":void 0},br:e.groupingColumn.pin==="LEFT"?1:void 0,gridColumn:e.groupingColumnGridColumn,pl:4*e.depth,children:n.jsxs(g.default,{clean:!0,onClick:()=>e.toggleRow(),cursor:"pointer",display:"flex",gap:1,ai:"center",children:[n.jsx(u.ExpandIcon,{fill:"currentColor",width:"14px",height:"14px",rotate:e.expanded?0:-90}),i.value]})},s);if(s===_){const l=e.allRows,d=l.every(p=>p.selected),h=!d&&l.some(p=>p.selected);return n.jsx(N,{column:i.column,children:n.jsx(I.default,{variant:"datagrid",m:1,checked:d,indeterminate:h,onChange:t})},s)}if(o!==e.groupingColumn.pin||s===T||s===v)return n.jsx(N,{column:i.column,children:i.value},s)})})}Y.displayName="DataGridGroupRow";function K(a){const{row:e}=a,t=w.useCallback(i=>{e.grid.toggleRowSelection(e.key)},[]);return n.jsx(C.default,{className:"grid-row",display:"contents",props:{role:"row"},children:e.cells.map(i=>n.jsx(N,{column:i.column,children:i.column.key===_?n.jsx(I.default,{variant:"datagrid",checked:e.selected,onChange:t}):i.value},i.column.key))})}K.displayName="DataGridRow";const Ce=10,F=20;function J(a){const{grid:e,scrollTop:t}=a,i=e.flatRows.value.length,s=Math.max(0,Math.floor(t/e.rowHeight)-F),o=e.props.def.visibleRowsCount??Ce,r=w.useMemo(()=>{console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid render rows");const c=o+F*2;return e.flatRows.value.take(c,s).map(d=>d instanceof X?n.jsx(Y,{row:d},d.key):n.jsx(K,{row:d},d.key))},[e.flatRows.value,s]);return console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid render DataGridBody"),n.jsx(m.default,{style:{height:e.rowHeight*o+e.rowHeight/5},children:n.jsx(m.default,{style:{height:`${i*e.rowHeight}px`},children:n.jsx(U.default,{width:"max-content",minWidth:"fit",transition:"none",style:{transform:`translateY(${s*e.rowHeight}px)`,gridTemplateColumns:e.gridTemplateColumns.value},children:r})})})}J.displayName="DataGridBody";function Q(a){const{column:e}=a,[t,i,s]=u.useVisibility({hideOnScroll:!0,event:"mousedown"}),[o,r]=w.useState({top:0,left:0}),c=w.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"),h=e.isLeaf&&e.grid.sortColumn===e.key,p=e.pin!=="LEFT",x=e.pin!=="RIGHT",k=!!e.pin,b=e.isLeaf&&e.key!==f,y=e.isLeaf&&e.key===f,j=l||d||h,G=p||x||k;return n.jsx(C.default,{position:"absolute",right:e.pin==="RIGHT"?2.5:4,top:"1/2",translateY:-3,ai:"center",children:n.jsxs(g.default,{component:"datagrid.header.cell.contextMenu",onClick:()=>i(!t),children:[n.jsx(S.Span,{component:"datagrid.header.cell.contextMenu.icon",children:n.jsx(u.DotsIcon,{fill:"currentColor"})}),t&&n.jsxs(q.default,{component:"datagrid.header.cell.contextMenu.tooltip",variant:{openLeft:c},onPositionChange:r,ref:s,children:[l&&n.jsxs(g.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.sortColumn("ASC"),children:[n.jsx(S.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(u.SortIcon,{width:"100%",fill:"currentColor"})}),"Sort Ascending"]}),d&&n.jsxs(g.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.sortColumn("DESC"),children:[n.jsx(S.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(u.SortIcon,{width:"100%",fill:"currentColor",rotate:180})}),"Sort Descending"]}),h&&n.jsxs(g.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.sortColumn(void 0),children:[n.jsx(m.default,{width:4}),"Clear Sort"]}),j&&(G||b||y)&&n.jsx(m.default,{bb:1,my:2,borderColor:"gray-300"}),p&&n.jsxs(g.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.pinColumn("LEFT"),children:[n.jsx(S.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(u.PinIcon,{width:"100%",fill:"currentColor"})}),"Pin Left"]}),x&&n.jsxs(g.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.pinColumn("RIGHT"),children:[n.jsx(S.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(u.PinIcon,{width:"100%",fill:"currentColor",rotate:-90})}),"Pin Right"]}),k&&n.jsxs(g.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:()=>e.pinColumn(),children:[n.jsx(m.default,{width:4}),"Unpin"]}),j&&G&&(b||y)&&n.jsx(m.default,{bb:1,my:2,borderColor:"gray-300"}),b&&n.jsxs(g.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:e.toggleGrouping,children:[n.jsx(S.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(u.GroupingIcon,{width:"100%",fill:"currentColor"})}),n.jsxs(m.default,{textWrap:"nowrap",children:["Group by ",e.header??e.key]})]}),y&&n.jsxs(g.default,{component:"datagrid.header.cell.contextMenu.tooltip.item",onClick:e.grid.unGroupAll,children:[n.jsx(S.Span,{component:"datagrid.header.cell.contextMenu.tooltip.item.icon",children:n.jsx(u.GroupingIcon,{width:"100%",fill:"currentColor"})}),n.jsx(m.default,{textWrap:"nowrap",children:"Un-Group All"})]})]})]})})}Q.displayName="DataGridHeaderCellContextMenu";function Z(a){const{column:e}=a;return n.jsx(C.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(m.default,{cursor:"col-resize",px:.75,className:"resizer",height:"fit",props:{onMouseDown:e.resizeColumn,onTouchStart:e.resizeColumn},children:n.jsx(m.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:c,leafs:l,grid:d,header:h,gridRows:p,widthVarName:x,leftVarName:k,rightVarName:b,inlineWidth:y}=e,j=t===T,G=t===f,R=t===v,D=t===_,M=i==="LEFT",B=i==="RIGHT",P=M||i==="RIGHT",W=M&&s===0,V=M&&r,le=B&&r,ae=B&&o===0,z=c&&!j&&!R&&!D,de=c?1:l.length,ue=!R&&!D,A=!R&&!D,O=w.useCallback(()=>{d.toggleSelectAllRows()},[]),ce=w.useMemo(()=>{if(R)return null;if(D){const E=d.selectedRows.size===d.props.data.length,H=!E&&d.selectedRows.size>0;return n.jsx(I.default,{variant:"datagrid",m:1,indeterminate:H,checked:E,onChange:O})}if(G){if(d.groupColumns.size===1){const E=d.columns.value.leafs.findOrThrow(H=>H.key===d.groupColumns.values().next().value);return E.header??E.key}return"Group"}return h??t},[d.groupColumns,d.selectedRows,O]);return n.jsx(C.default,{component:"datagrid.header.cell",variant:{isRowNumber:R,isPinned:P,isFirstLeftPinned:W,isLastLeftPinned:V,isFirstRightPinned:le,isLastRightPinned:ae,isSortable:z},gridRow:p,gridColumn:de,style:{width:`var(${x})`,left:M?`var(${k})`:void 0,right:B?`var(${b})`:void 0},children:!j&&n.jsxs(n.Fragment,{children:[n.jsx(C.default,{width:"fit",height:"fit",jc:e.align,props:{onClick:z?()=>e.sortColumn():void 0},children:n.jsxs(C.default,{overflow:"hidden",position:c?void 0:"sticky",ai:"center",transition:"none",pl:e.align?void 0:4,style:{left:i?void 0:`var(${d.leftEdgeVarName})`},children:[n.jsx(m.default,{overflow:"hidden",textOverflow:"ellipsis",textWrap:"nowrap",children:ce}),t===d.sortColumn&&n.jsx(m.default,{pl:(y??0)<58?0:2,children:n.jsx(u.SortIcon,{width:"16px",rotate:d.sortDirection==="ASC"?0:180,fill:"currentColor"})}),A&&n.jsx(m.default,{minWidth:10})]})}),ue&&n.jsx(Z,{column:e}),A&&n.jsx(Q,{column:e})]})})}ee.displayName="DataGridHeaderCell";function te(a){const{grid:e}=a,{isResizeMode:t}=e;return n.jsx(U.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]=w.useState(0),s=w.useCallback(u.FnUtils.throttle(o=>{i(o.target.scrollTop)},100),[]);return n.jsxs(m.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(C.default,{component:"datagrid.topBar.columnGroups",children:[n.jsx(S.Span,{component:"datagrid.topBar.columnGroups.icon",children:n.jsx(u.GroupingIcon,{width:"100%",fill:"currentColor"})}),Array.from(e.groupColumns,t=>{const i=e.columns.value.leafs.findOrThrow(s=>s.key===t);return n.jsxs(w.Fragment,{children:[n.jsx(u.ExpandIcon,{fill:"currentColor",width:"14px",height:"14px",rotate:-90}),n.jsxs(C.default,{component:"datagrid.topBar.columnGroups.item",children:[i.header??i.key,n.jsx(g.default,{component:"datagrid.topBar.columnGroups.item.icon",onClick:()=>e.toggleGrouping(i.key),children:n.jsx(u.CloseSvg,{fill:"currentColor",width:"100%"})})]})]},t)})]})}se.displayName="DataGridColumnGroups";function ne(a){const{grid:e}=a,[t,i,s]=u.useVisibility({event:"mousedown"}),o=w.useMemo(()=>e.columns.value.leafs.filter(r=>![T,v,_,f].includes(r.key)),[e.columns.value.leafs]);return n.jsxs(g.default,{component:"datagrid.topBar.contextMenu",onClick:()=>i(!t),children:[n.jsx(he.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(q.default,{component:"datagrid.topBar.contextMenu.tooltip",ref:s,children:o.map(r=>n.jsxs(g.default,{component:"datagrid.topBar.contextMenu.tooltip.item",onClick:c=>{c.stopPropagation(),r.toggleVisibility()},children:[n.jsx(I.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(C.default,{component:"datagrid.topBar",position:"relative",children:[n.jsx(ne,{grid:e}),n.jsx(se,{grid:e})]})}oe.displayName="DataGridTopBar";function xe(a){const[e,t]=w.useState(0),i=w.useRef();return i.current||(i.current=new we(a,()=>t(s=>s+1))),i.current}function re(a){const e=xe(a);return console.debug("\x1B[36m%s\x1B[0m","[react-box]: DataGrid render"),n.jsxs(m.default,{component:"datagrid",style:e.sizes.value,props:{role:"presentation"},children:[n.jsx(oe,{grid:e}),n.jsx(ie,{grid:e}),n.jsx(me,{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;