From 91e4c3ee17a8224d26fb6a3c315c84e2ec7f6171 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Sat, 29 Oct 2022 22:08:05 +0200 Subject: [PATCH] docs: documentation for v0.4 (#45) --- README.md | 29 +- docs/basics.md | 147 +++++-- docs/submission.md | 125 +++--- docs/validation.md | 405 +++++++++--------- examples/basic/src/App.tsx | 44 +- examples/remix-run/README.md | 42 +- .../remix-run/app/routes/async-validation.tsx | 141 +++--- examples/remix-run/app/routes/basic.tsx | 121 ------ examples/remix-run/app/routes/index.tsx | 7 +- .../app/routes/server-validation.tsx | 132 ++++++ examples/remix-run/app/routes/todos.tsx | 133 ++++++ examples/remix-run/app/routes/zod.tsx | 105 ++--- examples/todos/src/App.tsx | 10 +- examples/yup/README.md | 2 +- examples/yup/src/App.tsx | 19 +- examples/zod/README.md | 2 +- examples/zod/src/App.tsx | 20 +- guide/app/components.tsx | 6 +- guide/app/context.ts | 52 ++- guide/app/octokit.tsx | 7 +- guide/app/routes/_guide.$page.tsx | 10 +- guide/app/routes/_guide.api.$package.tsx | 12 +- guide/app/routes/_guide.examples.$name.tsx | 13 +- guide/app/routes/_guide.index.tsx | 13 +- guide/app/util.ts | 14 - guide/package.json | 2 +- guide/remix.env.d.ts | 1 - guide/server.js | 7 +- packages/conform-dom/index.ts | 45 +- packages/conform-react/README.md | 176 +++----- packages/conform-react/hooks.ts | 126 +++--- packages/conform-react/index.ts | 3 +- packages/conform-yup/README.md | 73 +++- packages/conform-yup/index.ts | 25 +- packages/conform-zod/README.md | 79 +++- packages/conform-zod/index.ts | 18 + playground/app/routes/employee.tsx | 18 +- playground/app/routes/login.tsx | 22 +- playground/app/routes/movie.tsx | 54 +-- playground/app/routes/payment.tsx | 28 +- playground/app/routes/signup.tsx | 29 +- playground/app/routes/student.tsx | 31 +- playground/app/routes/todos.tsx | 50 ++- tests/conform-dom.spec.ts | 14 +- tests/react.spec.ts | 24 +- 45 files changed, 1360 insertions(+), 1076 deletions(-) delete mode 100644 examples/remix-run/app/routes/basic.tsx create mode 100644 examples/remix-run/app/routes/server-validation.tsx create mode 100644 examples/remix-run/app/routes/todos.tsx diff --git a/README.md b/README.md index 2484812b..a9f5b5f1 100644 --- a/README.md +++ b/README.md @@ -2,26 +2,29 @@ Conform is a form validation library built on top of the [Constraint Validation](https://caniuse.com/constraint-validation) API. -- **Progressive Enhancement**: It is designed based on the [HTML specification](https://html.spec.whatwg.org/dev/form-control-infrastructure.html#the-constraint-validation-api). From validating the form to reporting error messages for each field, if you don't like part of the solution, just replace it with your own. -- **Framework Agnostic**: The DOM is the only dependency. Conform makes use of native [Web APIs](https://developer.mozilla.org/en-US/docs/Web/API) exclusively. You don't have to use React / Vue / Svelte to utilise this library. -- **Flexible Setup**: It can validates fields anywhere in the dom with the help of [form attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#form). Also enables CSS pseudo-classes like `:valid` and `:invalid`, allowing flexible styling across your form without the need to manipulate the class names. +- **Progressive Enhancement**: Its APIs are designed with progressive enhancement in mind to ensure a smooth and resillent experience before javascript is ready. +- **Server-first validation**: It simplifies the mental model by utilizing a server-first validation flow which submits your form for validation. +- **Lightweight**: It is only [4kB](https://bundlephobia.com/package/@conform-to/react) compressed thanks to all the native [Web APIs](https://developer.mozilla.org/en-US/docs/Web/API). _#useThePlatform_ ## Quick start - + ```tsx import { useForm, useFieldset } from '@conform-to/react'; export default function LoginForm() { const form = useForm({ - onSubmit(event, { submission }) { + onSubmit(event) { event.preventDefault(); - console.log(submission); + const formData = new FormData(event.currentTarget); + const value = Object.fromEntries(formData); + + console.log(value); }, }); - const { email, password } = useFieldset(form.ref); + const { email, password } = useFieldset(form.ref, form.config); return (
@@ -41,16 +44,6 @@ export default function LoginForm() { } ``` -Learn more about conform [here](https://conform.guide/basics) +Learn more about conform [here](https://conform.guide/basics). - -## API References - - -| Package | Description | Size | -| :------ | :---------- | :--- | -| [@conform-to/react](packages/conform-react) | View adapter for [react](https://github.com/facebook/react) | [![package size](https://img.shields.io/bundlephobia/minzip/@conform-to/react)](https://bundlephobia.com/package/@conform-to/react) | -| [@conform-to/yup](packages/conform-yup) | Schema resolver for [yup](https://github.com/jquense/yup) | [![package size](https://img.shields.io/bundlephobia/minzip/@conform-to/yup)](https://bundlephobia.com/package/@conform-to/yup) | -| [@conform-to/zod](packages/conform-zod) | Schema resolver for [zod](https://github.com/colinhacks/zod) | [![package size](https://img.shields.io/bundlephobia/minzip/@conform-to/zod)](https://bundlephobia.com/package/@conform-to/zod) | - diff --git a/docs/basics.md b/docs/basics.md index cd47a183..88eb1911 100644 --- a/docs/basics.md +++ b/docs/basics.md @@ -1,6 +1,6 @@ # Basics -In this section, we will cover how to build a simple login form by utilizing native constraint and then enhancing it with conform. +In this section, we will show you how to build a login form by utilizing the **Constraint Validation** API with **Conform**. @@ -8,8 +8,9 @@ In this section, we will cover how to build a simple login form by utilizing nat - [Installation](#installation) - [Quick start](#quick-start) +- [Constraint Validation](#constraint-validation) - [Capturing errors](#capturing-errors) -- [Styling input](#styling-input) +- [Customize messages](#customize-messages) - [Early reporting](#early-reporting) - [Demo](#demo) @@ -25,7 +26,7 @@ npm install @conform-to/react To begin, let's make a login form with 2 basic requirements: -- The **email** field should be a valid email address +- The **email** field should contain a valid email address - The **password** field should not be empty ```tsx @@ -36,7 +37,8 @@ export default function LoginForm() { event.preventDefault(); const formData = new FormData(event.currentTarget); - console.log(formData); + + console.log(Object.fromEntries(formData)); }} >