-
Notifications
You must be signed in to change notification settings - Fork 861
[EuiDataGrid] Set up ref that exposes focus/popover internal APIs
#5499
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
Merged
cee-chen
merged 9 commits into
elastic:feat/datagrid/5310
from
cee-chen:datagrid/5310/ref
Jan 11, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
43e1c69
Set up types
cee-chen 681f9d9
Set up forwardRef
cee-chen 14401ef
Add setFocusedCell API to returned grid ref obj
cee-chen 1609e45
Add colIndex prop to cell actions
cee-chen b51a614
Add documentation + example + props
cee-chen 3b7c4e2
Add changelog
cee-chen 941f0fa
[PR feedback] Types
constancecchen cd2b235
[PR feedback] Clean up unit test
cee-chen b6ac09f
[Rebase] Tweak useImperativeHandle location
cee-chen 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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { GuideSectionTypes } from '../../components'; | ||
| import { | ||
| EuiCode, | ||
| EuiCodeBlock, | ||
| 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 focus a specific cell within the data grid | ||
| dataGridRef.current.setFocusedCell({ rowIndex, colIndex }); | ||
| `; | ||
|
|
||
| export const DataGridRefExample = { | ||
| title: 'Data grid ref methods', | ||
| sections: [ | ||
| { | ||
| source: [ | ||
| { | ||
| type: GuideSectionTypes.JS, | ||
| 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> | ||
| <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> | ||
| </li> | ||
| </ul> | ||
| <EuiCodeBlock language="jsx">{dataGridRefSnippet}</EuiCodeBlock> | ||
| <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 }, | ||
| }, | ||
| ], | ||
| }; | ||
This file contains hidden or 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,196 @@ | ||
| import React, { useCallback, useMemo, useState, useRef } from 'react'; | ||
| import { fake } from 'faker'; | ||
|
|
||
| import { | ||
| EuiFlexGroup, | ||
| EuiFlexItem, | ||
| EuiSpacer, | ||
| EuiFormRow, | ||
| EuiFieldNumber, | ||
| EuiButton, | ||
| EuiDataGrid, | ||
| EuiModal, | ||
| EuiModalBody, | ||
| EuiModalFooter, | ||
| EuiModalHeader, | ||
| EuiModalHeaderTitle, | ||
| EuiText, | ||
| } from '../../../../src/components/'; | ||
|
|
||
| const raw_data = []; | ||
| for (let i = 1; i < 100; i++) { | ||
| raw_data.push({ | ||
| name: fake('{{name.lastName}}, {{name.firstName}}'), | ||
| email: fake('{{internet.email}}'), | ||
| location: fake('{{address.city}}, {{address.country}}'), | ||
| account: fake('{{finance.account}}'), | ||
| date: fake('{{date.past}}'), | ||
| }); | ||
| } | ||
|
|
||
| export default () => { | ||
| const dataGridRef = useRef(); | ||
|
|
||
| // Modal | ||
| const [isModalVisible, setIsModalVisible] = useState(false); | ||
| const [lastFocusedCell, setLastFocusedCell] = useState({}); | ||
|
|
||
| const closeModal = useCallback(() => { | ||
| setIsModalVisible(false); | ||
| dataGridRef.current.setFocusedCell(lastFocusedCell); // Set the data grid focus back to the cell that opened the modal | ||
| }, [lastFocusedCell]); | ||
|
|
||
| const showModal = useCallback(({ rowIndex, colIndex }) => { | ||
| setIsModalVisible(true); | ||
| setLastFocusedCell({ rowIndex, colIndex }); // Store the cell that opened this modal | ||
| }, []); | ||
|
|
||
| const openModalAction = useCallback( | ||
| ({ Component, rowIndex, colIndex }) => { | ||
| return ( | ||
| <Component | ||
| onClick={() => showModal({ rowIndex, colIndex })} | ||
| iconType="faceHappy" | ||
| aria-label="Open modal" | ||
| > | ||
| Open modal | ||
| </Component> | ||
| ); | ||
| }, | ||
| [showModal] | ||
| ); | ||
|
|
||
| // Columns | ||
| const columns = useMemo( | ||
| () => [ | ||
| { | ||
| id: 'name', | ||
| displayAsText: 'Name', | ||
| cellActions: [openModalAction], | ||
| }, | ||
| { | ||
| id: 'email', | ||
| displayAsText: 'Email address', | ||
| initialWidth: 130, | ||
| cellActions: [openModalAction], | ||
| }, | ||
| { | ||
| id: 'location', | ||
| displayAsText: 'Location', | ||
| cellActions: [openModalAction], | ||
| }, | ||
| { | ||
| id: 'account', | ||
| displayAsText: 'Account', | ||
| cellActions: [openModalAction], | ||
| }, | ||
| { | ||
| id: 'date', | ||
| displayAsText: 'Date', | ||
| cellActions: [openModalAction], | ||
| }, | ||
| ], | ||
| [openModalAction] | ||
| ); | ||
|
|
||
| // Column visibility | ||
| const [visibleColumns, setVisibleColumns] = useState(() => | ||
| columns.map(({ id }) => id) | ||
| ); | ||
|
|
||
| // Pagination | ||
| const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 25 }); | ||
| const onChangePage = useCallback( | ||
| (pageIndex) => | ||
| setPagination((pagination) => ({ ...pagination, pageIndex })), | ||
| [] | ||
| ); | ||
|
|
||
| // Manual cell focus | ||
| const [rowIndexAction, setRowIndexAction] = useState(0); | ||
| const [colIndexAction, setColIndexAction] = useState(0); | ||
|
|
||
| return ( | ||
| <> | ||
| <EuiFlexGroup alignItems="flexEnd" gutterSize="s" style={{ width: 500 }}> | ||
| <EuiFlexItem> | ||
| <EuiFormRow label="Row index"> | ||
| <EuiFieldNumber | ||
| min={0} | ||
| max={24} | ||
| value={rowIndexAction} | ||
| onChange={(e) => setRowIndexAction(Number(e.target.value))} | ||
| compressed | ||
| /> | ||
| </EuiFormRow> | ||
| </EuiFlexItem> | ||
| <EuiFlexItem> | ||
| <EuiFormRow label="Column index"> | ||
| <EuiFieldNumber | ||
| min={0} | ||
| max={4} | ||
| value={colIndexAction} | ||
| onChange={(e) => setColIndexAction(Number(e.target.value))} | ||
| compressed | ||
| /> | ||
| </EuiFormRow> | ||
| </EuiFlexItem> | ||
| <EuiFlexItem> | ||
| <EuiButton | ||
| size="s" | ||
| onClick={() => | ||
| dataGridRef.current.setFocusedCell({ | ||
| rowIndex: rowIndexAction, | ||
| colIndex: colIndexAction, | ||
| }) | ||
| } | ||
| > | ||
| Set cell focus | ||
| </EuiButton> | ||
| </EuiFlexItem> | ||
| </EuiFlexGroup> | ||
| <EuiSpacer /> | ||
|
|
||
| <EuiDataGrid | ||
| aria-label="Data grid demo" | ||
| columns={columns} | ||
| columnVisibility={{ visibleColumns, setVisibleColumns }} | ||
| rowCount={raw_data.length} | ||
| renderCellValue={({ rowIndex, columnId }) => | ||
| raw_data[rowIndex][columnId] | ||
| } | ||
| pagination={{ | ||
| ...pagination, | ||
| pageSizeOptions: [25], | ||
| onChangePage: onChangePage, | ||
| }} | ||
| height={400} | ||
| ref={dataGridRef} | ||
| /> | ||
| {isModalVisible && ( | ||
| <EuiModal onClose={closeModal} style={{ width: 500 }}> | ||
| <EuiModalHeader> | ||
| <EuiModalHeaderTitle> | ||
| <h2>Example modal</h2> | ||
| </EuiModalHeaderTitle> | ||
| </EuiModalHeader> | ||
|
|
||
| <EuiModalBody> | ||
| <EuiText> | ||
| <p> | ||
| When closed, this modal should re-focus into the cell that | ||
| toggled it. | ||
| </p> | ||
| </EuiText> | ||
| </EuiModalBody> | ||
|
|
||
| <EuiModalFooter> | ||
| <EuiButton onClick={closeModal} fill> | ||
| Close | ||
| </EuiButton> | ||
| </EuiModalFooter> | ||
| </EuiModal> | ||
| )} | ||
| </> | ||
| ); | ||
| }; |
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.