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-named-arrays

v0.4.0

Published

An extension of react-final-form-arrays that allows you to access elements inside an array by 'names'

Downloads

348

Readme

🏁 React Final Form Named Arrays

styled with prettier


Provides a component very similar to FieldArray, but that allows you to refer to items within the array by a unique key as well as by an index. This repo is for all intents and purposes a fork of react-final-form-arrays. The only reason it is not an actual fork is so that I can keep a separate fork with which to continue contributing to react-final-form-arrays.

Motivation and Philosophy

This component was born out of a need to asynchronously update values within a Field Array, whilst the order of the array is subject to change. A callback that uses an index is obviously going to cause problems, and so some kind of unique identifier is required.

(The actual use case, was uploading multiple files at the same time. We want to update the progress of individual files, but calling fields.update(index, value) would produce undesirable results if one upload was deleted whilst another was in progress - as the array indicies would all shift). We therefore need to be able to update by referring to some sort of unique key, rather than an index.

The basic idea is that each element in the array has a name (key), that can be used to refer to it's corresponding element. The FieldArray component keeps track of the names in a NAME_LIST stored in the fields meta data. Any changes made to the array are kept track of and applied to the NAME_LIST.

Installation

npm install react-final-form-named-arrays

API

react-final-form-named-arrays exports a component (FieldArray), a hook (useFieldArray), and an objects containing Mutators (see final-form docs). These three exports behave are very similar to the corresponding entities from react-final-form-arrays however there are a few key differences:

  • FieldArray and useFieldArray accept an additional prop getItemName (see below).
  • If the getItemName prop is absent, the behaviour will remain the same as a normal FieldArray
  • If getItemName is present:
    • The remove and update mutators can optionally be called with a string, instead of an index.
    • fields.map returns an iterator with an additional argument (value) - further detail below

getItemName?: (value: any, values: ?any[]) => string

Under the hood, FieldArray keeps track of an array of keys (or names) for all of the items in your array. Every time the array is rearranged in some way, the array of names is also updated. The allows you to call functions like update or remove with a key rather than an index. getItemName is the function used to assign a name to each item in your list. It is run only once for each item - either when the form is (re)initialised or when the item is added to the array. It is called with the following arguments:

value: is the item that is being added to the array values: is the value of the whole array. When items are being added to an array, the name is generated before it is added, so values will refer to the array before that item is added. When the array of names is seeded from initialValues values will be equal to initialValues

getItemName must return a string. It is important that the item 'name' be unique, and should be determined in such a way that

Mutators

// NEED TO ADD SOMETHING ABOUT USING MUTATORS WHEN NOT CURRIED VIA FIELDS E.G. USING THEM FROM 'FORM'

Example

General Warning about initial values

// Need to add some warnings about using changeValue

Roadmap

I am going to test-drive 'named-fields' as it is for a little while and see how well things work before taking it any further. There are a few changes I'm considering, but think it best just to try it out for a bit first.

  1. Add all of the mutators (concat, insert, removeBatch, shift, unshift, swap)
  2. Consider if move, should accept names as well as indexes.
  3. Improve testing of async situation