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(Upload): avoid scrolling when removing file from list #4380

Merged
merged 1 commit into from
Dec 11, 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
30 changes: 12 additions & 18 deletions packages/dnb-eufemia/src/components/upload/UploadFileListCell.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from 'react'
import React, { useCallback, useRef } from 'react'
import classnames from 'classnames'

// Components
Expand All @@ -25,7 +25,7 @@ import {
import { UploadFile, UploadFileNative } from './types'

// Shared
import { getPreviousSibling, warn } from '../../shared/component-helper'
import { getPreviousSibling } from '../../shared/component-helper'
import useUpload from './useUpload'
import { getFileTypeFromExtension } from './UploadVerify'
import UploadFileLink from './UploadFileListLink'
Expand Down Expand Up @@ -96,26 +96,20 @@ const UploadFileListCell = ({
const cellRef = useRef<HTMLLIElement>()
const exists = useExistsHighlight(id, file)

const handleDisappearFocus = () => {
try {
const cellElement = cellRef.current
const focusElement = getPreviousSibling(
'.dnb-upload',
cellElement
).querySelector(
'.dnb-upload__file-input-button'
) as HTMLButtonElement
focusElement.focus()
} catch (e) {
warn(e)
}
}
const handleDisappearFocus = useCallback(() => {
const cellElement = cellRef.current
const focusElement = getPreviousSibling(
'.dnb-upload',
cellElement
)?.querySelector('.dnb-upload__file-input-button') as HTMLButtonElement
focusElement?.focus({ preventScroll: true })
}, [cellRef])

const onDeleteHandler = () => {
const onDeleteHandler = useCallback(() => {
handleDisappearFocus()

onDelete()
}
}, [handleDisappearFocus, onDelete])

return (
<li
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ describe('UploadFileListCell', () => {
it('renders the delete button', () => {
render(<UploadFileListCell {...defaultProps} />)

const element = screen.getByRole('button')
const element = document.querySelector('button')

expect(element).toBeInTheDocument()
})
Expand All @@ -316,15 +316,15 @@ describe('UploadFileListCell', () => {
/>
)

const element = screen.getByRole('button')
const element = document.querySelector('button')

expect(element.textContent).toMatch(deleteButtonText)
})

it('renders button as tertiary', () => {
render(<UploadFileListCell {...defaultProps} />)

const element = screen.getByRole('button')
const element = document.querySelector('button')

expect(element.className).toMatch('dnb-button--tertiary')
})
Expand Down Expand Up @@ -357,7 +357,7 @@ describe('UploadFileListCell', () => {
onDelete={onDelete}
/>
)
const element = screen.getByRole('button')
const element = document.querySelector('button')

fireEvent.click(element)

Expand All @@ -374,7 +374,7 @@ describe('UploadFileListCell', () => {
}}
/>
)
const element = screen.getByRole('button')
const element = document.querySelector('button')

expect(element).toBeDisabled()
})
Expand All @@ -394,5 +394,45 @@ describe('UploadFileListCell', () => {
document.querySelector('.dnb-progress-indicator')
).not.toBeInTheDocument()
})

it('should set focus when clicking the delete button', () => {
const MockComponent = () => {
return (
<div className="dnb-upload">
<UploadFileListCell
{...defaultProps}
uploadFile={{
file: createMockFile('file.png', 100, 'image/png'),
}}
/>
<button className="dnb-upload__file-input-button">
Mock button
</button>
</div>
)
}
const { rerender } = render(<MockComponent />)

const removeButton = document.querySelector('button')
const uploadButton = document.querySelector(
'.dnb-upload__file-input-button'
)

expect(document.body).toHaveFocus()

fireEvent.click(removeButton)
expect(uploadButton).toHaveFocus()

const focus = jest.fn()
jest
.spyOn(HTMLElement.prototype, 'focus')
.mockImplementationOnce(focus)

rerender(<MockComponent />)

fireEvent.click(removeButton)
expect(focus).toHaveBeenCalledTimes(1)
expect(focus).toHaveBeenCalledWith({ preventScroll: true })
})
})
})
Loading