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