# FileField

Crate: `libero`
Import: `use libero::components::{FileField, FileTriggerArgs, Files};`
Feature: `file-field` (off by default, turns on `chip`, `select`, `alert`, `modal`, `action-icon`, `button`, `dialog`, `loader`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/form/file_field>
Index: [index.md](index.md) lists every other page
Description: Files picked from the system dialog or dropped on the control, as a one-line input or a drop surface.

Files picked from the system dialog or dropped on the control. It shows
`value` and asks for the next files through `onchange`. Both carry a `Files`,
so a single-file field and a `multiple` one read the same. `max_size` and
`max_files` refuse files past a limit; other checks go in `onchange`, where the
files are already in hand. Under Blitz the field opens the system file dialog.

## Usage

```rust
use dioxus::prelude::*;
use libero::components::{FileField, Files};

#[component]
fn Demo() -> Element {
    let mut files = use_signal(Files::default);

    rsx! {
        FileField {
            placeholder: "No file picked",
            value: files(),
            onchange: move |picked| files.set(picked),
            label: "Attachment",
        }
    }
}
```

## Props

### `FileField`

| Prop | Type | Default | Description |
|---|---|---|---|
| `value` | `Files` | empty | Strictly controlled. Pair it with `onchange`. Takes a `FileData`, an `Option<FileData>` or a `Vec<FileData>`. |
| `multiple` | `bool` | `false` | Lets the user pick and drop several files, each drawn as a chip. A single-file field shows the file name, and a new pick replaces it. |
| `max_files` | `Option<usize>` | - | With `multiple`, the most files a pick or a drop keeps; the rest are refused with `RejectReason::TooMany` and said aloud. |
| `max_size` | `Option<u64>` | - | The largest file in bytes a pick or a drop keeps; a larger one is refused with `RejectReason::TooLarge` and said aloud, the limit written as a size, "5.0 MB". |
| `variant` | `FileFieldVariant` | `input` | `input` is one line in the field frame. `dropzone` is a tall surface to drop onto or click, with the files as cards below it. A single-file dropzone hides its surface while it holds a file. |
| `accept` | `String` | - | The file types to take, such as `.pdf`, `image/png`, `image/*` or a comma-separated list. It applies to the picker and to a drop: a file of another type is refused, and the field says so. A dropzone also shows them as a hint under its prompt. |
| `capture` | `String` | - | Asks a phone for a fresh capture, `user` or `environment`. |
| `placeholder` | `String` | - | The `input` variant's Browse button text while nothing is picked. It is the dropzone's prompt when `children` is empty, which a `multiple` dropzone keeps after a pick. A dropzone with neither says the localization's `file_field.drop_file` or `drop_files`; the `input` variant shows nothing. |
| `clearable` | `bool` | `true` | Shows an x that empties the field; a single-file field's x names its file, "Clear report.pdf" (`file_field.clear_file`). `input` variant only: a dropzone's cards each have their own x. |
| `loading` | `bool` | `false` | Shows a `Loader` while an upload runs and marks the field busy. It blocks nothing, `disabled` does that. |
| `crop` | `CropOptions` | - | A single picked or dropped image opens in an `ImageCropper` dialog first, its box centred at 80% of the largest `aspect` allows: Apply hands `onchange` the cut file, Cancel drops it. A crop that fails keeps the dialog open on an error and hands over nothing. Under Blitz an AVIF passes through uncropped, and a WebP is cut lossless, so it can be larger than on the web. Ignored on a `multiple` field. `CropOptions` sets `aspect`, `shape`, `pan`, `max_size`, `overhang` and `backdrop`, as on `ImageCropper`. Needs the `image-cropper` feature: without it the file passes uncropped, with a warning in a debug build. |
| `oncrop` | `EventHandler<CropEvent>` | - | The box picked in the crop dialog, `event.rect`, and the file as picked, `event.original`, before the cut file reaches `onchange`. Keep the original to crop it again, or to upload it beside the cut. |
| `selection` | `Callback<SelectionArgs<FileData>, Element>` | `Chip`, or the file name | Draws one picked file, remove control included. `args.remove` removes it. Unset, a multiple `input` field draws a chip, a single one the file name, and a dropzone a card under its surface. |
| `trigger` | `Callback<FileTriggerArgs, Element>` | - | Draws the app's own control, an avatar or a button, in place of the Browse surface, at its own width: no chips, cards, clear x or drop target. The field keeps its label, helper, error, `name`, `accept`, `max_size`, `capture` and `crop`. Make it a focusable `button`, spread `args.attributes` onto it (a libero `Button` takes them as `attributes`), and call `args.open` from its click: a browser opens no picker outside a user gesture. The trigger starts at the field's start and follows its `align-items`; `FileFieldPart::Trigger` styles its box. Opening a `Menu` from it works too: `menu.a11y_attributes_on(args.attributes)` keeps the field's `id`, so the field's label names the menu. |
| `name` | `FieldName<Files>` | - | What the files post as. A removed file stops posting. A path such as `Claim::FIELDS.receipts()` also binds the files to the surrounding `Form`'s value when the field has no `onchange`. |
| `onchange` | `EventHandler<Files>` | - | Fires with the files the field should hold next, after a pick, a drop, a removal or a clear. A pick or a drop hands over only the files it brought, so on a `multiple` field it replaces the earlier ones, as a native file input does. A removal or a clear hands over what is left. |
| `onreject` | `EventHandler<Vec<FileRejection>>` | - | Fires with the files a pick or a drop brought that the field refused: a type `accept` excludes, a file past `max_files` or past the first on a field without `multiple`, or a file larger than `max_size`. Each `FileRejection` holds the `file` and a `reason`, `RejectReason::Type`, `TooMany` or `TooLarge`. The field already announces the refusal politely, in the localization's `file_field.rejected_type`, `rejected_many`, `rejected_max_files` (its limit as `files` counts it) and `rejected_large`. Use it to show the refusal on screen, for example in `status`. Upgrading from 0.6: `rejected_max` is `rejected_max_files`, its `{max}` holding the noun, so drop "files" from your own template. |
| `validate` | `Validators<Files>` | - | Rules over the files, shown once the field loses focus or its form is submitted. |
| `children` | `Element` | - | The dropzone's prompt. The `input` variant shows `placeholder` instead. |
| `size` | `Responsive<Size>` | `md` | Control height, font size and the chips' size. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`; the file chips follow it. |
| `radius` | `Responsive<ThemeAwareValue>` | `sm` | Corner radius of the frame. Or any CSS, e.g. `radius: "0"`. Or one per breakpoint, `radius: responsive(Size::Sm).md(Size::Xl)`. |
| `label` | `Caption` | - | The field's caption. It names the field and its Browse button. |
| `description` | `Caption` | - | Between the label and the control. Which files are wanted. |
| `label_trailing` | `Caption` | - | Content in the label row, outside the `<label>`, at the row's end: a button cannot sit inside the label. Takes a string or an `Element`. |
| `description_position` | `DescriptionPosition` | `Above` | Where the description sits: `above` the control, or `below` it. `below` keeps the controls of side-by-side fields level when only some have a description; give the row `align: "flex-start"`. Unset, the theme's `field.description_position`. A checkbox, switch or radio draws its description under the label either way. |
| `helper` | `Caption` | - | Under the control. Size limits, formats. |
| `status` | `FieldStatus` | `Valid` | Validation state, under the helper. A bare `&str` is an error; an empty one or `None` is `Valid`. |
| `required` | `bool` | `false` | Marks the field required and adds an asterisk to the label; a field without a visible `label` shows none, so give a required field one. Inside a `Form`, an empty one fails the submit. |
| `disabled` | `bool` | `false` | Disables picking and dropping, and dims the field. |
| `readonly` | `bool` | `false` | Focusable and posted with the form, but not editable. `disabled` drops the field from the tab order and the post instead. |

Like every component, `FileField` also takes the shared props `sx`, `class`,
`style`, `states`, `hidden_from`, `visible_from`, and any extra HTML attributes.

### `SelectionArgs`

| Prop | Type | Default | Description |
|---|---|---|---|
| `value` | `FileData` | - | The file this call draws. |
| `remove` | `Callback<()>` | - | Drops this file from the value. |
| `disabled` | `bool` | - | The field is disabled: `remove` does nothing, so draw no remove control. |
| `readonly` | `bool` | - | The field is read-only: `remove` does nothing, so draw no remove control. |

### `FileTriggerArgs`

| Prop | Type | Default | Description |
|---|---|---|---|
| `open` | `Callback<()>` | - | Opens the system picker. Call it inside the trigger's click. |
| `files` | `Files` | - | What the field holds, to draw an avatar or a name. |
| `disabled` | `bool` | - | The field is disabled or read-only: `open` does nothing. |
| `loading` | `bool` | - | The field's `loading`. |
| `attributes` | `Vec<Attribute>` | - | The field's id, its names, `aria-invalid`, `aria-busy` and the field's own attributes. Spread them onto the trigger. |

### `FileRejection`

| Prop | Type | Default | Description |
|---|---|---|---|
| `file` | `FileData` | - | The refused file. |
| `reason` | `RejectReason` | - | `Type`: `accept` excludes it. `TooMany`: it came past `max_files`, or past the first on a field without `multiple`. `TooLarge`: it is larger than `max_size`. |

### `Files`

| Method | Returns | Description |
|---|---|---|
| `one()` | `Option<FileData>` | The first file, a single-file field's whole value. |
| `into_vec()` | `Vec<FileData>` | Every file. |
| `deref` | `&[FileData]` | So `len`, `iter` and `is_empty` work directly. |

## Style API

Style a part with the `parts` prop, or address it as `[data-slot='…']` in your
own CSS. The names are stable. [Style API in Styling](styling.md#style-api)
explains how parts work.

| Part | `data-slot` | Description |
|---|---|---|
| `FileFieldPart::Label` | `label` | The label above the control. |
| `FileFieldPart::Required` | `required` | The required asterisk, in the label. |
| `FileFieldPart::LabelRow` | `label-row` | The row holding the label and its `label_trailing`; drawn only with a trailing slot. |
| `FileFieldPart::LabelTrailing` | `label-trailing` | The `label_trailing` slot, at the end of the label row. |
| `FileFieldPart::Description` | `description` | The caption between the label and the control. |
| `FileFieldPart::Frame` | `frame` | The `Input` variant's bordered box. |
| `FileFieldPart::Control` | `control` | The group holding the Browse button: inside the frame, or the dropzone's surface. |
| `FileFieldPart::Browse` | `browse` | The Browse button. |
| `FileFieldPart::Chip` | `chip` | One picked file's chip, `Input` variant. |
| `FileFieldPart::Trailing` | `trailing` | The loader and the clear button, `Input` variant. |
| `FileFieldPart::Card` | `card` | One picked file's card under the surface, `Dropzone` variant. |
| `FileFieldPart::Trigger` | `trigger` | The box round the app's `trigger`, at its content's width. |
| `FileFieldPart::Helper` | `helper` | The caption under the control. |
| `FileFieldPart::Status` | `status` | The validation message. |

## Accessibility

### Keyboard

| Key | Action |
|---|---|
| `Enter` or `Space` | On the Browse button: opens the picker. A click elsewhere on the field opens it too, except on a file of a `multiple` field. |
| `Left` or `Right` | `input` variant with `multiple`: moves along the files. |
| `Home` or `End` | `input` variant with `multiple`: jumps to the first or last file. |
| `Backspace` or `Delete` | `input` variant with `multiple`: removes the focused file. |
| `Left` | On the Browse button, with `multiple`: moves to the last file. |
| `Backspace` or `Delete` | On the Browse button: removes the last file. |

### Libero handles

- The field is a group named by its label, holding the picked files and a
  Browse button.
- In the `input` variant several files are one tab stop; a single file is
  none, Browse and clear act on it.
- A single-file dropzone holding its file hands its label, error and
  description to the card's remove button.
- In the `dropzone` variant each card's remove button is its own tab stop.
- A `readonly` field refuses a drop or a Browse press without a message: Browse
  says `aria-disabled` and no file is added.

### You must

- Without a `label`, pass `aria_label`, which names the group and the Browse button.
- A `trigger` is a focusable `button` carrying `args.attributes`, and calls `args.open` inside its click.

### Example

An attachment field, `FileField { label: "Attachment" }`: a group named
"Attachment". Browse is its tab stop: Enter opens the picker and Backspace
removes the picked file.

## Theme defaults

`FileFieldDefaults` on the theme.

| Field | Type | Description |
|---|---|---|
| `size` | `Size` | Default `size` when the prop is omitted. |
| `radius` | `Size` | Default `radius` when the prop is omitted. |
| `variant` | `FileFieldVariant` | Which control is drawn when the caller states no `variant`. |
| `clearable` | `bool` | Whether the clear button shows once a file is picked. |
| `dropzone_heights` | `Sizes<&'static str>` | Minimum height of the `Dropzone` surface, per size step. |

## CSS variables

The per-size scale is declared once. The level in effect resolves on the
surface and on the card list, so their children inherit it.

| Variable | Description |
|---|---|
| `--lsx-file-field-dropzone-height-<size>` | Surface height for that size step. |
| `--lsx-file-field-dropzone-height` | The height in effect. |
| `--lsx-file-field-padding` | The surface's and the cards' padding, off the field's scale. |
| `--lsx-file-field-radius` | The radius in effect, shared by the surface and the cards. |

## Data attributes

The field wrapper carries `size-*`, `radius-*`, and `disabled`, `required` and
the status token when they apply. The field's group carries the tokens below.

| Token | Condition |
|---|---|
| `multiple` | `multiple` is set, so the value slot wraps its chips. |
| `dragging` | A file is being dragged over the control. |
| `disabled` | `disabled` is set. |
