-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EuiDataGrid] Introduce
renderCustomToolbar
, `additionalDisplaySett…
…ings` and `allowResetButton` customization options (#7190) Co-authored-by: Tim Schnell <[email protected]> Co-authored-by: Matthias Wilhelm <[email protected]> Co-authored-by: Cee Chen <[email protected]> Co-authored-by: Cee Chen <[email protected]>
- Loading branch information
1 parent
3a653d4
commit 7767b10
Showing
14 changed files
with
497 additions
and
88 deletions.
There are no files selected for viewing
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
136 changes: 136 additions & 0 deletions
136
src-docs/src/views/datagrid/toolbar/render_custom_toolbar.tsx
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,136 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import { css } from '@emotion/react'; | ||
import { faker } from '@faker-js/faker'; | ||
|
||
import { | ||
EuiDataGrid, | ||
EuiDataGridSorting, | ||
EuiDataGridColumnSortingConfig, | ||
EuiDataGridToolbarProps, | ||
EuiButtonEmpty, | ||
EuiFormRow, | ||
EuiRange, | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
euiScreenReaderOnly, | ||
} from '../../../../../src'; | ||
|
||
const raw_data: Array<{ [key: string]: string }> = []; | ||
for (let i = 0; i < 5; i++) { | ||
raw_data.push({ | ||
name: `${faker.person.lastName()}, ${faker.person.firstName()}`, | ||
email: faker.internet.email(), | ||
location: `${faker.location.city()}, ${faker.location.country()}`, | ||
date: `${faker.date.past()}`, | ||
amount: faker.commerce.price({ min: 1, max: 1000, dec: 2, symbol: '$' }), | ||
}); | ||
} | ||
const columns = [ | ||
{ id: 'name', displayAsText: 'Name' }, | ||
{ id: 'email', displayAsText: 'Email address' }, | ||
{ id: 'location', displayAsText: 'Location' }, | ||
{ id: 'date', displayAsText: 'Date' }, | ||
{ id: 'amount', displayAsText: 'Amount' }, | ||
]; | ||
|
||
// Custom toolbar renderer | ||
const renderCustomToolbar: EuiDataGridToolbarProps['renderCustomToolbar'] = ({ | ||
hasRoomForGridControls, | ||
columnControl, | ||
columnSortingControl, | ||
displayControl, | ||
fullScreenControl, | ||
keyboardShortcutsControl, | ||
}) => { | ||
const mobileStyles = | ||
!hasRoomForGridControls && | ||
css` | ||
.euiDataGrid__controlBtn .euiButtonEmpty__text { | ||
${euiScreenReaderOnly()} | ||
} | ||
`; | ||
return ( | ||
<EuiFlexGroup | ||
responsive={false} | ||
gutterSize="s" | ||
justifyContent="spaceBetween" | ||
alignItems="center" | ||
css={mobileStyles} | ||
> | ||
<EuiFlexItem grow={false}> | ||
{hasRoomForGridControls && ( | ||
<EuiButtonEmpty size="xs" color="primary"> | ||
Custom left side | ||
</EuiButtonEmpty> | ||
)} | ||
</EuiFlexItem> | ||
|
||
<EuiFlexItem grow={false}> | ||
<EuiFlexGroup responsive={false} gutterSize="s" alignItems="center"> | ||
<EuiFlexItem grow={false}>{columnControl}</EuiFlexItem> | ||
<EuiFlexItem grow={false}>{columnSortingControl}</EuiFlexItem> | ||
<EuiFlexItem grow={false}>{keyboardShortcutsControl}</EuiFlexItem> | ||
<EuiFlexItem grow={false}>{displayControl}</EuiFlexItem> | ||
<EuiFlexItem grow={false}>{fullScreenControl}</EuiFlexItem> | ||
</EuiFlexGroup> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
); | ||
}; | ||
|
||
// Some additional custom settings to show in the Display popover | ||
const AdditionalDisplaySettings = () => { | ||
const [exampleSettingValue, setExampleSettingValue] = useState<number>(10); | ||
|
||
return ( | ||
<EuiFormRow label="Example additional setting" display="columnCompressed"> | ||
<EuiRange | ||
compressed | ||
fullWidth | ||
showInput | ||
min={1} | ||
max={100} | ||
step={1} | ||
value={exampleSettingValue} | ||
data-test-subj="exampleAdditionalSetting" | ||
onChange={(event) => { | ||
setExampleSettingValue(Number(event.currentTarget.value)); | ||
}} | ||
/> | ||
</EuiFormRow> | ||
); | ||
}; | ||
|
||
export default () => { | ||
// Column visibility | ||
const [visibleColumns, setVisibleColumns] = useState(() => | ||
columns.map(({ id }) => id) | ||
); | ||
|
||
// Sorting | ||
const [sortingColumns, setSortingColumns] = useState< | ||
EuiDataGridColumnSortingConfig[] | ||
>([]); | ||
const onSort = useCallback<EuiDataGridSorting['onSort']>((sortingColumns) => { | ||
setSortingColumns(sortingColumns); | ||
}, []); | ||
|
||
return ( | ||
<EuiDataGrid | ||
aria-label="Data grid custom toolbar demo" | ||
columns={columns} | ||
columnVisibility={{ visibleColumns, setVisibleColumns }} | ||
sorting={{ columns: sortingColumns, onSort }} | ||
rowCount={raw_data.length} | ||
renderCellValue={({ rowIndex, columnId }) => raw_data[rowIndex][columnId]} | ||
gridStyle={{ border: 'none', header: 'underline' }} | ||
renderCustomToolbar={renderCustomToolbar} | ||
toolbarVisibility={{ | ||
showDisplaySelector: { | ||
allowResetButton: false, | ||
additionalDisplaySettings: <AdditionalDisplaySettings />, | ||
}, | ||
}} | ||
/> | ||
); | ||
}; |
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
Oops, something went wrong.