BNA

BNA UI
26

File Picker

A customizable file picker component with validation, preview, and multiple file support.

Installation

pnpm dlx bna-ui add file-picker

Usage

import { FilePicker } from '@/components/ui/file-picker';
<FilePicker
  onFilesSelected={(files) => console.log('Selected files:', files)}
  onError={(error) => console.error('Error:', error)}
  fileType='all'
  multiple={true}
  maxFiles={5}
  placeholder='Select your files'
/>

Examples

Default

Image Only

Single File

With Validation

Custom Styling

Controlled

With File Info

API Reference

FilePicker

The main file picker component.

PropTypeDefaultDescription
onFilesSelected(files: SelectedFile[]) => void-Callback when files are selected.
onError?(error: string) => void-Callback when an error occurs.
fileType?'image' | 'document' | 'all''all'Type of files to allow.
multiple?booleanfalseWhether to allow multiple file selection.
maxFiles?number10Maximum number of files to select.
maxSizeBytes?number10MBMaximum file size in bytes.
allowedExtensions?string[]-Array of allowed file extensions.
placeholder?string'Select files'Placeholder text for the picker button.
disabled?booleanfalseWhether the picker is disabled.
style?ViewStyle-Additional styles for the container.
showFileInfo?booleantrueWhether to show file size information.
accessibilityLabel?string-Accessibility label for the picker button.
accessibilityHint?string-Accessibility hint for the picker button.

SelectedFile

The file object structure returned by the component.

PropertyTypeDescription
uristringThe file URI.
namestringThe file name.
type?stringThe file MIME type.
size?numberThe file size in bytes.
mimeType?stringThe file MIME type.

useFilePicker Hook

A hook for managing file picker state programmatically.

const { files, addFiles, removeFile, clearFiles, totalSize, isValid, errors } =
  useFilePicker({
    maxFiles: 5,
    maxSizeBytes: 5 * 1024 * 1024, // 5MB
    allowedExtensions: ['pdf', 'doc', 'docx'],
    onError: (error) => console.error(error),
  });

Options

PropertyTypeDefaultDescription
maxFiles?number10Maximum number of files.
maxSizeBytes?number10MBMaximum file size in bytes.
allowedExtensions?string[]-Array of allowed file extensions.
onError?(error: string) => void-Callback when an error occurs.

Return Value

PropertyTypeDescription
filesSelectedFile[]Array of selected files.
addFiles(files: SelectedFile[]) => voidFunction to add files.
removeFile(index: number) => voidFunction to remove a file by index.
clearFiles() => voidFunction to clear all files.
totalSizenumberTotal size of all files in bytes.
isValidbooleanWhether the current state is valid.
errorsstring[]Array of validation errors.

Utility Functions

createFileFromUri

const file = await createFileFromUri(uri, 'custom-name.pdf');

validateFiles

const { valid, errors } = validateFiles(files, {
  maxSize: 5 * 1024 * 1024,
  allowedExtensions: ['pdf', 'doc'],
  maxFiles: 3,
});

File Types

The component supports three file type modes:

  • 'all' - All file types (default)
  • 'image' - Images only (jpg, jpeg, png, gif, webp, etc.)
  • 'document' - All file types with document picker

Validation

The FilePicker includes built-in validation for:

  • File size - Configurable maximum size per file
  • File extensions - Whitelist of allowed extensions
  • File count - Maximum number of files
  • MIME types - Automatic validation based on file type

Accessibility

The FilePicker component is built with accessibility in mind:

  • Proper accessibility labels and hints
  • Screen reader support for file information
  • Keyboard navigation support
  • Clear error messaging
  • Semantic button structure

Best Practices

  1. Set appropriate file size limits to prevent memory issues
  2. Use specific file type restrictions when possible
  3. Provide clear error messages to guide users
  4. Show file previews when relevant
  5. Handle loading states for better UX
  6. Validate files on both client and server side