diff --git a/CHANGELOG.md b/CHANGELOG.md index 8859f335b10..8d1a040f154 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Fixed missing i18n token `EuiFilePicker`'s default prompt, and improved i18n string for `euiFilePicker.filesSelected` ([#5063](https://github.com/elastic/eui/pull/5063)) - Fixed `EuiDataGrid` sort button text pluralization ([#5043](https://github.com/elastic/eui/pull/5043)) - Fixed styles of `EuiButtonIcon` when passing `disabled` prop ([#5060](https://github.com/elastic/eui/pull/5060)) +- Fixed `EuiDataGrid` not clearing cell styles when column position changes ([#5068](https://github.com/elastic/eui/issues/5068)) **Theme: Amsterdam** diff --git a/src/components/datagrid/body/data_grid_cell.tsx b/src/components/datagrid/body/data_grid_cell.tsx index 8c787aea9fe..561c0aedd06 100644 --- a/src/components/datagrid/body/data_grid_cell.tsx +++ b/src/components/datagrid/body/data_grid_cell.tsx @@ -251,6 +251,12 @@ export class EuiDataGridCell extends Component< return false; } + componentDidUpdate(prevProps: EuiDataGridCellProps) { + if (this.props.columnId !== prevProps.columnId) { + this.setCellProps({}); + } + } + setCellProps = (cellProps: HTMLAttributes) => { this.setState({ cellProps }); }; diff --git a/src/components/datagrid/data_grid.test.tsx b/src/components/datagrid/data_grid.test.tsx index e5e4391ef0f..6d79eaac909 100644 --- a/src/components/datagrid/data_grid.test.tsx +++ b/src/components/datagrid/data_grid.test.tsx @@ -1467,6 +1467,48 @@ describe('EuiDataGrid', () => { ['1-ColumnB', '1-ColumnA'], ]); }); + + it('resets cell props on column reorder', () => { + const columnVisibility = { + visibleColumns: ['ColumnA', 'ColumnB'], + setVisibleColumns: (visibleColumns: string[]) => { + columnVisibility.visibleColumns = visibleColumns; + component.setProps({ columnVisibility }); + }, + }; + + const component = mount( + { + useEffect(() => { + if (columnId === 'ColumnB') { + setCellProps({ style: { color: 'blue' } }); + } + }, [columnId, rowIndex, setCellProps]); + + return `${rowIndex}-${columnId}`; + }} + /> + ); + + const getCellColorAt = (index: number) => + component + .find('[data-test-subj="dataGridRowCell"]') + .at(index) + .prop('style')?.color; + + expect(getCellColorAt(0)).toEqual(undefined); + expect(getCellColorAt(1)).toEqual('blue'); + + moveColumnToIndex(component, 'B', 0); + + expect(getCellColorAt(0)).toEqual('blue'); + expect(getCellColorAt(1)).toEqual(undefined); + }); }); describe('column sorting', () => { @@ -2171,7 +2213,7 @@ describe('EuiDataGrid', () => { }); }); - describe('rowHeighsOptions', () => { + describe('rowHeightsOptions', () => { it('all row heights options applied correctly', async () => { const component = mount(