@rilaykit/forms 0.1.1-alpha.1 → 0.1.2
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 +239 -0
- package/dist/index.d.mts +982 -152
- package/dist/index.d.ts +982 -152
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +29 -12
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 AND YOU CREATE
|
|
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,239 @@
|
|
|
1
|
+
# @rilaykit/forms
|
|
2
|
+
|
|
3
|
+
The form builder and React rendering layer for [RilayKit](https://rilay.dev) — build type-safe, headless forms from declarative schemas.
|
|
4
|
+
|
|
5
|
+
`@rilaykit/forms` provides a fluent builder API to define form configurations and headless React components to render them. State management is powered by Zustand with granular selectors for optimal re-render performance.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# pnpm (recommended)
|
|
11
|
+
pnpm add @rilaykit/core @rilaykit/forms
|
|
12
|
+
|
|
13
|
+
# npm
|
|
14
|
+
npm install @rilaykit/core @rilaykit/forms
|
|
15
|
+
|
|
16
|
+
# yarn
|
|
17
|
+
yarn add @rilaykit/core @rilaykit/forms
|
|
18
|
+
|
|
19
|
+
# bun
|
|
20
|
+
bun add @rilaykit/core @rilaykit/forms
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
> `@rilaykit/core` is a required peer dependency.
|
|
24
|
+
|
|
25
|
+
### Requirements
|
|
26
|
+
|
|
27
|
+
- React >= 18
|
|
28
|
+
- React DOM >= 18
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
### 1. Create Your Registry
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
import { ril } from '@rilaykit/core';
|
|
36
|
+
import { Input } from './components/Input';
|
|
37
|
+
|
|
38
|
+
const rilay = ril.create()
|
|
39
|
+
.addComponent('input', { renderer: Input });
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 2. Build a Form
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
import { required, email } from '@rilaykit/core';
|
|
46
|
+
|
|
47
|
+
const loginForm = rilay
|
|
48
|
+
.form('login')
|
|
49
|
+
.add({
|
|
50
|
+
id: 'email',
|
|
51
|
+
type: 'input',
|
|
52
|
+
props: { label: 'Email', type: 'email' },
|
|
53
|
+
validation: { validate: [required(), email()] },
|
|
54
|
+
})
|
|
55
|
+
.add({
|
|
56
|
+
id: 'password',
|
|
57
|
+
type: 'input',
|
|
58
|
+
props: { label: 'Password', type: 'password' },
|
|
59
|
+
validation: { validate: [required()] },
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 3. Render It
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
import { Form, FormField } from '@rilaykit/forms';
|
|
67
|
+
|
|
68
|
+
function LoginForm() {
|
|
69
|
+
const handleSubmit = (data: { email: string; password: string }) => {
|
|
70
|
+
console.log('Login:', data);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<Form formConfig={loginForm} onSubmit={handleSubmit}>
|
|
75
|
+
<FormField fieldId="email" />
|
|
76
|
+
<FormField fieldId="password" />
|
|
77
|
+
<button type="submit">Sign In</button>
|
|
78
|
+
</Form>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Features
|
|
84
|
+
|
|
85
|
+
### Fluent Form Builder
|
|
86
|
+
|
|
87
|
+
Construct forms with a chainable, type-safe API. Each field type and its props are validated at compile time.
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
const contactForm = rilay
|
|
91
|
+
.form('contact')
|
|
92
|
+
.add(
|
|
93
|
+
{ id: 'firstName', type: 'input', props: { label: 'First Name' } },
|
|
94
|
+
{ id: 'lastName', type: 'input', props: { label: 'Last Name' } },
|
|
95
|
+
)
|
|
96
|
+
.add({
|
|
97
|
+
id: 'message',
|
|
98
|
+
type: 'textarea',
|
|
99
|
+
props: { label: 'Message', rows: 5 },
|
|
100
|
+
validation: { validate: [required()] },
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// Serialize, clone, inspect
|
|
104
|
+
const json = contactForm.toJSON();
|
|
105
|
+
const variant = contactForm.clone('contact-v2');
|
|
106
|
+
const stats = contactForm.getStats();
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Headless React Components
|
|
110
|
+
|
|
111
|
+
Zero HTML, zero CSS. You provide the renderers, RilayKit handles state, validation, and orchestration.
|
|
112
|
+
|
|
113
|
+
| Component | Description |
|
|
114
|
+
|-----------|-------------|
|
|
115
|
+
| `<Form>` | Main wrapper — manages context, state, and submission |
|
|
116
|
+
| `<FormProvider>` | Context provider (used separately from Form when needed) |
|
|
117
|
+
| `<FormBody>` | Renders the full form body from configuration |
|
|
118
|
+
| `<FormField>` | Renders a single field by ID |
|
|
119
|
+
| `<FormRow>` | Renders a row of fields |
|
|
120
|
+
| `<FormSubmitButton>` | Submit button with loading/disabled state |
|
|
121
|
+
|
|
122
|
+
### Zustand-Powered Store
|
|
123
|
+
|
|
124
|
+
Each form instance gets its own Zustand store with granular selectors — only the fields that change trigger re-renders.
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
import {
|
|
128
|
+
useFieldValue,
|
|
129
|
+
useFieldErrors,
|
|
130
|
+
useFieldTouched,
|
|
131
|
+
useFieldState,
|
|
132
|
+
useFormValues,
|
|
133
|
+
useFormValid,
|
|
134
|
+
useFormDirty,
|
|
135
|
+
useFormSubmitting,
|
|
136
|
+
useFieldActions,
|
|
137
|
+
useFormActions,
|
|
138
|
+
} from '@rilaykit/forms';
|
|
139
|
+
|
|
140
|
+
function CustomField({ fieldId }: { fieldId: string }) {
|
|
141
|
+
const value = useFieldValue(fieldId);
|
|
142
|
+
const errors = useFieldErrors(fieldId);
|
|
143
|
+
const { setValue, setTouched } = useFieldActions(fieldId);
|
|
144
|
+
// ...
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Conditional Fields
|
|
149
|
+
|
|
150
|
+
Combined with `@rilaykit/core`'s condition system, fields show/hide reactively based on other field values.
|
|
151
|
+
|
|
152
|
+
```tsx
|
|
153
|
+
import { when } from '@rilaykit/core';
|
|
154
|
+
|
|
155
|
+
rilay.form('account')
|
|
156
|
+
.add({
|
|
157
|
+
id: 'accountType',
|
|
158
|
+
type: 'select',
|
|
159
|
+
props: { options: [{ value: 'business', label: 'Business' }] },
|
|
160
|
+
})
|
|
161
|
+
.add({
|
|
162
|
+
id: 'companyName',
|
|
163
|
+
type: 'input',
|
|
164
|
+
props: { label: 'Company Name' },
|
|
165
|
+
conditions: { visible: when('accountType').equals('business') },
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Validation
|
|
170
|
+
|
|
171
|
+
Supports built-in validators, Standard Schema libraries (Zod, Valibot, Yup...), and custom validators — all in the same field.
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
import { z } from 'zod';
|
|
175
|
+
import { required } from '@rilaykit/core';
|
|
176
|
+
|
|
177
|
+
validation: {
|
|
178
|
+
validate: [required(), z.string().email()],
|
|
179
|
+
validateOnBlur: true,
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## API Overview
|
|
184
|
+
|
|
185
|
+
### Builder
|
|
186
|
+
|
|
187
|
+
| Method | Description |
|
|
188
|
+
|--------|-------------|
|
|
189
|
+
| `form.create(ril, id?)` | Create a new form builder |
|
|
190
|
+
| `.add(...fields)` | Add fields (1-3 per row) |
|
|
191
|
+
| `.addSeparateRows(fields)` | Each field on its own row |
|
|
192
|
+
| `.updateField(id, updates)` | Update a field definition |
|
|
193
|
+
| `.removeField(id)` | Remove a field |
|
|
194
|
+
| `.setValidation(config)` | Set form-level validation |
|
|
195
|
+
| `.addFieldConditions(id, conditions)` | Add conditional logic |
|
|
196
|
+
| `.build()` | Produce the final `FormConfiguration` |
|
|
197
|
+
| `.toJSON()` / `.fromJSON(json)` | Serialize / deserialize |
|
|
198
|
+
| `.clone(newId?)` | Clone the form configuration |
|
|
199
|
+
|
|
200
|
+
### Hooks
|
|
201
|
+
|
|
202
|
+
| Hook | Description |
|
|
203
|
+
|------|-------------|
|
|
204
|
+
| `useFieldValue(id)` | Current field value |
|
|
205
|
+
| `useFieldErrors(id)` | Field validation errors |
|
|
206
|
+
| `useFieldTouched(id)` | Whether field has been touched |
|
|
207
|
+
| `useFieldState(id)` | Combined field state |
|
|
208
|
+
| `useFieldActions(id)` | `setValue`, `setTouched`, etc. |
|
|
209
|
+
| `useFieldConditions(id)` | Evaluated condition results |
|
|
210
|
+
| `useFormValues()` | All form values |
|
|
211
|
+
| `useFormValid()` | Whether form is valid |
|
|
212
|
+
| `useFormDirty()` | Whether form has unsaved changes |
|
|
213
|
+
| `useFormSubmitting()` | Whether form is submitting |
|
|
214
|
+
| `useFormActions()` | `submit`, `reset`, `validate`, etc. |
|
|
215
|
+
|
|
216
|
+
## Architecture
|
|
217
|
+
|
|
218
|
+
```
|
|
219
|
+
@rilaykit/core (registry, types, validation, conditions)
|
|
220
|
+
↑
|
|
221
|
+
@rilaykit/forms ← you are here
|
|
222
|
+
↑
|
|
223
|
+
@rilaykit/workflow (multi-step workflows)
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Documentation
|
|
227
|
+
|
|
228
|
+
Full documentation at [rilay.dev](https://rilay.dev):
|
|
229
|
+
|
|
230
|
+
- [Building Forms](https://rilay.dev/forms/building-forms)
|
|
231
|
+
- [Rendering Forms](https://rilay.dev/forms/rendering-forms)
|
|
232
|
+
- [Form Validation](https://rilay.dev/forms/validation)
|
|
233
|
+
- [Advanced Forms](https://rilay.dev/forms/advanced-forms)
|
|
234
|
+
- [Form Hooks](https://rilay.dev/forms/hooks)
|
|
235
|
+
- [API Reference](https://rilay.dev/api)
|
|
236
|
+
|
|
237
|
+
## License
|
|
238
|
+
|
|
239
|
+
MIT — see [LICENSE](./LICENSE) for details.
|