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

v1.5.16

Published

⚡ Fast, convenient and easy. Build forms in React with pleasure

Downloads

7

Readme

React-Formix

react-formix is a package that allows you to quickly and easily create controlled forms

Description is in progress 👨‍💻

There may be inaccuracies and incomprehensible examples :)

Get Started

🐇 Quick Start

  npm i react-formix mobx
import React from "react";
import { useFormix } from "react-formix";

export const Form: React.FC = () => {
	const myForm = useFormix({
		user: {
			firstname: "",
			surname: "",
		},
	});

	return (
		<form>
			<input {...myForm.bind("user.firstname")} />
			<input {...myForm.bind("user.surname")} />
		</form>
	);
};

Hooks

useFormix

import React from "react";
import { useFormix } from "react-formix";

export const Form: React.FC = () => {
	const myForm = useFormix({
		user: {
			firstname: "",
			surname: "",
		},
	});

	return (
		<form>
			<input {...myForm.bind("user.firstname")} />
			<input {...myForm.bind("user.surname")} />
			{/* OR */}
			<input {...myForm.$("user.firstname").bind()} />
			<input {...myForm.$("user.surname").bind()} />
		</form>
	);
};

useField

import React from "react";
import { useField } from "react-formix";

export const MyField: React.FC = () => {
	const myField = useField("fieldname", { defaultValue: "" });
	return <input {...myField.bind()} />;
};

Components

Formix

import React from "react";
import { Formix } from "react-formix";

export const MyForm: React.FC = () => {
	return (
		<Formix schema={{ user: { firstname: "", surname: "" } }}>
			{({ $, bind }) => {
				return (
					<>
						<input {...bind("user.firstname")} />
						<input {...bind("user.surname")} />
					</>
				);
			}}
		</Formix>
	);
};

Field

Field is a simple component that renders the required field from the available ones (by default: input, textArea or select) and passes props to it.

import React from "react";
import { useFormix, Field } from "react-formix";

export const MyForm: React.FC = () => {
	const myForm = useFormix({ nickname: "" });

	return (
		<form>
			{/* As input */}
			<Field {...myForm.bind("nickname")} />
			{/* Or */}
			<Field as='input' {...myForm.bind("nickname")} />
			{/* As select */}
			<Field as='select' {...myForm.bind("nickname")} />
			{/* As textArea */}
			<Field as='textArea' {...myForm.bind("nickname")} />
		</form>
	);
};

or you can create a custom Field

import React, { forwardRef } from "react";
import { useFormix, fieldFactory } from "react-formix";

export const MyInput = forwardRef<HTMLInputElement, React.ComponentProps<"input">>((props, ref) => {
	return <input {...props} ref={ref} style={{ color: "red" }} />;
});

export const MyTextarea = forwardRef<HTMLTextAreaElement, React.ComponentProps<"textarea">>(
	(props, ref) => {
		return <textarea {...props} ref={ref} style={{ color: "red" }} />;
	}
);

export const MyField = fieldFactory(
	{
		myInput: MyInput,
		myTextarea: MyTextarea,
		// you can pass the default key (in this case: myInput)
	},
	"myInput"
);

export const MyForm: React.FC = () => {
	const myForm = useFormix({ nickname: "" });

	return (
		<form>
			<MyField {...myForm.bind("nickname")} />
			{/* Or */}
			<MyField as='myInput' {...myForm.bind("nickname")} />
			<MyField as='myTextarea' {...myForm.bind("nickname")} />
		</form>
	);
};

Helpers

formSchema

import React from "react";
import { useFormix, formSchema, field } from "react-formix";

const schema = formSchema({
	user: {
		firstname: "",
		surname: field({
			defaultValue: "",
			validate: (value) => (typeof value === "string" && value.includes("test") ? "some error" : ""),
		}),
	},
});

export const MyForm: React.FC = () => {
	const myForm = useFormix(schema);
	return (
		<form>
			<input {...myForm.$("user.firstname").bind()} />
			<input {...myForm.$("user.surname").bind()} />
			{/* OR */}
			<input {...myForm.bind("user.firstname")} />
			<input {...myForm.bind("user.surname")} />
		</form>
	);
};

field

import React from "react";
import { useField, field, Field } from "react-formix";

const firstnameField = field({
	defaultValue: "",
	validate: (value) => (typeof value === "string" && value.includes("test") ? "some error" : ""),
});

export const MyForm: React.FC = () => {
	const myField = useField("firstname", firstnameField);
	return (
		<form>
			<input {...myField.bind()} />
		</form>
	);
};