UNPKG

@grafana/ui

Version:
1 lines 4.67 kB
{"version":3,"file":"hooks.mjs","sources":["../../../../src/components/Table/hooks.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport * as React from 'react';\nimport { VariableSizeList } from 'react-window';\n\nimport { DataFrame } from '@grafana/data';\n\nimport { GrafanaTableState } from './types';\n\n/**\n To have the custom vertical scrollbar always visible (https://github.com/grafana/grafana/issues/52136),\n we need to bring the element from the VariableSizeList scope to the outer Table container scope,\n because the VariableSizeList scope has overflow. By moving scrollbar to container scope we will have\n it always visible since the entire width is in view.\n Select the scrollbar element from the VariableSizeList scope\n */\nexport function useFixScrollbarContainer(\n variableSizeListScrollbarRef: React.RefObject<HTMLDivElement>,\n tableDivRef: React.RefObject<HTMLDivElement>\n) {\n useEffect(() => {\n if (variableSizeListScrollbarRef.current && tableDivRef.current) {\n const listVerticalScrollbarHTML = variableSizeListScrollbarRef.current.querySelector('.track-vertical');\n\n // Select Table custom scrollbars\n const tableScrollbarView = tableDivRef.current.firstChild;\n\n //If they exist, move the scrollbar element to the Table container scope\n if (tableScrollbarView && listVerticalScrollbarHTML) {\n listVerticalScrollbarHTML.remove();\n if (tableScrollbarView instanceof HTMLElement) {\n tableScrollbarView.querySelector(':scope > .track-vertical')?.remove();\n tableScrollbarView.append(listVerticalScrollbarHTML);\n }\n }\n }\n });\n}\n\n/**\n react-table caches the height of cells, so we need to reset them when expanding/collapsing rows.\n We use `lastExpandedOrCollapsedIndex` since collapsed rows disappear from `expandedIndexes` but still keep their expanded\n height.\n */\nexport function useResetVariableListSizeCache(\n extendedState: GrafanaTableState,\n listRef: React.RefObject<VariableSizeList>,\n data: DataFrame,\n hasUniqueId: boolean\n) {\n // Make sure we trigger the reset when keys change in any way\n const expandedRowsRepr = JSON.stringify(Object.keys(extendedState.expanded));\n\n useEffect(() => {\n // By default, reset all rows\n let resetIndex = 0;\n\n // If we have unique field, extendedState.expanded keys are not row indexes but IDs so instead of trying to search\n // for correct index we just reset the whole table.\n if (!hasUniqueId) {\n // If we don't have we reset from the last changed index.\n if (Number.isFinite(extendedState.lastExpandedOrCollapsedIndex)) {\n resetIndex = extendedState.lastExpandedOrCollapsedIndex!;\n }\n\n // Account for paging.\n resetIndex =\n extendedState.pageIndex === 0\n ? resetIndex - 1\n : resetIndex - extendedState.pageIndex - extendedState.pageIndex * extendedState.pageSize;\n }\n\n listRef.current?.resetAfterIndex(Math.max(resetIndex, 0));\n return;\n }, [\n extendedState.lastExpandedOrCollapsedIndex,\n extendedState.pageSize,\n extendedState.pageIndex,\n listRef,\n data,\n expandedRowsRepr,\n hasUniqueId,\n ]);\n}\n"],"names":[],"mappings":";;;AAeO,SAAS,wBAAA,CACd,8BACA,WAAA,EACA;AACA,EAAA,SAAA,CAAU,MAAM;AAnBlB,IAAA,IAAA,EAAA;AAoBI,IAAA,IAAI,4BAAA,CAA6B,OAAA,IAAW,WAAA,CAAY,OAAA,EAAS;AAC/D,MAAA,MAAM,yBAAA,GAA4B,4BAAA,CAA6B,OAAA,CAAQ,aAAA,CAAc,iBAAiB,CAAA;AAGtG,MAAA,MAAM,kBAAA,GAAqB,YAAY,OAAA,CAAQ,UAAA;AAG/C,MAAA,IAAI,sBAAsB,yBAAA,EAA2B;AACnD,QAAA,yBAAA,CAA0B,MAAA,EAAO;AACjC,QAAA,IAAI,8BAA8B,WAAA,EAAa;AAC7C,UAAA,CAAA,EAAA,GAAA,kBAAA,CAAmB,aAAA,CAAc,0BAA0B,CAAA,KAA3D,IAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAA8D,MAAA,EAAA;AAC9D,UAAA,kBAAA,CAAmB,OAAO,yBAAyB,CAAA;AAAA,QACrD;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC,CAAA;AACH;AAOO,SAAS,6BAAA,CACd,aAAA,EACA,OAAA,EACA,IAAA,EACA,WAAA,EACA;AAEA,EAAA,MAAM,mBAAmB,IAAA,CAAK,SAAA,CAAU,OAAO,IAAA,CAAK,aAAA,CAAc,QAAQ,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