Functional Weave
Code in Rust

react.form.file-upload@1.0.0

README.md

3,020 bytes · view raw

# react.form.file-upload

The GOV.UK file upload: a native `<input type="file">` with its label, hint
and error message, whose `accept` and `multiple` come from the same
`FileRules` that `form.file-check` checks. It is the UI and the checks only;
the bytes go straight to object storage with a signed URL from
`storage.signed-url` (an integration), so they never pass through your server.

```tsx
"use client";
import { FileUpload } from "#fune/react.form.file-upload@^1";

const rules = { required: true, accept: ".pdf,image/*", maxBytes: 10 * 1024 * 1024, multiple: true, maxFiles: 5 };
const [files, setFiles] = useState<FileInfo[]>([]);
const [raw, setRaw] = useState<File[]>([]);

<FileUpload id="evidence" label="Upload your evidence" hint="PDF or images, 10MB or smaller, up to 5 files"
  rules={rules} files={files} onChange={setFiles} onFiles={(f) => setRaw(f as File[])} showError={files.length > 0 || submitted} />

// On submit: the server checks the same FileInfo with validateFiles (any language),
// then signs one PUT URL per file (storage.signed-url's signUpload), and the browser
// PUTs each File to its URL.
```

- **A file input cannot be controlled.** The app keeps what `onChange`
  reports (each file's name, size and type) and passes it back as `files`:
  they are listed under the input, with their sizes (`formatFileSize`), and
  with `showError` each file in error is marked and its message shown beside
  it. `onFiles` hands over the browser's `File` objects for the upload; this
  component never reads them.
- **Check again on the server.** A file's name, size and type come from the
  browser and can be anything; `validateFiles` with the same rules on the
  server (and a size limit on the signed URL) is the real check, and neither
  says what a file actually contains.
- **Messages** are GOV.UK's: "Select a file", "The selected file must be a
  PDF", "The selected file must be 10MB or smaller", "The selected file is
  empty"; with several files, each names its file. The field shows the first;
  `error` shows another (such as "The selected file could not be uploaded –
  try again").
- **Accessible.** The label's `for`, `aria-describedby` (hint, then error),
  `aria-invalid` while an error shows, and the list of chosen files is
  `aria-live="polite"`, so a screen reader hears what was chosen.

It is a client component. Classes: those of `react.form.form-field`, plus
`fune-file-upload` (and `fune-file-upload--error`), `fune-file-list`,
`fune-file-list-item` (and `fune-file-list-item--error`), `fune-file-name`,
`fune-file-size` and `fune-file-error`.

Sources: GOV.UK Design System, "File upload"
https://design-system.service.gov.uk/components/file-upload/ ; HTML, the
accept attribute https://html.spec.whatwg.org/multipage/input.html#attr-input-accept .

## Notices

Portions derived from GOV.UK Frontend (https://github.com/alphagov/govuk-frontend), Copyright (c) 2017 Crown Copyright (Government Digital Service), under the MIT License; the full notice is in NOTICE.