Skip to content

useFile ​

Manages the state for a file input that is backed by the media library. Handles selection sync from the Vuex mediaLibrary store, drag-and-drop reorder, and item deletion.

File: vue/src/js/hooks/useFile.js
Props factory: makeFileProps


Usage ​

js
import { useFile, makeFileProps } from '@/hooks'

const props = defineProps({ ...makeFileProps() })
const {
  input,
  items,
  remainingItems,
  isDraggable,
  mediableActive,
  addLabel,
  deleteItem,
  deleteAll
} = useFile(props, context)

Props (via makeFileProps) ​

Extends makeInputProps and makeDraggableProps plus:

PropTypeDefaultDescription
mediaTypeString'file'Media type sent to the media library
nameStringrequiredField name — used as the media library slot key
itemLabelStringt('File')Label used in the add button
endpointString''Upload endpoint
maxNumber1Maximum number of files
noteString''Note text displayed in the input
fieldNoteString''Field-level note
filesizeMaxNumber0Maximum file size in bytes (0 = unlimited)
buttonOnTopBooleanfalseDisplay the add button above the file list

Returns ​

NameTypeDescription
inputRef<Array>The current file list (synced with modelValue)
itemsComputedRef<Array>Files selected via the media library store
remainingItemsComputedRef<Number>max - input.length
isDraggableComputedRef<Boolean>True when draggable and more than one file
mediableActiveRef<Boolean>Controls whether media library selections are applied
addLabelComputedRef<String>Localised add button label
deleteItem(index) => voidRemove file at index
deleteAll() => voidClear all files
(all useInput returns)id, updateModelValue, isEditing, etc.

Sync behaviour ​

The hook watches three sources and keeps them in sync:

  1. store.state.mediaLibrary.selected[props.name] — when the media library inserts files
  2. states.input — emits update:modelValue on change
  3. props.modelValue — external model updates are reflected in states.input

See Also ​