Skip to content

Commit

Permalink
fix(Forms): add support for sessionStorageId in Field.Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Dec 20, 2024
1 parent 41e4131 commit 927dd00
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
28 changes: 23 additions & 5 deletions packages/dnb-eufemia/src/extensions/forms/Field/Upload/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,21 @@ function UploadComponent(props: Props) {
[formsTr.errorRequired]
)

const fromInput = useCallback((value: UploadValue) => {
value.forEach((item, index) => {
value[index] = item

// Store the name in the value, to support session storage (serialization)
value[index]['name'] = item['name'] || item.file?.name
})

return value
}, [])

const preparedProps = {
errorMessages,
validateRequired,
fromInput,
...props,
}

Expand Down Expand Up @@ -128,10 +140,16 @@ function UploadComponent(props: Props) {
}, [files])

useEffect(() => {
// Files stored in session storage will not have a property (due to serialization).
const hasInvalidFiles = value?.some(({ file }) => !file?.name)
if (!hasInvalidFiles) {
setFiles(value)
if (Array.isArray(value)) {
setFiles(
value.map((item) => {
if (item.file && !(item.file instanceof File)) {
// To support session storage, we recreated the file blob.
item['file'] = new File([], item['name'])
}
return item
})
)
}
}, [setFiles, value])

Expand Down Expand Up @@ -173,7 +191,7 @@ function UploadComponent(props: Props) {
handleChange(existingFiles)
}
},
[files, setFiles, fileHandler, handleChange]
[setFiles, fileHandler, handleChange]
)

const changeHandler = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,13 @@ describe('Field.Upload', () => {
file: file1,
id: expect.any(String),
exists: expect.any(Boolean),
name: 'fileName-1.png',
},
{
file: file2,
id: expect.any(String),
exists: expect.any(Boolean),
name: 'fileName-2.png',
},
],
},
Expand Down Expand Up @@ -256,6 +258,7 @@ describe('Field.Upload', () => {
file: file1,
exists: false,
id: expect.anything(),
name: 'fileName-1.png',
},
{
errorMessage: nbShared.Upload.errorLargeFile.replace(
Expand All @@ -265,6 +268,7 @@ describe('Field.Upload', () => {
file: file2,
exists: false,
id: expect.anything(),
name: 'fileName-2.png',
},
],
},
Expand All @@ -276,6 +280,7 @@ describe('Field.Upload', () => {
file: file1,
exists: false,
id: expect.anything(),
name: 'fileName-1.png',
},
{
errorMessage: nbShared.Upload.errorLargeFile.replace(
Expand All @@ -285,6 +290,7 @@ describe('Field.Upload', () => {
file: file2,
exists: false,
id: expect.anything(),
name: 'fileName-2.png',
},
])

Expand All @@ -310,6 +316,7 @@ describe('Field.Upload', () => {
file: file1,
exists: false,
id: expect.anything(),
name: 'fileName-1.png',
},
],
},
Expand Down Expand Up @@ -353,6 +360,7 @@ describe('Field.Upload', () => {
exists: false,
file: file1,
id: expect.any(String),
name: 'fileName-1.png',
}),
],
},
Expand Down Expand Up @@ -399,6 +407,7 @@ describe('Field.Upload', () => {
exists: false,
file: file1,
id: expect.any(String),
name: 'fileName-1.png',
}),
],
},
Expand Down Expand Up @@ -468,6 +477,7 @@ describe('Field.Upload', () => {
file: file1,
exists: false,
id: expect.anything(),
name: 'fileName-1.png',
},
],
},
Expand Down Expand Up @@ -734,6 +744,7 @@ describe('Field.Upload', () => {
file: file1,
exists: false,
id: expect.anything(),
name: 'fileName-1.png',
},
],
},
Expand Down Expand Up @@ -1475,7 +1486,7 @@ describe('Field.Upload', () => {
})
})

it('should not set files from session storage if they are invalid', async () => {
it('should recreate files from session storage', async () => {
const file = createMockFile('fileName.png', 100, 'image/png')

const { unmount } = render(
Expand Down Expand Up @@ -1517,15 +1528,16 @@ describe('Field.Upload', () => {
expect(dataContext.internalDataRef.current.myFiles).toEqual([
{
exists: false,
file: {},
file: new File([], 'fileName.png'),
id: expect.any(String),
name: 'fileName.png',
},
])
const [title] = Array.from(document.querySelectorAll('p'))
expect(title).toHaveTextContent(nbShared.Upload.title)
expect(
document.querySelectorAll('.dnb-upload__file-cell').length
).toBe(0)
).toBe(1)
})

describe('transformIn and transformOut', () => {
Expand Down

0 comments on commit 927dd00

Please sign in to comment.