-
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.
test(Datagrid): add test coverage for new hook
- Loading branch information
1 parent
370d06e
commit 4c3a040
Showing
12 changed files
with
147 additions
and
60 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
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
/** | ||
* 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 = 0, | ||
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`); | ||
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.