Skip to content

Commit

Permalink
Added lineHeight configuration to rowHeightOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
chandlerprall committed Sep 9, 2021
1 parent eca5d49 commit 57aa7b6
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
15 changes: 8 additions & 7 deletions src-docs/src/views/datagrid/row_height_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,17 @@ export default () => {

const rowHeightsOptions = useMemo(
() => ({
lineHeight: '3rem',
defaultHeight: {
lineCount: 2,
},
rowHeights: {
1: {
lineCount: 5,
},
4: 140,
5: 80,
},
// rowHeights: {
// 1: {
// lineCount: 5,
// },
// 4: 140,
// 5: 80,
// },
}),
[]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ exports[`EuiDataGridBody renders 1`] = `
Object {
"height": 34,
"left": 0,
"lineHeight": undefined,
"position": "absolute",
"top": "0px",
"width": 20,
Expand Down Expand Up @@ -1230,6 +1231,7 @@ exports[`EuiDataGridBody renders 1`] = `
Object {
"height": 34,
"left": 20,
"lineHeight": undefined,
"position": "absolute",
"top": "0px",
"width": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exports[`EuiDataGridCell renders 1`] = `
role="gridcell"
style={
Object {
"lineHeight": undefined,
"width": undefined,
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/components/datagrid/body/data_grid_cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export class EuiDataGridCell extends Component<
style,
...rest
} = this.props;
const { rowIndex } = rest;
const { rowIndex, rowHeightsOptions } = rest;

const showCellButtons =
this.state.isFocused ||
Expand All @@ -359,7 +359,12 @@ export class EuiDataGridCell extends Component<
className: classNames(cellClasses, this.state.cellProps.className),
};

cellProps.style = { ...style, width, ...cellProps.style };
cellProps.style = {
...style, // from react-window
width, // column width, can be undefined
lineHeight: rowHeightsOptions?.lineHeight ?? undefined, // lineHeight configuration
...cellProps.style, // apply anything from setCellProps({style})
};

const handleCellKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
if (isExpandable) {
Expand Down
4 changes: 4 additions & 0 deletions src/components/datagrid/data_grid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ describe('EuiDataGrid', () => {
"color": "red",
"height": 34,
"left": 0,
"lineHeight": undefined,
"position": "absolute",
"top": "100px",
"width": 100,
Expand All @@ -591,6 +592,7 @@ describe('EuiDataGrid', () => {
"color": "blue",
"height": 34,
"left": 100,
"lineHeight": undefined,
"position": "absolute",
"top": "100px",
"width": 100,
Expand All @@ -610,6 +612,7 @@ describe('EuiDataGrid', () => {
"color": "red",
"height": 34,
"left": 0,
"lineHeight": undefined,
"position": "absolute",
"top": "134px",
"width": 100,
Expand All @@ -629,6 +632,7 @@ describe('EuiDataGrid', () => {
"color": "blue",
"height": 34,
"left": 100,
"lineHeight": undefined,
"position": "absolute",
"top": "134px",
"width": 100,
Expand Down
7 changes: 5 additions & 2 deletions src/components/datagrid/data_grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,11 @@ export const EuiDataGrid: FunctionComponent<EuiDataGridProps> = (props) => {
const [rowHeightUtils] = useState(new RowHeightUtils());

useEffect(() => {
rowHeightUtils.computeStylesForGridCell(gridStyles);
}, [gridStyles, rowHeightUtils]);
rowHeightUtils.computeStylesForGridCell(
gridStyles,
rowHeightsOptions?.lineHeight
);
}, [gridStyles, rowHeightUtils, rowHeightsOptions?.lineHeight]);

const classes = classNames(
'euiDataGrid',
Expand Down
4 changes: 4 additions & 0 deletions src/components/datagrid/data_grid_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,4 +705,8 @@ export interface EuiDataGridRowHeightsOptions {
* Defines the height for a specific row. It can be line count or just height.
*/
rowHeights?: Record<number, EuiDataGridRowHeightOption>;
/**
* Defines a global lineHeight style to apply to all cells
*/
lineHeight?: string;
}
9 changes: 8 additions & 1 deletion src/components/datagrid/row_height_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@ export class RowHeightUtils {
} = {};
private fakeCell = document.createElement('div');

computeStylesForGridCell(gridStyles: EuiDataGridStyle) {
computeStylesForGridCell(
gridStyles: EuiDataGridStyle,
lineHeight: string | undefined
) {
this.fakeCell.className = `
euiDataGridRowCell
${cellPaddingsToClassMap[gridStyles.cellPadding!]}
${fontSizesToClassMap[gridStyles.fontSize!]}
`;

// @ts-ignore it is valid to set `lineHeight` to undefined
this.fakeCell.style.lineHeight = lineHeight;

document.body.appendChild(this.fakeCell);
const allStyles = getComputedStyle(this.fakeCell);
this.styles = {
Expand Down

0 comments on commit 57aa7b6

Please sign in to comment.