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 48 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
elizabetdev Sep 15, 2021
9231c30
Fixing comments line
elizabetdev Sep 15, 2021
f664e6d
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
elizabetdev Sep 15, 2021
425de96
Removing HTML code
elizabetdev Sep 15, 2021
e5d766f
New section title
elizabetdev Sep 15, 2021
8bc5aac
Small logic optimization
chandlerprall Sep 15, 2021
4ac0a88
Improving texts
elizabetdev Sep 15, 2021
968ee10
Renaming files and improving "fixed heights rows" demo
elizabetdev Sep 15, 2021
16fcec5
Merge branch 'auto_height_data_grid' of https://github.com/VladLasits…
elizabetdev Sep 15, 2021
1a015b6
Better text :D
elizabetdev Sep 15, 2021
6e46f05
Merge branch 'master' into auto_height_data_grid
chandlerprall Sep 15, 2021
b8bce9c
Text again
elizabetdev Sep 15, 2021
34a38c3
Merge branch 'auto_height_data_grid' of https://github.com/VladLasits…
elizabetdev 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
elizabetdev Sep 20, 2021
0dff342
Merge remote-tracking branch 'upstream/master' into auto_height_data_…
elizabetdev Sep 20, 2021
a82612d
Better `gridStyle` font size explanation
elizabetdev 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
Expand Up @@ -2,6 +2,7 @@

- Added optional line numbers to `EuiCodeBlock` ([#4993](https://github.com/elastic/eui/pull/4993))
- Removed `emoticon` support and removed rendered `<div>` from `EuiMarkdownFormat` ([#5176](https://github.com/elastic/eui/pull/5176))
- 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))

**Bug fixes**

Expand Down
102 changes: 76 additions & 26 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,34 +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 @@ -76,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 />,
},
],
};
211 changes: 211 additions & 0 deletions src-docs/src/views/datagrid/row_auto_height.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import React, {
useCallback,
useState,
createContext,
useContext,
useMemo,
ReactNode,
} from 'react';
// @ts-ignore not configured to import json
import githubData from './row_auto_height_data.json';

import {
EuiDataGrid,
EuiDataGridProps,
} from '../../../../src/components/datagrid';
import { EuiLink } from '../../../../src/components/link';
import { EuiIcon } from '../../../../src/components/icon';
import { EuiToolTip } from '../../../../src/components/tool_tip';
import { EuiAvatar } from '../../../../src/components/avatar';
import { EuiBadge } from '../../../../src/components/badge';
import { EuiMarkdownFormat } from '../../../../src/components/markdown_editor';

interface DataShape {
html_url: string;
title: string;
user: {
login: string;
avatar_url: string;
};
labels: Array<{
name: string;
color: string;
}>;
comments: number;
created_at: string;
body?: string;
}

// convert strings to Date objects
for (let i = 0; i < githubData.length; i++) {
githubData[i].created_at = new Date(githubData[i].created_at);
}

type DataContextShape =
| undefined
| {
data: DataShape[];
};
const DataContext = createContext<DataContextShape>(undefined);

const columns = [
{
id: 'created',
displayAsText: 'Created',
schema: 'datetime',
isExpandable: false,
},
{
id: 'title',
displayAsText: 'Title',
isExpandable: false,
},
{
id: 'user',
displayAsText: 'User',
isExpandable: false,
},
{
id: 'body',
displayAsText: 'Description',
},
{
id: 'labels',
displayAsText: 'Labels',
isExpandable: false,
},
];

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

const RenderCellValue: EuiDataGridProps['renderCellValue'] = ({
rowIndex,
columnId,
isDetails,
}) => {
const { data } = useContext(DataContext)!;

const item = data[rowIndex];
let content: ReactNode = '';

if (columnId === 'title') {
content = (
<>
{item.comments >= 1 && (
<EuiToolTip content={`${item.comments} comments`}>
<EuiIcon type="editorComment" />
</EuiToolTip>
)}
<EuiLink href={item.html_url} target="blank" external>
{item.title}
</EuiLink>
</>
);
} else if (columnId === 'user') {
content = (
<>
<EuiAvatar
name={item.user.login}
imageUrl={item.user.avatar_url}
size="l"
/>
&nbsp;{item.user.login}
</>
);
} else if (columnId === 'labels') {
content = (
<>
{item.labels.map(({ name, color }) => (
<EuiBadge key={name} color={`#${color}`}>
{name}
</EuiBadge>
))}
</>
);
} else if (columnId === 'created') {
content = item.created_at.toString();
} else if (columnId === 'body') {
if (isDetails) {
// expanded in a popover
content = <EuiMarkdownFormat>{item.body ?? ''}</EuiMarkdownFormat>;
} else {
// a full issue description is a *lot* to shove into a cell
content = (item.body ?? '').slice(0, 300);
}
}

return content;
};

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

// ** Sorting config
const [sortingColumns, setSortingColumns] = useState([]);
const onSort = useCallback(
(sortingColumns) => {
setSortingColumns(sortingColumns);
},
[setSortingColumns]
);

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

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 rowHeightsOptions = useMemo(
() => ({
defaultHeight: 'auto' as const,
}),
[]
);

const dataContext = useMemo<DataContextShape>(
() => ({
data: raw_data,
}),
[]
);

return (
<DataContext.Provider value={dataContext}>
<EuiDataGrid
aria-label="Row height options with auto demo"
columns={columns}
columnVisibility={{ visibleColumns, setVisibleColumns }}
rowCount={raw_data.length}
height={400}
renderCellValue={RenderCellValue}
inMemory={{ level: 'sorting' }}
sorting={{ columns: sortingColumns, onSort }}
rowHeightsOptions={rowHeightsOptions}
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
pagination={{
...pagination,
pageSizeOptions: [50, 250, 1000],
onChangeItemsPerPage: onChangeItemsPerPage,
onChangePage: onChangePage,
}}
/>
</DataContext.Provider>
);
};
Loading