react-100-form
TypeScript icon, indicating that this package has built-in type declarations

0.2.4 • Public • Published

🎯 React 100 Form

YES! As the name says, only 💯 lines of code for a powerful CUSTOM HOOK.

YES! It has most features you might need when dealing with form.

✅ Zero dependencies (excelent bundle size)

✅ Only peer dependency: React

✅ 💥 1.6k gzipped 💥

Installation

npm install react-100-form

or

yarn add react-100-form

Simple Usage

const initialValues = {name: '', accepted: false};
const validate = (values) => {
	const errorMap = {};
	errorMap.name = values.name === '' && 'Required';
	errorMap.accepted = !values.accepted && 'Should be checked';
	return errorMap;
}
const doSubmit = (values) => { /* your submit logic */ };
const {
	errorMap,
	handleSubmit,
	isValid,
	values
} = useForm(initialValues, validate);
...	...
<form onSubmit={handleSubmit(doSubmit)}>
...	...
<input
	name="name"
	onBlur={handleBlur}
	onChange={handleChange}
	type="text"
	value={values.name}
/>
{errorMap.name && <span>{errorMap.name}</span>}
...	...
<input
	name="accepted"
	onBlur={handleBlur}
	onChange={handleChange}
	type="checkbox"
	value={values.accepted}
/>
{errorMap.accepted && <span>{errorMap.accepted}</span>}
...	...
<button type="submit" disabled={!isValid}>Submit</button>

All features

const {
	commit, // Set current values as inital state
	errorMap,
	handleBlur,
	handleChange,
	handleSubmit,
	isDirty,
	isSubmitting,
	isValid,
	rollBack, // Restore to initial state or to last committed state
	touchedMap,
	values,
} = useForm(
	initialValues,
	validate
);

Author

Mario Medrano Maldonado (Gendecoder) mario.medrano.maldonado@gmail.com

Package Sidebar

Install

npm i react-100-form

Weekly Downloads

2

Version

0.2.4

License

MIT

Unpacked Size

12.1 kB

Total Files

7

Last publish

Collaborators

  • gendecoder