diff --git a/packages/dnb-design-system-portal/src/docs/uilib/components/upload/Examples.tsx b/packages/dnb-design-system-portal/src/docs/uilib/components/upload/Examples.tsx index 2b3ad520a00..fd62aa24129 100644 --- a/packages/dnb-design-system-portal/src/docs/uilib/components/upload/Examples.tsx +++ b/packages/dnb-design-system-portal/src/docs/uilib/components/upload/Examples.tsx @@ -356,7 +356,7 @@ export const UploadOnFileDeleteAsync = () => ( export const UploadOnFileClick = () => ( {() => { diff --git a/packages/dnb-eufemia/src/components/upload/UploadFileListCell.tsx b/packages/dnb-eufemia/src/components/upload/UploadFileListCell.tsx index 74e0f96e76f..cc751c81175 100644 --- a/packages/dnb-eufemia/src/components/upload/UploadFileListCell.tsx +++ b/packages/dnb-eufemia/src/components/upload/UploadFileListCell.tsx @@ -2,7 +2,6 @@ import React, { useRef } from 'react' import classnames from 'classnames' // Components -import Anchor from '../../components/Anchor' import Button from '../button/Button' import Icon from '../../components/Icon' import FormStatus from '../../components/FormStatus' @@ -29,6 +28,7 @@ import { UploadFile, UploadFileNative } from './types' import { getPreviousSibling, warn } from '../../shared/component-helper' import useUpload from './useUpload' import { getFileTypeFromExtension } from './UploadVerify' +import UploadFileLink from './UploadFileListLink' // Will be deprecated - and then default to only showing the file icon, // and not file icon per file extension type @@ -188,21 +188,13 @@ const UploadFileListCell = ({ ) : (
- {onClick ? ( - - ) : ( - - {file.name} - - )} +
) } diff --git a/packages/dnb-eufemia/src/components/upload/UploadFileListLink.tsx b/packages/dnb-eufemia/src/components/upload/UploadFileListLink.tsx new file mode 100644 index 00000000000..9a96178d4ca --- /dev/null +++ b/packages/dnb-eufemia/src/components/upload/UploadFileListLink.tsx @@ -0,0 +1,71 @@ +import React from 'react' + +import Anchor from '../../components/Anchor' +import Button from '../button/Button' +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) + return + return ( + + ) +} + +export default UploadFileLink + +type UploadFileButtonProps = { + text: string + onClick?: () => void +} & SpacingProps + +const UploadFileButton = (props: UploadFileButtonProps) => { + const { text, onClick } = props + + const spacingClasses = createSpacingClasses(props) + return ( + + ) +} + +type UploadFileAnchorProps = { + text: string + href: string + download?: boolean +} & SpacingProps + +const UploadFileAnchor = (props: UploadFileAnchorProps) => { + const { text, href, download } = props + + const spacingClasses = createSpacingClasses(props) + + return ( + + {text} + + ) +} diff --git a/packages/dnb-eufemia/src/components/upload/__tests__/UploadFileListLink.test.tsx b/packages/dnb-eufemia/src/components/upload/__tests__/UploadFileListLink.test.tsx new file mode 100644 index 00000000000..5220fa8ce3a --- /dev/null +++ b/packages/dnb-eufemia/src/components/upload/__tests__/UploadFileListLink.test.tsx @@ -0,0 +1,112 @@ +import UploadFileLink, { UploadFileLinkProps } from '../UploadFileListLink' +import { fireEvent, render, screen } from '@testing-library/react' +import React from 'react' + +global.URL.createObjectURL = jest.fn(() => 'url') + +const defaultProps: UploadFileLinkProps = { + text: 'text', + href: 'href', + download: false, + onClick: undefined, +} + +describe('UploadFileListLink', () => { + describe('as an anchor', () => { + it('renders the anchor', () => { + render() + expect(document.querySelector('.dnb-button')).not.toBeInTheDocument() + expect(document.querySelector('.dnb-a')).toBeInTheDocument() + }) + + it('renders the anchor text', () => { + const fileName = 'file.png' + + render() + expect(screen.queryByText(fileName)).toBeInTheDocument() + }) + + it('renders the anchor href', () => { + const fileName = 'file.png' + + const href = 'mock-url' + + render( + + ) + + const anchorElement = screen.queryByText( + fileName + ) as HTMLAnchorElement + expect(anchorElement.href).toMatch(href) + }) + + it('renders the download attribute', () => { + const fileName = 'file.png' + + render( + + ) + + const element = document.querySelector('.dnb-a') + + expect(element).toHaveAttribute('download', fileName) + }) + + it('supports spacing props', () => { + render() + + const element = document.querySelector('.dnb-a') + expect(element).toHaveClass('dnb-space__top--large') + }) + }) + + describe('as a button', () => { + it('renders the button', () => { + render() + expect(document.querySelector('.dnb-a')).not.toBeInTheDocument() + expect(document.querySelector('.dnb-button')).toBeInTheDocument() + }) + + it('renders the button text', () => { + const fileName = 'file.png' + + render( + + ) + expect(screen.queryByText(fileName)).toBeInTheDocument() + }) + + it('executes onClick event when button is clicked', () => { + const onClick = jest.fn() + + render() + const element = document.querySelector('.dnb-button') + + fireEvent.click(element) + + expect(onClick).toHaveBeenCalledTimes(1) + }) + + it('supports spacing props', () => { + render( + + ) + + const element = document.querySelector('.dnb-button') + expect(element).toHaveClass('dnb-space__top--large') + }) + }) +}) diff --git a/packages/dnb-eufemia/src/extensions/forms/Value/Upload/Upload.tsx b/packages/dnb-eufemia/src/extensions/forms/Value/Upload/Upload.tsx index c7a6d7a30e2..ff0cb78f6a7 100644 --- a/packages/dnb-eufemia/src/extensions/forms/Value/Upload/Upload.tsx +++ b/packages/dnb-eufemia/src/extensions/forms/Value/Upload/Upload.tsx @@ -3,7 +3,6 @@ import classnames from 'classnames' import { useValueProps } from '../../hooks' import { ValueProps } from '../../types' import ValueBlock from '../../ValueBlock' -import { Anchor, Button } from '../../../../components' import Icon from '../../../../components/Icon' import ListFormat, { ListFormatProps, @@ -16,6 +15,7 @@ import { } from '../../../../components/upload/UploadVerify' import { Props as FieldUploadProps } from '../../Field/Upload/Upload' import { format } from '../../../../components/number-format/NumberUtils' +import { UploadFileLink } from '../../../../components/upload/UploadFileListLink' export type Props = ValueProps> & Omit & @@ -51,34 +51,21 @@ function Upload(props: Props) { } const imageUrl = URL.createObjectURL(file) + + const text = + file.name + (displaySize ? ' ' + getSize(file.size) : '') + return ( {getIcon(file)} - {onFileClick ? ( - - ) : ( - - {file.name} - {displaySize && getSize(file.size)} - - )} + + ) }) || undefined