
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
react-t-shirt-forms
Advanced tools
Sometimes life calls for more than a t-shirt. But t-shirts are easy, and comfortable - a good default until you need more.
Making a form in React is hard. Needlessly hard, with a lot of boilerplate - even when using any of the popular React form libraries.
T-Shirt Forms is intended to make creating simple forms simple.
It is powerful enough to handle complex and irregular cases, but it is more heavily geared towards reducing the boilerplate around regular forms.
If your app has lots of complex and irregular forms, then this library is probably not going to be the best solution. I would recommend looking at Formik instead.
npm install --save-dev react-t-shirt-forms
or
yarn add -D react-t-shirt-forms
See the API docs.
We show some basic examples on this page.
But you can see some more common flows in the examples.
import TShirtForm from "react-t-shirt-forms";
// by default no styling is included in your bundle
// but you can pull in a basic CSS file if you like
import "react-t-shirt-forms/dist/stylesheets/basic.min.css";
const formSchema = {
name: {
type: "string",
label: "Name",
initialValue: "Bruce Wayne"
},
email: {
type: "email",
label: "Email"
}
};
const onFormSubmit = async ({ formArgs }) => {
console.log(JSON.stringify(formArgs, null, 2));
};
const App = () => (
<TShirtForm schema={formSchema} handleSubmit={onFormSubmit} />
);
T-Shirt Forms allows integration with any validation library, but comes with out of the box hooks for yup.
import * as yup from "yup";
import { addValidation, yupSupport } from "react-t-shirt-forms";
// this tells T-Shirt Forms to use `yup` for any validations
// it also adds default validations for common fields
addValidation(yup, yupSupport);
Now you can use yup
validations in your form schema:
const formSchema = {
name: {
type: "string",
label: "Name",
validation: yup()
.string()
.min(10)
}
};
If you have a custom React component that you need to use for all password inputs, you can specify a global rule for this.
import { setSchemaDefaults } from "react-t-shirt-forms";
const MyCustomPasswordInput = ({ onChange, onBlur, value, label }) => (
<label>
{label}
<input
type="password"
value={value}
onChange={e => onChange(e.target.value)}
onBlur={e => onBlur(e)}
/>
</label>
);
setSchemaDefaults({
type: {
password: {
component: MyCustomPasswordInput
}
}
});
and now whenever you use a password input field
const formSchema = {
myPasswordField: {
type: "password",
label: "My custom password field"
}
};
you'll see that it's using your MyCustomPasswordInput
component.
T-Shirt Forms makes an effort to be minimal and modular.
Here's a look at its size, after tree-shaking, in different contexts:
Contributors are welcome! 😊
Check out the CONTRIBUTING.md.
FAQs
Simple forms, simple code.
The npm package react-t-shirt-forms receives a total of 0 weekly downloads. As such, react-t-shirt-forms popularity was classified as not popular.
We found that react-t-shirt-forms demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.