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] Allow consuming applications to control various internal state via ref #5590

Merged
merged 17 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## [`main`](https://github.com/elastic/eui/tree/main)

No public interface changes since `46.2.0`.
- Added the ability to control internal `EuiDataGrid` fullscreen, cell focus, and cell popover state via the `ref` prop ([#5590](https://github.com/elastic/eui/pull/5590))

## [`46.2.0`](https://github.com/elastic/eui/tree/v46.2.0)

Expand Down
2 changes: 2 additions & 0 deletions src-docs/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import { DataGridControlColumnsExample } from './views/datagrid/datagrid_control
import { DataGridFooterRowExample } from './views/datagrid/datagrid_footer_row_example';
import { DataGridVirtualizationExample } from './views/datagrid/datagrid_virtualization_example';
import { DataGridRowHeightOptionsExample } from './views/datagrid/datagrid_height_options_example';
import { DataGridRefExample } from './views/datagrid/datagrid_ref_example';

import { DatePickerExample } from './views/date_picker/date_picker_example';

Expand Down Expand Up @@ -489,6 +490,7 @@ const navigation = [
DataGridFooterRowExample,
DataGridVirtualizationExample,
DataGridRowHeightOptionsExample,
DataGridRefExample,
TableExample,
TableInMemoryExample,
].map((example) => createExample(example)),
Expand Down
17 changes: 17 additions & 0 deletions src-docs/src/views/datagrid/datagrid_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
EuiDataGridRowHeightsOptions,
EuiDataGridCellValueElementProps,
EuiDataGridSchemaDetector,
EuiDataGridRefProps,
} from '!!prop-loader!../../../../src/components/datagrid/data_grid_types';

const gridSnippet = `
Expand Down Expand Up @@ -164,6 +165,8 @@ const gridSnippet = `
);
},
}}
// Optional. For advanced control of internal data grid popover/focus state, passes back an object of API methods
ref={dataGridRef}
/>
`;

Expand Down Expand Up @@ -323,6 +326,19 @@ const gridConcepts = [
</span>
),
},
{
title: 'ref',
description: (
<span>
Passes back an object of internal <strong>EuiDataGridRefProps</strong>{' '}
methods for advanced control of data grid popover/focus state. See{' '}
<Link to="/tabular-content/data-grid-ref-methods">
Data grid ref methods
</Link>{' '}
for more details and examples.
</span>
),
},
];

export const DataGridExample = {
Expand Down Expand Up @@ -414,6 +430,7 @@ export const DataGridExample = {
EuiDataGridToolBarAdditionalControlsLeftOptions,
EuiDataGridPopoverContentProps,
EuiDataGridRowHeightsOptions,
EuiDataGridRefProps,
},
demo: (
<Fragment>
Expand Down
110 changes: 110 additions & 0 deletions src-docs/src/views/datagrid/datagrid_ref_example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import React from 'react';

import { GuideSectionTypes } from '../../components';
import { EuiCode, EuiSpacer, EuiCallOut } from '../../../../src/components';

import { EuiDataGridRefProps } from '!!prop-loader!../../../../src/components/datagrid/data_grid_types';
import DataGridRef from './ref';
const dataGridRefSource = require('!!raw-loader!./ref');
const dataGridRefSnippet = `const dataGridRef = useRef();
<EuiDataGrid ref={dataGridRef} {...props} />

// Mnaually toggle the data grid's full screen state
dataGridRef.current.setIsFullScreen(true);

// Mnaually focus a specific cell within the data grid
dataGridRef.current.setFocusedCell({ rowIndex, colIndex });

// Manually opens the popover of a specified cell within the data grid
dataGridRef.current.openCellPopover({ rowIndex, colIndex });

// Close any open cell popover
dataGridRef.current.closeCellPopover();
`;

export const DataGridRefExample = {
title: 'Data grid ref methods',
sections: [
{
source: [
{
type: GuideSectionTypes.TSX,
code: dataGridRefSource,
},
],
text: (
<>
<p>
For advanced use cases, and particularly for data grids that manage
associated modals/flyouts and need to manually control their grid
cell popovers & focus states, we expose certain internal methods via
the <EuiCode>ref</EuiCode> prop of EuiDataGrid. These methods are:
</p>
<ul>
<li>
<p>
<EuiCode>setIsFullScreen(isFullScreen)</EuiCode> - controls the
full screen state of the data grid. Accepts a true/false boolean
flag.
</p>
</li>
<li>
<EuiCode>setFocusedCell({'{ rowIndex, colIndex }'})</EuiCode> -
focuses the specified cell in the grid.
<EuiSpacer size="s" />
<EuiCallOut
iconType="accessibility"
title="Using this method is an accessibility requirement if your data
grid toggles a modal or flyout."
>
Your modal or flyout should restore focus into the grid on close
to prevent keyboard or screen reader users from being stranded.
</EuiCallOut>
<EuiSpacer size="m" />
</li>
<li>
<EuiCode>openCellPopover({'{ rowIndex, colIndex }'})</EuiCode> -
opens the specified cell&apos;s popover contents.
<EuiSpacer size="s" />
<EuiCallOut iconType="mapMarker" title="Handling cell location">
When using <EuiCode>setFocusedCell</EuiCode> or{' '}
<EuiCode>openCellPopover</EuiCode>, keep in mind:
<ul>
<li>
colIndex is affected by the user reordering or hiding
columns.
</li>
<li>
If the passed cell indices are outside the data grid&apos;s
total row count or visible column count, an error will be
thrown.
</li>
<li>
If the data grid is paginated or sorted, the grid will
handle automatically finding specified row index&apos;s
correct location for you.
</li>
</ul>
</EuiCallOut>
<EuiSpacer size="m" />
</li>
<li>
<p>
<EuiCode>closeCellPopover()</EuiCode> - closes any currently
open cell popover.
</p>
</li>
</ul>
<p>
The below example shows how to use the internal APIs for a data grid
that opens a modal via cell actions.
</p>
</>
),
components: { DataGridRef },
demo: <DataGridRef />,
snippet: dataGridRefSnippet,
props: { EuiDataGridRefProps },
},
],
};
Loading