impl/typescript.tsx
3,280 bytes · the TypeScript implementation · view raw
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
"use client";
import { useId, type JSX, type ReactNode } from "react";
import { formatFileSize, validateFiles, type FileInfo } from "./form_file_check.ts"; ← from form.file-check ^1.0.0 · built alongside by fune
import { FormField, fieldIds } from "./react_form_form_field.ts"; ← from react.form.form-field ^1.0.0 · built alongside by fune
import type { FileUploadProps } from "./react_form_file_upload_types.ts";
function present(node: ReactNode): boolean {
return node !== null && node !== undefined && node !== false && node !== "";
}
/**
* The GOV.UK file upload: a label, hint and error message tied to a native
* file input, whose accept and multiple come from the same FileRules that
* validateFiles checks, in the browser and again on the server. A file input
* cannot be controlled, so the app keeps what onChange reports and passes it
* back as files: they are listed under the input with their sizes, each one
* in error marked and its message beside it. The bytes go straight to storage
* with a signed URL (storage.signed-url): onFiles hands over the File objects
* for that, and this component never reads them.
*/
export function FileUpload(props: FileUploadProps): JSX.Element {
const auto = useId();
const { label, rules, hint, files, showError, error, onChange, onFiles, onBlur, disabled, className } = props;
const id = props.id ?? auto;
const chosen: readonly FileInfo[] = files ?? [];
// Checked on every render, so a bad rule fails loudly at once.
const check = validateFiles(chosen, rules);
const checking = !present(error) && showError === true;
const shown: ReactNode = present(error) ? error : checking && !check.valid ? check.message : null;
const ids = fieldIds(id, present(hint), present(shown));
const fileError = (index: number) => (checking ? check.errors.find((e) => e.index === index) : undefined);
return (
<FormField id={id} label={label} hint={hint} error={shown} className={className}>
<input
className={ids.error ? "fune-file-upload fune-file-upload--error" : "fune-file-upload"}
id={ids.control}
name={props.name ?? id}
type="file"
accept={rules.accept || undefined}
multiple={rules.multiple || undefined}
required={rules.required || undefined}
disabled={disabled}
aria-describedby={ids.describedBy ?? undefined}
aria-invalid={ids.error ? true : undefined}
onChange={(event) => {
const list = Array.from(event.target.files ?? []);
onChange?.(list.map((f) => ({ name: f.name, size: f.size, mimeType: f.type })));
onFiles?.(list);
}}
onBlur={onBlur ? () => onBlur() : undefined}
/>
{chosen.length > 0 ? (
<ul className="fune-file-list" id={`${id}-files`} aria-live="polite">
{chosen.map((file, index) => {
const problem = fileError(index);
return (
<li className={problem ? "fune-file-list-item fune-file-list-item--error" : "fune-file-list-item"} key={`${index}-${file.name}`}>
<span className="fune-file-name">{file.name}</span> <span className="fune-file-size">{formatFileSize(file.size)}</span>
{problem ? <span className="fune-file-error">{problem.message}</span> : null}
</li>
);
})}
</ul>
) : null}
</FormField>
);
}