@liam-michel/simple-form 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +202 -0
- package/dist/index.cjs +761 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +165 -0
- package/dist/index.d.ts +165 -0
- package/dist/index.js +714 -0
- package/dist/index.js.map +1 -0
- package/package.json +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Liam Michel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# @liam-michel/simple-form
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@liam-michel/simple-form)
|
|
4
|
+
[](https://www.npmjs.com/package/@liam-michel/simple-form)
|
|
5
|
+
|
|
6
|
+
A type-safe, schema-driven form library for React. Define a [Zod](https://zod.dev) schema and get validated, fully-typed form fields out of the box — built on [React Hook Form](https://react-hook-form.com) and [Radix UI](https://www.radix-ui.com).
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @liam-michel/simple-form
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
### Peer dependencies
|
|
15
|
+
|
|
16
|
+
You'll also need these in your project:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install react react-dom react-hook-form zod @hookform/resolvers \
|
|
20
|
+
@radix-ui/react-checkbox @radix-ui/react-select @radix-ui/react-slider \
|
|
21
|
+
@radix-ui/react-popover cmdk react-day-picker date-fns lucide-react \
|
|
22
|
+
clsx tailwind-merge
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
> The library uses Tailwind CSS class names. Make sure your project has Tailwind configured.
|
|
26
|
+
|
|
27
|
+
## Quick start
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
import { SimpleForm, TextField, NumberField, CheckboxField } from '@liam-michel/simple-form';
|
|
31
|
+
import { z } from 'zod';
|
|
32
|
+
|
|
33
|
+
const schema = z.object({
|
|
34
|
+
name: z.string().min(1, 'Name is required'),
|
|
35
|
+
age: z.number().min(1, 'Age is required'),
|
|
36
|
+
agree: z.boolean().refine((v) => v, 'You must agree'),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
function MyForm() {
|
|
40
|
+
return (
|
|
41
|
+
<SimpleForm
|
|
42
|
+
schema={schema}
|
|
43
|
+
defaultValues={{ name: '', age: 0, agree: false }}
|
|
44
|
+
onSubmit={async (data) => {
|
|
45
|
+
// data is fully typed as { name: string; age: number; agree: boolean }
|
|
46
|
+
console.log(data);
|
|
47
|
+
}}
|
|
48
|
+
>
|
|
49
|
+
<TextField<typeof schema> name="name" label="Name" placeholder="Enter your name" />
|
|
50
|
+
<NumberField<typeof schema> name="age" label="Age" />
|
|
51
|
+
<CheckboxField<typeof schema> name="agree" label="I agree to the terms" />
|
|
52
|
+
</SimpleForm>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## `createForm` — schema-bound factory
|
|
58
|
+
|
|
59
|
+
For a cleaner API without manual generics, use `createForm` to generate a full set of typed components from a single schema:
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
import { createForm } from '@liam-michel/simple-form';
|
|
63
|
+
import { z } from 'zod';
|
|
64
|
+
|
|
65
|
+
const schema = z.object({
|
|
66
|
+
email: z.string().email(),
|
|
67
|
+
bio: z.string(),
|
|
68
|
+
role: z.enum(['admin', 'user', 'viewer']),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const { Form, TextField, TextAreaField, SelectField } = createForm(schema);
|
|
72
|
+
|
|
73
|
+
function ProfileForm() {
|
|
74
|
+
return (
|
|
75
|
+
<Form
|
|
76
|
+
onSubmit={async (data) => console.log(data)}
|
|
77
|
+
defaultValues={{ email: '', bio: '', role: 'user' }}
|
|
78
|
+
>
|
|
79
|
+
<TextField name="email" label="Email" />
|
|
80
|
+
<TextAreaField name="bio" label="Bio" placeholder="Tell us about yourself" />
|
|
81
|
+
<SelectField
|
|
82
|
+
name="role"
|
|
83
|
+
label="Role"
|
|
84
|
+
options={[
|
|
85
|
+
{ value: 'admin', label: 'Admin' },
|
|
86
|
+
{ value: 'user', label: 'User' },
|
|
87
|
+
{ value: 'viewer', label: 'Viewer' },
|
|
88
|
+
]}
|
|
89
|
+
/>
|
|
90
|
+
</Form>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## `useSimpleForm` — external form control
|
|
96
|
+
|
|
97
|
+
Use the `useSimpleForm` hook when you need access to the form instance outside of the `<SimpleForm>` component (e.g. for programmatic control, multi-step forms, or watching values):
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
import { SimpleForm, TextField, useSimpleForm } from '@liam-michel/simple-form';
|
|
101
|
+
import { z } from 'zod';
|
|
102
|
+
|
|
103
|
+
const schema = z.object({
|
|
104
|
+
name: z.string().min(1),
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
function ControlledForm() {
|
|
108
|
+
const form = useSimpleForm({
|
|
109
|
+
schema,
|
|
110
|
+
defaultValues: { name: '' },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<SimpleForm schema={schema} form={form} onSubmit={async (data) => console.log(data)}>
|
|
115
|
+
<TextField<typeof schema> name="name" label="Name" />
|
|
116
|
+
</SimpleForm>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Field components
|
|
122
|
+
|
|
123
|
+
All fields share these common props:
|
|
124
|
+
|
|
125
|
+
| Prop | Type | Description |
|
|
126
|
+
|------|------|-------------|
|
|
127
|
+
| `name` | typed key from schema | The field name (type-safe based on your Zod schema) |
|
|
128
|
+
| `label` | `string` | Label text displayed above the field |
|
|
129
|
+
| `description` | `string?` | Helper text displayed below the field |
|
|
130
|
+
| `placeholder` | `string?` | Placeholder text |
|
|
131
|
+
| `disabled` | `boolean?` | Disable the field |
|
|
132
|
+
| `className` | `string?` | Additional CSS class for the wrapper |
|
|
133
|
+
| `showReset` | `boolean?` | Show a reset button to restore the default value |
|
|
134
|
+
|
|
135
|
+
### Available fields
|
|
136
|
+
|
|
137
|
+
| Component | Schema type | Extra props |
|
|
138
|
+
|-----------|------------|-------------|
|
|
139
|
+
| `TextField` | `z.string()` | `type?: HTMLInputTypeAttribute` (e.g. `"password"`, `"email"`) |
|
|
140
|
+
| `TextAreaField` | `z.string()` | — |
|
|
141
|
+
| `NumberField` | `z.number()` | — |
|
|
142
|
+
| `CheckboxField` | `z.boolean()` | — |
|
|
143
|
+
| `SelectField` | `z.enum(...)` | `options: { value, label }[]` |
|
|
144
|
+
| `SliderField` | `z.number()` | `min?`, `max?`, `step?` |
|
|
145
|
+
| `DateField` | `z.date()` | — |
|
|
146
|
+
| `MultiSelectField` | `z.array(z.string())` | `options: { value, label }[]` |
|
|
147
|
+
|
|
148
|
+
## `SimpleForm` props
|
|
149
|
+
|
|
150
|
+
| Prop | Type | Description |
|
|
151
|
+
|------|------|-------------|
|
|
152
|
+
| `schema` | `ZodType` | Zod schema for validation |
|
|
153
|
+
| `onSubmit` | `(data: Output) => Promise<void>` | Submit handler (receives validated & typed data) |
|
|
154
|
+
| `defaultValues` | `DefaultValues?` | Initial form values |
|
|
155
|
+
| `form` | `UseFormReturn?` | External form instance from `useSimpleForm` |
|
|
156
|
+
| `submitLabel` | `string?` | Custom label for the submit button (default: `"Submit"`) |
|
|
157
|
+
| `hideSubmit` | `boolean?` | Hide the default submit button |
|
|
158
|
+
| `renderSubmit` | `(state) => ReactNode` | Custom submit button renderer |
|
|
159
|
+
| `renderError` | `(message) => ReactNode` | Custom root error renderer |
|
|
160
|
+
| `children` | `ReactNode \| (form) => ReactNode` | Fields, or a render function receiving the form instance |
|
|
161
|
+
|
|
162
|
+
### Error handling
|
|
163
|
+
|
|
164
|
+
When `onSubmit` throws, errors are handled automatically:
|
|
165
|
+
|
|
166
|
+
- **Field-level errors**: Throw `{ data: { fieldErrors: { fieldName: "message" } } }` to set errors on specific fields.
|
|
167
|
+
- **Root-level errors**: Any other thrown error displays its `message` as a form-level error.
|
|
168
|
+
|
|
169
|
+
```tsx
|
|
170
|
+
<SimpleForm
|
|
171
|
+
schema={schema}
|
|
172
|
+
onSubmit={async (data) => {
|
|
173
|
+
const res = await api.createUser(data);
|
|
174
|
+
if (!res.ok) {
|
|
175
|
+
// Field-level errors
|
|
176
|
+
throw { data: { fieldErrors: { email: 'Email already taken' } } };
|
|
177
|
+
}
|
|
178
|
+
}}
|
|
179
|
+
>
|
|
180
|
+
{/* ... */}
|
|
181
|
+
</SimpleForm>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Type helpers
|
|
185
|
+
|
|
186
|
+
The library exports type helpers to extract field names by type from a schema:
|
|
187
|
+
|
|
188
|
+
```tsx
|
|
189
|
+
import type {
|
|
190
|
+
StringFieldsOf,
|
|
191
|
+
NumberFieldsOf,
|
|
192
|
+
BooleanFieldsOf,
|
|
193
|
+
DateFieldsOf,
|
|
194
|
+
SelectFieldsOf,
|
|
195
|
+
} from '@liam-michel/simple-form';
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
These are used internally by the field components and can be useful if you're building custom fields.
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
MIT
|