Files

52 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
import { useAppForm, Form } from '@/components/form';
import type { DialogProps } from '@/components/elements/dialog';
import { Dialog } from '@/components/elements/dialog';
import { join } from 'pathe';
2019-12-21 16:38:40 -08:00
2026-10-05 18:10:18 -04:00
type Props = DialogProps & {
2026-10-12 15:20:31 -04:00
/** The directory the new file is written to. */
directory: string;
2019-12-21 16:38:40 -08:00
onFileNamed: (name: string) => void;
};
2026-10-12 15:20:31 -04:00
/** Names a new file before the editor first writes it. */
export default function FileNameModal({ directory, onFileNamed, onClose, ...props }: Props) {
2026-10-05 18:10:18 -04:00
const form = useAppForm({
defaultValues: { fileName: '' },
onSubmit: async ({ value }) => {
onFileNamed(join(directory, value.fileName));
},
});
2019-12-21 16:38:40 -08:00
return (
2026-10-05 18:10:18 -04:00
<Dialog
onClose={() => {
form.reset();
onClose();
}}
{...props}
2019-12-21 16:38:40 -08:00
>
2026-10-05 18:10:18 -04:00
<Form form={form}>
<form.AppField
2026-10-08 16:53:20 -04:00
name='fileName'
2026-10-05 18:10:18 -04:00
validators={{ onChange: ({ value }) => (value.length >= 1 ? undefined : 'Required') }}
2019-12-21 16:38:40 -08:00
>
2026-10-05 18:10:18 -04:00
{(field) => (
<field.TextField
2026-10-08 16:53:20 -04:00
id='fileName'
label='File Name'
description='Enter the name that this file should be saved as.'
2020-07-04 17:57:24 -07:00
autoFocus
2019-12-21 16:38:40 -08:00
/>
2026-10-05 18:10:18 -04:00
)}
</form.AppField>
2026-10-08 16:53:20 -04:00
<div className='mt-6 text-right'>
2026-10-05 18:10:18 -04:00
<form.AppForm>
<form.SubmitButton>Create File</form.SubmitButton>
</form.AppForm>
</div>
</Form>
</Dialog>
2019-12-21 16:38:40 -08:00
);
2026-10-05 18:10:18 -04:00
}