Skip to content

Commit

Permalink
Fix rowClasses changes potentially wiping out --striped classes
Browse files Browse the repository at this point in the history
+ switch to `useUpdateEffect` for extra microperf, since `rowClasses` gets set on creation
  • Loading branch information
cee-chen committed Oct 20, 2023
1 parent d2d250a commit 15f525a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/components/datagrid/body/data_grid_row_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,13 @@ describe('row manager', () => {
expect(row0.classList.contains('world')).toBe(false);
expect(row1.classList.contains('test')).toBe(true);
});

it('correctly preserves EUI classes when adding/removing classes dynamically', () => {
rerender({ ...mockArgs, rowClasses: undefined });

expect(getRow(0).classList.value).toEqual(
'euiDataGridRow euiDataGridRow--striped'
);
});
});
});
14 changes: 10 additions & 4 deletions src/components/datagrid/body/data_grid_row_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* Side Public License, v 1.
*/

import { useRef, useCallback, useEffect, RefObject } from 'react';
import { useRef, useCallback, RefObject } from 'react';
import { useUpdateEffect } from '../../../services';

import { EuiDataGridRowManager, EuiDataGridStyle } from '../data_grid_types';

export const useRowManager = ({
Expand Down Expand Up @@ -76,13 +78,17 @@ export const useRowManager = ({
);

// Update row classes dynamically whenever a new prop is passed in
useEffect(() => {
useUpdateEffect(() => {
if (rowClasses) {
rowIdToElements.current.forEach((rowElement, rowIndex) => {
const euiClasses = Array.from(rowElement.classList)
.filter((className) => className.startsWith('euiDataGridRow'))
.join(' ');

if (rowClasses[rowIndex]) {
rowElement.classList.value = `euiDataGridRow ${rowClasses[rowIndex]}`;
rowElement.classList.value = `${euiClasses} ${rowClasses[rowIndex]}`;
} else {
rowElement.classList.value = 'euiDataGridRow'; // Clear any added classes
rowElement.classList.value = euiClasses; // Clear any added classes
}
});
}
Expand Down

0 comments on commit 15f525a

Please sign in to comment.