-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Upload & Value.Upload): share anchor & button (#4376)
- Loading branch information
Showing
5 changed files
with
205 additions
and
43 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
71 changes: 71 additions & 0 deletions
71
packages/dnb-eufemia/src/components/upload/UploadFileListLink.tsx
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,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 <UploadFileButton text={text} onClick={onClick} {...rest} /> | ||
return ( | ||
<UploadFileAnchor | ||
text={text} | ||
href={href} | ||
download={download} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
|
||
export default UploadFileLink | ||
|
||
type UploadFileButtonProps = { | ||
text: string | ||
onClick?: () => void | ||
} & SpacingProps | ||
|
||
const UploadFileButton = (props: UploadFileButtonProps) => { | ||
const { text, onClick } = props | ||
|
||
const spacingClasses = createSpacingClasses(props) | ||
return ( | ||
<Button | ||
icon={false} | ||
variant="tertiary" | ||
onClick={onClick} | ||
className={classNames(spacingClasses)} | ||
> | ||
{text} | ||
</Button> | ||
) | ||
} | ||
|
||
type UploadFileAnchorProps = { | ||
text: string | ||
href: string | ||
download?: boolean | ||
} & SpacingProps | ||
|
||
const UploadFileAnchor = (props: UploadFileAnchorProps) => { | ||
const { text, href, download } = props | ||
|
||
const spacingClasses = createSpacingClasses(props) | ||
|
||
return ( | ||
<Anchor | ||
target="_blank" | ||
href={href} | ||
download={download ? text : null} | ||
className={classNames('dnb-anchor--no-launch-icon', spacingClasses)} | ||
rel="noopener noreferrer" | ||
> | ||
{text} | ||
</Anchor> | ||
) | ||
} |
112 changes: 112 additions & 0 deletions
112
packages/dnb-eufemia/src/components/upload/__tests__/UploadFileListLink.test.tsx
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,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(<UploadFileLink {...defaultProps} />) | ||
expect(document.querySelector('.dnb-button')).not.toBeInTheDocument() | ||
expect(document.querySelector('.dnb-a')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders the anchor text', () => { | ||
const fileName = 'file.png' | ||
|
||
render(<UploadFileLink {...defaultProps} text={fileName} />) | ||
expect(screen.queryByText(fileName)).toBeInTheDocument() | ||
}) | ||
|
||
it('renders the anchor href', () => { | ||
const fileName = 'file.png' | ||
|
||
const href = 'mock-url' | ||
|
||
render( | ||
<UploadFileLink {...defaultProps} text={fileName} href={href} /> | ||
) | ||
|
||
const anchorElement = screen.queryByText( | ||
fileName | ||
) as HTMLAnchorElement | ||
expect(anchorElement.href).toMatch(href) | ||
}) | ||
|
||
it('renders the download attribute', () => { | ||
const fileName = 'file.png' | ||
|
||
render( | ||
<UploadFileLink | ||
{...defaultProps} | ||
text={fileName} | ||
download={true} | ||
/> | ||
) | ||
|
||
const element = document.querySelector('.dnb-a') | ||
|
||
expect(element).toHaveAttribute('download', fileName) | ||
}) | ||
|
||
it('supports spacing props', () => { | ||
render(<UploadFileLink {...defaultProps} top="large" />) | ||
|
||
const element = document.querySelector('.dnb-a') | ||
expect(element).toHaveClass('dnb-space__top--large') | ||
}) | ||
}) | ||
|
||
describe('as a button', () => { | ||
it('renders the button', () => { | ||
render(<UploadFileLink {...defaultProps} onClick={jest.fn()} />) | ||
expect(document.querySelector('.dnb-a')).not.toBeInTheDocument() | ||
expect(document.querySelector('.dnb-button')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders the button text', () => { | ||
const fileName = 'file.png' | ||
|
||
render( | ||
<UploadFileLink | ||
{...defaultProps} | ||
onClick={jest.fn()} | ||
text={fileName} | ||
/> | ||
) | ||
expect(screen.queryByText(fileName)).toBeInTheDocument() | ||
}) | ||
|
||
it('executes onClick event when button is clicked', () => { | ||
const onClick = jest.fn() | ||
|
||
render(<UploadFileLink {...defaultProps} onClick={onClick} />) | ||
const element = document.querySelector('.dnb-button') | ||
|
||
fireEvent.click(element) | ||
|
||
expect(onClick).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('supports spacing props', () => { | ||
render( | ||
<UploadFileLink | ||
{...defaultProps} | ||
onClick={jest.fn()} | ||
top="large" | ||
/> | ||
) | ||
|
||
const element = document.querySelector('.dnb-button') | ||
expect(element).toHaveClass('dnb-space__top--large') | ||
}) | ||
}) | ||
}) |
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