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 global custom cell height #4829

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
90 changes: 90 additions & 0 deletions src-docs/src/views/datagrid/cell_height.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React, { useState, useCallback } from 'react';
import { fake } from 'faker';

import { EuiDataGrid, EuiAvatar, EuiMark } from '../../../../src/components/';

const columns = [
{
id: 'avatar',
initialWidth: 80,
isResizable: false,
isExpandable: false,
},
{
id: 'team',
initialWidth: 100,
},
{
id: 'members',
isExpandable: false,
isTruncating: false,
},
];

const data = [];

for (let i = 1; i < 100; i++) {
const subdata = [];
for (let x = 1; x < 50; x++) {
subdata.push(
<>
{fake('{{name.firstName}}')}{' '}
<EuiMark>{fake('{{name.lastName}}')}</EuiMark>,{' '}
</>
);
}

data.push({
avatar: (
<EuiAvatar
size="xl"
name={fake('{{name.lastName}}, {{name.firstName}}')}
/>
),
team: fake('{{company.companyName}}'),
members: (
<div>
{subdata.map((sub, idx) => (
<span key={idx}>{sub}</span>
))}
</div>
),
});
}

export default () => {
const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 5 });

const [visibleColumns, setVisibleColumns] = useState(() =>
columns.map(({ id }) => id)
);

const setPageIndex = useCallback(
(pageIndex) => setPagination({ ...pagination, pageIndex }),
[pagination, setPagination]
);
const setPageSize = useCallback(
(pageSize) => setPagination({ ...pagination, pageSize, pageIndex: 0 }),
[pagination, setPagination]
);

return (
<EuiDataGrid
aria-label="DataGrid demonstrating column sizing constraints"
defaultColumnHeight={126}
columns={columns}
columnVisibility={{
visibleColumns: visibleColumns,
setVisibleColumns: setVisibleColumns,
}}
rowCount={data.length}
renderCellValue={({ rowIndex, columnId }) => data[rowIndex][columnId]}
pagination={{
...pagination,
pageSizeOptions: [5, 10, 25],
onChangeItemsPerPage: setPageSize,
onChangePage: setPageIndex,
}}
/>
);
};
26 changes: 26 additions & 0 deletions src-docs/src/views/datagrid/datagrid_styling_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const dataGridControlsSource = require('!!raw-loader!./additional_controls');
const dataGridControlsHtml = renderToHtml(DataGridControls);

import DataGridColumnWidths from './column_widths';
import DataGridCellHeight from './cell_height';
import DataGridColumnActions from './column_actions';
import DataGridColumnCellActions from './column_cell_actions';
const dataGridColumnWidthsSource = require('!!raw-loader!./column_widths');
Expand Down Expand Up @@ -274,6 +275,31 @@ export const DataGridStylingExample = {
},
demo: <DataGridColumnWidths />,
},
{
source: [
{
type: GuideSectionTypes.JS,
code: dataGridColumnWidthsSource,
},
{
type: GuideSectionTypes.HTML,
code: dataGridColumnWidthsHtml,
},
],
title: 'Default cell height',
text: (
<Fragment>
<p>Lets try out assigning a custom cell height</p>
</Fragment>
),
components: { DataGridCellHeight },
snippet: widthsSnippet,
props: {
EuiDataGrid,
EuiDataGridColumn,
},
demo: <DataGridCellHeight />,
},
{
source: [
{
Expand Down
2 changes: 1 addition & 1 deletion src/components/datagrid/_data_grid_data_row.scss
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@

.euiDataGridRowCell__expandFlex {
display: flex;
align-items: center;
align-items: flex-start;
}

.euiDataGridRowCell__expandContent {
Expand Down
2 changes: 2 additions & 0 deletions src/components/datagrid/_mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ $euiDataGridStyles: (
@mixin euiDataGridRowCell {
.euiDataGridRowCell {
@content;
align-items: flex-start;
overflow-y: hidden;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/components/datagrid/data_grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ type CommonGridProps = CommonProps &
* Sets the grid's width, forcing it to overflow in a scrollable container with cell virtualization
*/
width?: CSSProperties['width'];

defaultColumnHeight?: number;
};

// Force either aria-label or aria-labelledby to be defined
Expand Down Expand Up @@ -692,6 +694,7 @@ export const EuiDataGrid: FunctionComponent<EuiDataGridProps> = (props) => {
minSizeForControls = MINIMUM_WIDTH_FOR_GRID_CONTROLS,
height,
width,
defaultColumnHeight,
...rest
} = props;

Expand Down Expand Up @@ -1110,6 +1113,7 @@ export const EuiDataGrid: FunctionComponent<EuiDataGridProps> = (props) => {
columns={orderedVisibleColumns}
columnWidths={columnWidths}
defaultColumnWidth={defaultColumnWidth}
defaultColumnHeight={defaultColumnHeight}
toolbarHeight={toolbarDemensions.height}
leadingControlColumns={leadingControlColumns}
schema={mergedSchema}
Expand Down
6 changes: 5 additions & 1 deletion src/components/datagrid/data_grid_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface EuiDataGridBodyProps {
isFullScreen: boolean;
columnWidths: EuiDataGridColumnWidths;
defaultColumnWidth?: number | null;
defaultColumnHeight?: number | null;
leadingControlColumns?: EuiDataGridControlColumn[];
trailingControlColumns?: EuiDataGridControlColumn[];
columns: EuiDataGridColumn[];
Expand Down Expand Up @@ -313,6 +314,7 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
isFullScreen,
columnWidths,
defaultColumnWidth,
defaultColumnHeight,
leadingControlColumns = [],
trailingControlColumns = [],
columns,
Expand Down Expand Up @@ -519,7 +521,9 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
]
);

const [rowHeight, setRowHeight] = useState(INITIAL_ROW_HEIGHT);
const [rowHeight, setRowHeight] = useState(
defaultColumnHeight || INITIAL_ROW_HEIGHT
);
const getRowHeight = useCallback(() => rowHeight, [rowHeight]);
useEffect(() => {
if (gridRef.current) gridRef.current.resetAfterRowIndex(0);
Expand Down
13 changes: 11 additions & 2 deletions src/components/datagrid/data_grid_cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,20 @@ export class EuiDataGridCell extends Component<
}}
clickOutsideDisables={true}>
<div className="euiDataGridRowCell__expandFlex">
<div className="euiDataGridRowCell__expandContent">
<div
className={
column?.isTruncating !== false
? 'euiDataGridRowCell__expandContent'
: ''
}>
{screenReaderPosition}
<div
ref={this.setCellContentsRef}
className="euiDataGridRowCell__truncate">
className={
column?.isTruncating !== false
? 'euiDataGridRowCell__truncate'
: ''
}>
<EuiDataGridCellContent {...cellContentProps} />
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/components/datagrid/data_grid_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface EuiDataGridColumn {
* Whether this column's width can be changed by the user, defaults to true
*/
isResizable?: boolean;
isTruncating?: boolean;
/**
* Initial width (in pixels) of the column
*/
Expand Down