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

[Emotion] Convert final EuiDataGrid cell styles (Part 5) #8013

Merged
merged 28 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
175b2f8
Convert remaining `.euiDataGridRowCell` styles
cee-chen Sep 10, 2024
046bfef
Convert remaining `.euiDataGridRowCell__content` styles
cee-chen Sep 10, 2024
ba408b6
Update downstream snapshots
cee-chen Sep 9, 2024
91a349c
Convert remaining `.euiDataGridHeaderCell` styles
cee-chen Sep 10, 2024
89ad354
[refactor] Remove unnecessary `CellContent` FC
cee-chen Sep 10, 2024
45aeb31
Convert `.euiDataGridHeaderCell__content` styles
cee-chen Sep 10, 2024
95548bb
[refactor] Clean up column actions button/icon DOM+CSS
cee-chen Sep 10, 2024
ccde288
Convert `.euiDataGridHeaderCell__button` styles
cee-chen Sep 10, 2024
c9327ca
Add VRT/E2E tests for header styles
cee-chen Sep 10, 2024
8320712
[refactor] clean up header control column DOM
cee-chen Sep 10, 2024
25d92ff
Update downstream tests/snapshots
cee-chen Sep 10, 2024
480f098
[Enhancement] Replace unused header cell actions CSS
cee-chen Sep 1, 2024
22f27c8
Convert column resizer styles
cee-chen Sep 1, 2024
18ce982
Convert remaining `.euiDataGridFooterCell` styles
cee-chen Sep 9, 2024
2a80790
changelog
cee-chen Sep 10, 2024
64d3ce5
Delete all data grid Sass files
cee-chen Sep 1, 2024
9e68f6f
Final component Sass deletions 🥲
cee-chen Sep 10, 2024
d70c124
[tech debt] Address various Emotion component post-conversion TODOs
cee-chen Sep 10, 2024
5d47175
Final EuiDataGrid VRT updates
cee-chen Sep 10, 2024
d7d70c2
Fix Storybook control docgen for `gridStyle`/`rowHeightsOptions`/`too…
cee-chen Sep 10, 2024
456f5ef
Fix Cypress axe test
cee-chen Sep 10, 2024
64f1748
[PR feedback] Fix column actions button sizing
cee-chen Sep 11, 2024
fb0178c
[PR feedback] Remove `export` on internal consts
cee-chen Sep 11, 2024
3e6c32b
[refactor] avoid render prop + unmemoized array by creating a new fun…
cee-chen Sep 11, 2024
7594ac8
[a11y] Fix VO not correctly reading out SR text due to newlines in JSX
cee-chen Sep 11, 2024
bf7853e
[refactor] remove non-content children out of EuiDataGridCellContent
cee-chen Sep 11, 2024
f53c710
[cleanup] Replace `hidden` attribute with conditional JSX
cee-chen Sep 12, 2024
0b29898
[refactor] clean up focus trap aria-describedby text
cee-chen Sep 12, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { render } from '../../../../test/rtl';
import { RowHeightUtils } from '../../utils/__mocks__/row_heights';
import { mockFocusContext } from '../../utils/__mocks__/focus_context';
import { DataGridFocusContext } from '../../utils/focus';
import type { EuiDataGridProps } from '../../data_grid_types';

import { EuiDataGridCell } from './data_grid_cell';

Expand Down Expand Up @@ -104,6 +105,61 @@ describe('EuiDataGridCell', () => {
expect(mockPopoverContext.closeCellPopover).toHaveBeenCalledTimes(1);
});

describe('setCellProps', () => {
it('correctly merges props that also have EUI values', () => {
const RenderCellValue: EuiDataGridProps['renderCellValue'] = ({
setCellProps,
}) => {
useEffect(() => {
setCellProps({
style: { backgroundColor: 'black' },
css: { color: 'white' },
'data-test-subj': 'test',
className: 'helloWorld',
});
}, [setCellProps]);
return 'cell render';
};

const { getByTestSubject } = render(
<EuiDataGridCell {...requiredProps} renderCellValue={RenderCellValue} />
);

const cell = getByTestSubject('dataGridRowCell test'); // should have merged `data-test-subj` correctly
expect(cell).toHaveClass('euiDataGridRowCell helloWorld'); // should have merged `className` correctly
expect(cell).toHaveStyle('background-color: rgb(0, 0, 0)'); // should have merged `style` correctly
expect(cell).toHaveStyle('color: rgb(255, 255, 255)'); // should have applied consumer `css`
expect(cell.className).toMatch(/css-[\w\d]+-euiDataGridRowCell/); // should not have overridden EUI `css`
});

it('does not allow overriding certain EUI props/values', () => {
const RenderCellValue: EuiDataGridProps['renderCellValue'] = ({
setCellProps,
}) => {
useEffect(() => {
setCellProps({
// @ts-expect-error - deliberately passing omitted props
role: 'ignored',
tabIndex: 2,
'aria-rowindex': 99,
'data-gridcell-visible-row-index': -200,
});
}, [setCellProps]);
return 'cell render';
};

const { container } = render(
<EuiDataGridCell {...requiredProps} renderCellValue={RenderCellValue} />
);

const cell = container.firstElementChild;
expect(cell).toHaveAttribute('role', 'gridcell');
expect(cell).toHaveAttribute('tabIndex', '-1');
expect(cell).toHaveAttribute('aria-rowindex', '1');
expect(cell).toHaveAttribute('data-gridcell-visible-row-index', '0');
});
});

describe('shouldComponentUpdate', () => {
let shouldComponentUpdate: jest.SpyInstance;
let component: ReactWrapper;
Expand Down Expand Up @@ -489,7 +545,9 @@ describe('EuiDataGridCell', () => {
});

it('allows overriding column.isExpandable with setCellProps({ isExpandable })', () => {
const RenderCellValue = ({ setCellProps }: any) => {
const RenderCellValue: EuiDataGridProps['renderCellValue'] = ({
setCellProps,
}) => {
useEffect(() => {
setCellProps({ isExpandable: false });
}, [setCellProps]);
Expand Down
133 changes: 76 additions & 57 deletions packages/eui/src/components/datagrid/body/cell/data_grid_cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ import React, {
KeyboardEvent,
memo,
useMemo,
forwardRef,
MutableRefObject,
ReactElement,
HTMLAttributes,
} from 'react';
import { createPortal } from 'react-dom';

import { IS_JEST_ENVIRONMENT } from '../../../../utils';
import {
keys,
useEuiMemoizedStyles,
RenderWithEuiStylesMemoizer,
} from '../../../../services';
import { keys, useEuiMemoizedStyles } from '../../../../services';
import { EuiScreenReaderOnly } from '../../../accessibility';
import { EuiI18n } from '../../../i18n';
import { EuiTextBlockTruncate } from '../../../text_truncate';
Expand Down Expand Up @@ -636,58 +634,47 @@ export class EuiDataGridCell extends Component<

return (
<RenderCellInRow row={row}>
<RenderWithEuiStylesMemoizer>
{(stylesMemoizer) => {
const styles = stylesMemoizer(euiDataGridRowCellStyles);
const cssStyles = [styles.euiDataGridRowCell, cellProps?.css];
return (
<div
role="gridcell"
aria-rowindex={ariaRowIndex}
tabIndex={this.state.isFocused ? 0 : -1}
ref={this.cellRef}
{...cellProps}
css={cssStyles}
// Data attributes to help target specific cells by either data or current cell location
data-gridcell-column-id={this.props.columnId} // Static column ID name, not affected by column order
data-gridcell-column-index={this.props.colIndex} // Affected by column reordering
data-gridcell-row-index={this.props.rowIndex} // Index from data, not affected by sorting or pagination
data-gridcell-visible-row-index={this.props.visibleRowIndex} // Affected by sorting & pagination
onKeyDown={this.handleCellKeyDown}
onMouseEnter={this.onMouseEnter}
onMouseLeave={this.onMouseLeave}
>
<HandleInteractiveChildren
cellEl={this.cellRef.current}
updateCellFocusContext={this.updateCellFocusContext}
renderFocusTrap={!isExpandable}
>
<EuiDataGridCellContent
{...rest}
setCellProps={this.setCellProps}
column={column}
columnType={columnType}
isExpandable={isExpandable}
isExpanded={popoverIsOpen}
onExpandClick={this.handleCellExpansionClick}
popoverAnchorRef={this.popoverAnchorRef}
showCellActions={showCellActions}
isFocused={this.state.isFocused}
setCellContentsRef={this.setCellContentsRef}
rowHeight={rowHeight}
rowHeightUtils={rowHeightUtils}
isControlColumn={cellClasses.includes(
'euiDataGridRowCell--controlColumn'
)}
ariaRowIndex={ariaRowIndex}
rowIndex={rowIndex}
colIndex={colIndex}
/>
</HandleInteractiveChildren>
</div>
);
}}
</RenderWithEuiStylesMemoizer>
<GridCellDiv
{...cellProps}
ref={this.cellRef}
columnId={this.props.columnId}
columnIndex={this.props.colIndex}
rowIndex={rowIndex}
visibleRowIndex={this.props.visibleRowIndex}
aria-rowindex={ariaRowIndex}
tabIndex={this.state.isFocused ? 0 : -1}
onKeyDown={this.handleCellKeyDown}
onMouseEnter={this.onMouseEnter}
onMouseLeave={this.onMouseLeave}
>
<HandleInteractiveChildren
cellEl={this.cellRef.current}
updateCellFocusContext={this.updateCellFocusContext}
renderFocusTrap={!isExpandable}
>
<EuiDataGridCellContent
{...rest}
setCellProps={this.setCellProps}
column={column}
columnType={columnType}
isExpandable={isExpandable}
isExpanded={popoverIsOpen}
onExpandClick={this.handleCellExpansionClick}
popoverAnchorRef={this.popoverAnchorRef}
showCellActions={showCellActions}
isFocused={this.state.isFocused}
setCellContentsRef={this.setCellContentsRef}
rowHeight={rowHeight}
rowHeightUtils={rowHeightUtils}
isControlColumn={cellClasses.includes(
'euiDataGridRowCell--controlColumn'
)}
ariaRowIndex={ariaRowIndex}
rowIndex={rowIndex}
colIndex={colIndex}
/>
</HandleInteractiveChildren>
</GridCellDiv>
</RenderCellInRow>
);
}
Expand Down Expand Up @@ -725,3 +712,35 @@ const RenderTruncatedCellContent: FunctionComponent<{
);
});
RenderTruncatedCellContent.displayName = 'RenderTruncatedCellContent';

/**
* Function component utilities for easier hook usage
*/

const GridCellDiv = memo(
mgadewoll marked this conversation as resolved.
Show resolved Hide resolved
forwardRef<
HTMLDivElement,
HTMLAttributes<HTMLDivElement> & {
columnId: string;
columnIndex: number;
rowIndex: number;
visibleRowIndex: number;
}
>(({ columnId, columnIndex, rowIndex, visibleRowIndex, ...props }, ref) => {
const styles = useEuiMemoizedStyles(euiDataGridRowCellStyles);
return (
<div
ref={ref}
css={styles.euiDataGridRowCell}
{...props}
role="gridcell"
// Data attributes to help target specific cells by either data or current cell location
data-gridcell-column-id={columnId} // Static column ID name, not affected by column order
data-gridcell-column-index={columnIndex} // Affected by column reordering
data-gridcell-row-index={rowIndex} // Index from data, not affected by sorting or pagination
data-gridcell-visible-row-index={visibleRowIndex} // Affected by sorting & pagination
/>
);
})
);
GridCellDiv.displayName = 'GridCellDiv';
5 changes: 4 additions & 1 deletion packages/eui/src/components/datagrid/data_grid_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ interface SharedRenderCellElementProps {
}

export type EuiDataGridSetCellProps = CommonProps &
HTMLAttributes<HTMLDivElement> & {
Omit<
HTMLAttributes<HTMLDivElement>,
'role' | 'tabIndex' | 'aria-rowindex'
> & {
isExpandable?: boolean;
};

Expand Down