Documentation Index
Fetch the complete documentation index at: https://mintlify.com/kokonut-labs/kokonutui/llms.txt
Use this file to discover all available pages before exploring further.
A React hook that simplifies file input management with built-in validation for file size and type, plus state management for selected files.
Installation
Manually copy the hook from/hooks/use-file-input.ts into your project.
Usage
API Reference
Parameters
The hook accepts a configuration object:| Parameter | Type | Default | Description |
|---|---|---|---|
accept | string | undefined | File type restriction (e.g., “image/*”, “application/pdf”) |
maxSize | number | undefined | Maximum file size in megabytes (MB) |
Return Value
Returns an object with:| Property | Type | Description |
|---|---|---|
fileName | string | Name of the selected file |
error | string | Error message if validation fails |
fileInputRef | RefObject<HTMLInputElement> | Ref to attach to the file input element |
handleFileSelect | (e: ChangeEvent<HTMLInputElement>) => void | Handler for file input change event |
validateAndSetFile | (file?: File) => void | Function to manually validate and set a file |
clearFile | () => void | Function to clear the selected file |
fileSize | number | Size of the selected file in bytes |
Features
- File type validation - Restrict uploads to specific file types
- Size validation - Prevent files larger than specified limit
- Error handling - Built-in error messages for validation failures
- Clear functionality - Easy file removal with state reset
- File metadata - Access to file name and size
Example with Drag and Drop
Use Cases
- File upload forms with validation
- Image upload with preview
- Document upload with size limits
- Profile picture upload
- Drag and drop file interfaces
Validation Rules
File Size
- Specified in MB
- Checked before setting file state
- Error message: “File size must be less than MB”
File Type
- Uses MIME type matching
- Supports wildcards (e.g., “image/*”)
- Error message: “File type must be “
Notes
- File validation happens immediately on selection
- The actual file object is not stored in state (only metadata)
- Access the file through the input ref when needed for upload
- Clear function resets all state including errors