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

kingpin-react-form

v0.6.1

Published

Super fast ReactJS forms with almost null overhead.

Downloads

25

Readme

Kingpin React Form

ts license CI codecov

Kingpin react form cover

Super fast ReactJS forms with almost null overhead.

Features

  • 🪶 Super light-weight - Zero dependencies (< 2kb gzip)
  • 🌲 Tree shakable
  • 🎯 Top rendering performance
  • 😎 Declarative and easy to read
  • 🔫 Battle tested
  • 🔌 Pluggable/Extensible
  • 🔖 Design System ready
  • ⚙️ Native Typescript types

In few lines

Kingpin is a form library that aims to make the writing of forms on React easy like writing them directly on a HTML file.

To do it Kingpin lets each input to be just declared and forces it to follow the Single Responsibility Principle of each element (each input field take care just of it self).

The SRP lets each field to render independently without causing unhandled re-render side effects.

The main <Form /> component is the only interface for managing user set data, this choice in order to lead to a better forms develop and managing all the data in a single place (each state could be listened with the still available onChange element event).

Read more in the documentation.

Getting started

Install the package with your favourite package manager.

npm i kingpin-react-form
yarn add kingpin-react-form

Then create your form.

import { Error, Form, FormResult, Input } from 'kingpin-react-form'
import { FormEvent } from 'react'

function App(): JSX.Element {
  const submit = (e: FormEvent<HTMLFormElement>, form: FormResult) => {
    e.preventDefault()
    console.log(data)

    // data: {
    //     isValid: true,
    //     payload: {
    //       email: "",
    //       password: "",
    //       terms-acceptance: true
    //     }
    // }
  }

  return (
    <Form onSubmit={submit}>
      <Input name="email" type="email" />
      <Error name="email:error">The email doesn't exist</Error>
      <Input name="password" type="password" />
      <Error name="password:error">The password is wrong</Error>
      <Input name="terms-acceptance" type="checkbox" initialValue={true} />
      <button type="submit">Submit</button>
    </Form>
  )
}

Easy like a pie. Check how to handle errors in the documentation

Key concept

In order to make Kingpin efficient and reusable the entire state logic is managed within the <Form /> component (you shouldn't directly control each input value).

Each Kingpin action element (<Input />, <Textarea />, ...) needs a name prop in order to efficiently collect the result payload. The name should describe the input purpose; be aware of possible name conflicts!

Thanks to it the <Form /> is now able to easily handle the internal state, but how?

<Form /> is "just" a simple html <form>, so you can use it as usual. The most significant difference is that the onSubmit callback now takes two arguments: the "classic" event and an object which contains whether the form is valid or not (here about validation) and payload which is the name:value representation of its content.

Extend Kingpin inputs

Modern forms require more than just <input /> and <textarea /> components.

Kingpin plan is to add pluggable external components like Typeaheads, Slider, Dropdown ecc. specifically desinged and developed to work within a Kingpin Form.

In order to make Kingpin extensible by anyone it's possible create custom components with the withKingpin HOC.

To see how it works with a real example check the documentation.

Contributing

First off, thank you if you will consider to contribute to kingpin-react-form.

There are many ways to contribute like implementing a new feature, fixing bugs, improving the test suite or refining the documentation with fixes or examples.

The main kingpin's purpose is to be lightweight, full of functionalities and easy to work with. Be sure to follow this guidelines when you will do your implementation.

Any contribution or help will be appreciated.

License

This project is licensed under the MIT License - see the LICENSE file.