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): display files without anchor when their size is not given #4390

Merged
merged 4 commits into from
Dec 13, 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 @@ -524,3 +524,29 @@ export const OnFileClick = () => {
</ComponentBox>
)
}

export const DisplayFileAsNonClickable = () => {
return (
<ComponentBox
hideCode
scope={{ createMockFile }}
data-visual-test="upload-value-display-file-as-non-clickable"
>
<Value.Upload
label="Label text"
value={[
{
file: createMockFile('35217511.jpg', 0, 'image/png'),
exists: false,
id: '1',
},
{
file: createMockFile('1501870.jpg', undefined, 'image/png'),
exists: false,
id: '2',
},
]}
/>
</ComponentBox>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ import * as Examples from './Examples'

<Examples.OnFileClick />

### Display files as non-clickable

When file size is 0 or not given (`new File([], name, { type })`), the file is displayed as a span instead of an anchor. How ever, when `onFileClick` is given, the file will be clickable as a button.

<Examples.DisplayFileAsNonClickable />

<VisibleWhenVisualTest>
<Examples.LabelAndValueOnFileClick />
<Examples.ListVariantsOnFileClick />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const UploadFileListCell = ({
const { file, errorMessage, isLoading } = uploadFile
const hasWarning = errorMessage != null

const imageUrl = URL.createObjectURL(file)
const imageUrl = file?.size > 0 ? URL.createObjectURL(file) : null
const cellRef = useRef<HTMLLIElement>()
const exists = useExistsHighlight(id, file)

Expand Down
13 changes: 10 additions & 3 deletions packages/dnb-eufemia/src/components/upload/UploadFileListLink.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import React from 'react'

import classNames from 'classnames'
import Anchor from '../../components/Anchor'
import Button from '../button/Button'
import Span from '../../elements/Span'
import { SpacingProps } from '../space/types'
import { createSpacingClasses } from '../space/SpacingUtils'
import classNames from 'classnames'

export type UploadFileLinkProps = UploadFileAnchorProps &
UploadFileButtonProps

export const UploadFileLink = (props: UploadFileLinkProps) => {
const { onClick, text, href, download, ...rest } = props
if (onClick)

if (!onClick && !href) {
return <Span {...rest}>{text}</Span>
}

if (onClick) {
return <UploadFileButton text={text} onClick={onClick} {...rest} />
}

return (
<UploadFileAnchor
text={text}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,52 @@ describe('UploadFileListCell', () => {
})
})

it('renders a span when file size is 0', () => {
const fileName = 'file.png'

render(
<UploadFileListCell
{...defaultProps}
uploadFile={{ file: createMockFile(fileName, 0, 'image/png') }}
/>
)
expect(screen.queryByText(fileName).tagName).toBe('SPAN')
expect(screen.queryByText(fileName)).toHaveClass('dnb-span')
})

it('renders a span when file size is not given', () => {
const fileName = 'file.png'

render(
<UploadFileListCell
{...defaultProps}
uploadFile={{
file: createMockFile(fileName, undefined, 'image/png'),
}}
/>
)
expect(screen.queryByText(fileName).tagName).toBe('SPAN')
expect(screen.queryByText(fileName)).toHaveClass('dnb-span')
})

it('renders a button when file size is invalid, but onClick is given', () => {
const fileName = 'file.png'

render(
<UploadFileListCell
{...defaultProps}
uploadFile={{
file: createMockFile(fileName, undefined, 'image/png'),
}}
onClick={jest.fn()}
/>
)

expect(screen.queryByText(fileName).parentElement.tagName).toBe(
'BUTTON'
)
})

describe('File Anchor', () => {
it('renders the anchor', () => {
const fileName = 'file.png'
Expand All @@ -216,13 +262,14 @@ describe('UploadFileListCell', () => {
uploadFile={{ file: createMockFile(fileName, 100, 'image/png') }}
/>
)
expect(screen.queryByText(fileName)).toBeInTheDocument()
expect(screen.queryByText(fileName).tagName).toBe('A')
})

it('renders the anchor href', () => {
const fileName = 'file.png'
const mockUrl = 'mock-url'

const originalCreateObjectURL = global.URL.createObjectURL
global.URL.createObjectURL = jest.fn().mockReturnValueOnce(mockUrl)

render(
Expand All @@ -237,6 +284,8 @@ describe('UploadFileListCell', () => {
fileName
) as HTMLAnchorElement
expect(anchorElement.href).toMatch(mockUrl)

global.URL.createObjectURL = originalCreateObjectURL
})

it('renders the download attribute', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function UploadFileItem(
}
}

const imageUrl = URL.createObjectURL(file)
const imageUrl = file?.size > 0 ? URL.createObjectURL(file) : null

const text = file.name + (displaySize ? ' ' + getSize(file.size) : '')
const isLoading = fileIsLoading || loading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@ describe('Value.Upload', () => {
})
expect(screenshot).toMatchImageSnapshot()
})

it('have to match files as non-clickable', async () => {
const screenshot = await makeScreenshot({
selector:
'[data-visual-test="upload-value-display-file-as-non-clickable"]',
})
expect(screenshot).toMatchImageSnapshot()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,24 @@ describe('Value.Upload', () => {
})
})

it('renders a span when file size is 0', () => {
const fileName = 'file.png'

render(
<Value.Upload
value={[
{
file: createMockFile(fileName, 0, 'image/png'),
exists: false,
id: '1',
},
]}
/>
)
expect(screen.queryByText(fileName).tagName).toBe('SPAN')
expect(screen.queryByText(fileName)).toHaveClass('dnb-span')
})

describe('File Anchor', () => {
it('renders the anchor', () => {
const fileName = 'file.png'
Expand All @@ -337,7 +355,7 @@ describe('Value.Upload', () => {
]}
/>
)
expect(screen.queryByText(fileName)).toBeInTheDocument()
expect(screen.queryByText(fileName).tagName).toBe('A')
})

it('executes onFileClick event when button is clicked', () => {
Expand Down Expand Up @@ -415,6 +433,7 @@ describe('Value.Upload', () => {
const fileName = 'file.png'
const mockUrl = 'mock-url'

const originalCreateObjectURL = global.URL.createObjectURL
global.URL.createObjectURL = jest.fn().mockReturnValueOnce(mockUrl)

render(
Expand All @@ -432,6 +451,8 @@ describe('Value.Upload', () => {
fileName
) as HTMLAnchorElement
expect(anchorElement.href).toMatch(mockUrl)

global.URL.createObjectURL = originalCreateObjectURL
})

it('renders the download attribute', () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading