apps/mantine.dev/src/pages/core/file-input.mdx
import { FileInputDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.FileInput);
Set loading prop to display a loading indicator. By default, the loader is displayed on the right side of the input.
You can change the position with the loadingPosition prop to 'left' or 'right'. This is useful for async operations like API calls, searches, or validations:
When multiple is false:
import { useState } from 'react';
import { FileInput } from '@mantine/core';
function Demo() {
const [value, setValue] = useState<File | null>(null);
return <FileInput value={value} onChange={setValue} />;
}
When multiple is true:
import { useState } from 'react';
import { FileInput } from '@mantine/core';
function Demo() {
const [value, setValue] = useState<File[]>([]);
return <FileInput multiple value={value} onChange={setValue} />;
}
FileInput can be used with uncontrolled forms the same way as a native input[type="file"].
Set the name attribute to include file input value in FormData object on form submission.
To control the initial value in uncontrolled forms, use the defaultValue prop.
Example usage of uncontrolled FileInput with FormData:
import { FileInput } from '@mantine/core';
function Demo() {
return (
<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
const files = formData.getAll('file');
console.log('File input value:', files);
}}
>
<FileInput label="Upload your file" name="file" />
<button type="submit">Submit</button>
</form>
);
}
Set multiple to allow the user to pick more than one file:
Set the accept prop to restrict file selection to specific mime types:
Set the clearable prop to display a clear button in the right section of the input
when a file is selected. Note that if you define a custom right section, the clear button
will not be rendered.
FileInputProps type is a generic interface which accepts a single type argument:
the multiple value.
import type { FileInputProps } from '@mantine/core';
type SingleInputProps = FileInputProps<false>;
type MultipleInputProps = FileInputProps<true>;