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

namespaced-fieldset

v0.1.1

Published

A tiny React utils which gives uncontrolled <input> elements a power of namespaces, and allows constructing complex objects

Downloads

7

Readme

namespaced-fieldset

A tiny React utils which gives uncontrolled <input> elements a power of namespaces, and allows constructing complex objects

Motivation

When you create a form in React, you'd have to choose either of following strategies:

  • Controlled <input>s: Manage user's inputs in React states by yourself, calling setState() for every onChange events and setting value. On submit, refer to the states.
  • Uncontrolled <input>s: Pass defaultValue prop initially and let browser manage user's inputs for the rest. On submit, you construct user's inputs somehow.

This library comes in to help construct object of user's inputs from uncontrolled <input>s, while native <input>s only gives you flat key-value.

Demo

Try out live demo (source code is here)

Installation

npm i namespaced-fieldset

How it works

You can define namespace with <Fieldset>, which respects native <fieldset> for grouping inputs, then <Input>s are aware of namespaces defined in ancestor nodes.

<form>
    <Fieldset namespace="person[0]">
        <legend>first person</legend>
        <Input name="name" value="Jane" />
        <Input name="age" value="31" />
    </Fieldset>
    {/* You can also choose not to render <fieldset> with the headless component */}
    <Fieldset.Headless namespace="person[1]">
        <Input name="name" value="Jack" />
        <Input name="age" value="30" />
    </Fieldset.Headless>
</form>

Then you'll get inputs with namespaced names like this:

<form>
    <fieldset>
        <legend>first person</legend>
        <input name="person[0].name" value="Jane">
        <input name="person[0].age" value="31">
    </fieldset>
    <input name="person[1].name" value="Jack">
    <input name="person[1].age" value="30">
</form>

Finally, you can construct an object from form element using constructFromFormData():

constructFromFormData(new FormData(formElement));
// ...gives you:
({
    person: [
        { name: "Jane", age: "31" },
        { name: "Jack", age: "30" }
    ]
})

Refer to qs doc for how names are mapped to object keys.

Use with custom input-like components

You can turn your input-line components into ones which is aware of namespaces.

import {namespaceAware} from "namespaced-fieldset";

const DatePicker = ({name}) => (
    <div>
        {/* gorgeous UI here */}
        <input type="date" name={name} />
    </div>
);
// Turn into namespace-aware component
const NamespaceAwareDatePicker = namespaceAware(DatePicker);

const Page = () => {
    return (
        <Fieldset namespace="person[0]">
            <legend>first person</legend>
            <NamespaceAwareDatePicker name="birthday"/>
        </Fieldset>
    )
};

Use with Remix

This library greatly works with Remix, which embraces <form>s and Web standards. You can not only construct objects from <form>s on client side, but also on server side:

export const action = async ({request}) => {
    const text = await request.text();
    constructFromQueryString(text) // Benefit!
}
export default function () {
    return (
        <Form method="post">
            <Fieldset namespace="person">
                <legend>person</legend>
                <Input name="name" value="Jane"/>
                <Input name="age" value="31"/>
            </Fieldset>
        </Form>
    )
}
  1. User submits <Form>, then remix (or browser) serializes inputs to query string and sends it to server
  2. In action() or loader(), you can construct an object from the query string given by request.text()