Skip to content

Commit

Permalink
chore(Upload): improve typing of id property (#4403)
Browse files Browse the repository at this point in the history
  • Loading branch information
langz authored and tujoworker committed Dec 13, 2024
1 parent 8bd5020 commit f92b85e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ export const UploadPrefilledFileList = () => (

export const UploadBasic = () => (
<ComponentBox data-visual-test="upload-basic">
<Upload acceptedFileTypes={['jpg', 'png']} id="upload-basic" />
{() => {
const Component = () => {
const myUploadId = 'unique-id' // or a function, object or React Context reference

return (
<Upload acceptedFileTypes={['jpg', 'png']} id={myUploadId} />
)
}

return <Component />
}}
</ComponentBox>
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ The Upload component should be used in scenarios where the user has to upload an

```jsx
function YourComponent() {
const { files, setFiles } = Upload.useUpload('unique-id')
const myUploadId = 'unique-id' // or a function, object or React Context reference
const { files, setFiles } = Upload.useUpload(myUploadId)

React.useEffect(() => {
setFiles(
Expand All @@ -35,7 +36,7 @@ function YourComponent() {
)
}, [fileNameFromBackend])

return <Upload id="unique-id" />
return <Upload id={myUploadId} />
}
```

Expand Down
2 changes: 1 addition & 1 deletion packages/dnb-eufemia/src/components/upload/UploadDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PropertiesTableProps } from '../../shared/types'
export const UploadProperties: PropertiesTableProps = {
id: {
doc: 'Unique id used with the useUpload hook to manage the files.',
type: 'string',
type: ['string', 'Function', 'Object', 'React.Context'],
status: 'required',
},
acceptedFileTypes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function UploadDropzone({
className,
...rest
}: Partial<UploadAllProps>) {
const props = rest as Omit<UploadProps, 'title' | 'onChange'>
const props = rest as Omit<UploadProps, 'title' | 'onChange' | 'id'>
const context = useContext(UploadContext)
const [hover, setHover] = useState(false)
const hoverTimeout = useRef<NodeJS.Timer>()
Expand Down
3 changes: 2 additions & 1 deletion packages/dnb-eufemia/src/components/upload/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import type { SkeletonShow } from '../skeleton/Skeleton'
import type { LocaleProps, SpacingProps } from '../../shared/types'
import type { SharedStateId } from '../../shared/helpers/useSharedState'

export type UploadAcceptedFileTypes = string[]

Expand All @@ -16,7 +17,7 @@ export type UploadProps = {
/**
* unique id used with the useUpload hook to manage the files
*/
id: string
id: SharedStateId

/**
* list of accepted file types.
Expand Down
4 changes: 2 additions & 2 deletions packages/dnb-eufemia/src/components/upload/useUpload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useMemo } from 'react'
import { useSharedState } from '../../shared/helpers/useSharedState'
import type { UploadFile, UploadFileNative } from './types'
import type { UploadFile, UploadFileNative, UploadProps } from './types'

export type useUploadReturn = {
files: Array<UploadFile>
Expand All @@ -16,7 +16,7 @@ export type useUploadReturn = {
/**
* Use together with Upload with the same id to manage the files from outside the component.
*/
function useUpload(id: string): useUploadReturn {
function useUpload(id: UploadProps['id']): useUploadReturn {
const { data, extend } = useSharedState<{
files?: Array<UploadFile>
internalFiles?: Array<UploadFile>
Expand Down

0 comments on commit f92b85e

Please sign in to comment.