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-formality

v1.4.23

Published

React form field binding and handling

Downloads

115

Readme

React formality

A thin wrapper around forms and field components to easily allow validation and debugging in ReactJS.

What does it do?

It simplifies the handeling of validation through thinly wrapping your components in our Form and Field components. This library makes no assumptions about your layout or design, instead it exposes the values and the validation results of the fields through onSubmit, and allows you to retain control all along the way - it doesn't even mutate your component's state, thus allowing you to control when your component redraws or it state mutates.

How does it do it?

Integration hooks

The library allows you to integrate with the inner workings via a few integration hooks, here they are:

  • error prop - if there is a validation error, this prop will be passed to your component, and will be true
  • helperText prop - if there is a validation error, this prop will be passed to your component, and will contain the error message as passed in from validate.js
  • debug prop - if you set this prop on the Field component, we will log information about the events and lifecycle of the component, such as: getValue, validateField, handleChange, handleBlur, fieldCreated. Each will have information specific to the event or lifecycle.
  • onSubmit - the Form component has a special onSubmit event that you can subscribe to - it still receives the oriignal event, but also the form values, form validation result and the form validation result object, so that all you need to do is persist your data.

Note: All lifecycle integration hooks pass on their event, so that you can react on it separately if you wish. Eg: the onChange will still be called in your component, even though we're handling that event.

Built in validation

The built in validation uses validate.js to allow each component to be validated - it has a bunch of built in validators, however you can also define custom validations if you need them.

A few things to note:

  • Validation is initially run onChange, and subsequently each time the component renders, which follows the same pattern as most other validation libraries
  • We do not support async validation, if you need that please log a ticket.
  • onSubmit will trigger validation of all fields in the form that has

Examples

With a custom component

Say you have a custom input field component - the only requirement is that you handle the error and helperText props:

class InputComponent extends Component {
	render(){
		//	We extract the error and helperText props, so that the input doesn't receive them
		const { error, helperText, ...rest } = this.props;

		return <div>
			<input type="text"{...rest} />
			{/* Display validation error if it exists */}
			{error &&
				<div>
					<hr aria-hidden="true" style={{borderBottom: "1px solid red"}}/>
					<div style={{color: "red"}}>{helperText}</div>
				</div>
			}
		</div>
	}
};

With material-ui

Say you're using http://www.material-ui.com, and want to validate your email field before saving the values, you can simply do the following:

import React, { Component } from 'react';
import { TextField } from '@material-ui/core';
import { Field, Form, Store } from 'react-formality'

//	Setup our validations (you can also place it inline of the field), see https://validatejs.org/ for included validation methods
var validations = {
	emailAddress: {
		presence: true,
		email: true
	}
};

class MyApp extends Component {
	//	Use this to handle submitting of the data - it is passed the synthetic 
	//	event, values from the store, isValid, and the validation results.
	handleSubmit(e, values, isValid, validationResults){
		e.preventDefault();
		if(isValid) {
			console.log('Valid, so we can save here', values);
		} else {
			console.log('Invalid, so we won\'t save', values, validationResults);
		}
	}

	componentWillMount() {
		var me = this;
		this.setState({
			//	Create a store - you can optionally set values - they ae keyed by the name of the field
			store: new Store({
				emailAddress: "[email protected]"
			},
			//	This is called when the store values change - we don't mutate your state, so you must manually force an update
			function(){
				me.forceUpdate();
			})
		});
	}

	render() {
		return (
			<div className="route route__signup">
				<h1>Form test</h1>

				<Form onSubmit={this.handleSubmit.bind(this)} debug={true}>
					<div>
						{/* 
							Basic email field with validation
							The value will be stored in the store as 'emailAddress'
						*/}
						<Field
						name="emailAddress"
						store={this.state.store}
						placeholder="Email address"
						component={TextField}
						validate={validations.emailAddress}/>
					</div>
				</Form>

			</div>
		);
	}
}

export default MyApp;

So that will validate the email before submitting.