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] Introduce renderCustomToolbar, additionalDisplaySettings and allowResetButton customization options #7190

Merged
merged 25 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e14fb4c
adding api for additional display controls on the datagrid
timductive Aug 30, 2023
488e365
[EuiDataGrid] Introduce a renderCustomToolbar render prop (#7150)
kertal Sep 1, 2023
d87d061
Refactor how `renderCustomToolbar` works
jughosta Sep 14, 2023
8302ccd
Update tests and docs
jughosta Sep 14, 2023
f81e226
Update example and docs
jughosta Sep 14, 2023
3061aef
Add a changelog
jughosta Sep 14, 2023
228220b
Merge branch 'main' into render-custom-toolbar
jughosta Sep 14, 2023
b74c6f4
Merge branch 'main' into render-custom-toolbar
jughosta Sep 19, 2023
809a229
Update changelog
jughosta Sep 19, 2023
bae0d2e
Update allowResetButton logic
jughosta Sep 19, 2023
9d93d36
Update src/components/datagrid/data_grid_types.ts
jughosta Sep 19, 2023
315cdca
Update src/components/datagrid/data_grid_types.ts
jughosta Sep 19, 2023
bbf2d18
Rename to `EuiDataGridCustomToolbarProps`
jughosta Sep 19, 2023
67d6eed
Reduce the number of available custom toolbar props
jughosta Sep 19, 2023
b7d012a
Merge branch 'render-custom-toolbar' of github.com:jughosta/eui into …
jughosta Sep 19, 2023
84eb5fd
Simplify return type of `renderCustomToolbar`
jughosta Sep 19, 2023
8500388
Add tests
jughosta Sep 20, 2023
747577a
Update docs
jughosta Sep 20, 2023
b59347e
Merge branch 'main' into render-custom-toolbar
jughosta Sep 20, 2023
29c5061
Merge branch 'main' into render-custom-toolbar
jughosta Sep 22, 2023
bb177d3
Merge remote-tracking branch 'upstream/main' into render-custom-toolbar
cee-chen Sep 24, 2023
47f061c
[PR feedback] very minor copy tweaks
cee-chen Sep 24, 2023
6643774
[display_selector] Minor test cleanup
cee-chen Sep 24, 2023
2fa8e08
[PR feedback] Move `additionalDisplaySettings` docs example from main…
cee-chen Sep 24, 2023
ab6e553
Final docs pass
cee-chen Sep 24, 2023
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
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
9 changes: 6 additions & 3 deletions src-docs/src/views/datagrid/toolbar/_grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const DataGridStyle = ({
showFullScreenSelector,
allowDensity,
allowRowHeight,
allowResetButton,
allowHideColumns,
allowOrderingColumns,
}) => {
Expand Down Expand Up @@ -98,13 +99,15 @@ const DataGridStyle = ({
const toggleDisplaySelector = useMemo(() => {
if (
showDisplaySelector === true &&
(allowDensity === false || allowRowHeight === false)
(allowDensity === false ||
allowRowHeight === false ||
allowResetButton === false)
) {
return { allowDensity, allowRowHeight };
return { allowDensity, allowRowHeight, allowResetButton };
} else {
return showDisplaySelector;
}
}, [showDisplaySelector, allowDensity, allowRowHeight]);
}, [showDisplaySelector, allowDensity, allowRowHeight, allowResetButton]);

const toolbarVisibilityOptions = {
showColumnSelector: toggleColumnSelector,
Expand Down
2 changes: 2 additions & 0 deletions src-docs/src/views/datagrid/toolbar/_props.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const gridSnippets = {
showDisplaySelector: `showDisplaySelector: {
allowDensity: false;
allowRowHeight: false;
allowResetButton: false;
additionalDisplaySettings: <EuiButtonEmpty size="xs" />;
}`,
showSortSelector: 'showSortSelector: false',
showFullScreenSelector: 'showFullScreenSelector: false',
Expand Down
163 changes: 163 additions & 0 deletions src-docs/src/views/datagrid/toolbar/datagrid_custom_toolbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import React, { useCallback, useState } from 'react';
import { faker } from '@faker-js/faker';

import {
EuiDataGrid,
EuiDataGridColumnCellActionProps,
EuiDataGridPaginationProps,
EuiDataGridSorting,
EuiDataGridColumnSortingConfig,
EuiDataGridToolbarProps,
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 [exampleSettingValue, setExampleSettingValue] = useState<number>(10);

// Some additional custom settings to show in Display popover
const additionalDisplaySettings = (
<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>
);

// Custom toolbar renderer
const renderCustomToolbar: EuiDataGridToolbarProps['renderCustomToolbar'] = ({
hasRoomForGridControls,
columnControl,
columnSortingControl,
displayControl,
fullScreenControl,
keyboardShortcutsControl,
}) => {
return {
left: hasRoomForGridControls ? 'Custom left side' : null,
right: (
<>
{columnControl}
{columnSortingControl}
{keyboardShortcutsControl}
{displayControl}
{fullScreenControl}
</>
),
};
};

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' }}
toolbarVisibility={{
showDisplaySelector: {
allowResetButton: false,
additionalDisplaySettings,
},
}}
/>
</>
);
};
34 changes: 34 additions & 0 deletions src-docs/src/views/datagrid/toolbar/datagrid_toolbar_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const dataGridToolbarVisibilitySource = require('!!raw-loader!./_grid');
import DataGridControls from './additional_controls';
const dataGridControlsSource = require('!!raw-loader!./additional_controls');

import DataGridCustomToolbar from './datagrid_custom_toolbar';
const dataGridCustomToolbarSource = require('!!raw-loader!./datagrid_custom_toolbar');

import ToolbarPropsTable from './_props';

import {
Expand All @@ -17,6 +20,8 @@ import {
EuiDataGridToolBarVisibilityColumnSelectorOptions,
EuiDataGridToolBarVisibilityDisplaySelectorOptions,
EuiDataGridToolBarAdditionalControlsLeftOptions,
EuiDataGridToolbarProps,
EuiDataGridCustomToolbarOptions,
} from '!!prop-loader!../../../../../src/components/datagrid/data_grid_types';

/* eslint-disable local/css-logical-properties */
Expand Down Expand Up @@ -185,6 +190,35 @@ export const DataGridToolbarExample = {
},
demo: <DataGridControls />,
},
{
title: 'Custom toolbar layout',
source: [
{
type: GuideSectionTypes.TSX,
code: dataGridCustomToolbarSource,
},
],
text: (
<>
<p>
For advanced use cases, the <EuiCode>renderCustomToolbar</EuiCode>{' '}
prop for <EuiCode>EuiDataGrid</EuiCode> may be used to take complete
control over the placement of the toolbar controls (by returning{' '}
<EuiCode>left</EuiCode> and <EuiCode>right</EuiCode>) or the whole
toolbar element (by returning a react element). This may be useful
where custom layout (e.g., all buttons on the right side) are
required. The default individual controls will be available as
function parameters.
</p>
</>
),
demo: <DataGridCustomToolbar />,
props: {
EuiDataGridToolbarProps,
EuiDataGridCustomToolbarOptions,
},
// TODO: add a snippet
},
{
title: 'Toolbar props',
text: (
Expand Down
11 changes: 11 additions & 0 deletions src-docs/src/views/datagrid/toolbar/visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const DataGrid = () => {
const [showDisplaySelector, setShowDisplaySelector] = useState(true);
const [allowDensity, setAllowDensity] = useState(true);
const [allowRowHeight, setAllowRowHeight] = useState(true);
const [allowResetButton, setAllowResetButton] = useState(true);
const [showColumnSelector, setShowColumnSelector] = useState(true);
const [allowHideColumns, setAllowHideColumns] = useState(true);
const [allowOrderingColumns, setAllowOrderingColumns] = useState(true);
Expand Down Expand Up @@ -76,6 +77,9 @@ const DataGrid = () => {
const onAllowRowHeightChange = (optionId) => {
setAllowRowHeight(optionId === 'true');
};
const onAllowResetButtonChange = (optionId) => {
setAllowResetButton(optionId === 'true');
};

const onShowKeyboardShortcutsChange = (optionId) => {
setShowKeyboardShortcuts(optionId === 'true');
Expand Down Expand Up @@ -234,6 +238,12 @@ const DataGrid = () => {
onChange: onAllowRowHeightChange,
})}
</li>
<li>
{createItem('Show reset button', {
idSelected: allowResetButton.toString(),
onChange: onAllowResetButtonChange,
})}
</li>
</ul>
)}

Expand Down Expand Up @@ -266,6 +276,7 @@ const DataGrid = () => {
showFullScreenSelector={showFullScreenSelector}
allowDensity={allowDensity}
allowRowHeight={allowRowHeight}
allowResetButton={allowResetButton}
allowHideColumns={allowHideColumns}
allowOrderingColumns={allowOrderingColumns}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ exports[`EuiDataGrid rendering renders additional toolbar controls 1`] = `
aria-hidden="true"
class="euiButtonIcon__icon"
color="inherit"
data-euiicon-type="tableDensityNormal"
data-euiicon-type="controlsHorizontal"
/>
</button>
</span>
Expand Down Expand Up @@ -1037,7 +1037,7 @@ exports[`EuiDataGrid rendering renders control columns 1`] = `
aria-hidden="true"
class="euiButtonIcon__icon"
color="inherit"
data-euiicon-type="tableDensityNormal"
data-euiicon-type="controlsHorizontal"
/>
</button>
</span>
Expand Down Expand Up @@ -1841,7 +1841,7 @@ exports[`EuiDataGrid rendering renders custom column headers 1`] = `
aria-hidden="true"
class="euiButtonIcon__icon"
color="inherit"
data-euiicon-type="tableDensityNormal"
data-euiicon-type="controlsHorizontal"
/>
</button>
</span>
Expand Down Expand Up @@ -2321,7 +2321,7 @@ exports[`EuiDataGrid rendering renders with common and div attributes 1`] = `
aria-hidden="true"
class="euiButtonIcon__icon"
color="inherit"
data-euiicon-type="tableDensityNormal"
data-euiicon-type="controlsHorizontal"
/>
</button>
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports[`useDataGridDisplaySelector displaySelector renders a toolbar button/pop
className="euiDataGrid__controlBtn"
color="text"
data-test-subj="dataGridDisplaySelectorButton"
iconType="tableDensityNormal"
iconType="controlsHorizontal"
onClick={[Function]}
size="xs"
/>
Expand Down
Loading
Loading