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

formiga

v0.1.8

Published

The simplest -yet effective- form validator for React

Downloads

32

Readme

Formiga logo

NPM Version NPM Downloads

The simplest -yet effective- form validator for React: stick to -and empower- web standards (HTML5 Constraint Validation API) instead of ignore them.

Table of Contents

  1. Intro
  2. Premature Validation
  3. UI integrations
  4. Demo
  5. Install
  6. Getting started

Intro

formiga aims to provide full <form> state and validation functionalities with:

  • no dependencies
  • no boilerplate:
    • no wrapping components, just use standard <form>, <input>, <textarea> and <select> HTML elements
    • no API to learn: just a couple of hooks useForm and useInput
  • HTML validation at our service

Premature Validation

HTML5 Constraint Validation API checks for validity changes when the input changes. Depending on the browser, it may mean: when the input's value changes or just when the input loses the focus.

Formiga is here to make your Forms much nicer: with prematureValidation, the ValidityState is updated always while typing!

UI integrations

formiga cares just about state and validation of your forms. UI and styling is out of scope. That's why you will probably not use formiga directly, but some of the integrations with some UI library. List is tiny yet:

· formiga-reactstrap

Given formiga works with native HTML elements (<form>, <input>, <textarea>, <select>), you will find pretty easy to couple it with any UI library. Or even just with some custom CSS if you go minimalist, as in our Demo.

Demo

Check a live demo at formiga.afialapis.com.

Or run it locally with:

  npm run demo

Install

  npm i formiga

Getting started

Formiga provides just two hooks: useForm and useInput.

VForm will be the parent element. It just renders a form element, and provide a couple of render props (renderInputs and renderButtons) so you can render the rest.

Then, any input inside the Form that you want to be validated, must be wrapped within a VInput element.

Basic example

Let's check a basic example (try it at CodePen):

import React, {useState} from 'react'

const FormigaForm = () => {

  const form = useForm()
  const [name, setName]= useState('John Doe')
  const [age, _setAge]= useState('33') 

  const nameInput = useInput({
    type: 'text',
    disallowedValues: ["John Not Doe"],
    inputFilter: 'latin'
  })
  const ageInput = useInput({
    type: 'text',
    checkValue: (v) => !isNaN(v) && parseInt(v)>=18,
    inputFilter: 'int'
  })


  const handleSubmit = () => {
    let resume= ''

    form.elements
      .map((el) => {
        resume+= `Input ${el.name} ${el.valid ? 'is' : 'is not'} valid\n`
      })

    console.log(resume)
    //
    // Input name is valid
    // Input age is valid
  }


  return (  

    <form ref = {form.ref}>
        
      {/* A controlled input */}
      <input ref       = {nameInput.ref}
             name      = {'name'}
             className = {nameInput.valid ? 'valid' : 'invalid'}
             required  = {true}
             value     = {name}
             onChange  = {(event) => setName(event.target.value)}/>

      {/* An uncontrolled input */}
      <input ref       = {ageInput.ref}
             name      = {'age'}
             className = {ageInput.valid ? 'valid' : 'invalid'}
             required  = {true}
             defaultValue = {age}/>


      <button
             onClick  ={(_ev) => handleSubmit()}
             disabled = {! form.valid}>
        Save
      </button>

    </form>

  )
}