Skip to content

Commit

Permalink
feat(Value.Upload): adds event
Browse files Browse the repository at this point in the history
  • Loading branch information
langz committed Dec 6, 2024
1 parent c5abd0e commit 354897d
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ export const UploadOnFileClick = () => (
{
file: createMockFile('1501870.jpg', 123, 'image/png'),
},
{
file: createMockFile('35217511.jpg', 123, 'image/png'),
},
])
}, [])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ tabs:
key: '/demos'
- title: Properties
key: '/properties'
- title: Events
key: '/events'
breadcrumb:
- text: Forms
href: /uilib/extensions/forms/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,31 @@ export const ListTypes = () => {
</ComponentBox>
)
}

export const OnFileClick = () => {
return (
<ComponentBox hideCode scope={{ createMockFile }}>
<Value.Upload
label="Label text"
value={[
{
file: createMockFile('35217511.jpg', 1000000, 'image/png'),
exists: false,
id: '1',
},
{
file: createMockFile('1501870.jpg', 2000000, 'image/png'),
exists: false,
id: '2',
},
]}
onFileClick={({ fileItem }) => {
window.open(
'https://eufemia.dnb.no/images/avatars/' + fileItem.file.name,
'_blank',
)
}}
/>
</ComponentBox>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ import * as Examples from './Examples'
### Field.Upload path

<Examples.FieldUploadSelectionPath />

### Using `onFileClick`

<Examples.OnFileClick />
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
showTabs: true
---

import PropertiesTable from 'dnb-design-system-portal/src/shared/parts/PropertiesTable'
import { UploadValueEvents } from '@dnb/eufemia/src/extensions/forms/Value/Upload/UploadDocs'

## Events

<PropertiesTable props={UploadValueEvents} />
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
showTabs: true
---

import { UploadProperties } from '@dnb/eufemia/src/extensions/forms/Value/Upload/UploadDocs'
import { UploadValueProperties } from '@dnb/eufemia/src/extensions/forms/Value/Upload/UploadDocs'
import PropertiesTable from 'dnb-design-system-portal/src/shared/parts/PropertiesTable'
import { ValueProperties } from '@dnb/eufemia/src/extensions/forms/Value/ValueDocs'

## Properties

### Value-specific properties

<PropertiesTable props={UploadProperties} />
<PropertiesTable props={UploadValueProperties} />

### General properties

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,7 @@ const UploadFileListCell = ({
{onClick ? (
<Button
variant="tertiary"
className={classnames(
'dnb-anchor--no-launch-icon',
'dnb-upload__file-cell__title'
)}
className={classnames('dnb-upload__file-cell__title')}
onClick={onClick}
>
{file.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export type Props = Omit<
| 'filesAmountLimit'
| 'fileMaxSize'
| 'onFileDelete'
| 'onFileClick'
| 'skeleton'
| 'download'
> & {
fileHandler?: (
newFiles: UploadValue
Expand Down
53 changes: 36 additions & 17 deletions packages/dnb-eufemia/src/extensions/forms/Value/Upload/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@ import classnames from 'classnames'
import { useValueProps } from '../../hooks'
import { ValueProps } from '../../types'
import ValueBlock from '../../ValueBlock'
import { Anchor } from '../../../../components'
import { Anchor, Button } from '../../../../components'
import Icon from '../../../../components/Icon'
import ListFormat, {
ListFormatProps,
} from '../../../../components/list-format'
import type {
UploadFile,
UploadProps,
} from '../../../../components/upload/types'
import type { UploadFile } from '../../../../components/upload/types'
import { fileExtensionImages } from '../../../../components/upload/UploadFileListCell'
import {
BYTES_IN_A_MEGA_BYTE,
getFileTypeFromExtension,
} from '../../../../components/upload/UploadVerify'
import { Props as FieldUploadProps } from '../../Field/Upload/Upload'
import { format } from '../../../../components/number-format/NumberUtils'

export type Props = ValueProps<Array<UploadFile>> &
Omit<ListFormatProps, 'value'> &
Pick<UploadProps, 'download'> & {
Pick<FieldUploadProps, 'download' | 'onFileClick'> & {
displaySize?: boolean
}

Expand All @@ -35,6 +33,7 @@ function Upload(props: Props) {
listType,
download = false,
displaySize = false,
onFileClick,
...rest
} = useValueProps(props)

Expand All @@ -45,21 +44,41 @@ function Upload(props: Props) {
if (!file) {
return
}
const onFileClickHandler = () => {
if (typeof onFileClick === 'function') {
onFileClick({ fileItem: uploadFile })
}
}

const imageUrl = URL.createObjectURL(file)
return (
<span key={index}>
{getIcon(file)}
<Anchor
target="_blank"
href={imageUrl}
download={download ? file.name : null}
rel="noopener noreferrer"
className="dnb-anchor--no-launch-icon"
left="x-small"
>
{file.name}
{displaySize && getSize(file.size)}
</Anchor>
{onFileClick ? (
<Button
size="small"
variant="tertiary"
className={classnames('dnb-upload__file-cell__title')}
onClick={onFileClickHandler}
>
{file.name}
{displaySize && getSize(file.size)}
</Button>
) : (
<Anchor
target="_blank"
href={imageUrl}
download={download ? file.name : null}
className={classnames(
'dnb-anchor--no-launch-icon',
'dnb-upload__file-cell__title'
)}
rel="noopener noreferrer"
>
{file.name}
{displaySize && getSize(file.size)}
</Anchor>
)}
</span>
)
}) || undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { PropertiesTableProps } from '../../../../shared/types'

import { ListFormatProperties } from '../../../../components/list-format/ListFormatDocs'

export const UploadProperties: PropertiesTableProps = {
import { UploadFieldEvents } from '../../Field/Upload/UploadDocs'

export const UploadValueProperties: PropertiesTableProps = {
download: {
doc: 'Causes the browser to treat all listed files as downloadable instead of opening them in a new browser tab or window. Defaults to `false`.',
type: 'boolean',
Expand All @@ -15,3 +17,7 @@ export const UploadProperties: PropertiesTableProps = {
},
...ListFormatProperties,
}

export const UploadValueEvents: PropertiesTableProps = {
onFileClick: UploadFieldEvents.onFileClick,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { screen, render } from '@testing-library/react'
import { screen, render, fireEvent } from '@testing-library/react'
import { Value, Form } from '../../..'
import { createMockFile } from '../../../../../components/upload/__tests__/testHelpers'

Expand Down Expand Up @@ -339,6 +339,30 @@ describe('Value.Upload', () => {
expect(screen.queryByText(fileName)).toBeInTheDocument()
})

it('executes onFileClick event when button is clicked', () => {
const fileName = 'file.png'
const onFileClick = jest.fn()

render(
<Value.Upload
onFileClick={onFileClick}
value={[
{
file: createMockFile(fileName, 100, 'image/png'),
exists: false,
id: '1',
},
]}
/>
)
const buttonElement = screen.queryByText(
fileName
) as HTMLAnchorElement
fireEvent.click(buttonElement)

expect(onFileClick).toHaveBeenCalledTimes(1)
})

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

0 comments on commit 354897d

Please sign in to comment.