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] Auto-fit rows to content #4958

Merged
merged 74 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
83bb0c3
Add auto height possibility to row
VladLasitsa Jul 16, 2021
39bfc86
Fix eslint
VladLasitsa Jul 16, 2021
40c2cf0
Fix some problems with hiding column
VladLasitsa Jul 19, 2021
e76129d
Fix example
VladLasitsa Jul 19, 2021
c2de611
Fix example
VladLasitsa Jul 19, 2021
d06b9fa
Merge branch 'master' into auto_height_data_grid
VladLasitsa Jul 19, 2021
428860f
fix calculation height so that work for images and fix problems with …
VladLasitsa Jul 22, 2021
05c52a8
Fix tests
VladLasitsa Jul 23, 2021
eca4c2b
fix eslint
VladLasitsa Jul 23, 2021
82f698a
fix some problems
VladLasitsa Jul 26, 2021
253f16a
use resizeObserver instead of handling height in didMount method
VladLasitsa Jul 27, 2021
9fe0270
Fix some remarks
VladLasitsa Jul 28, 2021
8027a88
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
VladLasitsa Jul 28, 2021
2664080
fix some problems with shrinking
VladLasitsa Jul 30, 2021
517ebcb
Fix scrolling
VladLasitsa Jul 30, 2021
6f97f54
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
VladLasitsa Jul 30, 2021
752b354
Some performance improvements
VladLasitsa Aug 4, 2021
ce4ffa4
use callback ref for setting grid in rowHeigthsUtils instead of useEf…
VladLasitsa Aug 5, 2021
d79e523
Add new properties
VladLasitsa Aug 9, 2021
b4abd60
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
VladLasitsa Aug 9, 2021
e949d6b
add changelog
VladLasitsa Aug 9, 2021
ffa0926
Fix lint
VladLasitsa Aug 9, 2021
4a7fdda
convert autoheight example to TS
chandlerprall Aug 13, 2021
a7bdaef
Merge branch 'master' into auto_height_data_grid
chandlerprall Aug 18, 2021
1457544
With data
chandlerprall Aug 19, 2021
8fc8edc
examples cleanup
chandlerprall Aug 19, 2021
081d786
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
VladLasitsa Aug 30, 2021
0c3926e
Some fixes
VladLasitsa Aug 30, 2021
3fdb852
Add virtualizationOptions
VladLasitsa Aug 31, 2021
55b650b
Improve performance
VladLasitsa Sep 3, 2021
7ea6b88
Fix useEffect
VladLasitsa Sep 3, 2021
0425428
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
VladLasitsa Sep 3, 2021
5d470bd
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
VladLasitsa Sep 6, 2021
00bf264
Fix tests
VladLasitsa Sep 6, 2021
e12aeff
Fix lint
VladLasitsa Sep 6, 2021
2d670d0
Remove unnecessary changes
VladLasitsa Sep 6, 2021
fb162ce
Remove unnecessary changes
VladLasitsa Sep 6, 2021
fdf5756
Fix tests
VladLasitsa Sep 6, 2021
99a4d9e
Cleaned up lastUpdatedRow a bit
chandlerprall Sep 9, 2021
875d5e9
example cleanup
chandlerprall Sep 9, 2021
b45b17f
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
VladLasitsa Sep 13, 2021
ebe3ccf
Fix lint
VladLasitsa Sep 13, 2021
7a6a0d6
DRY out mock rowHeightUtils
cee-chen Sep 14, 2021
9536362
[EuiGridCell] Refactor + write test for new componentDidUpdate logic
cee-chen Sep 14, 2021
f58d629
[EuiGridCell] PR feedback: destructure props for readability
cee-chen Sep 14, 2021
1ec69e9
Merge branch 'master' into auto_height_data_grid
cee-chen Sep 14, 2021
b2e9976
fix nit's
VladLasitsa Sep 15, 2021
c1c52d4
Making datagrid matching the top snippet and improving demo
miukimiu Sep 15, 2021
9231c30
Fixing comments line
miukimiu Sep 15, 2021
f664e6d
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
miukimiu Sep 15, 2021
425de96
Removing HTML code
miukimiu Sep 15, 2021
e5d766f
New section title
miukimiu Sep 15, 2021
8bc5aac
Small logic optimization
chandlerprall Sep 15, 2021
4ac0a88
Improving texts
miukimiu Sep 15, 2021
968ee10
Renaming files and improving "fixed heights rows" demo
miukimiu Sep 15, 2021
16fcec5
Merge branch 'auto_height_data_grid' of https://github.com/VladLasits…
miukimiu Sep 15, 2021
1a015b6
Better text :D
miukimiu Sep 15, 2021
6e46f05
Merge branch 'master' into auto_height_data_grid
chandlerprall Sep 15, 2021
b8bce9c
Text again
miukimiu Sep 15, 2021
34a38c3
Merge branch 'auto_height_data_grid' of https://github.com/VladLasits…
miukimiu Sep 15, 2021
8f3d3df
Correctly identify the row heights page in the a11y test skipping
chandlerprall Sep 15, 2021
0c13260
Small copy cleanup
chandlerprall Sep 15, 2021
4744bf1
small example cleanup
chandlerprall Sep 15, 2021
6969933
Fix calculating defaultHeight after using density
VladLasitsa Sep 16, 2021
77bcbb2
Update mock
VladLasitsa Sep 16, 2021
d1d4305
make visibility property depends on calculating cell height
VladLasitsa Sep 16, 2021
110b903
Merge branch 'master' into auto_height_data_grid
chandlerprall Sep 16, 2021
5d589cc
make lint happy
chandlerprall Sep 16, 2021
675ebad
Merge branch 'master' into auto_height_data_grid
constancecchen Sep 16, 2021
9441e39
Revert "make visibility property depends on calculating cell height"
VladLasitsa Sep 16, 2021
9e3fc6d
snapshot
chandlerprall Sep 16, 2021
dbe8cf6
Updating example to make density work
miukimiu Sep 20, 2021
0dff342
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
miukimiu Sep 20, 2021
a82612d
Better `gridStyle` font size explanation
miukimiu Sep 20, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added `isDisabled` prop to `EuiFormRow` that disables the child field element ([#4908](https://github.com/elastic/eui/pull/4908))
- Added `auto` as value for `defaultHeight` in prop `rowHeightsOptions` in `EuiDataGrid` that allows to content auto-fit to row ([#4958](https://github.com/elastic/eui/pull/4958))

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

Expand Down
101 changes: 76 additions & 25 deletions src-docs/src/views/datagrid/datagrid_height_options_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import {
EuiCode,
EuiCodeBlock,
EuiSpacer,
EuiText,
} from '../../../../src/components';

import DataGridRowHeightOptions from './row_height_options';
const dataGridRowHeightOptionsSource = require('!!raw-loader!./row_height_options');
const dataGridRowHeightOptionsHtml = renderToHtml(DataGridRowHeightOptions);
import DataGridRowAutoHeight from './row_auto_height';
const dataGridRowAutoHeightSource = require('!!raw-loader!./row_auto_height');
const dataGridRowAutoHeightHtml = renderToHtml(DataGridRowAutoHeight);

const rowHeightsSnippet = `rowHeightsOptions = {
defaultHeight: {
Expand All @@ -26,8 +30,50 @@ const rowHeightsSnippet = `rowHeightsOptions = {
},
}`;

const autoRowHeightsSnippet = `
{
defaultHeight: 'auto', // each row auto fit to content except rows which was defined in 'rowHeights'
rowHeights: {
1: {
lineCount: 5, // for row which have index 1 we allow to show 5 lines after that we truncate
},
4: 140, // for row which have index 4 we set 140 pixel
},
}
`;

export const DataGridRowHeightOptionsExample = {
title: 'Data grid row height options',
intro: (
<Fragment>
<EuiText>
<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:
</p>
<ul>
<li>
<EuiCode>defaultHeight</EuiCode> - defines the default size for all
rows
</li>
<li>
<EuiCode>rowHeights</EuiCode> - overrides the height for a specific
row
</li>
</ul>
</EuiText>
<EuiSpacer />
<EuiCallOut color="warning" title="Rows have minimum height requirements">
<p>
Rows must be at least <strong>34 pixels</strong> tall so they can
render at least one line of text. If you provide a smaller height the
row will default to 34 pixels.
</p>
</EuiCallOut>
<EuiSpacer />
</Fragment>
),
sections: [
{
source: [
Expand All @@ -40,33 +86,9 @@ export const DataGridRowHeightOptionsExample = {
code: dataGridRowHeightOptionsHtml,
},
],
title: 'Fixed heights for rows',
text: (
<Fragment>
<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:
</p>
<ul>
<li>
<EuiCode>defaultHeight</EuiCode> - defines the default size for
all rows
</li>
<li>
<EuiCode>rowHeights</EuiCode> - overrides the height for a
specific row
</li>
</ul>
<EuiCallOut
color="warning"
title="Rows have minimum height requirements">
<p>
Rows must be at least <strong>34 pixels</strong> tall so they can
render at least one line of text. If you provide a smaller height
the row will default to 34 pixels.
</p>
</EuiCallOut>
<EuiSpacer />
<EuiCodeBlock language="javascript" paddingSize="s" isCopyable>
{rowHeightsSnippet}
</EuiCodeBlock>
Expand All @@ -75,5 +97,34 @@ export const DataGridRowHeightOptionsExample = {
components: { DataGridRowHeightOptions },
demo: <DataGridRowHeightOptions />,
},
{
source: [
{
type: GuideSectionTypes.JS,
code: dataGridRowAutoHeightSource,
},
{
type: GuideSectionTypes.HTML,
code: dataGridRowAutoHeightHtml,
},
],
title: 'Auto-fit row to content',
text: (
<Fragment>
<p>
Row height options also supports to set value so that rows auto fit
to content. Just provide <EuiCode>auto</EuiCode> as value for{' '}
<EuiCode>defaultHeight</EuiCode> or for one of the rows in{' '}
<EuiCode>rowHeights</EuiCode>
</p>
<EuiSpacer />
<EuiCodeBlock language="javascript" paddingSize="s" isCopyable>
{autoRowHeightsSnippet}
</EuiCodeBlock>
</Fragment>
),
components: { DataGridRowAutoHeight },
demo: <DataGridRowAutoHeight />,
},
],
};
175 changes: 175 additions & 0 deletions src-docs/src/views/datagrid/row_auto_height.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import React, {
useCallback,
useState,
createContext,
useContext,
useMemo,
useEffect,
} from 'react';
import { fake } from 'faker';

import {
EuiDataGrid,
EuiText,
EuiImage,
EuiButtonGroup,
EuiSpacer,
} from '../../../../src/components/';

const DataContext = createContext();

const columns = [
{
id: 'name',
displayAsText: 'Name',
defaultSortDirection: 'asc',
},
{
id: 'text',
},
];

// it is expensive to compute 10000 rows of fake data
// instead of loading up front, generate entries on the fly
const raw_data = [];

function RenderCellValue({ rowIndex, columnId }) {
const { data, adjustMountedCellCount, contentTypeSelected } = useContext(
DataContext
);

useEffect(() => {
adjustMountedCellCount(1);
return () => adjustMountedCellCount(-1);
}, [adjustMountedCellCount]);

if (data[rowIndex] == null) {
data[rowIndex] = {
name: fake('{{lorem.text}}'),
text: fake('{{lorem.text}}'),
};
}

const firstNumberSize = rowIndex < 7 && rowIndex > 0 ? rowIndex : 5;

return contentTypeSelected === 'images' ? (
<EuiImage
size={'original'}
alt="Fake img"
url={`https://source.unsplash.com/${firstNumberSize}00x${firstNumberSize}00/?starwars`}
/>
) : (
data[rowIndex][columnId]
);
}

const contentTypeOptions = [
{
id: 'text',
label: 'Text',
},
{
id: 'images',
label: 'Images',
},
];

export default () => {
// ** Pagination config
const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 50 });
const [contentTypeSelected, setContentTypeSelected] = useState('text');

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

const onContentTypeChange = useCallback(
(optionId) => setContentTypeSelected(optionId),
[setContentTypeSelected]
);

const onChangePage = useCallback(
(pageIndex) =>
setPagination((pagination) => ({ ...pagination, pageIndex })),
[setPagination]
);

// Column visibility
const [visibleColumns, setVisibleColumns] = useState(() =>
columns.map(({ id }) => id)
); // initialize to the full set of columns

const [mountedCellCount, setMountedCellCount] = useState(0);

const rowHeightsOptions = useMemo(
() => ({
defaultHeight: 'auto',
rowHeights: {
1: {
lineCount: 5,
},
4: contentTypeSelected === 'images' ? 240 : 140,
},
}),
[contentTypeSelected]
);

const dataContext = useMemo(
() => ({
data: raw_data,
contentTypeSelected,
adjustMountedCellCount: (adjustment) =>
setMountedCellCount(
(mountedCellCount) => mountedCellCount + adjustment
),
}),
[contentTypeSelected]
);

const grid = (
<EuiDataGrid
aria-label="Row height options with auto demo"
columns={columns}
columnVisibility={{ visibleColumns, setVisibleColumns }}
rowCount={10000}
height={400}
renderCellValue={RenderCellValue}
rowHeightsOptions={rowHeightsOptions}
pagination={{
...pagination,
pageSizeOptions: [50, 250, 1000],
onChangeItemsPerPage: onChangeItemsPerPage,
onChangePage: onChangePage,
}}
/>
);

return (
<DataContext.Provider value={dataContext}>
<EuiText>
<p>There are {mountedCellCount} rendered cells</p>
</EuiText>
<EuiSpacer />
<EuiText style={{ textAlign: 'center' }}>
<p>Type of content</p>
</EuiText>
<EuiSpacer />
<EuiButtonGroup
isFullWidth
buttonSize="compressed"
legend="Content type"
options={contentTypeOptions}
idSelected={contentTypeSelected}
onChange={onContentTypeChange}
/>
<EuiSpacer />
{grid}
</DataContext.Provider>
);
};
14 changes: 13 additions & 1 deletion src/components/datagrid/data_grid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ jest.mock('./row_height_utils', () => {
RowHeightUtils: jest.fn().mockImplementation(() => {
return {
computeStylesForGridCell: () => {},
clearHeightsCache: () => {},
setGrid: () => {},
getStylesForCell: () => ({
wordWrap: 'break-word',
wordBreak: 'break-word',
flexGrow: 1,
}),
isAutoHeight: () => false,
setRowHeight: () => {},
getRowHeight: () => 32,
getFont: () => null,
compareHeights: (currentRowHeight: number, cachedRowHeight: number) =>
currentRowHeight === cachedRowHeight,
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
getCalculatedHeight: (
heightOption: EuiDataGridRowHeightOption,
defaultHeight: number
Expand All @@ -46,7 +59,6 @@ jest.mock('./row_height_utils', () => {
},
};
}),
getStylesForCell: () => ({}),
};
});

Expand Down
Loading