0.0.44 • Published 4 months ago

feature-form v0.0.44

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Status: Experimental

feature-form is a straightforward, typesafe, and feature-based form library.

  • Lightweight & Tree Shakable: Function-based and modular design
  • Fast: Optimized for speed and efficiency, ensuring smooth user experience
  • Modular & Extendable: Easily extendable with features
  • Typesafe: Build with TypeScript for strong type safety
  • Standalone: Zero external dependencies, ensuring ease of use in various environments

📚 Examples

🌟 Motivation

Create a typesafe, straightforward, and lightweight form library designed to be modular and extendable with features.

⚖️ Alternatives

📖 Usage

import { createForm } from 'feature-form';
import { useForm } from 'feature-react/form';
import * as v from 'valibot';
import { vValidator } from 'validation-adapters/valibot';
import { zValidator } from 'validation-adapters/zod';
import * as z from 'zod';

interface TFormData {
	name: string;
	email: string;
}

const $form = createForm<TFormData>({
	fields: {
		name: {
			validator: zValidator(z.string().min(2).max(10)),
			defaultValue: ''
		},
		email: {
			validator: vValidator(v.pipe(v.string(), v.email())),
			defaultValue: ''
		}
	},
	onValidSubmit: (data) => console.log('ValidSubmit', data),
	onInvalidSubmit: (errors) => console.log('InvalidSubmit', errors)
});

export const MyFormComponent: React.FC = () => {
	const { handleSubmit, register, status } = useForm($form);

	return (
		<form onSubmit={handleSubmit()}>
			<div>
				<label>Name</label>
				<input {...register('name')} />
				{status('name').error && <span>{status('name').error}</span>}
			</div>
			<div>
				<label>Email</label>
				<input {...register('email')} />
				{status('email').error && <span>{status('email').error}</span>}
			</div>
			<button type="submit">Submit</button>
		</form>
	);
};

Validators (validation-adapters)

feature-form supports various validators such as Zod, Yup, Valibot and more.

import * as v from 'valibot';
import { vValidator } from 'validation-adapters/valibot';
import { zValidator } from 'validation-adapters/zod';
import * as z from 'zod';

const zodNameValidator = zValidator(
	z
		.string()
		.min(2)
		.max(10)
		.regex(/^([^0-9]*)$/)
);

const valibotNameValidator = vValidator(
	v.pipe(v.string(), v.minLength(2), v.maxLength(10), v.regex(/^([^0-9]*)$/))
);
0.0.43

4 months ago

0.0.44

4 months ago

0.0.40

4 months ago

0.0.41

4 months ago

0.0.42

4 months ago

0.0.37

4 months ago

0.0.38

4 months ago

0.0.39

4 months ago

0.0.36

4 months ago

0.0.33

6 months ago

0.0.34

6 months ago

0.0.35

6 months ago

0.0.32

6 months ago

0.0.31

7 months ago

0.0.30

8 months ago

0.0.20

10 months ago

0.0.21

10 months ago

0.0.22

10 months ago

0.0.23

10 months ago

0.0.24

10 months ago

0.0.25

10 months ago

0.0.18

10 months ago

0.0.19

10 months ago

0.0.26

10 months ago

0.0.27

10 months ago

0.0.28

9 months ago

0.0.29

8 months ago

0.0.17

10 months ago

0.0.16

11 months ago

0.0.15

11 months ago

0.0.14

12 months ago

0.0.13

12 months ago

0.0.12

12 months ago

0.0.11

12 months ago

0.0.10

12 months ago

0.0.9

12 months ago

0.0.8

12 months ago

0.0.7

12 months ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago