-
Notifications
You must be signed in to change notification settings - Fork 842
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] fix content header styles to ensure text alignment is applied #7720
Merged
mgadewoll
merged 7 commits into
elastic:main
from
mgadewoll:datagrid/7669-header-text-alignment
May 2, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
effb606
fix(EuiDataGrid): ensure manual text-align styles are applied
mgadewoll c182245
docs(storybook): add playground story for EuiDataGrid
mgadewoll 269cf33
chore: add changelog
mgadewoll b85c4f2
refactor(EuiDataGrid): DRY out header cell flex style
mgadewoll 157a862
docs(storybook): remove custom text alignment
mgadewoll 8bedf99
test(VRT): add reference images for EuiDataGrid
mgadewoll 53ade39
Merge branch 'main' into datagrid/7669-header-text-alignment
mgadewoll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file added
BIN
+138 KB
.loki/reference/chrome_desktop_Tabular_Content_EuiDataGrid_Playground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+129 KB
.loki/reference/chrome_mobile_Tabular_Content_EuiDataGrid_Playground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
**Bug fixes** | ||
|
||
- Fixed missing styles on header cells of `EuiDataGrid` that prevented content text alignment styles to apply | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,353 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React, { useCallback, useEffect, useState } from 'react'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { faker } from '@faker-js/faker'; | ||
|
||
import { enableFunctionToggleControls } from '../../../.storybook/utils'; | ||
import { EuiLink } from '../link'; | ||
import { EuiScreenReaderOnly } from '../accessibility'; | ||
import { EuiButtonIcon } from '../button'; | ||
|
||
import { DEFAULT_ROW_HEIGHT } from './utils/row_heights'; | ||
import { MINIMUM_WIDTH_FOR_GRID_CONTROLS } from './controls/data_grid_toolbar'; | ||
import type { | ||
EuiDataGridCellValueElementProps, | ||
EuiDataGridColumnCellActionProps, | ||
EuiDataGridColumnSortingConfig, | ||
EuiDataGridProps, | ||
} from './data_grid_types'; | ||
import { EuiDataGrid } from './data_grid'; | ||
|
||
const dataKeys = [ | ||
'name', | ||
'email', | ||
'account', | ||
'location', | ||
'date', | ||
'version', | ||
] as const; | ||
const raw_data = Array.from({ length: 10 }).map(() => { | ||
const email = faker.internet.email(); | ||
const name = `${faker.person.lastName()}, ${faker.person.firstName()}`; | ||
const suffix = faker.person.suffix(); | ||
return { | ||
name: { | ||
formatted: `${name} ${suffix}`, | ||
raw: name, | ||
}, | ||
email: { | ||
formatted: <EuiLink href="">{faker.internet.email()}</EuiLink>, | ||
raw: email, | ||
}, | ||
location: ( | ||
<> | ||
{`${faker.location.city()}, `} | ||
<EuiLink href="https://google.com">{faker.location.country()}</EuiLink> | ||
</> | ||
), | ||
date: `${faker.date.past()}`, | ||
account: faker.finance.accountNumber(), | ||
version: faker.system.semver(), | ||
}; | ||
}); | ||
|
||
const columns = [ | ||
{ | ||
id: 'name', | ||
displayAsText: 'Name', | ||
defaultSortDirection: 'asc' as const, | ||
cellActions: [ | ||
({ rowIndex, Component }: EuiDataGridColumnCellActionProps) => { | ||
const data = raw_data; | ||
const value = data[rowIndex].name.raw; | ||
return ( | ||
<Component | ||
onClick={() => alert(`Hi ${value}`)} | ||
iconType="heart" | ||
aria-label={`Say hi to ${value}!`} | ||
> | ||
Say hi | ||
</Component> | ||
); | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'email', | ||
displayAsText: 'Email address', | ||
initialWidth: 130, | ||
cellActions: [ | ||
({ rowIndex, Component }: EuiDataGridColumnCellActionProps) => { | ||
const data = raw_data; | ||
const value = data[rowIndex].email.raw; | ||
return ( | ||
<Component | ||
onClick={() => alert(value)} | ||
iconType="email" | ||
aria-label={`Send email to ${value}`} | ||
> | ||
Send email | ||
</Component> | ||
); | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'location', | ||
displayAsText: 'Location', | ||
}, | ||
{ | ||
id: 'account', | ||
displayAsText: 'Account', | ||
actions: { | ||
showHide: { label: 'Custom hide label' }, | ||
showMoveLeft: false, | ||
showMoveRight: false, | ||
additional: [ | ||
{ | ||
label: 'Custom action', | ||
onClick: () => {}, | ||
iconType: 'cheer', | ||
size: 'xs' as const, | ||
color: 'text' as const, | ||
}, | ||
], | ||
}, | ||
cellActions: [ | ||
({ | ||
rowIndex, | ||
Component, | ||
isExpanded, | ||
}: EuiDataGridColumnCellActionProps) => { | ||
const data = raw_data; | ||
const value = data[rowIndex].account; | ||
const onClick = isExpanded | ||
? () => alert(`Sent money to ${value} when expanded`) | ||
: () => alert(`Sent money to ${value} when not expanded`); | ||
return ( | ||
<Component | ||
onClick={onClick} | ||
iconType="faceHappy" | ||
aria-label={`Send money to ${value}`} | ||
> | ||
Send money | ||
</Component> | ||
); | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'date', | ||
displayAsText: 'Date', | ||
defaultSortDirection: 'desc' as const, | ||
}, | ||
{ | ||
id: 'version', | ||
displayAsText: 'Version', | ||
defaultSortDirection: 'desc' as const, | ||
initialWidth: 70, | ||
isResizable: false, | ||
actions: false as const, | ||
}, | ||
]; | ||
|
||
const RenderCellValue = ({ | ||
rowIndex, | ||
columnId, | ||
}: EuiDataGridCellValueElementProps) => { | ||
const data = raw_data; | ||
const row = data[rowIndex]; | ||
const columnName = columnId as (typeof dataKeys)[number]; | ||
const column = row[columnName]; | ||
|
||
const getFormatted = () => { | ||
if (typeof column === 'object') { | ||
const hasFormatted = 'formatted' in column; | ||
|
||
return hasFormatted ? column.formatted : column; | ||
} | ||
|
||
return typeof column === 'string' ? column : null; | ||
}; | ||
|
||
return data.hasOwnProperty(rowIndex) ? getFormatted() : null; | ||
}; | ||
|
||
const meta: Meta<EuiDataGridProps> = { | ||
title: 'Tabular Content/EuiDataGrid', | ||
component: EuiDataGrid, | ||
argTypes: { | ||
width: { control: 'text' }, | ||
height: { control: 'text' }, | ||
}, | ||
args: { | ||
minSizeForControls: MINIMUM_WIDTH_FOR_GRID_CONTROLS, | ||
}, | ||
}; | ||
enableFunctionToggleControls(meta, ['onColumnResize']); | ||
|
||
export default meta; | ||
type Story = StoryObj<EuiDataGridProps>; | ||
|
||
export const Playground: Story = { | ||
args: { | ||
columns, | ||
rowCount: 10, | ||
renderCellValue: RenderCellValue, | ||
trailingControlColumns: [ | ||
{ | ||
id: 'trailing-actions', | ||
width: 40, | ||
headerCellRender: () => ( | ||
<EuiScreenReaderOnly> | ||
<span>Trailing actions</span> | ||
</EuiScreenReaderOnly> | ||
), | ||
rowCellRender: () => <EuiButtonIcon iconType="boxesHorizontal" />, | ||
}, | ||
], | ||
leadingControlColumns: [ | ||
{ | ||
id: 'leading-actions', | ||
width: 40, | ||
headerCellRender: () => ( | ||
<EuiScreenReaderOnly> | ||
<span>Leading actions</span> | ||
</EuiScreenReaderOnly> | ||
), | ||
rowCellRender: () => <EuiButtonIcon iconType="boxesHorizontal" />, | ||
}, | ||
], | ||
// setup for easier testing/QA | ||
columnVisibility: { | ||
visibleColumns: [ | ||
'name', | ||
'email', | ||
'account', | ||
'location', | ||
'date', | ||
'amount', | ||
'phone', | ||
'version', | ||
], | ||
setVisibleColumns: () => {}, | ||
}, | ||
inMemory: { level: 'sorting' }, | ||
pagination: { | ||
pageIndex: 0, | ||
pageSize: 10, | ||
pageSizeOptions: [10, 20, 50], | ||
onChangeItemsPerPage: () => {}, | ||
onChangePage: () => {}, | ||
}, | ||
gridStyle: { | ||
fontSize: 'm', | ||
cellPadding: 'm', | ||
border: 'all', | ||
stripes: false, | ||
header: 'shade', | ||
footer: 'overline', | ||
stickyFooter: true, | ||
rowHover: 'highlight', | ||
rowClasses: {}, | ||
}, | ||
width: '', | ||
height: '', | ||
toolbarVisibility: { | ||
showColumnSelector: true, | ||
showDisplaySelector: true, | ||
showSortSelector: true, | ||
showKeyboardShortcuts: true, | ||
showFullScreenSelector: true, | ||
additionalControls: null, | ||
}, | ||
rowHeightsOptions: { | ||
defaultHeight: DEFAULT_ROW_HEIGHT, | ||
rowHeights: {}, | ||
lineHeight: undefined, | ||
scrollAnchorRow: undefined, | ||
}, | ||
}, | ||
render: (args: EuiDataGridProps) => <StatefulDataGrid {...args} />, | ||
}; | ||
|
||
const StatefulDataGrid = (props: EuiDataGridProps) => { | ||
const { pagination, sorting, columnVisibility, ...rest } = props; | ||
|
||
// Pagination | ||
const [_pagination, setPagination] = useState({ | ||
pageIndex: pagination?.pageIndex ?? 0, | ||
...pagination, | ||
}); | ||
const onChangeItemsPerPage = useCallback( | ||
(pageSize: number) => | ||
setPagination((pagination) => ({ | ||
...pagination, | ||
pageSize, | ||
pageIndex: 0, | ||
})), | ||
[setPagination] | ||
); | ||
const onChangePage = useCallback( | ||
(pageIndex: number) => | ||
setPagination((pagination) => ({ ...pagination, pageIndex })), | ||
[setPagination] | ||
); | ||
|
||
useEffect(() => { | ||
if (pagination) { | ||
setPagination((curentPagination) => ({ | ||
...curentPagination, | ||
...pagination, | ||
})); | ||
} | ||
}, [pagination]); | ||
|
||
// Sorting | ||
const [sortingColumns, setSortingColumns] = useState< | ||
EuiDataGridColumnSortingConfig[] | ||
>(sorting?.columns ?? []); | ||
const onSort = useCallback( | ||
(sortingColumns: EuiDataGridColumnSortingConfig[]) => { | ||
setSortingColumns(sortingColumns); | ||
}, | ||
[setSortingColumns] | ||
); | ||
|
||
useEffect(() => { | ||
if (sorting && Array.isArray(sorting.columns)) { | ||
setSortingColumns(sorting.columns); | ||
} | ||
}, [sorting]); | ||
|
||
// Column visibility | ||
const [visibleColumns, setVisibleColumns] = useState( | ||
columnVisibility?.visibleColumns ?? columns.map(({ id }) => id) // initialize to the full set of columns | ||
); | ||
|
||
useEffect(() => { | ||
if (columnVisibility?.visibleColumns != null) { | ||
setVisibleColumns(columnVisibility?.visibleColumns); | ||
} | ||
}, [columnVisibility]); | ||
|
||
return ( | ||
<EuiDataGrid | ||
{...rest} | ||
columnVisibility={{ visibleColumns, setVisibleColumns }} | ||
sorting={{ columns: sortingColumns, onSort }} | ||
pagination={{ | ||
..._pagination, | ||
onChangeItemsPerPage: onChangeItemsPerPage, | ||
onChangePage: onChangePage, | ||
}} | ||
/> | ||
); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
me initially seeing the 300+ line diff on this PR: panik
me seeing it's a storybook story: kalm
In all seriousness thanks for going the extra mile to add a storybook for EuiDataGrid! 🙌