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] adding custom toolbar for additional controls on the datagrid #7148

Closed
wants to merge 2 commits into from
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
170 changes: 170 additions & 0 deletions src-docs/src/views/datagrid/advanced/custom_toolbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import React, { useCallback, useState } from 'react';
import { faker } from '@faker-js/faker';

import {
EuiDataGrid,
EuiDataGridColumnCellActionProps,
EuiButtonIcon,
EuiDataGridPaginationProps,
EuiDataGridSorting,
EuiDataGridColumnSortingConfig,
EuiPopover,
EuiDataGridCustomToolbarProps,
EuiFormRow,
EuiRange,
} from '../../../../../src';

const raw_data: Array<{ [key: string]: string }> = [];
for (let i = 1; i < 100; i++) {
raw_data.push({
name: `${faker.name.lastName()}, ${faker.name.firstName()}`,
email: faker.internet.email(),
location: `${faker.address.city()}, ${faker.address.country()}`,
date: `${faker.date.past()}`,
amount: faker.commerce.price(1, 1000, 2, '$'),
});
}

const columns = [
{
id: 'name',
displayAsText: 'Name',
cellActions: [
({ Component }: EuiDataGridColumnCellActionProps) => (
<Component
onClick={() => alert('action')}
iconType="faceHappy"
aria-label="Some action"
>
Some action
</Component>
),
],
},
{
id: 'email',
displayAsText: 'Email address',
initialWidth: 130,
},
{
id: 'location',
displayAsText: 'Location',
},
{
id: 'date',
displayAsText: 'Date',
},
{
id: 'amount',
displayAsText: 'Amount',
},
];

export default () => {
// Column visibility
const [visibleColumns, setVisibleColumns] = useState(() =>
columns.map(({ id }) => id)
);

// Pagination
const [pagination, setPagination] = useState({ pageIndex: 0 });
const onChangePage = useCallback<EuiDataGridPaginationProps['onChangePage']>(
(pageIndex) => {
setPagination((pagination) => ({ ...pagination, pageIndex }));
},
[]
);
const onChangePageSize = useCallback<
EuiDataGridPaginationProps['onChangeItemsPerPage']
>((pageSize) => {
setPagination((pagination) => ({ ...pagination, pageSize }));
}, []);

// Sorting
const [sortingColumns, setSortingColumns] = useState<
EuiDataGridColumnSortingConfig[]
>([]);
const onSort = useCallback<EuiDataGridSorting['onSort']>((sortingColumns) => {
setSortingColumns(sortingColumns);
}, []);
const [isDisplaySelectorOpen, setIsDisplaySelectorOpen] = useState(false);

// Custom toolbar body renderer
const RenderCustomToolbar = ({
fullScreenSelector,
keyboardShortcuts,
rowHeightsControls,
densityControls,
columnSelector,
columnSorting,
}: EuiDataGridCustomToolbarProps) => {
return (
<div className="euiDataGrid__controls" data-test-subj="dataGridControls">
<div className="euiDataGrid__leftControls">
Always look at the left side of grid!
</div>
<div className="euiDataGrid__rightControls">
{fullScreenSelector}
{keyboardShortcuts}
<EuiPopover
button={
<EuiButtonIcon
iconType="controlsHorizontal"
aria-label="Custom grid controls"
onClick={() => setIsDisplaySelectorOpen(true)}
/>
}
isOpen={isDisplaySelectorOpen}
data-test-subj="dataGridDisplaySelectorPopover"
closePopover={() => setIsDisplaySelectorOpen(false)}
anchorPosition="downRight"
panelPaddingSize="s"
panelClassName="euiDataGrid__displayPopoverPanel"
>
{rowHeightsControls}
{densityControls}

<EuiFormRow label="Random Sample Size" display="columnCompressed">
<EuiRange
compressed
fullWidth
showInput
min={1}
max={100}
step={1}
value={10}
data-test-subj="randomSampleSize"
/>
</EuiFormRow>
</EuiPopover>
{columnSorting}
{columnSelector}
</div>
</div>
);
};

return (
<>
<EuiDataGrid
aria-label="Data grid custom body renderer demo"
columns={columns}
columnVisibility={{ visibleColumns, setVisibleColumns }}
sorting={{ columns: sortingColumns, onSort }}
inMemory={{ level: 'sorting' }}
pagination={{
...pagination,
onChangePage: onChangePage,
onChangeItemsPerPage: onChangePageSize,
}}
rowCount={raw_data.length}
renderCellValue={({ rowIndex, columnId }) =>
raw_data[rowIndex][columnId]
}
renderCustomToolbar={RenderCustomToolbar}
height={undefined}
gridStyle={{ border: 'none', header: 'underline' }}
/>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EuiCallOut,
EuiTitle,
EuiLink,
EuiDataGridCustomToolbarProps,
} from '../../../../../src/components';

import {
Expand Down Expand Up @@ -36,7 +37,9 @@ dataGridRef.current.closeCellPopover();
`;

import CustomRenderer from './custom_renderer';
import CustomToolbarRenderer from './custom_toolbar';
const customRendererSource = require('!!raw-loader!./custom_renderer');
const customToolbarSource = require('!!raw-loader!./custom_toolbar');
const customRendererSnippet = `const CustomGridBody = ({ visibleColumns, visibleRowData, Cell }) => {
const visibleRows = raw_data.slice(
visibleRowData.startRow,
Expand Down Expand Up @@ -253,5 +256,26 @@ export const DataGridAdvancedExample = {
snippet: customRendererSnippet,
props: { EuiDataGridCustomBodyProps },
},
{
title: 'Custom toolbar renderer',
source: [
{
type: GuideSectionTypes.TSX,
code: customToolbarSource,
},
],
text: (
<>
<p>
For advanced use cases, the <EuiCode>renderCustomToolbar</EuiCode>{' '}
prop may be used to take complete control over rendering the
toolbar. This may be useful where custom row layouts (e.g., all
button on the right side) are required.
</p>
</>
),
demo: <CustomToolbarRenderer />,
props: { EuiDataGridCustomToolbarProps },
},
],
};
21 changes: 21 additions & 0 deletions src-docs/src/views/datagrid/basics/datagrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import {
EuiFlyoutBody,
EuiFlyoutFooter,
EuiFlyoutHeader,
EuiFormRow,
EuiLink,
EuiModal,
EuiModalBody,
EuiModalFooter,
EuiModalHeader,
EuiModalHeaderTitle,
EuiPopover,
EuiRange,
EuiScreenReaderOnly,
EuiText,
EuiTitle,
Expand Down Expand Up @@ -406,12 +408,31 @@ export default () => {
console.log(eventData);
});

const customDisplaySetting = (
<EuiFormRow label="Random Sample Size" display="columnCompressed">
<EuiRange
compressed
fullWidth
showInput
min={1}
max={100}
step={1}
value={10}
data-test-subj="randomSampleSize"
/>
</EuiFormRow>
);
return (
<DataContext.Provider value={raw_data}>
<EuiDataGrid
aria-label="Data grid demo"
columns={columns}
columnVisibility={{ visibleColumns, setVisibleColumns }}
toolbarVisibility={{
showDisplaySelector: {
additionalDisplaySettings: customDisplaySetting,
},
}}
trailingControlColumns={trailingControlColumns}
rowCount={raw_data.length}
renderCellValue={RenderCellValue}
Expand Down
Loading