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