Skip to content

CdsFileInput

New in v4.0

A file field: the user attaches documents by a click, by dragging or by pasting from the clipboard. Around the native input type="file" the component gathers everything a form needs: a label, a description, messages, a counter, clearing and a filter by file types.

Storybook GitLab
Test coverage: 100% — good coverage
API in specs
81 / 81 props, events, slots and exposed members with a test
Test cases
106 unit · 4 browser
Known bugs
11 pinned by an it.fails test
Code coverage
100% lines · 92.1% branches measured in v4.0.0-alpha.13
A11y (axe)
11 recommendations in 12 stories measured in v4.0.0-alpha.11

If you need a prominent upload zone with a list of files, take CdsFileUpload; sending the files to the server is the job of CdsUploader.

Anatomy ​

The component is built on the same base as CdsTextInput: the label, the description, the messages, validation and the colors work the same way. What the field has of its own is the upload icon on the left (prepend-icon, upload by default), the names of the chosen files instead of the typed text and the file counter in the bottom block. Clearing (clearable) is on out of the box.

Variations ​

Label and hints ​

label names the field, the requirements for the files go into description, and placeholder hints that a file can be dragged into the field. A chosen file is shown by its name.

Several files ​

The multiple prop allows choosing several files at once — their names are listed separated by commas.

Showing as tags ​

The chips prop shows every file as a separate tag (CdsTag) — a list of several files reads more easily.

Size and counter ​

show-size adds the size to the file name, counter shows the number and the total size under the field. Long names are truncated in the middle — the threshold is set by truncate-length (22 characters by default).

Filter by type ​

filter-by-type accepts MIME types, wildcard types and extensions separated by commas (image/*,.pdf). The files that do not fit never get into the model — the rejected event reports them: show the user what exactly was rejected and why.

States ​

disabled blocks the field, readonly lets you look at the selection but not change it, loading shows an indicator — while the files are going to the server, for example.

Slots ​

The zones of the field match CdsTextInput: prepend / append outside the border, prepend-inner / append-inner inside it. The extra selection slot replaces the standard list of file names.

Slot nameDescription
labelLabel content (replaces label)
descriptionDescription content (replaces description)
prependElement before the field (replaces the upload icon)
appendElement after the field
prepend-innerElement at the start of the field, inside the border
append-innerElement at the end of the field, inside the border
selectionYour own display of the chosen files
clearYour own clear icon (with clearable)
loaderYour own loading indicator (with loading)
counterCounter content
detailsThe whole bottom block — messages and counter
messagesContent of the messages block

The selection slot receives the truncated file names (fileNames) and the total size (totalBytes, totalBytesReadable).

Guidelines ​

Scenarios ​

Submitting a form with attachments ​

The field takes part in CdsForm validation on a par with the others: rules receive the chosen files — check that the attachments are there and do not exceed the size limit before sending them to the server. chips and counter help the user keep track of the list.

An upload button without a field ​

The hide-input prop hides the field, leaving only the clickable icon — handy for embedding the upload into a toolbar or a comment line. Show the chosen files next to it yourself: here they are tags with closable, and a click on the cross removes a file from the model.

An image preview ​

filter-by-type="image/*" lets images through only, and the preview is built from the model value through URL.createObjectURL. Do not forget to release the previous URL — otherwise the chosen files pile up in the memory of the tab.

API ​

Props

NameTypeDefaultDescription
activeboolean | undefined—Active state of the field
activeColorstring | undefined—Color of the component in its active state
appendIconIconValue | undefined—Icon in the append slot, after the content
appendInnerIconIconValue | undefined—Icon in the inner append slot, after the content of the field
baseColorstring | undefined—Color of the component in its inactive state
bgColorstring | undefined—Background color of the component
centerAffixboolean | undefined—Vertically center the appendInner, prependInner, clearIcon and label elements.
chipsboolean | undefined—Show the chosen files as tags (CdsTag) instead of a list of names.
clearableboolean | undefinedtrueShows the clear button
clearIconIconValue | undefined'remove'Icon of the clear button
colorstring | undefined—Component color
counterboolean | undefined—Show a counter of the number (and, with showSize, of the total size) of the chosen files.
counterSizeStringstring | undefined'$cds.fileInput.counterSize'Localization key for the text of the counter with the file size.
counterStringstring | undefined'$cds.fileInput.counter'Localization key for the text of the file count counter.
descriptionstring | undefined—Description of the component, shown under the label
dirtyboolean | undefined—Marks that the value of the field was changed by the user
disabledboolean | null | undefinednullDisabled state
errorboolean | undefined—Error state
errorMessagesstring | readonly string[] | null | undefined[]Puts the component into the error state and passes custom error messages. They are merged with any checks defined in the rules prop. This field does not trigger validation.
filterByTypestring | undefined—Filtering of files by MIME type or extension (image/*,.pdf, for example). The rejected files do not get into the model.
focusedboolean | undefined—Controls the focus state of the component. When true, the component is considered focused and may apply the corresponding styles or behavior.
hideDetailsboolean | "auto" | undefined—Hides the details block (messages and hints). With the 'auto' value the block is hidden when there are no messages
hideInputboolean | undefined—Hide the field, leaving only the prepend icon (to use it as a button, for example).
hideMessagesboolean | undefined—Hide the messages under the input
hintstring | undefined—Hint text shown under the input
iconColorstring | boolean | undefined—Icon color. Accepts a boolean or a color string
idstring | undefined—Unique identifier of the component
labelstring | undefined—Label text of the component
loadingstring | boolean | undefined—Loading indicator for the component
maxErrorsstring | number | undefined1Maximum number of validation errors shown
maxWidthstring | number | undefined—Maximum component width
messagesstring | readonly string[] | undefined[]Messages shown under the input
minWidthstring | number | undefined—Minimum component width
modelValueFile | File[] | null | undefined(props) => props.multiple ? [] : nullValue of the model: an array of files (with multiple), a single file or null.
multipleboolean | undefined—Allow selecting several files.
namestring | undefined—Name of the input
persistentClearableboolean | undefined—Always show the clear button, not only on hover or focus
persistentHintboolean | undefined—Always show the hint, not only on focus
placeholderstring | undefined—Placeholder text shown in an empty field (while no files are chosen yet).
prependIconIconValue | undefined'upload'Icon in the prepend slot, before the content
prependInnerIconIconValue | undefined—Icon in the inner prepend slot, before the content of the field
readonlyboolean | null | undefinednullRead-only state
reverseboolean | undefined—Reverses the order of the elements inside the field
rulesreadonly ValidationRule[] | undefined[]Accepts an array of items of different types: functions, booleans and strings. Functions take the input value as an argument and must return either true / false, or a string with an error message. The input goes into the error state if a function returns false (or any value in the array is false) or is a string.
showSizeboolean | 1000 | 1024 | undefinedfalseShow the size of the chosen files. A boolean or the base: 1000 (kB) or 1024 (KiB).
sizenumber | "xs" | "sm" | "md" | "lg" | "xl" | undefined'md'Size of the component
textColorstring | undefined—Text color of the component
themestring | undefined—Component theme
truncateLengthstring | number | undefined22Maximum length of a file name before it is truncated (the characters at the start and at the end are kept).
validateOnValidateOn | undefined—Changes the type of the event that triggers validation
validationValueany—Value used when applying the validation rules
widthstring | number | undefined—Component width

Events

NameTypeDescription
click:control[e: MouseEvent]Fires on a click in the main area of the control.
mousedown:control[e: MouseEvent]Fires on a mouse button press in the main area of the control.
rejected[files: File[]]Fires when some of the files were rejected by the filterByType filter.
update:focused[focused: boolean]Fires when the element gains or loses focus.
update:modelValue[value: File | File[]]Fires when the list of the chosen files changes.

Slots

NameTypeDescription
appendCdsInputSlotAdds an element after the input
append-innerCdsInputDefaultSlotSlot added at the end of the input
clearCdsInputDefaultSlot & { props: Record<string, any>; }Slot for a custom clear icon (shown when the clearable prop is true).
counterCdsCounterSlotSlot for a custom counter.
description{ description: string | undefined; props: Record<string, any>; }Default slot of the CdsDescription component
detailsCdsInputSlotSlot for custom content that lets you customize how the messages are displayed
label{ label: string | undefined; props: Record<string, any>; }Default slot of the CdsLabel component
loaderLoaderSlotPropsSlot for a custom loader (shown when the loading prop is true).
messageCdsMessageSlotSlot for customizing the content of a message
messagesCdsInputSlotSlot for custom content that lets you customize how the messages are displayed
prependCdsInputSlotAdds an element before the input
prepend-innerCdsInputDefaultSlotSlot added at the start of the input
selectionCdsFileInputSelectionSlotSlot for a custom display of the chosen files.

Exposed

NameTypeDescription
controlRefHTMLElement | undefinedRef to the control of the input
fieldIconColorstring | undefinedIcon color
isDirtybooleanWhether the input is dirty
isDisabledbooleanWhether the input is disabled
isPurebooleanWhether the input is pure
isReadonlybooleanWhether the input is read-only
isValidboolean | nullWhether the input is valid
isValidatingbooleanWhether the input is being validated
reset() => Promise<void>Resets the input to its initial state
resetValidation() => Promise<void>Resets the validation of the input
validate(silent?: boolean) => Promise<string[]>Validates the input