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

valium

v0.0.64

Published

A Form validator for React, using and customizing HTML5 Constraint Validation API

Downloads

105

Readme

valium

Valium logo

NPM Version Dependency Status NPM Downloads

A Form validator for React, using and customizing HTML5 Constraint Validation API.

Table of Contents

  1. Demo
  2. HTML5 Constraint Validation API?
  3. Premature Validation?
  4. Install
  5. Getting started
  6. Docs

Demo

Check a live demo at valium.afialapis.com

HTML5 Constraint Validation API?

Yes: instead of providing lots of verbose-methods-you-must-learn to validate your inputs, Valium will just:

  • let you render your input elements completely as you need
  • check validation constraints in those inputs (required, minLength, maxLength, pattern, ...)
  • provide you the validation status so you can render them in consequence

Even more: Valium extends these constraints:

  • it accepts some custom constraints (allowedValues or disallowedValues lists, doRepeat or doNotRepeat other form's fields, or even a custom checkValue callback)
  • checks these custom constraints and updates accordingly the element's ValidityState

Premature Validation?

HTML5 Constraint Validation API checks for validity changes when the input changes. Depending on the browser, it means: when the input loses the focus.

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

Install

  npm i valium

Getting started

Valium provides just two elements: VForm and VInput.

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';
import {VForm, VInput} from 'valium'

const MyValidatedForm = () => {

  const [myText, setMyText]= React.useState('')
  
  return (
      <VForm 
        renderButtons= {(valid, elements) => null}

         renderInputs= {(formActions) => 
           <VInput
              type                 = "text"
              /* formActions must be passed to every VInput */
              formActions          = {formActions}
              /* Valium provides some custom constraints */
              disallowedValues     = {["NO"]}
              /* prematureValidation will not wait input to lose focus */
              prematureValidation  = {true}
              render = {(valid, message, inputRef) => 
                <div>
                  <input ref       = {inputRef}
                         name      = 'myText'
                         className = {valid ? 'valid' : 'invalid'}
                         value     = {myText}
                         onChange  = {(event) => setMyText(event.target.value)}
                         /* HTML Validation constraints are managed directly on HTML input elements*/
                         required  = {true}
                         minLength = {2}
                         maxLength = {50}
                  />
                  <div className="valium-example-input-feedback">
                    {message}
                  </div>
                </div>
              }
            /> 
          }
      />
  )
} 

Docs

For complete docs, check valium.afialapis.com