HextaUI

File input (UI SDK): Controlled

Pass value and onValueChange. A controller from createFileInputController() opens the picker from any button, and clears or adds files.

No files
"use client"

import * as React from "react"

import { Button } from "@/components/ui/button"
import {
  FileInput,
  FileInputItem,
  FileInputList,
  createFileInputController,
  formatFileSize,
} from "@/components/ui/ui-sdk-file-input"

function UiSdkFileInputControlled() {
  const [files, setFiles] = React.useState<File[]>([])
  const [picker] = React.useState(createFileInputController)
  const total = files.reduce((sum, file) => sum + file.size, 0)

  return (
    <div className="flex w-full max-w-md flex-col gap-3">
      <div className="flex flex-wrap items-center gap-2">
        <Button size="sm" variant="outline" onClick={() => picker.open()}>
          Choose files
        </Button>
        <Button
          size="sm"
          variant="ghost"
          disabled={files.length === 0}
          onClick={() => picker.clear()}
        >
          Clear
        </Button>
        <span className="ms-auto text-sm text-muted-foreground tabular-nums">
          {files.length === 0
            ? "No files"
            : `${files.length} ${files.length === 1 ? "file" : "files"}, ${formatFileSize(total)}`}
        </span>
      </div>
      <FileInput
        multiple
        controller={picker}
        value={files}
        onValueChange={(value) => setFiles(value)}
      >
        <FileInputList aria-label="Chosen files">
          <FileInputItem />
        </FileInputList>
      </FileInput>
    </div>
  )
}

export { UiSdkFileInputControlled }

This adds the example as components/examples/ui-sdk-file-input/controlled.tsx, together with the File input component and the HextaUI theme tokens it needs.

pnpm dlx shadcn@latest add https://hextaui.com/r/ui-sdk-file-input-controlled.json

To add only the component, follow the File input installation.

Props in this example

The File input props this example sets, from the API reference.

PropTypeDefault
value
File[]–
onValueChangedetails has the reason and the added, removed and rejected files.
(value: File[], details) => void–
multiple
booleanfalse
controllerFrom createFileInputController().
FileInputController–

Every prop is in the File input API reference.

See them all, with keyboard support and accessibility notes, on the File input page.