formo-react 0.0.0-stage → 0.2.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 +201 -0
- package/README.md +21 -2
- package/dist/components/FormoBlueprint.d.ts +5 -0
- package/dist/components/FormoBlueprint.js +30 -0
- package/dist/components/FormoButton.d.ts +6 -0
- package/dist/components/FormoButton.js +23 -0
- package/dist/components/FormoCheckbox.d.ts +7 -0
- package/dist/components/FormoCheckbox.js +10 -0
- package/dist/components/FormoChecklist.d.ts +5 -0
- package/dist/components/FormoChecklist.js +15 -0
- package/dist/components/FormoChoices.d.ts +8 -0
- package/dist/components/FormoChoices.js +33 -0
- package/dist/components/FormoContext.d.ts +27 -0
- package/dist/components/FormoContext.js +69 -0
- package/dist/components/FormoErrors.d.ts +5 -0
- package/dist/components/FormoErrors.js +14 -0
- package/dist/components/FormoField.d.ts +17 -0
- package/dist/components/FormoField.js +148 -0
- package/dist/components/FormoForm.d.ts +15 -0
- package/dist/components/FormoForm.js +76 -0
- package/dist/components/FormoGroup.d.ts +5 -0
- package/dist/components/FormoGroup.js +15 -0
- package/dist/components/FormoHelp.d.ts +6 -0
- package/dist/components/FormoHelp.js +9 -0
- package/dist/components/FormoInput.d.ts +6 -0
- package/dist/components/FormoInput.js +24 -0
- package/dist/components/FormoLabel.d.ts +6 -0
- package/dist/components/FormoLabel.js +9 -0
- package/dist/components/FormoProvider.d.ts +11 -0
- package/dist/components/FormoProvider.js +13 -0
- package/dist/components/FormoRest.d.ts +6 -0
- package/dist/components/FormoRest.js +23 -0
- package/dist/components/FormoSelect.d.ts +6 -0
- package/dist/components/FormoSelect.js +36 -0
- package/dist/components/FormoUneditable.d.ts +6 -0
- package/dist/components/FormoUneditable.js +12 -0
- package/dist/core/builder.d.ts +42 -0
- package/dist/core/builder.js +166 -0
- package/dist/core/controls.d.ts +27 -0
- package/dist/core/controls.js +24 -0
- package/dist/core/fields.d.ts +63 -0
- package/dist/core/fields.js +177 -0
- package/dist/core/messages.d.ts +35 -0
- package/dist/core/messages.js +69 -0
- package/dist/core/normalize.d.ts +35 -0
- package/dist/core/normalize.js +234 -0
- package/dist/core/options.d.ts +44 -0
- package/dist/core/options.js +146 -0
- package/dist/core/rules.d.ts +41 -0
- package/dist/core/rules.js +230 -0
- package/dist/core/store.d.ts +155 -0
- package/dist/core/store.js +830 -0
- package/dist/core/theme.d.ts +20 -0
- package/dist/core/theme.js +21 -0
- package/dist/core/tree.d.ts +74 -0
- package/dist/core/tree.js +119 -0
- package/dist/core/utils.d.ts +35 -0
- package/dist/core/utils.js +206 -0
- package/dist/hooks/subscriptions.d.ts +38 -0
- package/dist/hooks/subscriptions.js +65 -0
- package/dist/hooks/useFormo.d.ts +18 -0
- package/dist/hooks/useFormo.js +25 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +27 -0
- package/dist/schema/form.d.ts +92 -0
- package/dist/schema/form.js +304 -0
- package/dist/schema/index.d.ts +22 -0
- package/dist/schema/index.js +21 -0
- package/dist/schema/ir.d.ts +189 -0
- package/dist/schema/ir.js +130 -0
- package/dist/schema/json-schema.d.ts +25 -0
- package/dist/schema/json-schema.js +351 -0
- package/dist/schema/laravel.d.ts +57 -0
- package/dist/schema/laravel.js +555 -0
- package/dist/schema/openapi.d.ts +24 -0
- package/dist/schema/openapi.js +67 -0
- package/dist/schema/policy.d.ts +60 -0
- package/dist/schema/policy.js +315 -0
- package/dist/schema/spec.d.ts +75 -0
- package/dist/schema/spec.js +1 -0
- package/dist/schema/values.d.ts +35 -0
- package/dist/schema/values.js +151 -0
- package/dist/schema/zod.d.ts +79 -0
- package/dist/schema/zod.js +236 -0
- package/dist/types.d.ts +400 -0
- package/dist/types.js +1 -0
- package/package.json +70 -4
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import type { ComponentType, ReactNode } from 'react';
|
|
2
|
+
import type { StandardSchemaLike } from './schema/spec';
|
|
3
|
+
import type { FormoStore } from './core/store';
|
|
4
|
+
export interface RuleContext {
|
|
5
|
+
/** Every value of the form */
|
|
6
|
+
values: Record<string, any>;
|
|
7
|
+
/** Values of the group the field belongs to (`@ref` params resolve here first) */
|
|
8
|
+
parentValues: Record<string, any>;
|
|
9
|
+
/** Dotted path of the field (`address.zip`) */
|
|
10
|
+
path: string;
|
|
11
|
+
}
|
|
12
|
+
/** A rule function (typed like formo-vue's: a group rule can declare its value, `(rows: Phone[]) => …`) */
|
|
13
|
+
export type RuleFn = (value: any, params: any[], ctx: RuleContext) => boolean | string | Promise<boolean | string>;
|
|
14
|
+
/**
|
|
15
|
+
* A rule in any of formo-vue's notations:
|
|
16
|
+
* - `'required'`, `'min:3'`, `'required|min:3'`
|
|
17
|
+
* - `(value, params, ctx) => true | false | 'message'`
|
|
18
|
+
* - a tuple: `['regex', [/^[a-z]+$/]]`, `[myRule, [3]]`
|
|
19
|
+
* - an object with a per-rule message and a checklist label: `{ rule: 'regex', params: [/x/], message, label }`
|
|
20
|
+
* - VeeValidate object notation: `{ required: true, min: 3 }`
|
|
21
|
+
* - a Standard Schema (zod…)
|
|
22
|
+
*/
|
|
23
|
+
export type RuleSpec = string | RuleFn | [string | RuleFn, unknown[]?] | {
|
|
24
|
+
rule: string | RuleFn;
|
|
25
|
+
params?: unknown[];
|
|
26
|
+
message?: string;
|
|
27
|
+
label?: string;
|
|
28
|
+
} | StandardSchemaLike | Record<string, unknown>;
|
|
29
|
+
/**
|
|
30
|
+
* How a field shows its validation state:
|
|
31
|
+
* - `first` the first failing rule's message (default)
|
|
32
|
+
* - `all` every failing rule's message
|
|
33
|
+
* - `checklist` every rule as a live requirement list (✗ / ✓), e.g. password rules
|
|
34
|
+
*/
|
|
35
|
+
export type ErrorDisplay = 'first' | 'all' | 'checklist';
|
|
36
|
+
/** One line of a requirement checklist */
|
|
37
|
+
export interface ChecklistItem {
|
|
38
|
+
/** Rule name (`min`, `mixed_case`…) */
|
|
39
|
+
rule: string;
|
|
40
|
+
/** Requirement text (`At least 8 characters`) */
|
|
41
|
+
label: string;
|
|
42
|
+
/** Whether the current value meets it */
|
|
43
|
+
passed: boolean;
|
|
44
|
+
/** The error message this rule would produce */
|
|
45
|
+
message: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Settings that cascade from `useFormo(def, { config })` / `<FormoForm config>`
|
|
49
|
+
* to the form, then to each group (`config` on a form or group definition).
|
|
50
|
+
*/
|
|
51
|
+
export interface FormoConfig {
|
|
52
|
+
/** Default error display for every field below (`first`, `all`, `checklist`) */
|
|
53
|
+
errorDisplay?: ErrorDisplay;
|
|
54
|
+
/**
|
|
55
|
+
* Groups nest their fields' values under their alias (`address.street`) — `false`
|
|
56
|
+
* on a group makes it visual only: its fields' values stay at the parent's level.
|
|
57
|
+
* Cascades like any config. Default `true`
|
|
58
|
+
*/
|
|
59
|
+
namespaces?: boolean;
|
|
60
|
+
[key: string]: unknown;
|
|
61
|
+
}
|
|
62
|
+
export interface FormoOption {
|
|
63
|
+
value: unknown;
|
|
64
|
+
label: string;
|
|
65
|
+
disabled?: boolean;
|
|
66
|
+
/** Any extra data a custom component may want (icons, descriptions...) */
|
|
67
|
+
[key: string]: unknown;
|
|
68
|
+
}
|
|
69
|
+
export interface FormoOptionGroup {
|
|
70
|
+
label: string;
|
|
71
|
+
options: FormoOption[];
|
|
72
|
+
disabled?: boolean;
|
|
73
|
+
}
|
|
74
|
+
export type FormoOptionItem = FormoOption | FormoOptionGroup;
|
|
75
|
+
/**
|
|
76
|
+
* Everything `opts` accepts (same as formo-vue):
|
|
77
|
+
*
|
|
78
|
+
* - `{ run: 'Running', swim: 'Swimming' }` value => label
|
|
79
|
+
* - `{ Exercise: { run: 'Running' }, Dates: {...} }` option groups
|
|
80
|
+
* - `['a', 'b']` the value is the label
|
|
81
|
+
* - `[{ value, label, disabled? }]`, `[[value, label]]`
|
|
82
|
+
* - `[{ label: 'Group', options: [...] }]` option groups
|
|
83
|
+
*/
|
|
84
|
+
export type FormoOptionsInput = Record<string, unknown> | Array<string | number | FormoOptionItem | [unknown, string]>;
|
|
85
|
+
/** What `editable` functions receive */
|
|
86
|
+
export interface FormoValueContext {
|
|
87
|
+
values: Record<string, any>;
|
|
88
|
+
parentValues: Record<string, any>;
|
|
89
|
+
node: FieldNode;
|
|
90
|
+
path: string;
|
|
91
|
+
}
|
|
92
|
+
/** Props every control component gets */
|
|
93
|
+
export interface FormoControlProps {
|
|
94
|
+
field: FormoHandle;
|
|
95
|
+
}
|
|
96
|
+
/** Props of a read-only view (`editable: false`) */
|
|
97
|
+
export interface FormoReadOnlyProps {
|
|
98
|
+
field: FormoHandle;
|
|
99
|
+
/** The value as text, formatted like the default read-only view (option labels, Yes / No…) */
|
|
100
|
+
text: string;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* A custom control (`component` on a field). It may implement its own read-only
|
|
104
|
+
* view as a static `readOnly` component — shown when the field is `editable: false`.
|
|
105
|
+
* Without one, the default read-only view (`FormoUneditable`) is used.
|
|
106
|
+
*
|
|
107
|
+
* ```tsx
|
|
108
|
+
* function StarRating({ field }: FormoControlProps) { ... }
|
|
109
|
+
* StarRating.readOnly = ({ field, text }: FormoReadOnlyProps) => <Stars value={field.value} />
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export type FormoControlComponent<P = any> = ComponentType<P> & {
|
|
113
|
+
readOnly?: ComponentType<FormoReadOnlyProps>;
|
|
114
|
+
};
|
|
115
|
+
export interface ControlModel {
|
|
116
|
+
prop?: string;
|
|
117
|
+
event?: string;
|
|
118
|
+
get?: (value: unknown) => unknown;
|
|
119
|
+
set?: (value: unknown) => unknown;
|
|
120
|
+
}
|
|
121
|
+
export interface FormoControl {
|
|
122
|
+
__formoControl: true;
|
|
123
|
+
component: ComponentRef;
|
|
124
|
+
props?: (field: FormoFieldHandle) => Record<string, unknown>;
|
|
125
|
+
model?: ControlModel;
|
|
126
|
+
}
|
|
127
|
+
export type ComponentRef = FormoControlComponent | FormoControl | string;
|
|
128
|
+
export type FormoSlotFn = ReactNode | ((field: FormoFieldHandle) => ReactNode);
|
|
129
|
+
export interface FormoFieldSlots {
|
|
130
|
+
label?: FormoSlotFn;
|
|
131
|
+
input?: FormoSlotFn;
|
|
132
|
+
help?: FormoSlotFn;
|
|
133
|
+
checklist?: FormoSlotFn;
|
|
134
|
+
errors?: FormoSlotFn;
|
|
135
|
+
}
|
|
136
|
+
export interface FormoFieldViewProps {
|
|
137
|
+
formo: FormoFieldHandle;
|
|
138
|
+
slots: {
|
|
139
|
+
label: ReactNode;
|
|
140
|
+
input: ReactNode;
|
|
141
|
+
help: ReactNode;
|
|
142
|
+
checklist: ReactNode;
|
|
143
|
+
errors: ReactNode;
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
export interface FormoViews {
|
|
147
|
+
field?: ComponentType<FormoFieldViewProps>;
|
|
148
|
+
label?: ComponentType<{
|
|
149
|
+
field?: FormoFieldHandle;
|
|
150
|
+
}>;
|
|
151
|
+
help?: ComponentType<{
|
|
152
|
+
field?: FormoFieldHandle;
|
|
153
|
+
}>;
|
|
154
|
+
checklist?: ComponentType<{
|
|
155
|
+
field?: FormoFieldHandle;
|
|
156
|
+
}>;
|
|
157
|
+
errors?: ComponentType<{
|
|
158
|
+
field?: FormoFieldHandle;
|
|
159
|
+
}>;
|
|
160
|
+
}
|
|
161
|
+
export interface FormoTheme {
|
|
162
|
+
__formoTheme: true;
|
|
163
|
+
views: FormoViews;
|
|
164
|
+
components: Record<string, ComponentRef>;
|
|
165
|
+
classes?: Record<string, string>;
|
|
166
|
+
}
|
|
167
|
+
export interface FieldDef {
|
|
168
|
+
alias?: string | number;
|
|
169
|
+
/** `input`, `input|email`, `textarea`, `select`, `checkbox`, `checkboxes`, `radios`, `switch`, `button`, `group`, `form` */
|
|
170
|
+
driver?: string;
|
|
171
|
+
type?: string;
|
|
172
|
+
/** Initial value. `value` is accepted as an alias */
|
|
173
|
+
val?: unknown;
|
|
174
|
+
value?: unknown;
|
|
175
|
+
/** The field's label — `false` / `null` hides it (formo-vue); default: the humanized alias */
|
|
176
|
+
label?: string | false | null;
|
|
177
|
+
placeholder?: string;
|
|
178
|
+
/** Help text rendered under the control */
|
|
179
|
+
message?: string;
|
|
180
|
+
required?: boolean;
|
|
181
|
+
/** `false` (or a function returning false) shows the value as text instead of a control */
|
|
182
|
+
editable?: boolean | ((ctx: FormoValueContext) => boolean);
|
|
183
|
+
disabled?: boolean;
|
|
184
|
+
render?: boolean;
|
|
185
|
+
ignore?: boolean | Record<string, unknown>;
|
|
186
|
+
/** `true` drops the field's value from what's submitted (and skips its validation) */
|
|
187
|
+
exclude?: boolean | Record<string, unknown>;
|
|
188
|
+
rules?: RuleSpec | RuleSpec[];
|
|
189
|
+
/** Per-rule messages for this field: `{ required: 'Tell us your name!' }` */
|
|
190
|
+
errorMessages?: Record<string, string>;
|
|
191
|
+
error_messages?: Record<string, string>;
|
|
192
|
+
/** `first` (default), `all` failing messages, or a live `checklist` of every rule */
|
|
193
|
+
errorDisplay?: ErrorDisplay;
|
|
194
|
+
/** Shortcut for `errorDisplay: 'checklist'` */
|
|
195
|
+
checklist?: boolean;
|
|
196
|
+
/** Per-rule requirement texts for the checklist: `{ min: 'Eight characters or more' }` */
|
|
197
|
+
requirements?: Record<string, string>;
|
|
198
|
+
opts?: FormoOptionsInput;
|
|
199
|
+
/** Alias of `opts` */
|
|
200
|
+
options?: FormoOptionsInput;
|
|
201
|
+
/** Selects: an empty first option — `true` ("Select item") or the placeholder text */
|
|
202
|
+
blank?: boolean | string;
|
|
203
|
+
/** Settings for this form / group and everything below it (`{ errorDisplay: 'all' }`) */
|
|
204
|
+
config?: FormoConfig;
|
|
205
|
+
/**
|
|
206
|
+
* Subforms: their fields — a list, or keyed by alias (formo-vue): a definition,
|
|
207
|
+
* a driver name, a builder; falsy entries are skipped (`admin: isAdmin && { driver: 'checkbox' }`)
|
|
208
|
+
*/
|
|
209
|
+
fields?: Array<FieldInput | FormoBuildable | false | null | undefined> | Record<string, FieldDef | string | FormoBuildable | false | null | undefined>;
|
|
210
|
+
/** Repeatable rows: this group's `fields` are the template of one row, its value an array of rows */
|
|
211
|
+
blueprint?: boolean;
|
|
212
|
+
/** Blueprints: rows it starts with (without `val`), and can't go below */
|
|
213
|
+
min?: number;
|
|
214
|
+
/** Blueprints: the most rows it can have */
|
|
215
|
+
max?: number;
|
|
216
|
+
/** Blueprints: the add button's text (default "Add another") */
|
|
217
|
+
addLabel?: string;
|
|
218
|
+
/** Blueprints: the remove buttons' text (default "Remove") */
|
|
219
|
+
removeLabel?: string;
|
|
220
|
+
/** Blueprints: each row's heading — `:index` (from 1) and `:label` placeholders (default ":label #:index") */
|
|
221
|
+
rowLabel?: string;
|
|
222
|
+
component?: ComponentRef;
|
|
223
|
+
props?: Record<string, unknown>;
|
|
224
|
+
model?: ControlModel;
|
|
225
|
+
view?: ComponentType<FormoFieldViewProps> | string;
|
|
226
|
+
/** Control attributes (`multiple` on a select, `type: 'submit'` on a button…) */
|
|
227
|
+
attr?: Record<string, any>;
|
|
228
|
+
[key: string]: unknown;
|
|
229
|
+
}
|
|
230
|
+
export type FieldInput = string | FieldDef | [string, string?, unknown?, FieldDef?];
|
|
231
|
+
export interface FieldNode {
|
|
232
|
+
alias: string | number;
|
|
233
|
+
driver: string;
|
|
234
|
+
type?: string;
|
|
235
|
+
val?: unknown;
|
|
236
|
+
/** Resolved label: `''` when hidden (`label: false | null`) */
|
|
237
|
+
label?: string;
|
|
238
|
+
placeholder?: string;
|
|
239
|
+
message?: string;
|
|
240
|
+
required: boolean;
|
|
241
|
+
editable: boolean | ((ctx: FormoValueContext) => boolean);
|
|
242
|
+
disabled: boolean;
|
|
243
|
+
render: boolean;
|
|
244
|
+
exclude?: boolean | Record<string, unknown>;
|
|
245
|
+
rules: RuleSpec[];
|
|
246
|
+
errorMessages: Record<string, string>;
|
|
247
|
+
/** Undefined: inherited from `config.errorDisplay` */
|
|
248
|
+
errorDisplay?: ErrorDisplay;
|
|
249
|
+
requirements: Record<string, string>;
|
|
250
|
+
/** Options, normalized (option groups kept) */
|
|
251
|
+
options: FormoOptionItem[];
|
|
252
|
+
blank?: boolean | string;
|
|
253
|
+
config?: FormoConfig;
|
|
254
|
+
fields: FieldNode[];
|
|
255
|
+
blueprint: boolean;
|
|
256
|
+
min?: number;
|
|
257
|
+
max?: number;
|
|
258
|
+
addLabel?: string;
|
|
259
|
+
removeLabel?: string;
|
|
260
|
+
rowLabel?: string;
|
|
261
|
+
component?: ComponentRef;
|
|
262
|
+
props?: Record<string, unknown>;
|
|
263
|
+
model?: ControlModel;
|
|
264
|
+
view?: ComponentType<FormoFieldViewProps> | string;
|
|
265
|
+
attr: Record<string, any>;
|
|
266
|
+
raw: FieldDef;
|
|
267
|
+
}
|
|
268
|
+
/** A builder (`Formo`) — anything that produces a definition */
|
|
269
|
+
export interface FormoBuildable {
|
|
270
|
+
toSchema(): FieldDef;
|
|
271
|
+
}
|
|
272
|
+
export type FormoDefinition = FieldDef | FormoBuildable;
|
|
273
|
+
export type OutcomeResult = 'success' | 'invalid' | 'rejected';
|
|
274
|
+
export type RejectedReason = 'validation' | 'session' | 'server' | 'handler' | 'error';
|
|
275
|
+
export interface FormoOutcome {
|
|
276
|
+
result: OutcomeResult;
|
|
277
|
+
reason?: RejectedReason;
|
|
278
|
+
data?: unknown;
|
|
279
|
+
errors?: Record<string, string>;
|
|
280
|
+
error?: unknown;
|
|
281
|
+
}
|
|
282
|
+
/** `field`, `group` (a subform) or `blueprint` (repeatable rows) */
|
|
283
|
+
export type FormoHandleKind = 'field' | 'group' | 'blueprint';
|
|
284
|
+
/**
|
|
285
|
+
* A reference to a field, subform or repeatable group of a form — what
|
|
286
|
+
* `<FormoField field={form.fields.email} />` renders. Groups have `fields`,
|
|
287
|
+
* blueprints have `rows`, `add()`, `remove()`, `canAdd`, `canRemove` (formo-vue's handles).
|
|
288
|
+
*
|
|
289
|
+
* `form.fields.*` handles are live: one object per path, their getters read the
|
|
290
|
+
* current state (reading them while rendering doesn't subscribe — components
|
|
291
|
+
* subscribe with `useFormoField(handle)`). The `field` that controls and
|
|
292
|
+
* `<FormoField>` children receive is a snapshot, re-rendered when it changes.
|
|
293
|
+
*/
|
|
294
|
+
export interface FormoHandle {
|
|
295
|
+
kind: FormoHandleKind;
|
|
296
|
+
/** Path in the form values (`address.street`, `phones[0].number`) */
|
|
297
|
+
path: string;
|
|
298
|
+
alias: string | number;
|
|
299
|
+
node: FieldNode;
|
|
300
|
+
/** The form it belongs to */
|
|
301
|
+
form: FormoController;
|
|
302
|
+
/** Its value (a group: its values object, a blueprint: its array of rows) */
|
|
303
|
+
value: unknown;
|
|
304
|
+
/** The first error message (a group's / blueprint's own error: from its `rules`) */
|
|
305
|
+
error: string | undefined;
|
|
306
|
+
/** Every error message (several for `errorDisplay: 'all' | 'checklist'` fields) */
|
|
307
|
+
errors: string[];
|
|
308
|
+
invalid: boolean;
|
|
309
|
+
touched: boolean;
|
|
310
|
+
required: boolean;
|
|
311
|
+
/** Resolved: `false` renders the read-only view */
|
|
312
|
+
editable: boolean;
|
|
313
|
+
disabled: boolean;
|
|
314
|
+
/** `first`, `all` or `checklist` — the field's own, or inherited from `config` */
|
|
315
|
+
errorDisplay: ErrorDisplay;
|
|
316
|
+
/** Live requirement list — only filled for `checklist` fields */
|
|
317
|
+
checklist: ChecklistItem[];
|
|
318
|
+
/** The field's options (selects, radios, checkboxes) */
|
|
319
|
+
options: FormoOptionItem[];
|
|
320
|
+
setValue: (value: unknown) => void;
|
|
321
|
+
setBlur: () => void;
|
|
322
|
+
/** Subforms: their children, by alias */
|
|
323
|
+
fields?: FormoFields;
|
|
324
|
+
/** Blueprints: one handle per row — `rows.map((row) => <View key={row.key}>…)` */
|
|
325
|
+
rows?: FormoRowHandle[];
|
|
326
|
+
/** Blueprints: add a row (the row's defaults, merged with `values`) — unless `max` is reached */
|
|
327
|
+
add?: (values?: Record<string, unknown>) => void;
|
|
328
|
+
/** Blueprints: remove a row — unless `min` is reached */
|
|
329
|
+
remove?: (index: number) => void;
|
|
330
|
+
/** Blueprints: below `max`? */
|
|
331
|
+
canAdd?: boolean;
|
|
332
|
+
/** Blueprints: above `min`? */
|
|
333
|
+
canRemove?: boolean;
|
|
334
|
+
}
|
|
335
|
+
/** Children of a form / subform / row, by alias */
|
|
336
|
+
export type FormoFields = {
|
|
337
|
+
readonly [alias: string]: FormoHandle;
|
|
338
|
+
};
|
|
339
|
+
/** A field's handle (every handle has the field members) */
|
|
340
|
+
export type FormoFieldHandle = FormoHandle;
|
|
341
|
+
export interface FormoGroupHandle extends FormoHandle {
|
|
342
|
+
kind: 'group';
|
|
343
|
+
fields: FormoFields;
|
|
344
|
+
}
|
|
345
|
+
export interface FormoBlueprintHandle extends FormoHandle {
|
|
346
|
+
kind: 'blueprint';
|
|
347
|
+
rows: FormoRowHandle[];
|
|
348
|
+
add: (values?: Record<string, unknown>) => void;
|
|
349
|
+
remove: (index: number) => void;
|
|
350
|
+
canAdd: boolean;
|
|
351
|
+
canRemove: boolean;
|
|
352
|
+
}
|
|
353
|
+
/** One row of a repeatable group: `form.fields.phones.rows[0].fields.number` */
|
|
354
|
+
export interface FormoRowHandle {
|
|
355
|
+
/** Stable across adds / removes — use it as the React `key` */
|
|
356
|
+
key: string | number;
|
|
357
|
+
index: number;
|
|
358
|
+
/** `phones[0]` */
|
|
359
|
+
path: string;
|
|
360
|
+
/** The row's heading (`rowLabel`, default ":label #:index") */
|
|
361
|
+
label: string;
|
|
362
|
+
fields: FormoFields;
|
|
363
|
+
/** Above the blueprint's `min`? */
|
|
364
|
+
canRemove: boolean;
|
|
365
|
+
/** Remove this row (unless `min` is reached) */
|
|
366
|
+
remove: () => void;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* What `useFormo` returns — a stable object: reading `values`, `errors`, `dirty`…
|
|
370
|
+
* always gives the current state, but doesn't re-render the component reading it.
|
|
371
|
+
* To render them, subscribe: `useFormoState(form, (s) => s.dirty)`, `useFormoValue(form, 'email')`.
|
|
372
|
+
*/
|
|
373
|
+
export interface FormoController {
|
|
374
|
+
/** The form's store: state, per-path subscriptions (what the hooks use) */
|
|
375
|
+
store: FormoStore;
|
|
376
|
+
root: FieldNode;
|
|
377
|
+
values: Record<string, any>;
|
|
378
|
+
/** The first error per path (groups and blueprints included; `''` is the form's own) */
|
|
379
|
+
errors: Record<string, string>;
|
|
380
|
+
/** Every error per path — fields with `errorDisplay: 'all' | 'checklist'` can have several */
|
|
381
|
+
allErrors: Record<string, string[]>;
|
|
382
|
+
touched: Record<string, boolean>;
|
|
383
|
+
dirty: boolean;
|
|
384
|
+
submitting: boolean;
|
|
385
|
+
outcome: FormoOutcome | null;
|
|
386
|
+
/** Handles, nested like the form: `form.fields.address.fields.zip`, `form.fields.phones.rows[0].fields.number` */
|
|
387
|
+
fields: FormoFields;
|
|
388
|
+
/** A handle by path (`address.zip`, `phones[0].number`), else by alias (the shallowest match) */
|
|
389
|
+
field: (pathOrAlias: string) => FormoHandle | undefined;
|
|
390
|
+
val: (path: string, value?: unknown) => any;
|
|
391
|
+
load: (values: Record<string, any>) => void;
|
|
392
|
+
reset: (values?: Record<string, any>) => void;
|
|
393
|
+
/**
|
|
394
|
+
* Set errors, by path — server keys work too (`phones.0.number`), and nested
|
|
395
|
+
* objects (`{ address: { zip: '…', ':self': '…' } }`)
|
|
396
|
+
*/
|
|
397
|
+
setErrors: (errors: Record<string, unknown>) => void;
|
|
398
|
+
validate: () => Promise<boolean>;
|
|
399
|
+
submit: (handler?: (values: Record<string, any>) => unknown) => Promise<boolean>;
|
|
400
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "formo-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Formo — forms as objects — for React.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/bmidget/formo-react.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://bmidget.github.io/formo-react/",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/bmidget/formo-react/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"react",
|
|
16
|
+
"forms",
|
|
17
|
+
"formo",
|
|
18
|
+
"validation",
|
|
19
|
+
"zod",
|
|
20
|
+
"laravel",
|
|
21
|
+
"json-schema",
|
|
22
|
+
"typescript"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./schema": {
|
|
33
|
+
"types": "./dist/schema/index.d.ts",
|
|
34
|
+
"import": "./dist/schema/index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"sideEffects": false,
|
|
41
|
+
"scripts": {
|
|
42
|
+
"dev": "vite",
|
|
43
|
+
"docs:dev": "vitepress dev docs",
|
|
44
|
+
"build:docs": "vitepress build docs",
|
|
45
|
+
"preview:docs": "vitepress preview docs",
|
|
46
|
+
"build:playground": "vite build",
|
|
47
|
+
"preview": "vite preview",
|
|
48
|
+
"typecheck": "tsc --noEmit",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"test:watch": "vitest",
|
|
51
|
+
"check": "bun run typecheck && bun run test",
|
|
52
|
+
"build": "tsc -p tsconfig.build.json",
|
|
53
|
+
"prepublishOnly": "bun run check && bun run build"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"react": ">=18.0.0",
|
|
57
|
+
"react-dom": ">=18.0.0"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@types/node": "^26.6.11",
|
|
61
|
+
"@types/react": "^19.0.0",
|
|
62
|
+
"@types/react-dom": "^19.0.0",
|
|
63
|
+
"@vitejs/plugin-react": "^6.0.0",
|
|
64
|
+
"jsdom": "^28.0.0",
|
|
65
|
+
"react": "^19.0.0",
|
|
66
|
+
"react-dom": "^19.0.0",
|
|
67
|
+
"typescript": "~5.9.3",
|
|
68
|
+
"vite": "^8.0.0",
|
|
69
|
+
"vitepress": "^1.6.4",
|
|
70
|
+
"vitest": "^3.2.0"
|
|
71
|
+
}
|
|
72
|
+
}
|