Appearance
useFileDialog
Open file dialog with ease
Demo
Usage
tsx
import { useFileDialog } from '@reause/core'
import { useListener } from '@reause/shared'
const {
files,
open,
reset,
onChange,
onCancel,
} = useFileDialog({
accept: 'image/*', // Set to accept only image files
directory: true, // Select directories instead of files if set true
})
useListener(onChange, (files) => {
/** do something with files */
})
useListener(onCancel, () => {
/** do something on cancel */
})With buttons:
tsx
import { useFileDialog } from '@reause/core'
function Component() {
const { files, open, reset } = useFileDialog()
return (
<div>
<button type="button" onClick={() => open()}>
Choose files
</button>
<button type="button" disabled={!files} onClick={() => reset()}>
Reset
</button>
</div>
)
}Type Declarations
ts
export interface UseFileDialogOptions {
document?: Document | null;
multiple?: boolean;
accept?: string;
capture?: string;
reset?: boolean;
directory?: boolean;
initialFiles?: Array<File> | FileList;
input?: RefObject<HTMLInputElement | null>;
}
export interface UseFileDialogReturn {
files: FileList | null;
open: (localOptions?: Partial<UseFileDialogOptions>) => void;
reset: () => void;
onChange: (fn: (files: FileList | null) => void) => () => void;
onCancel: (fn: () => void) => () => void;
}
export function useFileDialog(options?: UseFileDialogOptions): UseFileDialogReturn