UNPKG

@progress/kendo-react-grid

Version:

React Data Grid (Table) provides 100+ ready-to-use data grid features. KendoReact Grid package

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