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

react-useformless

v1.3.0

Published

A library to use controlled forms into react apps

Downloads

143

Readme

useFormless

react-useformless is a simple library that allows you to control forms with react-hooks approach

Awesome Build Status styled with standard

Why useFormless?

  • Works with nested forms 🎉
  • Zero dependencies 🆓
  • Includes helpers for field and form tags, it makes a easy for use

Getting Started

Step 1: Install it

$ yarn add react-useformless

$ npm install react-useformless

Step 2: Import it 📦

import useFormless from 'react-useformless';

Step 3: Set state and options

useFormless accepts an optional object that lets you add validations handlers

  1. Options(optional)
{
 initialValues: {
   name: '',
   email: '',
 },
 validate: (name, value) => {
   // This function receives [name and value] as parameters and will return either a string with the error or an empty string.
   // You can do your validations as follow

   // 1.- Define an object with function validations for each name in the form,
   // functions receive the value and return an error for the field
   const validators = {
     name: validateNameFunction,
     email: validateEmailFunction
     // ... else function validations for each field
   }

   // 2.- Get the function for the given `name` and then call it with the passed value
   const errorFn = validators[name]
   return errorFn(value)
 },
 onError: (ev: DOMEvent, { values, errors }) => {
   ev.preventDefault()
   // If you decide using onSubmit function provided by formless, this function will be fired after the submit error
   // It receives DOMevent so you do whatever you want after it ends
 },
 onSuccess: (ev: DOMEvent, { values }) => {
   ev.preventDefault()
   //Same as error option, but this one is fired on success
 }
}

Step 4: Use it! 💡

Create an instance of useFormless and render it. Easy, isn't it? 💃🏻


const { values, errors, inputProps, onSubmit } = useFormless({ initialValues, validate, onSuccess, onError });

return(
 <section>
   <h1>Basic example</h1>
   <form onSubmit={onSubmit}>
     <label htmlFor="name">Name</label>
     <input id="name" type="text" {...inputProps('name', 'armando')}/>
     <p style={{color: 'red', fontSize: 9}}>{errors.name}</p>
     <label htmlFor="email">email</label>
     <input id="email" type="password" {...inputProps('password')}/>
     <p style={{color: 'red', fontSize: 9}}>{errors.email}</p>
     <input type="submit" value="Login"/>
   </form>
 </section>
)

if you prefer it, get started 🎮 with this snippet in CodeSandbox

Prerequisites

React hooks are live now, so what are you waiting for? useFormless is now updated with React v16.8 and ready to use

API

useFormless provides you a clean and easy-to-use API that you can attach to any component.

Notice: Use react-hooks into functional components.

Objects returned:

| Name | Description | | ----------------- | --------------------------------------------------------------------- | | values: Object | contains all values using the key as name, see example above | | errors: Object | contains all errors using the key as name | | touched: Object | contains all values that have been touched/modified |

Common behavior for forms:

| Function | Description | | ----------------------------------------- | ----------------------------------------------------------- | | setValue(name: string, value: any) | set a value and validate it | | getValue(name: string) | get a value given a name | | setValues({}: values) | set all values (including party forms) but it doesn't trogger validations| | touchFieldname) | mark the passed value as touched | | reset() | set all values as initialValues Object | | party.create(name: string, { validate: function})) | you can create nested forms, this function receives a name and validates option that allows you to validate this party only, also it works like an object returned by useFormless and another party function to continue adding more nested forms | | validateForm() | Run validations, set errors and mark all objects as touched | | validateValue() | Run validations, set errors and mark all objects as touched | | validateParty( | Run validations only for a nested form, set errors and mark all objects as touched | | isValid: boolean | true: is for a valid form, false: is for an invalid form |

Helpers

useFormless splits the behavior and the UI, so your components become more reusables.

| Function | Description | | -------------------------------- | ----------- | | inputProps(name: String): Object | This function will return custom props {name, value, onChange, onBlur}, pass this object to your input component directly, see example | | inputCheckboxProps(name: String): Object | Same as inputProps but for checkbox inputs| | onSubmit(SyntacticEvent): void | Handle submit event, this will trigger either onSuccess or onError functions| | field | an object with functions for fields | | party | an object with functions for party forms | | form | an object with functions for forms |

See more about it in the documentation official DOCS

Examples

<form id="my-form" onSubmit={onSubmit}>

<input id="my-input" type="text" {...inputProps('email')}>

Contribute

Running the tests

useFormless uses jest and react-testing-library

Built With

  • react16.8 - The web framework used
  • yarn - For dependencies management

Authors

Acknowledgments

License

useFormless is MIT licensed