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

reactiforms

v0.1.6

Published

A React library to quickly create efficient react forms.

Downloads

4

Readme

Reactiforms

This little package uses React context to minimize re-rendering of forms. Only the Input field rerenders when changes are made to inputs. Additional form utilities provided to make working with form data a little easier.

Provide a validate function as a prop to Input and you can easily check if your form state is valid on submission with isFormValid.

Broadcast of Input and Form state to subscribers coming soon.


How to Use

In your Form component file, import createFormStore.

import { createForm } from "reactiforms";

Create the Form object and extract the Form provider and Input components, along with any utilities you may need.

const { Form, Input, getFormValues, isFormValid, resetForm } = createForm();

Form and Input are wrappers over <form /> and <input /> and are compatible with regular html attributes. Props for React controlled forms are unnecessary as they are built into the wrapper - you do not need onChange and value props.

  <Form
    onSubmit={(e) => {
      // use like a vanilla submit handler.
      // Default behavior already prevented.

      if(isFormValid()){

        // do the thing
        console.log(getFormValues());

        // handy reset function provided
        resetForm();
      }
    }}
  >
    <label htmlFor="input-1a">1a: </label>
    <Input
      id="input-1"
      name="input-1"
      runOnFocus={() => console.log("running on focus")}
      runOnBlur={() => console.log("running on blur")}
      validate={(text) => (text.length >= 4)}
    />
    <button>Submit</button>
  </Form>

Available Form methods

Destructure (or read) these from createForm(). NOTE: Inputs are registered on mount so getters running before React's commit phase will read from an empty Form.

  • getFormValues: Invoke for KVPs of all input values of your Form. Keys are the id properties given to each input. If inputs have no id, they are assigned a counter.
  • getFormInputs: Invoke for all Inputs of Form as KVPs.
  • getInput: Invoke with id of input you want. If input is not in the Form returns undefined instead.
  • isFormValid: Returns a boolean after reading the isValid property of all Inputs.
  • resetForm: Use to manually reset input values.

Available Form properties

You may configure these properties by giving createFormStore an initial options object. Most of these features are coming soon.

  • handleSubmit: Placeholder function that simply warns if onSubmit handler not provided.
  • requireSpinner: Feature coming soon. Defaults to false. Use this flag to cause form to use your provided spinner on submission.
  • spinner: Boolean Feature coming soon. Flag to start spinner on submission. Flip to false to turn spinner off when async event completes. Setter will be provided.
  • spinnerTimeout: null | number Feature coming soon. Use to provide a hard coded spinner run time.

Default Input properties

Input component prop default values below. Props not listed here will be passed into the input primitive. Expect a React warning if using a prop not listed.

  • inputKey: null Used as an internal identifier. Borrows the value of id if there is one, otherwise will be assigned a counter value.
  • type: text
  • name: ""
  • touched: false
  • isValid: null
  • initialInputValue: ""
  • validate: null
  • runOnChange: null
  • runOnFocus: null
  • runOnBlur: null
  • runOnInvalid: null

Todos

  • dynamic styles when input data is invalid
  • subscriptions to input and form data

Contributions welcome! Please create an Issue if you spot an area of opportunity. Pull Requests for created issues are also welcome and appreciated.