Type-safe, declarative and performant React form & validation library
A stable release has been reached. Further development of new features is currently not planned. Bug fixes will be provided if needed, feature requests wil still be considered.
handleChange
function for dealing with change events automaticallynpm install @virtuslab/formts
import { FormSchemaBuilder, FormFields } from "@virtuslab/formts";
const Schema = new FormSchemaBuilder()
.fields({ answer: FormFields.string() })
.errors<string>()
.build();
import { FormValidatorBuilder } from "@virtuslab/formts";
const validator = new FormValidatorBuilder(Schema)
.validate(
Schema.answer,
val => (val === "" ? "Required!" : null),
val => (val !== "42" ? "Wrong answer!" : null)
)
.build();
import { useFormController, FormProvider } from "@virtuslab/formts";
const MyForm: React.FC = () => {
const controller = useFormController({ Schema, validator });
return (
<FormProvider controller={controller}>
<AnswerField />
<FormActions />
</FormProvider>
);
};
import { useField } from "@virtuslab/formts";
const AnswerField: React.FC = () => {
const field = useField(Schema.answer);
return (
<section>
<label htmlFor={field.id}>
What is the answer to the meaning of life, the universe, and everything?
</label>
<input
id={field.id}
value={field.value}
onChange={field.handleChange}
onBlur={field.handleBlur}
/>
<div className="error">{field.error}</div>
</section>
);
};
import { useFormHandle } from "@virtuslab/formts";
const FormActions: React.FC = () => {
const form = useFormHandle(Schema);
return (
<section>
<button
type="submit"
disabled={form.isValid === false}
onClick={() => form.submit(console.log)}
>
Submit!
</button>
<button type="reset" onClick={form.reset}>
Reset
</button>
</section>
);
};
Currently API documentation is available in form of:
Play around with the code on CodeSandbox to learn Formts API and features:
a) Step-by-step introduction:
b) HTML input bindings:
c) Advanced examples:
Some of the reasons outlined below are no longer valid as react-hook-form has improved since this project started. It offers good type-safety and performance. If for some reason you are not happy with it however, this library offers different approach to many aspects of form management and a powerful validation API - so it may suit your needs better.
Most popular form libraries like Formik
and react-hook-form
are written in
Typescript but are not designed with type-safe API as a primary concern.
There is some evidence that this is important for some people:
There are some existing truly type-safe react form solutions, but each has a costly dependency associated with it:
MobX
fp-ts
ReasonML
There are SOME form libraries with really good performance (react-hook-form). However combining convenient Context based hook API with React performance optimizations is still problematic. See https://react-hook-form.com/advanced-usage#FormProviderPerformance for example.
Usually form libs "outsource" advanced validation to schema-based libraries such
as Yup
. This seems like a great idea but has some limitations:
yup
and similar packages are not designed with type-safety in mindrequired
validator does not impact type of values.number | ""
, this is because HTML number inputs
work with ""
as empty value.Generated using TypeDoc