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

[EuiDataGrid] Add lineHeight to rowHeightsOptions #5140

Closed
wants to merge 15 commits into from
Closed
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `37.6.1`.
- Added `lineHeight` to `rowHeightsOptions` in `EuiDataGrid` ([#5140](https://github.com/elastic/eui/pull/5140))

## [`37.6.1`](https://github.com/elastic/eui/tree/v37.6.1)

Expand Down
13 changes: 9 additions & 4 deletions src-docs/src/views/datagrid/datagrid_height_options_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ const dataGridRowHeightOptionsHtml = renderToHtml(DataGridRowHeightOptions);

const rowHeightsSnippet = `rowHeightsOptions = {
defaultHeight: {
lineCount: 2, // default every row to 2 lines of text. Also we can provide height in pixels
lineCount: 2, // default every row to 2 lines of text. We can also provide height in pixels
},
rowHeights: {
1: {
lineCount: 5, // for row which have index 1 we allow to show 5 lines after that we truncate
lineCount: 5, // for the row with index 1 we show 5 lines, after that we truncate
},
4: 140, // for row which have index 4 we set 140 pixel
4: 140, // for the row with index 4 we set height to 140 pixels
5: 80,
},
}`;
Expand All @@ -45,7 +45,8 @@ export const DataGridRowHeightOptionsExample = {
<p>
Row height options can be passed down to the grid through the{' '}
<EuiCode>rowHeightsOptions</EuiCode> prop. It accepts an object
configuring the default height and/or specific row heights:
configuring the default height, specific row heights and line
height:
</p>
<ul>
<li>
Expand All @@ -56,6 +57,10 @@ export const DataGridRowHeightOptionsExample = {
<EuiCode>rowHeights</EuiCode> - overrides the height for a
specific row
</li>
<li>
<EuiCode>lineHeight</EuiCode> - increases the default line-height
when set to <EuiCode>extra</EuiCode>
</li>
</ul>
<EuiCallOut
color="warning"
Expand Down
36 changes: 36 additions & 0 deletions src/components/datagrid/_data_grid_data_row.scss
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,30 @@
flex-grow: 1;
}

@include euiDataGridStyles(fontSizeSmall, lineHeightExtra) {
@include euiDataGridRowCell {
.euiDataGridRowCell__contentByHeight {
@include lineHeightFromBaseline(2.5) ;
}
}
}

@include euiDataGridStyles(lineHeightExtra) {
@include euiDataGridRowCell {
.euiDataGridRowCell__contentByHeight {
@include lineHeightFromBaseline(3.5);
}
}
}

@include euiDataGridStyles(fontSizeLarge, lineHeightExtra) {
@include euiDataGridRowCell {
.euiDataGridRowCell__contentByHeight {
@include lineHeightFromBaseline(3.5);
}
}
}

.euiDataGridRowCell__contentByHeight {
flex-grow: 1;
height: 100%;
Expand Down Expand Up @@ -237,6 +261,14 @@

.euiDataGridRowCell--fontSizeSmall {
@include euiFontSizeXS;

&.euiDataGridRowCell--lineHeightExtra {
@include lineHeightFromBaseline(2.5);
}
}

.euiDataGridRowCell--lineHeightExtra {
@include lineHeightFromBaseline(3.5);
}

@include euiDataGridStyles(fontSizeLarge) {
Expand All @@ -247,6 +279,10 @@

.euiDataGridRowCell--fontSizeLarge {
@include euiFontSize;

&.euiDataGridRowCell--lineHeightExtra {
@include lineHeightFromBaseline(3.5);
}
}

// Padding alternates
Expand Down
1 change: 1 addition & 0 deletions src/components/datagrid/_mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ $euiDataGridStyles: (
'fontSizeLarge'
'noControls'
'stickyFooter'
'lineHeightExtra'
);

// All this does is take some of the above and make a sibling selector
Expand Down
15 changes: 12 additions & 3 deletions src/components/datagrid/data_grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
EuiDataGridStyleBorders,
EuiDataGridStyleCellPaddings,
EuiDataGridStyleFontSizes,
EuiDataGridRowLineHeights,
EuiDataGridStyleFooter,
EuiDataGridStyleHeader,
EuiDataGridStyleRowHover,
Expand All @@ -71,6 +72,13 @@ const fontSizesToClassMap: { [size in EuiDataGridStyleFontSizes]: string } = {
l: 'euiDataGrid--fontSizeLarge',
};

const lineHeightsToClassMap: {
[size in EuiDataGridRowLineHeights]: string;
} = {
regular: '',
extra: 'euiDataGrid--lineHeightExtra',
};

const headerToClassMap: { [header in EuiDataGridStyleHeader]: string } = {
shade: 'euiDataGrid--headerShade',
underline: 'euiDataGrid--headerUnderline',
Expand Down Expand Up @@ -489,7 +497,7 @@ export const EuiDataGrid: FunctionComponent<EuiDataGridProps> = (props) => {
minSizeForControls,
height,
width,
rowHeightsOptions,
rowHeightsOptions = { lineHeight: 'regular' },
...rest
} = props;

Expand Down Expand Up @@ -688,12 +696,13 @@ export const EuiDataGrid: FunctionComponent<EuiDataGridProps> = (props) => {
const [rowHeightUtils] = useState(new RowHeightUtils());

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

const classes = classNames(
'euiDataGrid',
fontSizesToClassMap[gridStyles.fontSize!],
lineHeightsToClassMap[rowHeightsOptions.lineHeight!],
bordersToClassMap[gridStyles.border!],
headerToClassMap[gridStyles.header!],
footerToClassMap[gridStyles.footer!],
Expand Down
6 changes: 6 additions & 0 deletions src/components/datagrid/data_grid_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,8 @@ export type EuiDataGridRowHeightOption =
| number
| ExclusiveUnion<{ lineCount: number }, { height: number }>;

export type EuiDataGridRowLineHeights = 'regular' | 'extra';

export interface EuiDataGridRowHeightsOptions {
/**
* Defines the default size for all rows. It can be line count or just height.
Expand All @@ -705,4 +707,8 @@ export interface EuiDataGridRowHeightsOptions {
* Defines the height for a specific row. It can be line count or just height.
*/
rowHeights?: Record<number, EuiDataGridRowHeightOption>;
/**
* Adds extra line-height to cells
*/
lineHeight?: EuiDataGridRowLineHeights;
}
14 changes: 13 additions & 1 deletion src/components/datagrid/row_height_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
EuiDataGridStyle,
EuiDataGridRowHeightOption,
EuiDataGridRowHeightsOptions,
EuiDataGridRowLineHeights,
} from './data_grid_types';

const cellPaddingsToClassMap: Record<EuiDataGridStyleCellPaddings, string> = {
Expand All @@ -28,6 +29,11 @@ const fontSizesToClassMap: Record<EuiDataGridStyleFontSizes, string> = {
l: 'euiDataGridRowCell--fontSizeLarge',
};

const lineHeightSizesToClassMap: Record<EuiDataGridRowLineHeights, string> = {
regular: '',
extra: 'euiDataGridRowCell--lineHeightExtra',
};

function getNumberFromPx(style?: string) {
return style ? parseInt(style.replace('px', ''), 10) : 0;
}
Expand All @@ -42,12 +48,18 @@ export class RowHeightUtils {
} = {};
private fakeCell = document.createElement('div');

computeStylesForGridCell(gridStyles: EuiDataGridStyle) {
computeStylesForGridCell(
gridStyles: EuiDataGridStyle,
rowHeightsOptions: EuiDataGridRowHeightsOptions
) {
let lineHeight = rowHeightsOptions && rowHeightsOptions.lineHeight;
this.fakeCell.className = `
euiDataGridRowCell
${cellPaddingsToClassMap[gridStyles.cellPadding!]}
${fontSizesToClassMap[gridStyles.fontSize!]}
`;
if (lineHeight === 'extra')
this.fakeCell.classList.add(lineHeightSizesToClassMap[lineHeight]);
document.body.appendChild(this.fakeCell);
const allStyles = getComputedStyle(this.fakeCell);
this.styles = {
Expand Down