-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Datagrid): expandable/nested review fixes, custom hook for keepin…
…g expander focus (v1) (#3864) * chore(Datagrid): keep expander focus, remove feat flags * chore: update snapshot * chore: remove all padding from expanded area td
- Loading branch information
1 parent
7994c5c
commit 0048ef8
Showing
13 changed files
with
147 additions
and
70 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
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
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
50 changes: 50 additions & 0 deletions
50
packages/ibm-products/src/components/Datagrid/useFocusRowExpander.js
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,50 @@ | ||
/** | ||
* Copyright IBM Corp. 2023, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { useEffect } from 'react'; | ||
|
||
// Focuses the row expander after a nested/expandable row state change. | ||
// We have to add this workaround because react-table is re-rendering the entire row | ||
// which removes the focus from the expander and interrupts the keyboard navigation | ||
// flow. | ||
export const useFocusRowExpander = ({ | ||
instance, | ||
lastExpandedRowIndex, | ||
blockClass, | ||
activeElement, | ||
}) => { | ||
useEffect(() => { | ||
// We need to return at this point so that the focus is not stolen from | ||
// other interactive elements in the Datagrid | ||
if (!activeElement.classList.contains(`${blockClass}__row-expander`)) { | ||
return; | ||
} | ||
const tableId = instance?.tableId; | ||
const rowElements = document.querySelectorAll(`#${tableId} tbody tr`); | ||
if (lastExpandedRowIndex) { | ||
const rowElementsArray = Array.from(rowElements); | ||
const activeRow = rowElementsArray.filter((r) => { | ||
if (r.getAttribute('data-nested-row-id') === lastExpandedRowIndex) { | ||
return r; | ||
} | ||
return null; | ||
}); | ||
if (activeRow.length) { | ||
const rowExpander = activeRow[0].querySelector( | ||
`.${blockClass}__row-expander` | ||
); | ||
rowExpander.focus(); | ||
} | ||
} | ||
}, [ | ||
instance?.tableId, | ||
instance?.expandedRows, | ||
lastExpandedRowIndex, | ||
blockClass, | ||
activeElement, | ||
]); | ||
}; |
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
Oops, something went wrong.