# @salmonui/inputs/file-upload API reference

> 18 exported symbols of @salmonui/inputs/file-upload: 1 component, 1 function, 10 interfaces and 6 types.

Canonical: https://salmonui.dev/docs/reference/inputs/file-upload

```ts
import { … } from '@salmonui/inputs/file-upload';
```

## Components

### FileUpload

Kind: component

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FileUpload

```ts
function FileUpload(props: FileUploadProps): ReactNode;
```

File picker with a drop zone, validation, a file list and an optional
upload queue. `ref` is a `FileUploadApi`; unknown props go
to the native file input, `className` / `style` to the root.

Props: [`FileUploadProps`](https://salmonui.dev/docs/reference/inputs/file-upload/FileUploadProps) (its members are the props)

## Functions

### formatFileSize

Kind: function

Page: https://salmonui.dev/docs/reference/inputs/file-upload/formatFileSize

```ts
function formatFileSize(bytes: number, locale?: string): string;
```

Formats a byte count for people: `formatFileSize(1536, 'en-US')` is
`"1.5 kB"`, `formatFileSize(1, 'de-DE')` is `"1 Byte"`. Uses 1024 steps,
at most one fraction digit above bytes, and `locale` (default `'en-US'`,
never the host locale, so server and client agree).

## Interfaces

### BeforeClearEvent

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/file-upload/BeforeClearEvent

```ts
interface BeforeClearEvent extends FileUploadCancelableEvent {
    readonly files: readonly FileEntry[];
}
```

Members (3):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `files` | `readonly files: readonly FileEntry[];` |  |  |
| `defaultPrevented` | `defaultPrevented: boolean;` | inherited |  |
| `preventDefault` | `preventDefault: () => void;` | inherited |  |

### BeforeFileEvent

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/file-upload/BeforeFileEvent

```ts
interface BeforeFileEvent extends FileUploadCancelableEvent {
    readonly file: FileEntry;
}
```

Members (3):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `file` | `readonly file: FileEntry;` |  |  |
| `defaultPrevented` | `defaultPrevented: boolean;` | inherited |  |
| `preventDefault` | `preventDefault: () => void;` | inherited |  |

### BeforeFilesAddEvent

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/file-upload/BeforeFilesAddEvent

```ts
interface BeforeFilesAddEvent extends FileUploadCancelableEvent {
    readonly files: File[];
    readonly source: 'picker' | 'drop' | 'api';
}
```

Members (4):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `files` | `readonly files: File[];` |  | The files about to be added; mutate it to drop or add files. |
| `source` | `readonly source: 'picker' \| 'drop' \| 'api';` |  |  |
| `defaultPrevented` | `defaultPrevented: boolean;` | inherited |  |
| `preventDefault` | `preventDefault: () => void;` | inherited |  |

### FileEntry

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FileEntry

```ts
interface FileEntry {
    readonly id: string;
    readonly name: string;
    readonly size: number;
    readonly type: string;
    readonly file: File | null;
    readonly path: string;
    readonly status: FileStatus;
    readonly progress: number;
    readonly error: FileErrorCode | null;
    readonly reason?: unknown;
    readonly result?: unknown;
}
```

One file in the list. Entries are immutable; every change makes a new one.

Members (11):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `id` | `readonly id: string;` |  |  |
| `name` | `readonly name: string;` |  |  |
| `size` | `readonly size: number;` |  |  |
| `type` | `readonly type: string;` |  |  |
| `file` | `readonly file: File \| null;` |  | The picked or dropped file; `null` for preloaded entries. |
| `path` | `readonly path: string;` |  | Folder-relative path for directory picks and drops, else the name. |
| `status` | `readonly status: FileStatus;` |  |  |
| `progress` | `readonly progress: number;` |  | 0..1 while uploading, 1 when uploaded. |
| `error` | `readonly error: FileErrorCode \| null;` |  |  |
| `reason` | `readonly reason?: unknown;` | optional | The transport's rejection reason (status `error`, or error `remove`). |
| `result` | `readonly result?: unknown;` | optional | The transport's resolved value (status `uploaded`). |

### FileRenderContext

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FileRenderContext

```ts
interface FileRenderContext {
    readonly file: FileEntry;
    readonly status: FileStatus;
    readonly progress: number;
    readonly error: FileErrorCode | null;
    readonly statusText: string;
    readonly sizeText: string;
    readonly remove: () => void;
    readonly retry: () => void;
    readonly cancel: () => void;
}
```

Argument of `renderFile`.

Members (9):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `file` | `readonly file: FileEntry;` |  |  |
| `status` | `readonly status: FileStatus;` |  |  |
| `progress` | `readonly progress: number;` |  |  |
| `error` | `readonly error: FileErrorCode \| null;` |  |  |
| `statusText` | `readonly statusText: string;` |  | Localized status or error text. |
| `sizeText` | `readonly sizeText: string;` |  | `formatFileSize(file.size, locale)`. |
| `remove` | `readonly remove: () => void;` |  |  |
| `retry` | `readonly retry: () => void;` |  |  |
| `cancel` | `readonly cancel: () => void;` |  |  |

### FileUploadApi

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FileUploadApi

```ts
interface FileUploadApi {
    readonly element: HTMLDivElement | null;
    upload(ids?: readonly string[]): void;
    cancel(id: string): void;
    retry(id: string): void;
    remove(id: string): void;
    clear(): void;
    getFiles(): readonly FileEntry[];
    open(): void;
    focus(): void;
}
```

The `ref` of `FileUpload`.

Members (9):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `element` | `readonly element: HTMLDivElement \| null;` |  | The root element. |
| `upload` | `upload(ids?: readonly string[]): void;` |  | Starts queued files (all, or the given ids). |
| `cancel` | `cancel(id: string): void;` |  |  |
| `retry` | `retry(id: string): void;` |  |  |
| `remove` | `remove(id: string): void;` |  |  |
| `clear` | `clear(): void;` |  | Removes every file (`onBeforeClear` can veto). |
| `getFiles` | `getFiles(): readonly FileEntry[];` |  |  |
| `open` | `open(): void;` |  | Opens the file picker. |
| `focus` | `focus(): void;` |  | Focuses the drop zone button. |

### FileUploadCancelableEvent

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FileUploadCancelableEvent

```ts
interface FileUploadCancelableEvent {
    readonly defaultPrevented: boolean;
    preventDefault(): void;
}
```

A cancellable event (`preventDefault()` vetoes the action).

Members (2):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `defaultPrevented` | `readonly defaultPrevented: boolean;` |  |  |
| `preventDefault` | `preventDefault(): void;` |  |  |

### FileUploadProps

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FileUploadProps

```ts
interface FileUploadProps extends FieldCommonProps, NativeInput {
    readonly files?: readonly (FileEntry | FilePreload)[] | undefined;
    readonly defaultFiles?: readonly (FileEntry | FilePreload)[] | undefined;
    readonly onFilesChange?: ((files: readonly FileEntry[], details: FilesChangeDetails) => void) | undefined;
    readonly accept?: string | undefined;
    readonly multiple?: boolean | undefined;
    readonly directory?: boolean | undefined;
    readonly minSize?: number | undefined;
    readonly maxSize?: number | undefined;
    readonly maxFiles?: number | undefined;
    readonly upload?: UploadTransport | undefined;
    readonly remove?: RemoveTransport | undefined;
    readonly autoUpload?: boolean | undefined;
    readonly concurrency?: number | undefined;
    readonly dropTarget?: RefObject<Element | null> | undefined;
    readonly dropEffect?: 'copy' | 'move' | 'link' | 'none' | undefined;
    readonly showFileList?: boolean | undefined;
    readonly renderFile?: ((ctx: FileRenderContext) => ReactNode) | undefined;
    readonly renderTrigger?: ((ctx: {
        readonly open: () => void;
        readonly text: string;
    }) => ReactNode) | undefined;
    readonly localeText?: FileUploadLocaleText | undefined;
    readonly locale?: string | undefined;
    readonly onBeforeFilesAdd?: ((event: BeforeFilesAddEvent) => void) | undefined;
    readonly onBeforeUpload?: ((event: BeforeFileEvent) => void) | undefined;
    readonly onUploadProgress?: ((file: FileEntry) => void) | undefined;
    readonly onUploadSuccess?: ((file: FileEntry, result: unknown) => void) | undefined;
    readonly onUploadError?: ((file: FileEntry, reason: unknown) => void) | undefined;
    readonly onUploadComplete?: ((files: readonly FileEntry[]) => void) | undefined;
    readonly onBeforeRemove?: ((event: BeforeFileEvent) => void) | undefined;
    readonly onBeforeClear?: ((event: BeforeClearEvent) => void) | undefined;
    readonly className?: string | undefined;
    readonly style?: CSSProperties | undefined;
    readonly classNames?: {
        readonly trigger?: string;
        readonly list?: string;
        readonly item?: string;
        readonly label?: string;
        readonly description?: string;
        readonly error?: string;
    } | undefined;
    readonly ref?: Ref<FileUploadApi> | undefined;
}
```

Members (39):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `label` | `label?: ReactNode;` | optional, inherited |  |
| `description` | `description?: ReactNode;` | optional, inherited |  |
| `errorMessage` | `errorMessage?: ((ctx: { readonly errors: readonly string[]; }) => ReactNode) \| ReactNode;` | optional, inherited | Error text (or a function of the errors) shown while the field is invalid. |
| `invalid` | `invalid?: boolean;` | optional, inherited | Forces the error state. |
| `validation` | `validation?: FieldValidation;` | optional, inherited | Rules in the Data Grid vocabulary. |
| `validateOn` | `validateOn?: ValidateOn;` | optional, inherited | When errors appear; default the `Form`'s, else `'blur'`. |
| `size` | `size?: FieldSize;` | optional, inherited | Metric set. Default `'md'`. |
| `files` | `readonly files?: readonly (FileEntry \| FilePreload)[] \| undefined;` | optional | Controlled list; preloaded entries default to status `'uploaded'`. |
| `defaultFiles` | `readonly defaultFiles?: readonly (FileEntry \| FilePreload)[] \| undefined;` | optional |  |
| `onFilesChange` | `readonly onFilesChange?: ((files: readonly FileEntry[], details: FilesChangeDetails) => void) \| undefined;` | optional |  |
| `accept` | `readonly accept?: string \| undefined;` | optional | Extensions and MIME types (`'.pdf,image/*'`); other files are `invalid` with error `type`. |
| `multiple` | `readonly multiple?: boolean \| undefined;` | optional |  |
| `directory` | `readonly directory?: boolean \| undefined;` | optional | Picks and drops folders (`webkitdirectory`, drops walked recursively). |
| `minSize` | `readonly minSize?: number \| undefined;` | optional | Bytes; smaller files get error `tooSmall`. |
| `maxSize` | `readonly maxSize?: number \| undefined;` | optional | Bytes; larger files get error `tooLarge`. |
| `maxFiles` | `readonly maxFiles?: number \| undefined;` | optional | Files beyond the limit get error `tooMany`. |
| `upload` | `readonly upload?: UploadTransport \| undefined;` | optional | Transport; without it the files only submit with the form. |
| `remove` | `readonly remove?: RemoveTransport \| undefined;` | optional | Server removal of uploaded files. |
| `autoUpload` | `readonly autoUpload?: boolean \| undefined;` | optional | Uploads as soon as files are added. Default `true`. |
| `concurrency` | `readonly concurrency?: number \| undefined;` | optional | Parallel uploads; `1` is sequential. Default unlimited. |
| `dropTarget` | `readonly dropTarget?: RefObject<Element \| null> \| undefined;` | optional | Another element that accepts drops. |
| `dropEffect` | `readonly dropEffect?: 'copy' \| 'move' \| 'link' \| 'none' \| undefined;` | optional | `dataTransfer.dropEffect` while dragging over; `'none'` refuses drops. Default `'copy'`. |
| `showFileList` | `readonly showFileList?: boolean \| undefined;` | optional | Default `true`. |
| `renderFile` | `readonly renderFile?: ((ctx: FileRenderContext) => ReactNode) \| undefined;` | optional | Content of one list item. |
| `renderTrigger` | `readonly renderTrigger?: ((ctx: {     readonly open: () => void;     readonly text: string; }) => ReactNode) \| undefined;` | optional | Content of the drop zone button. |
| `localeText` | `readonly localeText?: FileUploadLocaleText \| undefined;` | optional |  |
| `locale` | `readonly locale?: string \| undefined;` | optional | Locale of file sizes. Default `'en-US'`. |
| `onBeforeFilesAdd` | `readonly onBeforeFilesAdd?: ((event: BeforeFilesAddEvent) => void) \| undefined;` | optional |  |
| `onBeforeUpload` | `readonly onBeforeUpload?: ((event: BeforeFileEvent) => void) \| undefined;` | optional |  |
| `onUploadProgress` | `readonly onUploadProgress?: ((file: FileEntry) => void) \| undefined;` | optional |  |
| `onUploadSuccess` | `readonly onUploadSuccess?: ((file: FileEntry, result: unknown) => void) \| undefined;` | optional |  |
| `onUploadError` | `readonly onUploadError?: ((file: FileEntry, reason: unknown) => void) \| undefined;` | optional |  |
| `onUploadComplete` | `readonly onUploadComplete?: ((files: readonly FileEntry[]) => void) \| undefined;` | optional |  |
| `onBeforeRemove` | `readonly onBeforeRemove?: ((event: BeforeFileEvent) => void) \| undefined;` | optional |  |
| `onBeforeClear` | `readonly onBeforeClear?: ((event: BeforeClearEvent) => void) \| undefined;` | optional |  |
| `className` | `readonly className?: string \| undefined;` | optional |  |
| `style` | `readonly style?: CSSProperties \| undefined;` | optional |  |
| `classNames` | `readonly classNames?: {     readonly trigger?: string;     readonly list?: string;     readonly item?: string;     readonly label?: string;     readonly description?: string;     readonly error?: string; } \| undefined;` | optional | Part classes: `trigger`, `list`, `item`, `label`, `description`, `error`. |
| `ref` | `readonly ref?: Ref<FileUploadApi> \| undefined;` | optional |  |

### FilesChangeDetails

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FilesChangeDetails

```ts
interface FilesChangeDetails {
    readonly source: 'pointer' | 'api';
    readonly reason: 'add' | 'remove' | 'clear' | 'status' | 'progress' | 'reset';
}
```

Second argument of `onFilesChange`.

Members (2):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `source` | `readonly source: 'pointer' \| 'api';` |  |  |
| `reason` | `readonly reason: 'add' \| 'remove' \| 'clear' \| 'status' \| 'progress' \| 'reset';` |  |  |

### UploadContext

Kind: interface

Page: https://salmonui.dev/docs/reference/inputs/file-upload/UploadContext

```ts
interface UploadContext {
    readonly signal: AbortSignal;
    readonly onProgress: (loaded: number, total: number) => void;
}
```

Second argument of the transport.

Members (2):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `signal` | `readonly signal: AbortSignal;` |  |  |
| `onProgress` | `readonly onProgress: (loaded: number, total: number) => void;` |  | Reports progress; applied at most once per frame. |

## Types

### FileErrorCode

Kind: type

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FileErrorCode

```ts
type FileErrorCode = 'type' | 'tooSmall' | 'tooLarge' | 'tooMany' | 'upload' | 'remove';
```

Why a file is `invalid` (`type`, `tooSmall`, `tooLarge`, `tooMany`) or failed (`upload`, `remove`).

### FilePreload

Kind: type

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FilePreload

```ts
type FilePreload = Partial<FileEntry> & {
    readonly name: string;
    readonly size: number;
};
```

A preloaded entry (`defaultFiles` / `files`): status defaults to `'uploaded'`.

Members (11):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `id` | `id?: string;` | optional, inherited |  |
| `name` | `name: string;` | inherited |  |
| `size` | `size: number;` | inherited |  |
| `type` | `type?: string;` | optional, inherited |  |
| `file` | `file?: File \| null;` | optional, inherited | The picked or dropped file; `null` for preloaded entries. |
| `path` | `path?: string;` | optional, inherited | Folder-relative path for directory picks and drops, else the name. |
| `status` | `status?: FileStatus;` | optional, inherited |  |
| `progress` | `progress?: number;` | optional, inherited | 0..1 while uploading, 1 when uploaded. |
| `error` | `error?: FileErrorCode \| null;` | optional, inherited |  |
| `reason` | `reason?: unknown;` | optional, inherited | The transport's rejection reason (status `error`, or error `remove`). |
| `result` | `result?: unknown;` | optional, inherited | The transport's resolved value (status `uploaded`). |

### FileStatus

Kind: type

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FileStatus

```ts
type FileStatus = 'queued' | 'uploading' | 'uploaded' | 'error' | 'canceled' | 'invalid';
```

Status of one file entry.

### FileUploadLocaleText

Kind: type

Page: https://salmonui.dev/docs/reference/inputs/file-upload/FileUploadLocaleText

```ts
type FileUploadLocaleText = FieldValidationText & {
    readonly [K in TextKey]?: string;
};
```

Strings: the trigger and buttons, one per status and one
per error code; `{0}` is the file name, the limit or the count.

Members (35):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `validationCreditcard` | `validationCreditcard?: string;` | optional, inherited |  |
| `validationDate` | `validationDate?: string;` | optional, inherited |  |
| `validationDateIso` | `validationDateIso?: string;` | optional, inherited |  |
| `validationDigits` | `validationDigits?: string;` | optional, inherited |  |
| `validationEmail` | `validationEmail?: string;` | optional, inherited |  |
| `validationMax` | `validationMax?: string;` | optional, inherited |  |
| `validationMaxLength` | `validationMaxLength?: string;` | optional, inherited |  |
| `validationMin` | `validationMin?: string;` | optional, inherited |  |
| `validationMinLength` | `validationMinLength?: string;` | optional, inherited |  |
| `validationNumber` | `validationNumber?: string;` | optional, inherited |  |
| `validationPattern` | `validationPattern?: string;` | optional, inherited |  |
| `validationRange` | `validationRange?: string;` | optional, inherited |  |
| `validationRangeLength` | `validationRangeLength?: string;` | optional, inherited |  |
| `validationRequired` | `validationRequired?: string;` | optional, inherited |  |
| `validationTel` | `validationTel?: string;` | optional, inherited |  |
| `validationUrl` | `validationUrl?: string;` | optional, inherited |  |
| `validationValidate` | `validationValidate?: string;` | optional, inherited |  |
| `browse` | `browse?: string;` | optional, inherited |  |
| `cancelFile` | `cancelFile?: string;` | optional, inherited |  |
| `canceled` | `canceled?: string;` | optional, inherited |  |
| `clearAll` | `clearAll?: string;` | optional, inherited |  |
| `error` | `error?: string;` | optional, inherited |  |
| `filesAdded` | `filesAdded?: string;` | optional, inherited |  |
| `queued` | `queued?: string;` | optional, inherited |  |
| `remove` | `remove?: string;` | optional, inherited |  |
| `removeFile` | `removeFile?: string;` | optional, inherited |  |
| `retryFile` | `retryFile?: string;` | optional, inherited |  |
| `tooLarge` | `tooLarge?: string;` | optional, inherited |  |
| `tooMany` | `tooMany?: string;` | optional, inherited |  |
| `tooSmall` | `tooSmall?: string;` | optional, inherited |  |
| `type` | `type?: string;` | optional, inherited |  |
| `upload` | `upload?: string;` | optional, inherited |  |
| `uploadAll` | `uploadAll?: string;` | optional, inherited |  |
| `uploaded` | `uploaded?: string;` | optional, inherited |  |
| `uploading` | `uploading?: string;` | optional, inherited |  |

### RemoveTransport

Kind: type

Page: https://salmonui.dev/docs/reference/inputs/file-upload/RemoveTransport

```ts
type RemoveTransport = (file: FileEntry, context: {
    readonly signal: AbortSignal;
}) => Promise<unknown>;
```

Optional server removal of an uploaded file.

### UploadTransport

Kind: type

Page: https://salmonui.dev/docs/reference/inputs/file-upload/UploadTransport

```ts
type UploadTransport = (file: File, context: UploadContext) => Promise<unknown>;
```

The transport: resolves when the server accepted the file.
