import { type DialogFilter, open } from '@tauri-apps/plugin-dialog'; import { FilePlus02Icon } from '@untitled-theme/icons-solid'; import { createSignal, For, Match, onMount, splitProps, Switch } from 'solid-js'; import Button from './Button'; export interface FilePickerOptions { directory?: boolean; multiple?: boolean; defaultPath?: string; title?: string; filters?: DialogFilter[]; }; export interface FilePickerRef { open: () => void; } export default function FilePicker(componentProps: FilePickerOptions & { onPicked?: (paths: string[]) => void; ref?: (ref: FilePickerRef) => void; }) { const [props, options] = splitProps(componentProps, ['onPicked', 'ref']); const [file, setFile] = createSignal([]); onMount(() => { if (options.defaultPath) setFile([options.defaultPath]); props.ref?.({ open: openPicker, }); }); async function openPicker() { const path = await open(options) as string[] | string | null; // The type automatically infers from the selected options, but we don't want that here if (Array.isArray(path)) setFile(path); else if (path === null) setFile([]); else setFile([path]); props.onPicked?.(file()); } return (
); }