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

fix(Table.Accordion): prevent accordion from opening on label click #3228

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export function useTableAccordion({
* https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement
*/
target.tagName !== 'INPUT' &&
target.tagName !== 'LABEL' &&
/**
* Let the user select text,
* without triggering the accordion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ describe('TableAccordion', () => {
<tbody>
<Tr>
<Td>
<input type="text" />
<label>
Label
<input type="text" />
</label>
<button>button</button>
</Td>
<Td.AccordionContent>accordion content</Td.AccordionContent>
Expand All @@ -321,6 +324,7 @@ describe('TableAccordion', () => {
)

const trElement = document.querySelector('tr')
const labelElement = document.querySelector('label')
const inputElem = trElement.querySelector('input')
const buttonElem = trElement.querySelector('button')

Expand Down Expand Up @@ -350,6 +354,12 @@ describe('TableAccordion', () => {
'dnb-table__tr--expanded'
)

fireEvent.click(labelElement)

expect(Array.from(trElement.classList)).not.toContain(
'dnb-table__tr--expanded'
)

fireEvent.click(buttonElem)

expect(Array.from(trElement.classList)).toContain(
Expand Down Expand Up @@ -390,6 +400,22 @@ describe('TableAccordion', () => {
'dnb-table__tr--expanded'
)

jest
.spyOn(document, 'activeElement', 'get')
.mockReturnValue(labelElement)

fireEvent.keyDown(labelElement, { keyCode: 13 }) // enter

expect(Array.from(trElement.classList)).not.toContain(
'dnb-table__tr--expanded'
)

fireEvent.keyDown(labelElement, { keyCode: 32 }) // space

expect(Array.from(trElement.classList)).not.toContain(
'dnb-table__tr--expanded'
)

jest.spyOn(document, 'activeElement', 'get').mockReturnValue(null)
})

Expand Down
Loading