{"version":3,"file":"DataGrid.mjs","names":[],"sources":["../../../src/data-grid/DataGrid.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport {\n  type RowData,\n  type Table as DataGridType,\n} from '@tanstack/react-table';\nimport classNames from 'classnames';\nimport {\n  useEffect,\n  useImperativeHandle,\n  useMemo,\n  useRef,\n  useState,\n} from 'react';\n\nimport { type CommonProps, type HtmlAttributes } from '../_common/types';\nimport { useResizeObserver } from '../hooks';\nimport { type Skeleton } from '../skeleton';\nimport {\n  ActionButton,\n  ActionCell,\n  Body,\n  BodyCell,\n  BodyRow,\n  ColumnControls,\n  DropDownCell,\n  Header,\n  HeaderCell,\n  HeaderTitle,\n  InlineEditCell,\n  LoadingPlaceholder,\n  Navigation,\n  NoDataIcon,\n  NoDataPlaceholder,\n  NoDataPlaceholderContentWrapper,\n  Pagination,\n  PaginationArrowButton,\n  PaginationNumericButton,\n  PaginationNumericButtons,\n  ResizingBar,\n  RowsPerPage,\n  Scrollable,\n  TableResults,\n} from './Components';\nimport {\n  type DataGridComponentsInterface,\n  DataGridContext,\n  type DataGridContextType,\n} from './data-grid-context';\nimport { useTableNav } from './data-grid-nav';\nimport { updateColumnWidths } from './helpers';\nimport {\n  type DataGridStyleRule,\n  type DataGridStyleRuleValueConverters,\n} from './style-rules/types';\n\nenum BorderStyleEnum {\n  AllSides = 'all-sides',\n  Horizontal = 'horizontal',\n  Vertical = 'vertical',\n  None = 'none',\n}\n\nexport type BorderStyle = `${BorderStyleEnum}`;\n\nenum HeaderStyleEnum {\n  Filled = 'filled',\n  Clean = 'clean',\n}\n\nexport type HeaderStyle = `${HeaderStyleEnum}`;\n\nexport type DataGridProps<T extends RowData> = {\n  styling?: Partial<{\n    /** Alternate the background color of rows */\n    hasZebraStriping: boolean;\n    /** Type of border that will be used */\n    borderStyle: BorderStyle;\n    /** If header should have styles or not (Aura example) */\n    headerStyle: HeaderStyle;\n    /** If background hover effects should be enabled on rows and header cells */\n    hasHoverEffects: boolean;\n    /** If table should have less padding */\n    isCompact: boolean;\n  }>;\n  /** Resizing the width of columns */\n  isResizable?: boolean;\n  /** Enables table's loading state */\n  isLoading?: boolean;\n  /**\n   * Enables keyboard navigation.\n   * To control keyboard navigation programmatically\n   * for edge cases, pass a `ref` to `tableNavRef`\n   */\n  isKeyboardNavigable?: boolean;\n  /**\n   * Enables automatic resizing of columns.\n   * If `isResizable` is `true`, the columns will only be automatically resized on initial render of the table.\n   * If `isResizable` is `false`, the columns will be resized automatically to always fit the table width (assuming that column maxWidths allow for it).\n   * Note: Action columns are a fixed size of 40px, and Pinned columns are not resized automatically.\n   * @defaultValue true\n   */\n  isAutoResizingColumns?: boolean;\n  /**\n   * Reference to the tableNav instance\n   * for programmatic control\n   */\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  tableNavRef?: React.RefObject<any>;\n  /** Table instance created from `useTable` */\n  tableInstance: DataGridType<T>;\n  /** Table sub-component overrides */\n  components?: DataGridComponentsInterface<T>;\n  /** @deprecated Use htmlAttributes, className and style on the root element instead */\n  rootProps?: HtmlAttributes<'div'>;\n  isSkeletonLoading?: boolean;\n  skeletonProps?: React.ComponentProps<typeof Skeleton>;\n  /** The target element for portaled elements to be rendered in, e.g. the modal root if the data grid is inside a modal.\n   * This is used in for example the DropdownCell, RowsPerPage, and ColumnControls.\n   */\n  portalTarget?: HTMLElement | null;\n  /** Rule-based conditional styling for cells and rows. */\n  styleRules?: DataGridStyleRule[];\n  /** Per-column converters that teach the style-rule engine how to interpret complex cell values. */\n  styleRuleValueConverters?: DataGridStyleRuleValueConverters;\n  ref?: React.Ref<HTMLDivElement>;\n};\n\nconst DEFAULT_STYLING: Required<DataGridProps<unknown>['styling']> = {\n  borderStyle: BorderStyleEnum.Horizontal,\n  hasHoverEffects: true,\n  hasZebraStriping: false,\n  headerStyle: HeaderStyleEnum.Filled,\n  isCompact: false,\n};\n\nconst DataGridComponent = function DataGrid<T extends RowData = unknown>({\n  isResizable = true,\n  isLoading = false,\n  isKeyboardNavigable = true,\n  isAutoResizingColumns = true,\n  tableInstance,\n  components,\n  rootProps,\n  styling = {},\n  tableNavRef,\n  isSkeletonLoading = false,\n  skeletonProps,\n  ref,\n  portalTarget = document.body,\n  styleRules,\n  styleRuleValueConverters,\n  htmlAttributes,\n  className,\n  style,\n  ...restProps\n}: CommonProps<'div', DataGridProps<T>>) {\n  const { className: rootClassName = '', style: rootStyle = {} } =\n    rootProps || {};\n\n  const [isScrollable, setIsScrollable] = useState(false);\n  const [isMediumNavigation, setIsMediumNavigation] = useState(false);\n  const [isSmallNavigation, setIsSmallNavigation] = useState(false);\n  const tableRef = useRef<HTMLDivElement>(null);\n  const scrollableContainerRef = useRef<HTMLDivElement>(null);\n\n  tableInstance.setOptions((prevOptions) => {\n    return {\n      ...prevOptions,\n      enableSorting: isSkeletonLoading ? false : prevOptions.enableSorting,\n    };\n  });\n\n  // Triggers when the whole container is resized\n  useResizeObserver({\n    onResize: (entry) => {\n      if (entry.width === undefined) {\n        return;\n      }\n      isAutoResizingColumns &&\n        !isResizable &&\n        updateColumnWidths(tableInstance, entry.width); // don't resize columns when the table is resizable due to problems with the resize handler.\n      setIsScrollable(tableInstance.getTotalSize() > entry.width);\n      setIsMediumNavigation(entry.width <= 680);\n      setIsSmallNavigation(entry.width <= 480);\n    },\n    ref: scrollableContainerRef as React.RefObject<HTMLElement>,\n  });\n\n  // Triggers when the individual columns are resized\n  useResizeObserver({\n    onResize: () => {\n      if (scrollableContainerRef.current === null) {\n        return;\n      }\n      setIsScrollable(\n        tableInstance.getTotalSize() >\n          scrollableContainerRef.current.clientWidth,\n      );\n    },\n    ref: tableRef as React.RefObject<HTMLElement>,\n  });\n\n  const Styling = useMemo(\n    () => ({\n      ...DEFAULT_STYLING,\n      ...styling,\n    }),\n    [styling],\n  );\n\n  const Components = useMemo(\n    () =>\n      ({\n        ...DataGridComponents,\n        ...components,\n      }) as DataGridContextType<T>['components'],\n    [components],\n  );\n  const ScrollableContainer = Components?.Scrollable\n    ? Components.Scrollable\n    : Scrollable;\n\n  const { listeners, tableNav } = useTableNav({\n    isDebug: false,\n  });\n\n  /** Surface the tableNav instance for programmatic control if needed */\n  useImperativeHandle(tableNavRef, () => tableNav, [tableNav]);\n\n  useEffect(() => {\n    if (isAutoResizingColumns && isResizable) {\n      updateColumnWidths(\n        tableInstance,\n        scrollableContainerRef.current?.clientWidth\n          ? scrollableContainerRef.current.clientWidth - 1 // -1 to avoid horizontal scrollbar\n          : 0,\n      );\n    }\n  }, [isAutoResizingColumns, tableInstance, isResizable]);\n\n  return (\n    <DataGridContext.Provider\n      value={\n        {\n          components: Components,\n          dataGridNav: tableNav,\n          hasResizeColumns: isAutoResizingColumns,\n          isKeyboardNavigationEnabled: isKeyboardNavigable,\n          isLoading: isLoading,\n          isMediumNavigation,\n          isResizable: isResizable,\n          isSkeletonLoading,\n          isSmallNavigation,\n          portalTarget: portalTarget,\n          skeletonProps,\n          styleRuleValueConverters,\n          styleRules,\n          tableProps: tableInstance,\n        } as DataGridContextType<T>\n      }\n    >\n      <div\n        // Root props needs to be first in order to override className\n        {...rootProps}\n        className={classNames('ndl-data-grid-root', rootClassName, className, {\n          'ndl-data-grid-border-horizontal':\n            Styling.borderStyle === BorderStyleEnum.Horizontal ||\n            Styling.borderStyle === BorderStyleEnum.AllSides,\n          'ndl-data-grid-border-vertical':\n            Styling.borderStyle === BorderStyleEnum.Vertical ||\n            Styling.borderStyle === BorderStyleEnum.AllSides,\n          'ndl-data-grid-compact': Styling.isCompact,\n          'ndl-data-grid-focusable-cells': isKeyboardNavigable,\n          'ndl-data-grid-header-filled':\n            Styling.headerStyle === HeaderStyleEnum.Filled,\n          'ndl-data-grid-hover-effects': Styling.hasHoverEffects,\n          'ndl-data-grid-resizable': isResizable,\n          'ndl-data-grid-zebra-striping': Styling.hasZebraStriping,\n        })}\n        ref={ref}\n        style={{ ...rootStyle, ...style }}\n        {...restProps}\n        {...htmlAttributes}\n        {...(isKeyboardNavigable ? listeners : {})}\n      >\n        {/* Make DataGrid horizontally scrollable and avoid horizontal scrollbar have an extra vertical scrollbar (esp. Safari) */}\n        {/* This behavior can be overridden by consumers through replacing the scrolling container */}\n        <ScrollableContainer\n          ref={scrollableContainerRef}\n          htmlAttributes={{\n            onKeyDown: (event) => {\n              if (isScrollable && !isKeyboardNavigable) {\n                if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {\n                  event.stopPropagation();\n                }\n              }\n            },\n            // Ensure that the user can focus and scroll inside the table with arrow keys even if there is no interactive element inside\n            tabIndex: isScrollable && !isKeyboardNavigable ? 0 : undefined,\n          }}\n        >\n          {/* Table content Area */}\n          <div\n            className=\"ndl-div-table\"\n            role={isKeyboardNavigable ? 'grid' : 'table'}\n            aria-busy={isLoading ? 'true' : 'false'}\n            ref={tableRef}\n          >\n            {/* DataGrid Header Area */}\n            {Components?.Header && <Components.Header />}\n            {Components?.Body && <Components.Body />}\n          </div>\n        </ScrollableContainer>\n        {/* Bottom Navigation Area */}\n        {Components?.Navigation && <Components.Navigation />}\n      </div>\n    </DataGridContext.Provider>\n  );\n};\n\nconst DataGridComponents = {\n  ActionButton,\n  ActionCell,\n  Body,\n  BodyCell,\n  BodyRow,\n  ColumnControls,\n  DropDownCell,\n  Header,\n  HeaderCell,\n  HeaderTitle,\n  InlineEditCell,\n  LoadingPlaceholder,\n  Navigation,\n  NoDataIcon,\n  NoDataPlaceholder,\n  NoDataPlaceholderContentWrapper,\n  Pagination,\n  PaginationArrowButton,\n  PaginationNumericButton,\n  PaginationNumericButtons,\n  ResizingBar,\n  RowsPerPage,\n  Scrollable,\n  TableResults,\n};\n\nconst DataGrid = Object.assign(DataGridComponent, DataGridComponents);\n\nexport { DataGrid };\n"],"mappings":";;;;;;;;;;;;CA2JE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;;;;;;;;;;;;;;;;;AA1BF,MAAM,kBAA+D;CACnE,aAAA;CACA,iBAAiB;CACjB,kBAAkB;CAClB,aAAA;CACA,WAAW;AACb;AAEA,MAAM,oBAAoB,SAAS,SAAsC,MAoBhC;CApBgC,IAAA,EACvE,cAAc,MACd,YAAY,OACZ,sBAAsB,MACtB,wBAAwB,MACxB,eACA,YACA,WACA,UAAU,CAAC,GACX,aACA,oBAAoB,OACpB,eACA,KACA,eAAe,SAAS,MACxB,YACA,0BACA,gBACA,WACA,UAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,EAAE,WAAW,gBAAgB,IAAI,OAAO,YAAY,CAAC,MACzD,aAAa,CAAC;CAEhB,MAAM,CAAC,cAAc,mBAAmB,SAAS,KAAK;CACtD,MAAM,CAAC,oBAAoB,yBAAyB,SAAS,KAAK;CAClE,MAAM,CAAC,mBAAmB,wBAAwB,SAAS,KAAK;CAChE,MAAM,WAAW,OAAuB,IAAI;CAC5C,MAAM,yBAAyB,OAAuB,IAAI;CAE1D,cAAc,YAAY,gBAAgB;EACxC,OAAA,eAAA,eAAA,CAAA,GACK,WAAA,GAAA,CAAA,GAAA,EACH,eAAe,oBAAoB,QAAQ,YAAY,cAAA,CACzD;CACF,CAAC;CAGD,kBAAkB;EAChB,WAAW,UAAU;GACnB,IAAI,MAAM,UAAU,KAAA,GAClB;GAEF,yBACE,CAAC,eACD,mBAAmB,eAAe,MAAM,KAAK;GAC/C,gBAAgB,cAAc,aAAa,IAAI,MAAM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