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

@prototyp/skeleform

v1.0.2

Published

React/React-Native form management utility

Downloads

41

Readme

skeleform

Form management and validation toolkit originally a part of https://github.com/prototypdigital/skeletor

Usage

Handle form value updates and validation with useForm. Full TypeScript support, ability to configure optional parameters, custom validation rules.

Flexible in its function, supports multiple validation approaches through callbacks:

  1. For on-change validation, use update("prop", value, true)
  2. To trigger standalone validation, use validate("prop")
  3. Validate entire form with validateForm()

The order of importance when it comes to validating fields is:

  1. Custom validation rule - will get the value as-is for your rule to validate it fully. The configuration rule receives the current value, state and optional flag values for you to validate against.
  2. If there is no custom validation rule and the primitive value check returns false (there is no value), check if the field is optional. If optional, the validation result is true, otherwise it is false;
  3. If all previous checks have not been triggered, return the primitive "has value" check result. This will return false for values such as: null | undefined | NaN | "" | Invalid Date. This will return true if you have a complex value type such as object | Array, so in case you have a complex value type use a custom validation rule to get what you need.

Example 1: Simple use case with standalone validation on blur:

const {state, validation, update, validate} = useForm({email: "", password: "");

...
<input
  ...
  value={state.email}
	onChange={(e) =>  update("email", e.currentTarget.value)}
	onBlur={() => validate("email")}
/>
...

Example 2: Simple use case with on-change validation:

// See example 1
...
onChange={(e) =>  update("email", e.currentTarget.value, true)}
...

Example 3: Simple use case with on-submit validation:

// See example 1

function submit() {
	if(!validateForm()) {
		// Throw invalid error
		// validation object will be populated
	}
}

...
<input
  ...
	value={state.email}
	onUpdate={(e) =>  update("email", e.currentTarget.value)}
/>
<button onPress={submit}>Submit</button>

Validation has three possible values:

  1. undefined = validation of the field value has not been done yet.
  2. false = validation has failed, the state value is not considered valid.
  3. true = validation has succeeded, the state value is considered valid.

What does that mean? Well, it means that when you have validation.email === false, you should show an error message on the field. undefined | true are not relevant for rendering, only functionally to block form submit events.

Example 4: Form configuration

const { state, validation, update, validate } = useForm(
{ firstName: "", middleName: "", lastName: ""},
{
	// If left empty, validation.middleName will be true
	optional: ["middleName"],
	// validation.lastName is invalid if lastName is <3 characters long
	// state can be used to compare with other values (ie repeat password)
	rules: { lastName: (value, state, optional) => value.length >= 3,
}

Example 5: useFormUtils

Utility functions to help with standalone state validation, such as when re-validating a list of form values in a parent components.

const { stateValidation } =  useFormUtils<Person>();

function validatePeople() {
	return people.every(person => stateValidation(person).valid);
}

Other utilities: doesValueExist, validateByRule, isOptional, fieldValidation, stateValidation. Some are meant for internal useForm usage, such as fieldValidation and validateByRule, but are not unusable.