Table: Add initial row index (#82200)

* Add initial row index prop in table
This commit is contained in:
Galen Kistler
2024-02-09 07:49:48 -06:00
committed by GitHub
parent 984d2da9ae
commit de4acb27ce
4 changed files with 28 additions and 3 deletions

View File

@@ -44,6 +44,7 @@ interface RowsListProps {
onCellFilterAdded?: TableFilterActionCallback; onCellFilterAdded?: TableFilterActionCallback;
timeRange?: TimeRange; timeRange?: TimeRange;
footerPaginationEnabled: boolean; footerPaginationEnabled: boolean;
initialRowIndex?: number;
} }
export const RowsList = (props: RowsListProps) => { export const RowsList = (props: RowsListProps) => {
@@ -66,9 +67,10 @@ export const RowsList = (props: RowsListProps) => {
listHeight, listHeight,
listRef, listRef,
enableSharedCrosshair = false, enableSharedCrosshair = false,
initialRowIndex = undefined,
} = props; } = props;
const [rowHighlightIndex, setRowHighlightIndex] = useState<number | undefined>(undefined); const [rowHighlightIndex, setRowHighlightIndex] = useState<number | undefined>(initialRowIndex);
const theme = useTheme2(); const theme = useTheme2();
const panelContext = usePanelContext(); const panelContext = usePanelContext();
@@ -203,6 +205,7 @@ export const RowsList = (props: RowsListProps) => {
({ index, style, rowHighlightIndex }: { index: number; style: CSSProperties; rowHighlightIndex?: number }) => { ({ index, style, rowHighlightIndex }: { index: number; style: CSSProperties; rowHighlightIndex?: number }) => {
const indexForPagination = rowIndexForPagination(index); const indexForPagination = rowIndexForPagination(index);
const row = rows[indexForPagination]; const row = rows[indexForPagination];
let additionalProps: React.HTMLAttributes<HTMLDivElement> = {};
prepareRow(row); prepareRow(row);
@@ -210,11 +213,13 @@ export const RowsList = (props: RowsListProps) => {
if (rowHighlightIndex !== undefined && row.index === rowHighlightIndex) { if (rowHighlightIndex !== undefined && row.index === rowHighlightIndex) {
style = { ...style, backgroundColor: theme.components.table.rowHoverBackground }; style = { ...style, backgroundColor: theme.components.table.rowHoverBackground };
additionalProps = {
'aria-selected': 'true',
};
} }
return ( return (
<div <div
{...row.getRowProps({ style })} {...row.getRowProps({ style, ...additionalProps })}
className={cx(tableStyles.row, expandedRowStyle)} className={cx(tableStyles.row, expandedRowStyle)}
onMouseEnter={() => onRowHover(index, data)} onMouseEnter={() => onRowHover(index, data)}
onMouseLeave={onRowLeave} onMouseLeave={onRowLeave}

View File

@@ -89,6 +89,7 @@ function getTestContext(propOverrides: Partial<Props> = {}) {
onSortByChange, onSortByChange,
onCellFilterAdded, onCellFilterAdded,
onColumnResize, onColumnResize,
initialRowIndex: undefined,
}; };
Object.assign(props, propOverrides); Object.assign(props, propOverrides);
@@ -657,4 +658,19 @@ describe('Table', () => {
expect(subTable.style.height).toBe('108px'); expect(subTable.style.height).toBe('108px');
}); });
}); });
describe('when mounted with scrolled to specific row', () => {
it('the row should be visible', async () => {
getTestContext({
initialRowIndex: 2,
});
expect(getTable()).toBeInTheDocument();
const rows = within(getTable()).getAllByRole('row');
expect(rows).toHaveLength(5);
let selected = within(getTable()).getByRole('row', { selected: true });
expect(selected).toBeVisible();
});
});
}); });

View File

@@ -48,6 +48,7 @@ export const Table = memo((props: Props) => {
cellHeight = TableCellHeight.Sm, cellHeight = TableCellHeight.Sm,
timeRange, timeRange,
enableSharedCrosshair = false, enableSharedCrosshair = false,
initialRowIndex = undefined,
} = props; } = props;
const listRef = useRef<VariableSizeList>(null); const listRef = useRef<VariableSizeList>(null);
@@ -307,6 +308,7 @@ export const Table = memo((props: Props) => {
tableStyles={tableStyles} tableStyles={tableStyles}
footerPaginationEnabled={Boolean(enablePagination)} footerPaginationEnabled={Boolean(enablePagination)}
enableSharedCrosshair={enableSharedCrosshair} enableSharedCrosshair={enableSharedCrosshair}
initialRowIndex={initialRowIndex}
/> />
</div> </div>
) : ( ) : (

View File

@@ -97,6 +97,8 @@ export interface Props {
/** @alpha Used by SparklineCell when provided */ /** @alpha Used by SparklineCell when provided */
timeRange?: TimeRange; timeRange?: TimeRange;
enableSharedCrosshair?: boolean; enableSharedCrosshair?: boolean;
// The index of the field value that the table will initialize scrolled to
initialRowIndex?: number;
} }
/** /**