mirror of https://github.com/kern/filepizza
feat(upload): allow adding more files before starting (#293)
parent
dc56fba614
commit
ce3a27b69c
@ -0,0 +1,45 @@
|
||||
import React, { useRef, useCallback, JSX } from 'react'
|
||||
import { UploadedFile } from '../types'
|
||||
|
||||
export default function AddFilesButton({
|
||||
onAdd,
|
||||
}: {
|
||||
onAdd: (files: UploadedFile[]) => void
|
||||
}): JSX.Element {
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
const handleClick = useCallback(() => {
|
||||
fileInputRef.current?.click()
|
||||
}, [])
|
||||
|
||||
const handleChange = useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
if (e.target.files) {
|
||||
onAdd(Array.from(e.target.files) as UploadedFile[])
|
||||
e.target.value = ''
|
||||
}
|
||||
},
|
||||
[onAdd],
|
||||
)
|
||||
|
||||
return (
|
||||
<>
|
||||
<input
|
||||
id="add-files-input"
|
||||
type="file"
|
||||
ref={fileInputRef}
|
||||
className="hidden"
|
||||
multiple
|
||||
onChange={handleChange}
|
||||
/>
|
||||
<button
|
||||
id="add-files-button"
|
||||
type="button"
|
||||
onClick={handleClick}
|
||||
className="block cursor-pointer relative py-3 px-6 text-base font-bold text-stone-700 dark:text-stone-200 bg-white dark:bg-stone-800 border-2 border-stone-700 dark:border-stone-700 rounded-lg transition-all duration-300 ease-in-out outline-none hover:shadow-md active:shadow-inner focus:shadow-outline"
|
||||
>
|
||||
Add more files
|
||||
</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
/// <reference types="@playwright/test" />
|
||||
import { test, expect } from '@playwright/test'
|
||||
import { createTestFile, uploadFile, addFile } from './helpers'
|
||||
|
||||
test('user can add more files before starting upload', async ({ page }) => {
|
||||
const file1 = createTestFile('first.txt', 'A')
|
||||
const file2 = createTestFile('second.txt', 'B')
|
||||
|
||||
await uploadFile(page, file1)
|
||||
|
||||
// Add another file using the add files button
|
||||
await addFile(page, file2)
|
||||
|
||||
// Both files should be listed
|
||||
await expect(page.getByText(file1.name)).toBeVisible()
|
||||
await expect(page.getByText(file2.name)).toBeVisible()
|
||||
})
|
||||
Loading…
Reference in New Issue