react-filepond

repository·master·Indexed 24 days ago

https://github.com/pqina/react-filepond

A React wrapper for the FilePond JavaScript library, providing an accessible and responsive file upload component. It supports features such as async uploading, image optimization, and plugin integration via registerPlugin. The package includes the FilePond component for functional and class components, as well as the FileStatus enum for tracking file states.

Tokens
2.6K
Snippets
4
Records
9
Agent score
84%

What's inside react-filepond

  1. Basic setup with React Hooks

    master

    To implement FilePond in a functional component using Hooks, follow these steps:

    1. Import FilePond and registerPlugin from react-filepond.
    2. Import the core FilePond CSS: filepond/dist/filepond.min.css.
    3. (Optional) Install and import FilePond plugins (e.g., filepond-plugin-image-preview) along with their respective CSS.
    4. Use registerPlugin to activate the plugins.
    5. Use the <FilePond /> component, managing the file state via onupdatefiles.
    import React, { useState } from 'react'
    import ReactDOM from 'react-dom'
    
    // Import React FilePond
    import { FilePond, registerPlugin } from 'react-filepond'
    
    // Import FilePond styles
    import 'filepond/dist/filepond.min.css'
    
    // Import the Image EXIF Orientation and Image Preview plugins
    // Note: These need to be installed separately
    // `npm i filepond-plugin-image-preview filepond-plugin-image-exif-orientation --save`
    import FilePondPluginImageExifOrientation from 'filepond-plugin-image-exif-orientation'
    import FilePondPluginImagePreview from 'filepond-plugin-image-preview'
    import 'filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css'
    
    // Register the plugins
    registerPlugin(FilePondPluginImageExifOrientation, FilePondPluginImagePreview)
    
    // Our app
    function App() {
      const [files, setFiles] = useState([])
      return (
        <div className="App">
          <FilePond
            files={files}
            onupdatefiles={setFiles}
            allowMultiple={true}
            maxFiles={3}
            server="/api"
            name="files" /* sets the file input name, it's filepond by default */
            labelIdle='Drag & Drop your files or <span class="filepond--label-action">Browse</span>'
          />
        </div>
      )
    }
  2. Basic setup with React Class Components

    master

    To implement FilePond in a class component, use a ref to access the FilePond instance and manage file state within this.state. Use the oninit prop to handle initialization and onupdatefiles to sync the component state with the current file list.

    import React, { useState } from 'react'
    import ReactDOM from 'react-dom'
    
    // Import React FilePond
    import { FilePond, registerPlugin } from "react-filepond";
    
    // Import FilePond styles
    import "filepond/dist/filepond.min.css";
    
    // Import the Image EXIF Orientation and Image Preview plugins
    // Note: These need to be installed separately
    import FilePondPluginImageExifOrientation from "filepond-plugin-image-exif-orientation";
    import FilePondPluginImagePreview from "filepond-plugin-image-preview";
    import "filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css";
    
    // Register the plugins
    registerPlugin(FilePondPluginImageExifOrientation, FilePondPluginImagePreview);
    
    // Our app
    class App extends Component {
      constructor(props) {
        super(props);
    
        this.state = {
          // Set initial files, type 'local' means this is a file
          // that has already been uploaded to the server (see docs)
          files: [
            {
              source: "index.html",
              options: {
                type: "local"
              }
            }
          ]
        };
      }
    
      handleInit() {
        console.log("FilePond instance has initialised", this.pond);
      }
    
      render() {
        return (
          <div className="App">
            <FilePond
              ref={ref => (this.pond = ref)}
              files={this.state.files}
              allowMultiple={true}
              allowReorder={true}
              maxFiles={3}
              server="/api"
              name="files" /* sets the file input name, it's filepond by default */
              oninit={() => this.handleInit()}
              onupdatefiles={fileItems => {
                // Set currently active file objects to this.state
                this.setState({
                  files: fileItems.map(fileItem => fileItem.file)
                });
              }}
            />
          </div>
        );
      }
    }
  3. Manage the example project with npm scripts

    master

    The example project is built using Create React App. You can use the following npm scripts to manage the development lifecycle:

    • Development: Use npm start to run the app in development mode. The app will be available at http://localhost:3000 and will reload automatically on edits.
    • Testing: Use npm test to launch the test runner in interactive watch mode.
    • Production Build: Use npm run build to create an optimized, minified production build in the build folder.
    • Ejecting: Use npm run eject to remove the single build dependency and copy all configuration files (Webpack, Babel, etc.) directly into your project for full control. Warning: This is a one-way operation and cannot be undone.
    npm start
    npm test
    npm run build
    npm run eject
  4. Access FilePond instance methods via FilePond component

    master

    The FilePond React component exposes the underlying FilePond instance methods directly on the component instance. This allows you to call core FilePond API methods from a React ref.

    Note that certain methods are filtered out to prevent conflicts with React's internal lifecycle. The following methods are not exposed directly via the component instance:

    • setOptions
    • on / off / onOnce
    • appendTo / insertAfter / insertBefore
    • isAttachedTo
    • replaceElement / restoreElement
    • destroy
  5. FilePond Component Props

    master

    The <FilePond /> component accepts several props to control its behavior:

    • files: An array of files to display in the component.
    • onupdatefiles: Callback function triggered when the file list changes. In functional components, this can be used to update state directly. In class components, it is typically used to map fileItems back to the state.
    • allowMultiple: Boolean indicating if multiple files can be selected.
    • maxFiles: The maximum number of files allowed.
    • server: The server endpoint URL for async uploading (e.g., "/api").
    • name: The name of the file input (defaults to "filepond").
    • labelIdle: The label displayed when no files are present. Supports HTML for custom styling (e.g., labelIdle='Drag & Drop or <span class="filepond--label-action">Browse</span>').
    • allowReorder: Boolean indicating if files can be reordered.
    • oninit: Callback function triggered when the FilePond instance is initialized.
    • ref: Used to access the underlying FilePond instance.
  6. Register FilePond plugins

    master
    To extend FilePond's functionality (e.g., adding image editing or file type validation), you must use the registerPlugin method exported from react-filepond. This method should be called before or during the application setup to ensure plugins are available to the FilePond component.
  7. Use the FilePond React component

    master

    The FilePond component is the primary way to integrate FilePond into a React application. It wraps a standard HTML file input and initializes a FilePond instance automatically when the component mounts.

    All props passed to the <FilePond /> component are forwarded to the underlying FilePond instance as configuration options via setOptions.

    Common props include:

    • name: The name of the file input.
    • id: The ID of the file input.
    • className: CSS classes for the input.
    • allowMultiple: Boolean to allow multiple file selection.
    • required: Boolean to make the input required.
    • acceptedFileTypes: String defining accepted file types (passed to the accept attribute of the input).
    • captureMethod: Specifies the capture method for mobile devices.