File selection
Collect one or more files with accessible native selection and validation.
Default
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let defaultFileSelection =
fveField {
fveLabel {
_for "selected-file"
"File"
}
fveFileSelection {
_id "selected-file"
_name "selectedFile"
}
}This example also uses Field. See their Installation sections for any additional setup.
On this page
Installation
Copy this component and its required dependencies into the consumer-owned Components project.
dotnet fve add file-selection --config src/Acme.Components/fve.jsonAdd to src/Acme.Web/js/app.js once, alongside your other component imports:
import '../../Acme.Components/Components/Assets/file-selection.js';The CLI resolves dependencies and preserves consumer-authored files. See Installation for project initialization, Tailwind, the shared runtime, and building/loading your app entry.
Usage
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let defaultFileSelection =
fveField {
fveLabel {
_for "selected-file"
"File"
}
fveFileSelection {
_id "selected-file"
_name "selectedFile"
}
}FileSelection keeps a native chooser and always provides a quiet Clear action when files are selected. Clear is hidden while empty or unavailable for editing; required fields can still be cleared. Clearing returns focus to the chooser and emits native input/change events. Its accessible name identifies the field and, for a single selection, the filename. The native control owns the filename or multiple-file count—there is no automatic duplicate list. Field owns the label, help and error.
Validate a selection on change, or a missing required file when submitting—not when the file picker takes focus or is cancelled. accept only guides the picker. The validation example checks the filename extension for local feedback and uses setCustomValidity; your server validates the actual upload, including content, type and size. Selection alone does not upload anything.
A richer multi-file list can offer per-file Remove actions and, when useful, Clear all. Keep that upload/queue composition in your application rather than adding it to every file input.
Multiple files
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let fileSelectionExample =
fveField {
fveLabel {
_for "selected-files"
"Files"
}
fveFileSelection {
_id "selected-files"
_name "selectedFiles"
_accept ".csv,text/csv"
_multiple true
_ariaDescribedby "selected-files-help"
}
fveFieldDescription {
_id "selected-files-help"
"Choose one or more CSV files."
}
}This example also uses Field. See their Installation sections for any additional setup.
Validation
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let pdfSelectionValidation =
"const file = el.files[0]; const message = !file ? 'Select a PDF document.' : !file.name.toLowerCase().endsWith('.pdf') ? 'The selected file must be a PDF.' : ''; el.setCustomValidity(message); $_pdf_selection_error = message;"
let invalidFileSelectionExample =
fveField {
fveLabel { _for "required-file"; "Document (required)" }
fveFileSelection {
_id "required-file"
_name "requiredFile"
_accept ".pdf,application/pdf"
_required true
_ariaDescribedby "required-file-help"
_dataSignals "{_pdf_selection_error: ''}"
_dataOn ("change", pdfSelectionValidation)
_dataOn ("invalid", pdfSelectionValidation)
_dataOn ("reset", ["window"], "if (evt.target === el.form) queueMicrotask(() => { if (!evt.defaultPrevented) { el.setCustomValidity(''); $_pdf_selection_error = ''; } })")
_dataAttr ("aria-invalid", "$_pdf_selection_error ? 'true' : null")
_dataAttr ("aria-describedby", "$_pdf_selection_error ? 'required-file-help required-file-error' : 'required-file-help'")
}
fveFieldDescription { _id "required-file-help"; "PDF only." }
fveFieldError {
_id "required-file-error"
_style "display:none"
_dataShow "$_pdf_selection_error"
_dataText "$_pdf_selection_error"
}
}This example also uses Field. See their Installation sections for any additional setup.
Disabled
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let disabledFileSelectionExample =
fveField {
fveLabel {
_for "disabled-files"
"Files unavailable"
}
fveFileSelection {
_id "disabled-files"
_name "disabledFiles"
_disabled true
}
}This example also uses Field. See their Installation sections for any additional setup.
API reference
module FileSelectionDsl =
val fveFileSelection : ComponentElementBuilder