Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 Expose ref to Datagrid api #3685

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 77 additions & 60 deletions packages/eds-data-grid-react/src/EdsDataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import {
import { useVirtualizer } from '@tanstack/react-virtual'
import {
CSSProperties,
forwardRef,
HTMLAttributes,
useCallback,
useEffect,
useMemo,
useRef,
useState,
ForwardedRef,
} from 'react'
import styled from 'styled-components'
import { TableProvider } from './EdsDataGridContext'
Expand All @@ -39,66 +41,70 @@ import {
addPxSuffixIfInputHasNoPrefix,
logDevelopmentWarningOfPropUse,
} from './utils'
import { mergeRefs } from '@equinor/eds-utils'

export function EdsDataGrid<T>({
rows,
columns,
columnResizeMode,
pageSize,
rowSelection,
enableRowSelection,
enableMultiRowSelection,
enableSubRowSelection,
selectedRows,
rowSelectionState,
enableColumnFiltering,
debug,
enablePagination,
enableSorting,
stickyHeader,
stickyFooter,
onSelectRow,
onRowSelectionChange,
caption,
enableVirtual,
virtualHeight,
columnVisibility,
columnVisibilityChange,
emptyMessage,
columnOrder,
cellClass,
cellStyle,
rowClass,
rowStyle,
headerClass,
headerStyle,
footerClass,
footerStyle,
externalPaginator,
onSortingChange,
manualSorting,
sortingState,
columnPinState,
scrollbarHorizontal,
width,
minWidth,
height,
getRowId,
rowVirtualizerInstanceRef,
tableInstanceRef,
columnSizing,
onColumnResize,
expansionState,
setExpansionState,
getSubRows,
defaultColumn,
onRowContextMenu,
onRowClick,
onCellClick,
enableFooter,
enableSortingRemoval,
...rest
}: EdsDataGridProps<T> & HTMLAttributes<HTMLDivElement>) {
function EdsDataGridInner<T>(
{
rows,
columns,
columnResizeMode,
pageSize,
rowSelection,
enableRowSelection,
enableMultiRowSelection,
enableSubRowSelection,
selectedRows,
rowSelectionState,
enableColumnFiltering,
debug,
enablePagination,
enableSorting,
stickyHeader,
stickyFooter,
onSelectRow,
onRowSelectionChange,
caption,
enableVirtual,
virtualHeight,
columnVisibility,
columnVisibilityChange,
emptyMessage,
columnOrder,
cellClass,
cellStyle,
rowClass,
rowStyle,
headerClass,
headerStyle,
footerClass,
footerStyle,
externalPaginator,
onSortingChange,
manualSorting,
sortingState,
columnPinState,
scrollbarHorizontal,
width,
minWidth,
height,
getRowId,
rowVirtualizerInstanceRef,
tableInstanceRef,
columnSizing,
onColumnResize,
expansionState,
setExpansionState,
getSubRows,
defaultColumn,
onRowContextMenu,
onRowClick,
onCellClick,
enableFooter,
enableSortingRemoval,
...rest
}: EdsDataGridProps<T> & HTMLAttributes<HTMLDivElement>,
ref: ForwardedRef<HTMLDivElement>,
) {
logDevelopmentWarningOfPropUse({
virtualHeight: {
value: virtualHeight,
Expand Down Expand Up @@ -354,6 +360,10 @@ export function EdsDataGrid<T>({
}

const parentRef = useRef<HTMLDivElement>(null)
const combinedRef = useMemo(
() => mergeRefs<HTMLDivElement>(parentRef, ref),
[parentRef, ref],
)

/**
* Virtualization setup
Expand Down Expand Up @@ -404,7 +414,7 @@ export function EdsDataGrid<T>({
{...rest}
className={`table-wrapper ${rest.className ?? ''}`}
style={{ ...rest.style, ...tableWrapperStyle }}
ref={parentRef}
ref={combinedRef}
$height={height}
$width={width}
$scrollbarHorizontal={scrollbarHorizontal}
Expand Down Expand Up @@ -567,3 +577,10 @@ const TableWrapper = styled.div<{
contain: ${({ $height, $width }) =>
Boolean($height) && Boolean($width) ? 'strict' : 'unset'};
`

export const EdsDataGrid = forwardRef(EdsDataGridInner) as <T>(
props: EdsDataGridProps<T> &
HTMLAttributes<HTMLDivElement> & {
ref?: ForwardedRef<HTMLDivElement>
},
) => ReturnType<typeof EdsDataGridInner>
Loading