@progress/kendo-react-grid
Version:
React Data Grid (Table) provides 100+ ready-to-use data grid features. KendoReact Grid package
1,056 lines (1,055 loc) • 38.2 kB
JavaScript
/**
* @license
*-------------------------------------------------------------------------------------------
* Copyright © 2026 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the package root for more information
*-------------------------------------------------------------------------------------------
*/
"use client";
import * as d from "react";
import { useAdaptiveModeContext as $t, canUseDOM as Q, getActiveElement as Jt, useDir as Xt, setScrollbarWidth as qe, cloneArray as Yt, RowHeightService as Qt, getter as Z } from "@progress/kendo-react-common";
import { filterBy as Zt } from "@progress/kendo-data-query";
import { getSelectionOptions as se, populateClipboardData as pt, ClipboardActionType as ke, getEditableOptions as en, tableKeyboardNavigation as q, ClipboardService as tn, TableKeyboardNavigationContext as nn, updateLeft as rn, updateRight as on, getGroupExpandableOptions as an, groupExpandReducer as dn, getDetailExpandableOptions as cn, detailExpandReducer as ln, getSelectedState as _e, editReducer as sn, EDIT_ACTION as un, closestTagName as ue, getSelectedStateFromKeyDown as gn, getColumnIndex as fn, getRowIndex as mn } from "@progress/kendo-react-data-tools";
import { ColumnResize as Pn } from "./drag/ColumnResize.mjs";
import { CommonDragLogic as hn } from "./drag/CommonDragLogic.mjs";
import { getDefaultHeadContextMenuItems as Rn, getDefaultBodyContextMenuItems as vn, isRowReorderEnabled as bn, calcRowHeight as Cn, sanitizeColumns as In, getFlatColumnsState as En, getDataAsArray as wn } from "./utils/index.mjs";
import { VirtualScroll as xn } from "./VirtualScroll.mjs";
import { GridContextMenu as yn } from "./contextMenu/GridContextMenu.mjs";
import { GridContextMenuAnchorPart as $e } from "./contextMenu/enums.mjs";
import { normalizeSortable as Sn, firstLevelSortSeqMap as kn } from "./sortCommon.mjs";
import { BasePDFExport as Dn } from "./BasePDFExport.mjs";
import { BaseCSVExport as Kn } from "./BaseCSVExport.mjs";
import { GridContext as Mn } from "./utils/GridContext.mjs";
import { operators as Hn } from "./filterCommon.mjs";
const Tn = (t, E, h, D) => {
const _ = Object.keys(E.cells).map((y) => h.findIndex(($) => $.field === y)), O = Object.keys(E.cells).length > 0 ? _ : !0;
return t.reduce((y, $) => (y[$[D != null ? D : "id"]] = O, y), {});
}, Je = (t, E, h, D) => E.reduce((O, y) => ({
...O,
...Tn(Zt(t, y), y, h, D)
}), {}), Jn = (t) => {
const E = t.gridProps.isClient, [h, D] = d.useState({}), _ = $t(), O = d.useRef(null), y = d.useRef(null), $ = (e) => {
e.event.preventDefault(), D({
...h,
show: !0,
offset: {
left: e.event.pageX,
top: e.event.pageY
},
dataItem: e.dataItem,
field: e.field
});
}, De = () => {
D({});
}, C = d.useMemo(() => t.columnsRef.find((e) => e.field === h.field), [t.columnsRef, h]), Ke = d.useMemo(() => {
const e = t.gridProps.sortable && (C == null ? void 0 : C.sortable);
return Rn({
pdf: !!t.gridProps.pdf,
sortable: !!e,
selectable: se(t.gridProps.selectable).enabled,
clipboard: !!t.gridProps.clipboard
});
}, [C, t.gridProps.sortable, t.gridProps.selectable, t.gridProps.clipboard]), Me = d.useMemo(() => (t.gridProps.sortable && (C == null || C.sortable), vn({
pdf: !!t.gridProps.pdf,
selectable: se(t.gridProps.selectable).enabled,
clipboard: !!t.gridProps.clipboard,
rowReorderable: bn(t.gridProps.rowReorderable)
})), [C, t.gridProps.sortable, t.gridProps.selectable, t.gridProps.clipboard]), p = d.useMemo(() => {
const e = (C == null ? void 0 : C.contextMenu) || t.gridProps.contextMenu, n = typeof e == "function" ? e(h) : e;
if (n && h.offset) {
const o = h.dataItem ? $e.body : $e.head, r = n[o], i = h.dataItem ? Me : Ke;
return r === !1 ? void 0 : r === !0 || r === void 0 ? i : r;
}
}, [t.gridProps.contextMenu, h, Me, Ke, C]), Xe = (e) => {
var c, f, l, u, s, b, m, k, z;
const n = e.event.item, o = {
target: x.current,
syntheticEvent: e.event.syntheticEvent,
nativeEvent: e.event.nativeEvent,
menuItem: n,
...e
};
t.gridProps.onContextMenuItemClick && g(t.gridProps.onContextMenuItemClick, o);
const r = w(), i = {
selectedField: "",
componentId: t.id,
dataItems: r,
dataItem: e.dataItem,
startRowIndex: -1,
endRowIndex: -1,
startColIndex: -1,
endColIndex: -1,
ctrlKey: !1,
altKey: !1,
metaKey: !1,
shiftKey: !1,
isDrag: !1,
...se(t.gridProps.selectable),
...o
}, a = r.findIndex((Y) => Y === e.dataItem);
switch ((c = n.data) == null ? void 0 : c.action) {
case "SortCommand":
if (C) {
const Y = (f = n.name) == null ? void 0 : f.toLowerCase().includes("asc"), jt = ((l = n.name) == null ? void 0 : l.toLowerCase().includes("desc")) ? "desc" : void 0, qt = Y ? "asc" : jt, _t = n.name ? qt : void 0;
He(e.event.syntheticEvent, C, _t);
}
break;
case "SelectRowCommand":
mt(i);
break;
case "SelectAllRowsCommand":
ft(i);
break;
case "ExportPDFCommand":
fe();
break;
case "ClearSelectionCommand":
ht(i);
break;
case "ReorderRowCommand":
X.current = e.dataItem, (u = n.name) != null && u.toLowerCase().includes("rowup") && a > 0 && J(e.event.syntheticEvent, a - 1, "before"), (s = n.name) != null && s.toLowerCase().includes("rowdown") && a < r.length - 1 && J(e.event.syntheticEvent, a + 1, "after"), (b = n.name) != null && b.toLowerCase().includes("rowtop") && J(e.event.syntheticEvent, 0, "before"), (m = n.name) != null && m.toLowerCase().includes("rowbottom") && J(e.event.syntheticEvent, r.length - 1, "after");
break;
case "CopySelectionCommand":
oe(
ke.copy,
e.event.nativeEvent,
{
copyHeaders: !((k = n.name) != null && k.toLowerCase().includes("noheaders"))
},
e.dataItem,
e.field
);
break;
case "PasteCommand":
oe(
ke.paste,
e.event.nativeEvent,
{
copyHeaders: !((z = n.name) != null && z.toLowerCase().includes("noheaders"))
},
e.dataItem,
e.field
);
break;
}
De();
}, ee = () => {
const e = t.columnsRef.filter((n) => n.declarationIndex >= 0 && n.parentIndex === -1);
return In(e);
}, Ye = (e, n, o) => {
if (t.gridProps.onContextMenu && E) {
const r = {
target: x.current,
syntheticEvent: e,
nativeEvent: e.nativeEvent,
dataItem: n,
field: o
};
g(t.gridProps.onContextMenu, r);
}
t.gridProps.contextMenu && $({
event: e,
dataItem: n,
field: o
});
}, Qe = (e) => {
if (e.target !== e.currentTarget)
return;
clearTimeout(we.current), v.current && (v.current.table = A.current);
const n = e.currentTarget.scrollLeft, o = e.currentTarget.scrollTop;
t.gridProps.columnVirtualization && (!S || o === Ee.current) && (we.current = window.setTimeout(() => {
N();
}, 0)), t.gridProps.scrollLeftRef && (t.gridProps.scrollLeftRef.current = n), F.current && F.current.setScrollLeft(n), L.current && L.current.setScrollLeft(n), v.current && o !== Ee.current && v.current.scrollHandler(e), t.gridProps.onScroll && E && g(t.gridProps.onScroll, {
...R(e)
}), Ee.current = o;
}, Ze = (e) => {
q.onKeyDown(e, {
navigatable: t.gridProps.navigatable || !1,
contextStateRef: T,
navigationStateRef: V,
onNavigationAction: et,
columns: ee()
}), q.onGetSnapshotBeforeUpdate({
document: K(),
contextStateRef: T,
navigationStateRef: V
});
const n = {
dataItems: w(),
mode: P.mode,
cell: P.cell,
componentId: t.id,
selectedField: "",
...R(e)
};
t.gridProps.onKeyDown && E && g(t.gridProps.onKeyDown, n);
}, pe = (e) => {
q.onFocus(e, {
navigatable: !!t.gridProps.navigatable,
contextStateRef: T
});
}, et = (e) => {
const { columnsRef: n } = t;
if (e.action === "moveToNextPage" && bt(e.event), e.action === "moveToPrevPage" && Ct(e.event), e.focusElement && (e.action === "reorderToRight" || e.action === "reorderToLeft")) {
const o = parseInt(e.focusElement.ariaColIndex, 10) - 1;
if (!n[o].reorderable)
return;
if (e.action === "reorderToRight" && o < t.columnsRef.length - 1) {
if (n[o + 1].disableReorder)
return;
ne(o, o + 1, e.event);
}
if (e.action === "reorderToLeft" && o > 0) {
if (n[o - 1].disableReorder)
return;
ne(o, o - 1, e.event);
}
}
if (e.action === "select" && st(e.event), t.gridProps.onNavigationAction && E) {
const o = {
focusElement: e.focusElement,
...R(e.event)
};
g(t.gridProps.onNavigationAction, o);
}
}, tt = (e, n) => {
t.gridProps.onRowClick && ue(e.target, "TD") && g(t.gridProps.onRowClick, {
dataItem: n,
...R(e)
});
}, nt = (e, n) => {
t.gridProps.onRowDoubleClick && ue(e.target, "TD") && g(t.gridProps.onRowDoubleClick, {
dataItem: n,
...R(e)
});
}, rt = (e) => {
t.gridProps.onItemChange && g(t.gridProps.onItemChange, {
...R(e.syntheticEvent),
dataItem: e.dataItem,
field: void 0,
value: void 0
});
}, ot = (e) => {
t.gridProps.onItemChange && t.gridProps.onEditChange && g(t.gridProps.onEditChange, {
edit: {},
...R(e.syntheticEvent)
});
}, it = (e, n, o) => {
if (je.enabled && je.mode === "incell" && t.gridProps.dataItemKey) {
const r = sn(t.gridProps.edit, {
type: un.ENTER_FIELD_EDIT,
payload: { id: n[t.gridProps.dataItemKey], field: o }
});
t.gridProps.onEditChange && g(t.gridProps.onEditChange, {
edit: r,
...R(e)
});
}
}, at = (e, n) => {
var r;
if (cn(t.detailExpandable).enabled) {
const i = ln((r = t.gridProps.detailExpand) != null ? r : {}, e);
t.gridProps.onDetailExpandChange && g(t.gridProps.onDetailExpandChange, {
...R(n),
detailExpand: i
});
}
}, dt = (e, n) => {
var r;
const o = an(
typeof t.gridProps.groupable == "object" ? t.gridProps.groupable.expandable !== !1 : t.gridProps.groupable
);
if (o.enabled) {
const i = dn((r = t.gridProps.groupExpand) != null ? r : [], e, o);
t.gridProps.onGroupExpandChange && g(t.gridProps.onGroupExpandChange, {
...R(n),
groupExpand: i
});
}
}, He = (e, n, o) => {
const { allowUnsort: r, mode: i } = Sn(
t.gridProps.sortable || !1,
n.sortable || !1
), a = (t.gridProps.sort || []).filter((l) => l.field === n.field)[0], c = o || kn[r][a && a.dir || ""], f = i === "single" ? [] : (t.gridProps.sort || []).filter((l) => l.field !== n.field);
c !== "" && n.field && f.push({ field: n.field, dir: c }), Te(f, e);
}, ct = (e) => {
t.gridProps.onItemChange && g(t.gridProps.onItemChange, {
...R(e.syntheticEvent),
dataItem: e.dataItem,
dataIndex: e.dataIndex,
field: e.field,
value: e.value
});
}, lt = (e) => {
var n;
if (t.gridProps.onSelectionChange && P.enabled) {
const { event: o, dataItem: r, dataIndex: i, columnIndex: a } = e, c = {
...R(o),
dataItem: r,
startColIndex: a,
endColIndex: a,
startRowIndex: i,
endRowIndex: i,
dataItems: w(),
altKey: !1,
ctrlKey: !1,
shiftKey: !1,
metaKey: !1,
mode: P.mode,
cell: P.cell,
isDrag: !1,
componentId: t.id,
selectedField: ""
};
g(t.gridProps.onSelectionChange, {
...c,
select: t.gridProps.dataItemKey ? _e({
event: c,
selectedState: (n = t.gridProps.select) != null ? n : {},
dataItemKey: t.gridProps.dataItemKey
}) : {}
});
}
}, st = (e) => {
var l, u;
if (!P.enabled || !t.gridProps.dataItemKey)
return;
const n = {
dataItems: w(),
mode: P.mode,
cell: P.cell,
componentId: t.id,
selectedField: "",
...R(e)
}, o = gn({
event: n,
selectedState: (l = t.gridProps.select) != null ? l : {},
dataItemKey: t.gridProps.dataItemKey
});
if (o === t.gridProps.select)
return;
const r = e.target, i = ue(r, "TD"), a = ue(r, "TR"), c = fn(i), f = mn(a);
if (c !== void 0 && f !== void 0) {
const s = (u = wn(t.gridProps.data)) == null ? void 0 : u[f];
t.gridProps.onSelectionChange && g(t.gridProps.onSelectionChange, {
...n,
select: o,
dataItem: s,
startRowIndex: f,
startColIndex: c,
startDataItem: s,
endDataItem: s,
endRowIndex: f,
endColIndex: c,
ctrlKey: e.ctrlKey,
altKey: e.altKey,
metaKey: e.metaKey,
shiftKey: e.shiftKey,
isDrag: !1
});
}
}, ut = (e) => {
if (t.gridProps.onHeaderSelectionChange && P.enabled) {
const n = w();
g(t.gridProps.onHeaderSelectionChange, {
select: e.syntheticEvent.target.checked ? n.reduce((o, r) => (t.gridProps.dataItemKey && Z(t.gridProps.dataItemKey)(r) !== void 0 && (o[Z(t.gridProps.dataItemKey)(r)] = !0), o), {}) : {},
field: e.field,
nativeEvent: e.syntheticEvent && e.syntheticEvent.nativeEvent,
syntheticEvent: e.syntheticEvent,
target: x.current,
dataItems: n
});
}
}, W = (e, n) => {
t.gridProps.onSelectionChange && P.enabled && g(t.gridProps.onSelectionChange, {
...e,
select: n
});
}, gt = (e) => {
var n;
if (t.gridProps.onSelectionChange && P.enabled) {
const o = w()[e.startRowIndex], r = w()[e.endRowIndex], i = {
syntheticEvent: void 0,
target: x.current,
selectedField: "",
componentId: t.id,
dataItems: w(),
dataItem: null,
startDataItem: o,
endDataItem: r,
...e
}, a = _e({
event: i,
selectedState: (n = t.gridProps.select) != null ? n : {},
// Thats kinda strange, even through the `dataItemKey` is required by the `getSelectedState`
// it does work correctly even without it
dataItemKey: t.gridProps.dataItemKey
});
W(i, a);
}
}, ft = (e) => {
if (t.gridProps.onSelectionChange && P.enabled) {
const n = e.dataItems[0], o = e.dataItems[e.dataItems.length - 1], r = {}, i = {
...e,
startDataItem: n,
endDataItem: o,
startRowIndex: 0,
endRowIndex: e.dataItems.length - 1,
startColIndex: 0,
endColIndex: t.columnsRef.length - 1
};
e.dataItems.forEach((a) => {
const f = Z(t.gridProps.dataItemKey)(a);
r[f] = e.cell ? [...Array(t.columnsRef.length).keys()] : !0;
}), W(i, r);
}
}, mt = (e) => {
if (t.gridProps.onSelectionChange && P.enabled) {
const o = Z(t.gridProps.dataItemKey)(e.dataItem), r = 0, i = t.columnsRef.length - 1, a = e.dataItems.findIndex(
(s) => s[t.gridProps.dataItemKey] === e.dataItem[t.gridProps.dataItemKey]
), l = {
...e,
startDataItem: a,
endDataItem: a,
startRowIndex: a,
endRowIndex: a,
startColIndex: r,
endColIndex: i
}, u = e.mode === "multiple" ? t.gridProps.select || {} : {};
u[o] === !0 || Array.isArray(u[o]) && u[o].length === t.columnsRef.length ? delete u[o] : u[o] = e.cell ? [...Array(t.columnsRef.length).keys()] : !0, W(l, u);
}
}, Pt = (e) => {
if (!P.enabled)
return;
const n = w(), o = {
syntheticEvent: void 0,
nativeEvent: void 0,
target: x.current,
selectedField: "",
componentId: t.id,
dataItems: n,
dataItem: null,
startDataItem: null,
endDataItem: null,
ctrlKey: !1,
altKey: !1,
metaKey: !1,
shiftKey: !1,
isDrag: !1,
mode: P.mode,
cell: P.cell
};
if (!e || e.length === 0) {
const s = {
...o,
startRowIndex: -1,
endRowIndex: -1,
startColIndex: -1,
endColIndex: -1
};
W(s, {});
return;
}
const r = Je(
t.gridProps.data,
e,
t.columnsRef,
t.gridProps.dataItemKey
), i = {};
Object.keys(r).forEach((s) => {
r[s] && (i[s] = P.cell ? [...Array(t.columnsRef.length).keys()] : !0);
});
let a = -1, c = -1, f = n[0] || null, l = n[n.length - 1] || null;
if (t.gridProps.dataItemKey) {
const s = Z(t.gridProps.dataItemKey);
for (let b = 0; b < n.length; b++) {
const m = s(n[b]);
m !== void 0 && r[m] && (a === -1 && (a = b, f = n[b]), c = b, l = n[b]);
}
}
const u = {
...o,
startRowIndex: a,
endRowIndex: c,
startColIndex: 0,
endColIndex: t.columnsRef.length - 1,
startDataItem: f,
endDataItem: l
};
W(u, i);
}, ht = (e) => {
t.gridProps.onSelectionChange && P.enabled && W(e, {});
}, B = (e, n, o, r, i) => {
vt();
const a = t.gridProps.onDataStateChange;
if (e) {
const c = { ...R(r), ...n, targetEvent: i };
g(e, c);
} else a && g(a, {
...R(r),
targetEvent: i || {},
dataState: {
...Ht(),
...o
}
});
}, te = (e, n, o) => {
var r;
S && t.gridProps.pageable && ((r = v.current) == null || r.reset()), B(
t.gridProps.onPageChange,
{ page: e },
{ skip: e.skip, take: e.take },
n,
o
);
}, Rt = (e, n, o) => {
var r;
t.gridProps.pageable || (r = t.gridProps.group) != null && r.length ? t.gridProps.virtualSkipRef && (t.gridProps.virtualSkipRef.current = e.skip, N()) : te(e, n, o);
}, ge = () => {
let e = t.gridProps.total || 0;
return Array.isArray(t.gridProps.data) ? e = e || t.gridProps.data.length : t.gridProps.data && (e = e || t.gridProps.data.total), e;
}, vt = () => {
S && t.gridProps.virtualSkipRef && (t.gridProps.virtualSkipRef.current = 0);
}, bt = (e) => {
var i, a;
const n = (a = (i = t.gridProps.take) != null ? i : t.gridProps.pageSize) != null ? a : 0, o = (t.gridProps.skip || 0) + n, r = ge();
o < r && te({ skip: o, take: n }, e);
}, Ct = (e) => {
var r, i;
const n = (i = (r = t.gridProps.take) != null ? r : t.gridProps.pageSize) != null ? i : 0, o = (t.gridProps.skip || 0) - n;
o >= 0 && te({ skip: o, take: n }, e);
}, It = (e) => {
te({ skip: e.skip, take: e.take }, e.syntheticEvent, e.targetEvent);
}, Te = (e, n) => {
B(
t.gridProps.onSortChange,
{ sort: e },
{ sort: e, ...S && !t.gridProps.pageable ? { skip: 0 } : {} },
n
);
}, Et = (e, n) => {
B(
t.gridProps.onFilterChange,
{ filter: e },
{ filter: e, skip: 0 },
n
);
}, wt = (e) => {
if (!e || e.length === 0) {
Ge({});
return;
}
const n = Je(
t.gridProps.data,
e,
t.columnsRef,
t.gridProps.dataItemKey
);
Ge({ ...n });
}, Ge = (e) => {
B(
t.gridProps.onHighlightChange,
{ highlight: e },
{},
{}
);
}, xt = (e) => {
const n = t.gridProps.searchFields || t.columnsRef.map((i) => i.field) || [], o = e.nativeEvent.target.value, r = {
logic: "or",
filters: n.filter((i) => i !== void 0).map((i) => {
var a;
return typeof i == "string" ? { field: i, value: o, operator: "contains" } : {
value: o,
operator: (a = i.operator) != null ? a : "contains",
field: i.field,
ignoreCase: i.ignoreCase
};
})
};
B(
t.gridProps.onSearchChange,
{
search: r
},
{ skip: 0 },
e.syntheticEvent
);
}, fe = () => {
var e;
(e = O.current) == null || e.save();
}, Ae = async () => {
var e;
await ((e = y.current) == null ? void 0 : e.save());
}, Le = () => {
var e;
return ((e = y.current) == null ? void 0 : e.getBlob()) || null;
}, yt = async (e) => {
var n, o;
(n = t.gridProps) != null && n.onPdfExport && await ((o = t.gridProps) == null ? void 0 : o.onPdfExport(e));
}, St = (e) => {
var n, o;
return (n = t.gridProps) != null && n.onCsvExport ? (o = t.gridProps) == null ? void 0 : o.onCsvExport(e) : e;
}, me = (e, n) => {
const o = n.nativeEvent ? n : { nativeEvent: n.nativeEvent || n.originalEvent };
let r = {};
t.isVirtualScroll && e.length && !t.gridProps.pageable && (r = { take: void 0 }), e.length === 0 && t.gridProps.navigatable && (be.current = !0), B(
t.gridProps.onGroupChange,
{ group: e },
{ group: e, skip: 0, ...r },
o
);
}, Pe = (e) => {
if (t.gridProps.onColumnsStateChange) {
const n = {
target: x.current,
columnsState: e
};
g(t.gridProps.onColumnsStateChange, n);
}
}, ne = (e, n, o) => {
const { columnsRef: r, columnsState: i } = t, a = r[e], c = En(i), f = a.depth, l = (m) => {
do
m++;
while (m < r.length && r[m].depth > f);
return m;
}, u = r.splice(e, l(e) - e);
r.splice(e < n ? l(n - u.length) : n, 0, ...u), r.filter((m) => m.declarationIndex >= 0).forEach((m, k) => {
m.orderIndex = k;
const z = c.find((Y) => Y.id === m.id);
z && (z.orderIndex = k);
});
const s = r[e].locked && r[n].locked;
rn(t.columnsMapRef, r, s || ce.current), on(t.columnsMapRef, r, s || ce.current), ve.current && (ce.current = !1, ve.current = !1);
const b = ee();
if (N(), t.gridProps.onColumnReorder) {
const m = {
target: x.current,
columns: b,
columnId: a.id,
nativeEvent: o
};
g(t.gridProps.onColumnReorder, m);
}
Pe(i);
}, J = (e, n, o) => {
const r = typeof t.gridProps.rowReorderable == "object" ? t.gridProps.rowReorderable.enabled : t.gridProps.rowReorderable;
if (o === "forbidden" || !r || !X.current)
return;
const i = t.dataRef[n];
t.gridProps.onRowReorder && g(t.gridProps.onRowReorder, {
draggedDataItems: [X.current],
droppedDataItem: i == null ? void 0 : i.dataItem,
dropPosition: o,
nativeEvent: e.originalEvent,
dragEvent: e,
target: x.current
}), X.current = null;
}, kt = (e, n, o) => {
if (t.gridProps.group === void 0)
return;
const r = t.gridProps.group.slice();
r.splice(n, 0, ...r.splice(e, 1)), me(r, o);
}, Fe = (e, n, o) => {
const r = t.columnsRef[e].field;
if (!r)
return;
const i = (t.gridProps.group || []).slice();
i.splice(n, 0, { field: r }), me(i, o);
}, Dt = (e, n) => {
const o = H.current.getCurrentGroupsLength;
Fe(e, o, n);
}, he = () => {
var o, r, i, a, c, f;
if (t.gridProps.dataLayoutMode === "stacked") {
const l = ((o = M.current) == null ? void 0 : o.offsetWidth) || 0;
if ((r = I.current) != null && r.colGroupMain && l) {
const u = I.current.colGroupMain.children, s = u.length - 1;
if (s >= 0) {
let b = 0;
for (let k = 0; k < s; k++) {
const z = parseFloat((u[k].width || 0).toString()) || u[k].clientWidth;
b += z;
}
const m = Math.max(0, l - b);
u[s].width = m + "px", (a = (i = I.current.colGroupHeader) == null ? void 0 : i.children) != null && a[s] && (I.current.colGroupHeader.children[s].width = m + "px"), (f = (c = I.current.colGroupFooter) == null ? void 0 : c.children) != null && f[s] && (I.current.colGroupFooter.children[s].width = m + "px");
}
}
F.current && F.current.setWidth(l), L.current && L.current.setWidth(l), A.current && (A.current.style.width = l ? l + "px" : "");
return;
}
let e = 0;
if (!I.current.colGroupMain)
return;
const n = I.current.colGroupMain.children;
for (let l = 0; l < n.length; l++) {
const u = n[l].width;
if (!u) {
e = 0;
break;
}
e += parseFloat(u.toString());
}
e = Math.round(e), F.current && F.current.setWidth(e), L.current && L.current.setWidth(e), A.current && (A.current.style.width = e ? e + "px" : "");
}, ze = () => {
var e;
t.gridProps.widthRef && (t.gridProps.widthRef.current = ((e = M.current) == null ? void 0 : e.offsetWidth) || 0);
}, re = () => {
var e;
t.gridProps.containerHeightRef && (t.gridProps.containerHeightRef.current = ((e = G.current) == null ? void 0 : e.offsetHeight) || 0);
}, Kt = () => {
const e = t.gridProps.minRowHeightRef;
if (e && !e.current && !t.gridProps.rowHeight) {
const n = Cn(j.current);
n && (e.current = n, N());
}
}, oe = d.useCallback(
(e, n, o, r, i) => {
var l;
if (!Mt() && !o || !e)
return;
const a = {
type: e,
nativeEvent: n,
columns: t.columnsRef,
dataItemKey: t.gridProps.dataItemKey || "",
dataItem: r,
field: i,
...typeof t.gridProps.clipboard != "boolean" ? t.gridProps.clipboard : {},
...o
}, c = w(), f = pt({
event: a,
data: c,
selectedState: (l = t.gridProps.select) != null ? l : {},
previousCopiedItems: Ve.current
});
e !== ke.paste && (Ve.current = f.copiedItems), t.gridProps.onClipboard && E && g(t.gridProps.onClipboard, {
...a,
...f
});
},
[
t.gridProps.select,
t.gridProps.dataItemKey,
t.gridProps.data,
t.gridProps.clipboard,
t.gridProps.onClipboard
]
), Mt = () => {
var i, a, c;
if (!Q)
return !1;
const e = Jt(K()), n = e ? e.matches(".k-table-td") ? e : (i = K()) == null ? void 0 : i.body : (a = K()) == null ? void 0 : a.body, o = n.closest(".k-grid-container"), r = n && ((c = M.current) == null ? void 0 : c.contains(n));
return !!(n && r && o);
}, Oe = (e, n, o, r, i, a, c) => {
he(), ce.current = !0, ve.current = !0, t.gridProps.onColumnResize && E && g(t.gridProps.onColumnResize, {
columns: ee(),
nativeEvent: r,
targetColumnId: c,
index: e,
newWidth: n,
oldWidth: o,
end: i,
target: x.current
}), i && Pe(a);
}, Ht = () => {
var e;
return {
filter: t.gridProps.filter,
sort: t.gridProps.sort,
skip: t.gridProps.skip,
take: (e = t.gridProps.take) != null ? e : t.gridProps.pageSize,
group: t.gridProps.group
};
}, R = (e) => ({
nativeEvent: e && e.nativeEvent,
syntheticEvent: e,
target: x.current
}), Tt = (e) => ({
...e,
nativeEvent: void 0,
syntheticEvent: void 0,
target: void 0,
targetEvent: void 0,
focusElement: void 0
}), g = (e, n) => {
if (t.gridProps.isClient) {
e(n);
return;
}
e(Tt(n));
}, Gt = () => {
var e, n, o;
if (j.current && ((e = j.current) == null ? void 0 : e.getElementsByClassName("k-grid-edit-row").length) > 0) {
Ie.current = !1, (n = document.activeElement) != null && n.closest(".k-grid-edit-row") ? Ce.current = document.activeElement : Ce.current = void 0;
const r = Array.from((o = j.current) == null ? void 0 : o.getElementsByClassName("k-grid-edit-row"));
r.length > ye.current.length ? de.current = r.filter(
(i) => !ye.current.includes(i)
)[0] : r.length === 1 && (de.current = r[0], Ie.current = !0), ye.current = r;
}
}, w = () => t.dataRef.filter((e) => e.rowType === "data").map((e) => e.dataItem), K = () => {
var e;
if (Q)
return ((e = ie()) == null ? void 0 : e.ownerDocument) || document;
}, ie = () => M.current, Re = d.useCallback(
(e) => {
var r, i;
if (!v.current || !((r = v.current) != null && r.container) || t.gridProps.scrollable === "none")
return;
U.current && U.current.disconnect();
const { rowIndex: n } = e, o = ie();
if (S) {
const a = ((i = v.current.rowHeightService) == null ? void 0 : i.offset(n)) || 0;
v.current.container.scroll(0, a);
} else if (o) {
const a = n < 1 ? o.querySelector("tbody > tr:nth-child(1)") : o.querySelector(`tbody > tr:nth-child(${n + 1})`);
a && G.current && (G.current.scrollTop = a.offsetTop);
}
},
[t.gridProps.scrollable]
), We = (e) => JSON.stringify(e.map((n) => ({ id: n.id, field: n.field, title: n.title, children: n.children }))), At = () => We(Vt) === We(t.columnsRef), Lt = () => {
le.current = window.innerWidth, At() || N();
}, Ft = () => {
var a;
const { virtualTotal: e, virtualPageSize: n, gridProps: o } = t, r = v.current, i = t.gridProps.rowHeight || ((a = o.minRowHeightRef) == null ? void 0 : a.current) || 0;
r && (r.fixedScroll = o.fixedScroll || !1, r.PageChange = Rt, r.pageSize = n, r.scrollableVirtual = S, r.container = G.current, r.tableBody = j.current, r.scrollHeightContainer = Ne.current, r.table = A.current, (!r.rowHeightService || r.total !== e) && i && (r.total = e, r.rowHeightService = new Qt(e, i)));
}, Be = d.useCallback(
(e) => {
const n = { rowIndex: xe.current };
e.forEach((o) => {
o.boundingClientRect.height !== o.intersectionRect.height && Re(n);
});
},
[Re]
), N = () => {
t.gridProps.forceUpdate && t.gridProps.forceUpdate();
}, zt = (e) => e.left !== void 0 ? Se !== "rtl" ? { left: e.left, right: e.right } : { left: e.right, right: e.left } : {}, x = d.useRef(null), v = d.useRef(void 0), I = d.useRef(void 0), H = d.useRef(void 0), T = d.useRef(void 0), V = d.useRef(void 0), ae = d.useRef(void 0), U = d.useRef(null), j = d.useRef(null), Ot = d.useRef(null), G = d.useRef(null), A = d.useRef(null), Wt = d.useRef(null), M = d.useRef(null), Ne = d.useRef(null), L = d.useRef(null), F = d.useRef(null), ve = d.useRef(!1), be = d.useRef(!1), de = d.useRef(void 0), Ce = d.useRef(void 0), Ie = d.useRef(!1), ce = d.useRef(!0), Ee = d.useRef(0), we = d.useRef(void 0), xe = d.useRef(void 0), ye = d.useRef([]), Ve = d.useRef([]), le = d.useRef(0), X = d.useRef(null), Bt = d.useRef(null), Nt = d.useRef(null), Se = Xt(M), S = t.isVirtualScroll, Vt = d.useMemo(() => d.Children.toArray(t.gridProps.children), [t.gridProps.children]), Ue = t.gridProps.groupable === !0 || typeof t.gridProps.groupable == "object" && t.gridProps.groupable.enabled !== !1, P = se(t.gridProps.selectable), je = en(t.gridProps.editable), Ut = le.current && _ && le.current <= _.medium && t.gridProps.adaptive;
return d.useMemo(() => {
q.onConstructor({
navigatable: !!t.gridProps.navigatable,
contextStateRef: T,
navigationStateRef: V,
idPrefix: t.id
}), v.current = new xn();
}, []), d.useMemo(() => {
var e;
(e = v.current) == null || e.reset();
}, [
t.gridProps.scrollable,
t.gridProps.total,
t.gridProps.filter,
t.gridProps.group,
Ue,
t.gridProps.sort,
t.gridProps.rowHeight
]), d.useEffect(() => (t.gridProps.clipboard && (ae.current = new tn(oe), ae.current.addEventListeners(K())), () => {
ae.current && ae.current.removeEventListeners(K());
}), [t.gridProps.onClipboard, t.gridProps.clipboard, oe, K]), d.useEffect(() => (ze(), he(), qe(), q.onComponentDidMount({
scope: M.current || void 0,
contextStateRef: T,
navigationStateRef: V
}), () => {
clearTimeout(we.current);
}), []), d.useEffect(() => {
var e;
ze(), he(), qe(), S && (re(), Kt(), (e = v.current) == null || e.update()), Gt(), q.onComponentDidUpdate({
scope: M.current || void 0,
contextStateRef: T,
navigationStateRef: V,
focusFirst: be.current,
newEditableRow: de.current,
singleEditRow: Ie.current,
lastActiveElement: Ce.current,
navigatable: t.gridProps.navigatable
}), be.current = !1, de.current = void 0;
}), d.useEffect(() => {
if (Q) {
const e = {
rootMargin: "0px",
threshold: 0.9
};
U.current = window.IntersectionObserver && new window.IntersectionObserver(Be, e) || null;
}
}, [Be]), d.useEffect(() => {
var n;
let e;
return Q && window.ResizeObserver && (e = new window.ResizeObserver(() => {
Lt(), S && re();
}), e.observe((n = K()) == null ? void 0 : n.body)), () => {
e == null || e.disconnect();
};
}, []), d.useEffect(() => {
if (!Q || !window.ResizeObserver || !S || !G.current)
return;
const e = () => {
var i, a;
const o = ((i = t.gridProps.containerHeightRef) == null ? void 0 : i.current) || 0;
re();
const r = ((a = t.gridProps.containerHeightRef) == null ? void 0 : a.current) || 0;
(o === 0 && r > 0 || Math.abs(r - o) > 10) && N();
}, n = new window.ResizeObserver(e);
return n.observe(G.current), () => {
n.disconnect();
};
}, [S, re]), d.useImperativeHandle(
x,
() => ({
get element() {
return ie();
},
props: t.gridProps,
get columns() {
return ee();
},
scrollIntoView: (e) => {
var r;
if (!((r = v.current) != null && r.container) || t.gridProps.scrollable === "none")
return;
const { rowIndex: n } = e;
xe.current = n;
const o = ie();
if (U.current && o) {
U.current.disconnect();
const i = o.querySelector(`[absolute-row-index="${xe.current}"]`);
i ? U.current.observe(i) : Re(e);
}
},
fitColumns: (e) => {
I.current.dblClickHandler(null, e);
},
exportAsPdf: fe,
saveAsCsv: Ae,
getCsvBlob: Le,
getTotal: ge,
getLeafDataItems: w
})
), d.useImperativeHandle(t.gridRef, () => x.current), d.useMemo(() => {
I.current = new Pn(Oe);
}, [t.gridProps.onColumnResize, t.columnsRef]), d.useMemo(() => {
H.current = new hn(ne, kt, Fe);
}, [
t.gridProps.onColumnReorder,
t.gridProps.onGroupChange,
t.gridProps.group,
t.columnsRef,
t.gridProps.groupable
]), I.current.resizable = t.gridProps.resizable || !1, I.current.columns = t.columnsRef, I.current.columnsState = Yt(t.columnsState), H.current.reorderable = t.gridProps.reorderable || !1, H.current.groupable = Ue, H.current.columns = t.columnsRef, H.current.dir = Se, Ft(), /* @__PURE__ */ d.createElement(
Mn.Provider,
{
value: {
isClient: E,
rowReorder: J,
activeDragRowDataItemRef: X,
reorderRowDragTargetRef: Bt,
reorderRowDropTargetRef: Nt,
dir: Se,
getCellPositionStyle: zt,
dataItemKey: t.gridProps.dataItemKey,
columnsState: t.columnsState,
columnsRef: t.columnsRef,
hiddenColumnsRef: t.hiddenColumnsRef,
onColumnsStateChange: Pe,
groupable: t.gridProps.groupable,
group: t.gridProps.group,
reorderable: t.gridProps.reorderable,
defaultGroup: t.gridProps.defaultGroup,
groupChange: me,
selectionRelease: gt,
pagerPageChange: It,
onContextMenu: Ye,
rowClick: tt,
rowDblClick: nt,
cellClick: it,
headerCellClick: He,
itemChange: ct,
onDialogEditCancel: ot,
onDialogEditSubmit: rt,
columnReorder: ne,
onResize: Oe,
getTotal: ge,
sortable: t.gridProps.sortable,
pageable: t.gridProps.pageable,
pageSize: t.gridProps.pageSize,
sort: t.gridProps.sort,
skip: t.gridProps.skip,
take: t.gridProps.take,
defaultSort: t.gridProps.defaultSort,
sortChange: Te,
filterable: t.gridProps.filterable,
filter: t.gridProps.filter,
defaultFilter: t.gridProps.defaultFilter,
filterOperators: t.gridProps.filterOperators || Hn,
getLeafDataItems: w,
filterChange: Et,
applyHighlightDescriptor: wt,
applySelectionDescriptor: Pt,
highlight: t.gridProps.highlight,
select: t.gridProps.select,
searchChange: xt,
exportAsPdf: fe,
exportAsCsv: Ae,
getCsvBlob: Le,
onHeaderSelectionChange: ut,
columnGroupChange: Dt,
onKeyDown: Ze,
onFocus: pe,
scrollHandler: Qe,
selectionChange: lt,
mobileMode: Ut,
adaptiveColumnMenuRef: le.current,
adpativeTitle: t.gridProps.adaptiveTitle,
adaptive: t.gridProps.adaptive,
dispatchDetailExpand: at,
dispatchGroupExpand: dt,
columnResizeRef: I,
dragLogicRef: H,
navigationStateRef: V,
tableElementRef: A,
tableBodyElementRef: j,
headerElementRef: Ot,
containerElementRef: G,
headTableElementRef: Wt,
elementRef: M,
virtualScrollHeightContainerRef: Ne,
footerRef: L,
headerRef: F,
vsRef: v
}
},
/* @__PURE__ */ d.createElement(nn.Provider, { value: T.current }, t.children),
/* @__PURE__ */ d.createElement(
yn,
{
show: h.show && (p == null ? void 0 : p.length),
dataItem: h.dataItem,
field: h.field,
items: p,
offset: h.offset,
onClose: De,
onSelect: Xe
}
),
t.gridProps.pdf && /* @__PURE__ */ d.createElement(
Dn,
{
gridProps: t.gridProps,
innerGrid: t.innerGrid,
pdf: typeof t.gridProps.pdf == "object" ? t.gridProps.pdf : {},
onPdfExport: yt,
ref: (e) => O.current = e
}
),
t.gridProps.csv && /* @__PURE__ */ d.createElement(
Kn,
{
gridProps: t.gridProps,
csv: typeof t.gridProps.csv == "object" ? t.gridProps.csv : {},
onCsvExport: St,
columnsState: t.columnsState,
ref: (e) => y.current = e
}
)
);
};
export {
Jn as GridClientWrapper
};