npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-final-form-file-field

v0.0.6

Published

Easy add a file upload field to your final-form form.

Downloads

247

Readme

React Final Form File Field

styled with prettier


Easily add a file upload field to your react-final-form form. FileField is agnostic about; how you store your files in the field, how you upload files, and how you display the files / uploader. But provides tools to make this easier.

Installation

Update this when published

Examples

see Storybook

Basic Usage

The FilesField registers a field array, and provides props to render your file uploader.

<Form onSubmit={onSubmit} mutators={arrayMutators}>
    {({ handleSubmit }) => (
	<form onSubmit={handleSubmit}>
	    <FilesField name="files" onFileLoad={onFileLoad}>
		{({ uploadFiles, files }) => (
		    <>
			// render uploader
			<button onClick={uploadFiles}>Upload Files</button>

			//render files
			{files.map((name, index, file) => (<p>file</p>))}
		    </>
		)}
	    </FilesField>
	</form>
    )}
</Form>;

However you can also use the useFilesField hook to achieve just the same thing. You just have to make sure you render the input that is used under-the-cover, the hook provides everything you need to pass it though.

const MyFileUploader = ({fieldName, onFileLoad, config}) => {
    const { uploadFiles, inputProps, files } = useFilesField(
	fieldName,
	onFileAdd,
	config
    )
    return(
	<>
	    // this will be hidden
	    <input {...inputProps} />

	    // render uploader
	    <button onClick={uploadFiles}>Upload Files</button>

	    //render files
	    {files.map((name, index, file) => (<p>file</p>))}
	</>
   )
}

API

The API for react-final-form-file-field consists of 2 components (FileItem and FilesField and 1 hook (useFilesField).

FileItem

A component to represent a file. It selects the appropriate font-awesome icon based on the mime-type, and provides adds two buttons to delete and download the file.

Props:

  • fileName: (string) required.
  • removeFile: (function) required. An event handler that is passed to the delete buttons onClick. It should remove the file from the field.
  • downloadFile (string | function) optional. If downloadFile is a function, it will be called when the download file button is pressed. If downloadFile is a string, then the download button will be a link, and when clicked it will navigate to the value of downloadFile. If this prop is not supplied the download button will not be rendered.
  • inProgress: (bool) optional. If true, a progress bar will be displayed underneath the file (designed to show upload progress). If inProgress is true, then the progress prop must also be supplied.
  • progress : (number) optional. This should be a number between 0 and 100 representing the percentage to show on the progress bar.
  • mimeType: (string) optional. FileItem will attempt to display the most appropriate icon based on this value. If absent it will display a 'standard' file icon.

All other props will be passed to the li element that makes up FileItem.