Back to Mantine

File Input

apps/mantine.dev/src/pages/core/file-input.mdx

9.3.03.2 KB
Original Source

import { FileInputDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';

export default Layout(MDX_DATA.FileInput);

Usage

<InputFeatures component="FileInput" element="input" /> <Demo data={FileInputDemos.usage} />

Loading state

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:

<Demo data={FileInputDemos.loading} />

Controlled

When multiple is false:

tsx
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:

tsx
import { useState } from 'react';
import { FileInput } from '@mantine/core';

function Demo() {
  const [value, setValue] = useState<File[]>([]);
  return <FileInput multiple value={value} onChange={setValue} />;
}

Uncontrolled

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:

tsx
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>
  );
}

Multiple

Set multiple to allow the user to pick more than one file:

<Demo data={FileInputDemos.multiple} />

Accept

Set the accept prop to restrict file selection to specific mime types:

<Demo data={FileInputDemos.accept} />

Clearable

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.

<Demo data={FileInputDemos.clearable} /> <ClearSectionMode /> <Demo data={FileInputDemos.clearSectionMode} />

Custom value component

<Demo data={FileInputDemos.valueComponent} />

Error state

<Demo data={FileInputDemos.error} />

Disabled state

<Demo data={FileInputDemos.disabled} /> <InputSections component="FileInput" /> <Demo data={FileInputDemos.sections} /> <StylesApiSelectors component="FileInput" /> <Demo data={FileInputDemos.stylesApi} /> <GetElementRef component="FileInput" refType="button" /> <InputAccessibility component="FileInput" />

FileInputProps type

FileInputProps type is a generic interface which accepts a single type argument: the multiple value.

tsx
import type { FileInputProps } from '@mantine/core';

type SingleInputProps = FileInputProps<false>;
type MultipleInputProps = FileInputProps<true>;