@codeleap/form 7.0.0 → 7.0.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.
Files changed (54) hide show
  1. package/dist/fields/bool.js +19 -0
  2. package/dist/fields/bool.js.map +1 -0
  3. package/dist/fields/date.js +63 -0
  4. package/dist/fields/date.js.map +1 -0
  5. package/dist/fields/file.js +20 -0
  6. package/dist/fields/file.js.map +1 -0
  7. package/dist/fields/group.js +20 -0
  8. package/dist/fields/group.js.map +1 -0
  9. package/dist/fields/index.d.ts +3 -3
  10. package/dist/fields/index.js +42 -0
  11. package/dist/fields/index.js.map +1 -0
  12. package/dist/fields/list.js +55 -0
  13. package/dist/fields/list.js.map +1 -0
  14. package/dist/fields/number.js +39 -0
  15. package/dist/fields/number.js.map +1 -0
  16. package/dist/fields/selectable.js +26 -0
  17. package/dist/fields/selectable.js.map +1 -0
  18. package/dist/fields/text.js +20 -0
  19. package/dist/fields/text.js.map +1 -0
  20. package/dist/hooks/index.js +8 -0
  21. package/dist/hooks/index.js.map +1 -0
  22. package/dist/hooks/useField.js +27 -0
  23. package/dist/hooks/useField.js.map +1 -0
  24. package/dist/hooks/useValidate.js +58 -0
  25. package/dist/hooks/useValidate.js.map +1 -0
  26. package/dist/index.js +22 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/lib/Field.d.ts +4 -4
  29. package/dist/lib/Field.js +351 -0
  30. package/dist/lib/Field.js.map +1 -0
  31. package/dist/lib/Form.d.ts +2 -2
  32. package/dist/lib/Form.js +201 -0
  33. package/dist/lib/Form.js.map +1 -0
  34. package/dist/lib/factories.js +18 -0
  35. package/dist/lib/factories.js.map +1 -0
  36. package/dist/lib/index.js +21 -0
  37. package/dist/lib/index.js.map +1 -0
  38. package/dist/lib/useFieldBinding.js +51 -0
  39. package/dist/lib/useFieldBinding.js.map +1 -0
  40. package/dist/types/field.js +3 -0
  41. package/dist/types/field.js.map +1 -0
  42. package/dist/types/form.js +3 -0
  43. package/dist/types/form.js.map +1 -0
  44. package/dist/types/globals.js +3 -0
  45. package/dist/types/globals.js.map +1 -0
  46. package/dist/types/index.js +3 -0
  47. package/dist/types/index.js.map +1 -0
  48. package/dist/types/validation.js +3 -0
  49. package/dist/types/validation.js.map +1 -0
  50. package/dist/validators/index.js +18 -0
  51. package/dist/validators/index.js.map +1 -0
  52. package/dist/validators/zod.js +47 -0
  53. package/dist/validators/zod.js.map +1 -0
  54. package/package.json +10 -10
@@ -0,0 +1,351 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Field = exports.ValidationError = void 0;
4
+ const nanostores_1 = require("nanostores");
5
+ const types_1 = require("@codeleap/types");
6
+ const react_1 = require("@nanostores/react");
7
+ const useFieldBinding_1 = require("./useFieldBinding");
8
+ const react_2 = require("react");
9
+ const logger_1 = require("@codeleap/logger");
10
+ /**
11
+ * Thrown inside a validator function to signal a structured validation failure
12
+ * without propagating as an unhandled exception. The `data` payload is forwarded
13
+ * verbatim as the `error` field of the resulting {@link ValidationResult}.
14
+ *
15
+ * Throw this instead of returning `{ isValid: false }` when you need to exit
16
+ * validation from a nested helper that cannot easily return a value.
17
+ */
18
+ class ValidationError extends Error {
19
+ constructor(data) {
20
+ super();
21
+ this.data = data;
22
+ }
23
+ }
24
+ exports.ValidationError = ValidationError;
25
+ /**
26
+ * Base class for all form fields. Owns the nanostores atom that holds the
27
+ * field's current value, runs synchronous validation on demand, and exposes
28
+ * React hooks (`use`, `useValue`, `useValidation`) that components call to
29
+ * subscribe to reactive updates.
30
+ *
31
+ * **Validation lifecycle**
32
+ * Validation is always synchronous and called eagerly — there is no deferred
33
+ * or debounced step. `validate()` runs against the current atom value each
34
+ * time it is called, so repeated calls are cheap but not memoised. Error
35
+ * visibility is decoupled from validity: errors are hidden until
36
+ * `revealError()` is called (or the field has been blurred while starting
37
+ * with an undefined value), allowing UX to control when messages appear.
38
+ *
39
+ * **Initialization order constraint**
40
+ * The constructor calls `loadState()` synchronously. If `defaultValue` is a
41
+ * Promise or a function that returns a Promise, the atom is initialised with
42
+ * `undefined` immediately and the resolved value is set asynchronously once
43
+ * the promise settles. Do not read `initialValue` before the promise resolves.
44
+ *
45
+ * **Platform methods**
46
+ * `measurePosition`, `scrollTo`, and `getPadding` delegate to static method
47
+ * slots (`methodMeasurePosition`, `methodScrollTo`, `methodGetPadding`) that
48
+ * must be assigned by the platform layer (web or mobile) before use; calling
49
+ * them without an implementation throws.
50
+ *
51
+ * **Prop transformers**
52
+ * `props()` pipes the field's properties through every registered transformer
53
+ * in insertion order. Transformers are global and shared across all field
54
+ * instances via `Field.transformers`.
55
+ */
56
+ class Field {
57
+ /**
58
+ * Marks the field's error as visible. Typically called by `Form.validate`
59
+ * when `revealErrors: true` is passed, or imperatively after a failed submit.
60
+ * Has no effect on the underlying validity — only on whether the error
61
+ * message is shown to the user.
62
+ */
63
+ revealError() {
64
+ this.errorRevealed.set(true);
65
+ }
66
+ /**
67
+ * Resets error visibility to hidden without changing the field's value or
68
+ * validity state. Useful when clearing a form section programmatically
69
+ * while preserving the current value.
70
+ */
71
+ hideError() {
72
+ this.errorRevealed.set(false);
73
+ }
74
+ get isErrorRevealed() {
75
+ return this.errorRevealed.get();
76
+ }
77
+ constructor(options) {
78
+ this.properties = {};
79
+ this.options = options;
80
+ this.ref = (0, react_2.createRef)();
81
+ this.loadState();
82
+ this.errorRevealed = (0, nanostores_1.atom)(false);
83
+ this.setValue = this.setValue.bind(this);
84
+ this.use = this.use.bind(this);
85
+ this.useBinding = this.useBinding.bind(this);
86
+ this.revealError = this.revealError.bind(this);
87
+ this.hideError = this.hideError.bind(this);
88
+ this.properties = this.toInternalProperties(options);
89
+ }
90
+ get name() {
91
+ return this.options.name;
92
+ }
93
+ get value() {
94
+ return this.state.get();
95
+ }
96
+ get isValid() {
97
+ const res = this.validate();
98
+ return res.isValid;
99
+ }
100
+ toInternalProperties(options) {
101
+ const internalKeys = new Set(['name', 'defaultValue', 'state', 'validate', 'loader', 'onValueChange']);
102
+ const values = Object.assign({ name: this.name }, Object.fromEntries(Object.entries(options).filter(([key]) => !internalKeys.has(key))));
103
+ return Object.assign({ field: this }, values);
104
+ }
105
+ /**
106
+ * Replaces the field's internal atom with a slice of the owning `Form`'s
107
+ * global state atom and migrates the current value into it. Called
108
+ * automatically by `Form.attachState()` during construction — do not call
109
+ * this manually unless you are building a custom form container.
110
+ */
111
+ attach(to) {
112
+ const val = this.value;
113
+ this.state = to;
114
+ this.setValue(val);
115
+ }
116
+ setValue(to) {
117
+ if (this.options.onValueChange)
118
+ this.options.onValueChange(to);
119
+ return this.state.set(to);
120
+ }
121
+ useValue() {
122
+ const value = (0, react_1.useStore)(this.state);
123
+ return value;
124
+ }
125
+ resetValue() {
126
+ this.setValue(this.initialValue);
127
+ this.errorRevealed.set(false);
128
+ }
129
+ /**
130
+ * Primary React hook for consuming a field inside a component. Subscribes to
131
+ * the field's value atom and validation state, and optionally wires an
132
+ * imperative ref handle via `useFieldBinding`. Must be called
133
+ * unconditionally at the component's top level.
134
+ *
135
+ * The `changed` flag compares the current value to the value captured at
136
+ * field construction time (or after the last `resetValue`), not to any
137
+ * previous render.
138
+ */
139
+ use(impl, deps) {
140
+ const value = this.useValue();
141
+ const validation = this.useValidation();
142
+ // Yes, this is dangerous and doesn't follow the rules, but not passing an implementation to imperative handle after passing it once would break the app anyway
143
+ if (impl) {
144
+ this.useBinding(impl, deps);
145
+ }
146
+ const changed = value != this.initialValue;
147
+ return {
148
+ validation,
149
+ value,
150
+ setValue: this.setValue,
151
+ changed,
152
+ representation: this.toRepresentation(value),
153
+ options: this.options,
154
+ };
155
+ }
156
+ useBinding(...args) {
157
+ (0, useFieldBinding_1.useFieldBinding)(this.ref, ...args);
158
+ }
159
+ changed() {
160
+ return this.state.get() != this.initialValue;
161
+ }
162
+ // If we make this async, the js engine will not delay further execution while the funcion is not done. This way we wait until we know wheter there's a promise or not
163
+ loadState() {
164
+ var _a, _b, _c;
165
+ let defaultValuePromise = undefined;
166
+ let defaultValue = undefined;
167
+ if (types_1.TypeGuards.isFunction(this.options.defaultValue)) {
168
+ const v = this.options.defaultValue();
169
+ if (types_1.TypeGuards.isPromise(v)) {
170
+ defaultValuePromise = v;
171
+ }
172
+ else {
173
+ defaultValue = v;
174
+ }
175
+ }
176
+ else {
177
+ const v = (_a = this.options) === null || _a === void 0 ? void 0 : _a.defaultValue;
178
+ if (types_1.TypeGuards.isPromise(v)) {
179
+ defaultValuePromise = v;
180
+ }
181
+ else {
182
+ defaultValue = v;
183
+ }
184
+ }
185
+ this.state = (_c = (_b = this === null || this === void 0 ? void 0 : this.options) === null || _b === void 0 ? void 0 : _b.state) !== null && _c !== void 0 ? _c : (0, nanostores_1.atom)(defaultValue);
186
+ if (!defaultValuePromise) {
187
+ this.initialValue = defaultValue;
188
+ }
189
+ if (!!defaultValuePromise) {
190
+ this.log('debug', 'Waiting for initial value');
191
+ return defaultValuePromise
192
+ .then((v) => {
193
+ this.state.set(v);
194
+ this.initialValue = v;
195
+ this.log('debug', `Got initial value`, v);
196
+ })
197
+ .catch(e => {
198
+ this.log('error', `Failed to resolve default value`, e);
199
+ });
200
+ }
201
+ else {
202
+ return Promise.resolve();
203
+ }
204
+ }
205
+ formatLog(...args) {
206
+ const [firstArgument, ...otherArgs] = args;
207
+ let PREFIX = `(FIELD: ${this.name})`;
208
+ if (types_1.TypeGuards.isString(firstArgument)) {
209
+ PREFIX += ` ${firstArgument}`;
210
+ }
211
+ else {
212
+ otherArgs.unshift(firstArgument);
213
+ }
214
+ const logArgs = [
215
+ PREFIX,
216
+ ...otherArgs
217
+ ];
218
+ return logArgs;
219
+ }
220
+ /**
221
+ * Runs the field's validator synchronously against `value` (or the current
222
+ * atom value when omitted). A {@link ValidationError} thrown inside the
223
+ * validator is caught and converted to `{ isValid: false, error: e.data }`;
224
+ * any other exception is re-thrown.
225
+ *
226
+ * This method is called on every render inside `useValidation` — keep
227
+ * validators fast and free of side effects.
228
+ */
229
+ validate(value) {
230
+ const validate = this.options.validate;
231
+ const valueToCheck = types_1.TypeGuards.isUndefined(value) ? this.state.get() : this.toInternalValue(value);
232
+ try {
233
+ const result = validate(valueToCheck, {});
234
+ return result;
235
+ }
236
+ catch (e) {
237
+ if (e instanceof ValidationError) {
238
+ return {
239
+ isValid: false,
240
+ error: e.data
241
+ };
242
+ }
243
+ throw e;
244
+ }
245
+ }
246
+ /**
247
+ * React hook that returns reactive validation state for the field's current
248
+ * value. Error display policy:
249
+ * - If the field started with an `undefined` value (`startedUnset`), the
250
+ * error is hidden until the user has blurred the input **or** until
251
+ * `revealError()` has been called.
252
+ * - If the field started with a defined value, the error is shown
253
+ * immediately whenever the field is invalid.
254
+ *
255
+ * `message` is resolved from `readableError` first, falling back to the
256
+ * first element's `.message` when `error` is an array.
257
+ */
258
+ useValidation() {
259
+ var _a, _b;
260
+ const value = this.useValue();
261
+ const revealed = (0, react_1.useStore)(this.errorRevealed);
262
+ const isUnset = typeof value === 'undefined';
263
+ const startedUnset = (0, react_2.useRef)(isUnset).current;
264
+ const isSet = !isUnset;
265
+ const validation = this.validate(value);
266
+ const isValid = validation.isValid;
267
+ const isInvalid = !isValid;
268
+ const hasChanged = this.initialValue != value;
269
+ const message = (_a = validation.readableError) !== null && _a !== void 0 ? _a : (Array.isArray(validation.error) ? (_b = validation.error[0]) === null || _b === void 0 ? void 0 : _b.message : undefined);
270
+ const errorDisplayRequiresBlur = startedUnset;
271
+ const [hasBlurred, setHasBlurred] = (0, react_2.useState)(false);
272
+ const showError = isInvalid && (errorDisplayRequiresBlur ? revealed || hasBlurred : true);
273
+ return {
274
+ onInputBlurred() {
275
+ setHasBlurred(true);
276
+ },
277
+ hasBlurred,
278
+ hasChanged,
279
+ startedUnset,
280
+ isSet,
281
+ isInvalid,
282
+ isValid,
283
+ message,
284
+ showError,
285
+ isUnset,
286
+ validation,
287
+ value
288
+ };
289
+ }
290
+ log(level = 'log', ...args) {
291
+ if (Field.enableLogs)
292
+ logger_1.logger[level](...this.formatLog(...args));
293
+ }
294
+ toInternalValue(v) {
295
+ return v;
296
+ }
297
+ toRepresentation(v) {
298
+ return v;
299
+ }
300
+ /**
301
+ * Registers a global prop transformer under `key`. Transformers are applied
302
+ * in insertion order by `props()`. Re-registering an existing key replaces
303
+ * the previous function.
304
+ */
305
+ static attachTransformer(key, fn) {
306
+ Field.transformers.set(key, fn);
307
+ }
308
+ /**
309
+ * Removes the transformer registered under `key`. Idempotent — no-op if the
310
+ * key is not present.
311
+ */
312
+ static detachTransformer(key) {
313
+ Field.transformers.delete(key);
314
+ }
315
+ /**
316
+ * Returns the field's properties after running them through every registered
317
+ * global transformer. The raw `properties` object is built from `options`
318
+ * with internal keys (`name`, `defaultValue`, `state`, `validate`, `loader`,
319
+ * `onValueChange`) stripped out, plus a `field` reference to `this`.
320
+ * Use this when passing field metadata to a component that does not use the
321
+ * `use()` hook directly.
322
+ */
323
+ props() {
324
+ return Array.from(Field.transformers.values()).reduce((acc, transformer) => transformer(acc), this.properties);
325
+ }
326
+ measurePosition(wrapperRef) {
327
+ return Field.methodMeasurePosition(this, wrapperRef);
328
+ }
329
+ scrollTo(scrollRef, measure) {
330
+ return Field.methodScrollTo(this, scrollRef, measure);
331
+ }
332
+ getPadding() {
333
+ return Field.methodGetPadding(this);
334
+ }
335
+ }
336
+ exports.Field = Field;
337
+ Field.transformers = new Map();
338
+ Field.enableLogs = false;
339
+ Field.getProps = (field) => {
340
+ throw new Error('Field.getProps not implemented');
341
+ };
342
+ Field.methodMeasurePosition = (field, wrapperRef) => {
343
+ throw new Error('Field.measurePosition not implemented');
344
+ };
345
+ Field.methodScrollTo = (field, scrollRef, measure) => {
346
+ throw new Error('Field.scrollTo not implemented');
347
+ };
348
+ Field.methodGetPadding = (field) => {
349
+ throw new Error('Field.getPadding not implemented');
350
+ };
351
+ //# sourceMappingURL=Field.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Field.js","sourceRoot":"","sources":["../../src/lib/Field.ts"],"names":[],"mappings":";;;AAGA,2C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G,KAAK,AAAR,CAAQ;AAElB,cAAQ,GAAG,CAAC,KAAqB,EAAE,EAAE;IAC1C,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;AACnD,CAAC,AAFc,CAEd;AAEM,2BAAqB,GAAG,CAAC,KAAqB,EAAE,UAAe,EAA+B,EAAE;IACrG,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAA;AAC1D,CAAC,AAF2B,CAE3B;AAEM,oBAAc,GAAG,CAAC,KAAqB,EAAE,SAAc,EAAE,OAA2B,EAAiB,EAAE;IAC5G,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;AACnD,CAAC,AAFoB,CAEpB;AAEM,sBAAgB,GAAG,CAAC,KAAqB,EAAU,EAAE;IAC1D,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAA;AACrD,CAAC,AAFsB,CAEtB"}
@@ -34,7 +34,7 @@ declare class Form<T extends FormDef> {
34
34
  get isChanged(): boolean;
35
35
  changed(): boolean;
36
36
  get isValid(): boolean;
37
- slice<K extends FieldPaths<T>>(field: K): import("nanostores").WritableAtom<unknown>;
37
+ slice<K extends FieldPaths<T>>(field: K): import("nanostores", { with: { "resolution-mode": "import" } }).WritableAtom<unknown>;
38
38
  iterFields<V>(cb: (field: FieldTuples<T>, index: number) => V): V[];
39
39
  /**
40
40
  * Bulk-sets field values from a partial record. Boolean fields require an
@@ -62,7 +62,7 @@ declare class Form<T extends FormDef> {
62
62
  * the field key does not exist in this form's shape, making typos a runtime
63
63
  * error rather than a silent no-op.
64
64
  */
65
- register(field: FieldPaths<T>): import("..").IFieldProps;
65
+ register(field: FieldPaths<T>): import("../types").IFieldProps;
66
66
  use<Selected>(selector: FormSelector<T, Selected>): Selected;
67
67
  useReset(): void;
68
68
  useShared<Selected>(selector: FormSelector<T, Selected>): Selected;
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useForm = useForm;
4
+ exports.form = form;
5
+ const store_1 = require("@codeleap/store");
6
+ const types_1 = require("@codeleap/types");
7
+ const react_1 = require("react");
8
+ const hooks_1 = require("@codeleap/hooks");
9
+ function buildState(def) {
10
+ const stateArg = {};
11
+ for (const [name, field] of Object.entries(def)) {
12
+ stateArg[name] = field.value;
13
+ }
14
+ return stateArg;
15
+ }
16
+ /**
17
+ * Container that owns the shared nanostores `GlobalState` atom for a group of
18
+ * fields and wires each field's individual atom to a slice of that shared
19
+ * state. This ensures a single source of truth: reading `form.values` always
20
+ * reflects the live atom values of every field.
21
+ *
22
+ * **Initialization order**
23
+ * The constructor calls `attachState()` synchronously, which replaces each
24
+ * field's own atom with a derived slice and sets the field's `name` from its
25
+ * object key in the shape. Fields must therefore be fully constructed before
26
+ * being passed to `Form`.
27
+ *
28
+ * **React usage**
29
+ * - `use(selector)` — mounts the form in a component, subscribes to state
30
+ * changes via `selector`, and resets all field values on unmount. Prefer
31
+ * this for full-page forms.
32
+ * - `useShared(selector)` — subscribes without the unmount-reset side-effect.
33
+ * Use when the form outlives the component (e.g. a global singleton).
34
+ *
35
+ * **Validation**
36
+ * `validate()` runs every field's synchronous validator and returns a map of
37
+ * results keyed by field name. Pass `revealErrors: true` to simultaneously
38
+ * flip `errorRevealed` on every field, triggering error display in the UI.
39
+ */
40
+ class Form {
41
+ constructor(id, shape) {
42
+ this.id = id;
43
+ this.fields = shape;
44
+ this.state = (0, store_1.globalState)(buildState(this.fields));
45
+ this.attachState();
46
+ this.use = this.use.bind(this);
47
+ this.useShared = this.useShared.bind(this);
48
+ this.useReset = this.useReset.bind(this);
49
+ }
50
+ get values() {
51
+ return this.state.get();
52
+ }
53
+ get isChanged() {
54
+ return this.changed();
55
+ }
56
+ changed() {
57
+ for (const [fieldName, field] of Object.entries(this.fields)) {
58
+ if (field.changed())
59
+ return true;
60
+ }
61
+ return false;
62
+ }
63
+ get isValid() {
64
+ const res = this.validate();
65
+ return Object.values(res).every((result) => result.isValid);
66
+ }
67
+ slice(field) {
68
+ const fieldSlice = (0, store_1.createStateSlice)(this.state, (v) => v[field], (value) => {
69
+ return {
70
+ [field]: value
71
+ };
72
+ });
73
+ return fieldSlice;
74
+ }
75
+ iterFields(cb) {
76
+ const results = [];
77
+ let index = 0;
78
+ for (const [name, field] of Object.entries(this.fields)) {
79
+ const result = cb([name, field], index);
80
+ results.push(result);
81
+ index++;
82
+ }
83
+ return results;
84
+ }
85
+ /**
86
+ * Bulk-sets field values from a partial record. Boolean fields require an
87
+ * explicit `boolean` value; all other fields skip `undefined` and falsy
88
+ * values. Use `resetValues()` to restore all fields to their initial values.
89
+ */
90
+ setValues(values) {
91
+ this.iterFields(([name, field]) => {
92
+ const value = values === null || values === void 0 ? void 0 : values[name];
93
+ if (field._type === 'BOOLEAN' && types_1.TypeGuards.isBoolean(value))
94
+ field.setValue(value);
95
+ else if (value)
96
+ field.setValue(value);
97
+ });
98
+ }
99
+ resetValues() {
100
+ this.iterFields(([name, field]) => {
101
+ field.resetValue();
102
+ });
103
+ }
104
+ attachState() {
105
+ this.iterFields(([name, field]) => {
106
+ field.options.name = name;
107
+ field.attach(this.slice(name));
108
+ });
109
+ }
110
+ /**
111
+ * Returns the first field (in definition order) that fails validation, along
112
+ * with its `ValidationResult`. Returns `undefined` when all fields are
113
+ * valid. Useful for scrolling to the first error on submit.
114
+ */
115
+ firstInvalid() {
116
+ for (const [fieldName, field] of Object.entries(this.fields)) {
117
+ const validation = field.validate();
118
+ if (!validation.isValid)
119
+ return {
120
+ field,
121
+ validation,
122
+ };
123
+ }
124
+ }
125
+ validate(options) {
126
+ const { fields, revealErrors } = options !== null && options !== void 0 ? options : {};
127
+ const validateFields = fields !== null && fields !== void 0 ? fields : Object.keys(this.fields);
128
+ const results = this.iterFields(([name, field]) => {
129
+ if (!validateFields.includes(name))
130
+ return null;
131
+ return [name, field.validate()];
132
+ });
133
+ const resultMap = Object.fromEntries(results.filter((v) => !types_1.TypeGuards.isNil(v)));
134
+ if (revealErrors) {
135
+ this.iterFields(([_, field]) => {
136
+ field.revealError();
137
+ });
138
+ }
139
+ return resultMap;
140
+ }
141
+ /**
142
+ * Returns the transformed props for `field` via `Field.props()`. Throws if
143
+ * the field key does not exist in this form's shape, making typos a runtime
144
+ * error rather than a silent no-op.
145
+ */
146
+ register(field) {
147
+ if (!this.fields[field]) {
148
+ throw new Error(`Field "${field}" not found in "${this.id}" form`);
149
+ }
150
+ return this.fields[field].props();
151
+ }
152
+ use(selector) {
153
+ const value = this.useShared(selector);
154
+ this.useReset();
155
+ return value;
156
+ }
157
+ useReset() {
158
+ (0, hooks_1.useUnmount)(() => {
159
+ this.resetValues();
160
+ });
161
+ }
162
+ useShared(selector) {
163
+ const [selected, setSelected] = (0, react_1.useState)(() => selector(this));
164
+ const reselect = (0, react_1.useCallback)(() => {
165
+ setSelected(selector(this));
166
+ }, [selector]);
167
+ (0, react_1.useEffect)(() => {
168
+ return this.state.listen((value, previous) => {
169
+ if (value != previous) {
170
+ reselect();
171
+ }
172
+ });
173
+ }, [reselect]);
174
+ return selected;
175
+ }
176
+ }
177
+ /**
178
+ * Creates a `Form` instance that is stable for the lifetime of the component
179
+ * (memoised on `name`). Use this when the form is local to a single mounted
180
+ * component. For forms that must survive unmounts (e.g. multi-step flows),
181
+ * construct the `Form` outside React via `form()`.
182
+ *
183
+ * Note: `def` is only read on the first render — changing it after mount has
184
+ * no effect.
185
+ */
186
+ function useForm(name, def) {
187
+ const form = (0, react_1.useMemo)(() => {
188
+ return new Form(name, def);
189
+ }, [name]);
190
+ return form;
191
+ }
192
+ /**
193
+ * Constructs a `Form` outside of React — suitable for module-level singletons,
194
+ * server-side construction, or multi-step flows where the form must outlive
195
+ * any individual component. Values are **not** automatically reset on unmount;
196
+ * call `resetValues()` explicitly when needed.
197
+ */
198
+ function form(...args) {
199
+ return new Form(...args);
200
+ }
201
+ //# sourceMappingURL=Form.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Form.js","sourceRoot":"","sources":["../../src/lib/Form.ts"],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CAAW,QAAmC;QACrD,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,IAAA,gBAAQ,EAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;QAE9D,MAAM,QAAQ,GAAG,IAAA,mBAAW,EAAC,GAAG,EAAE;YAChC,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;QAC7B,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAA;QAEd,IAAA,iBAAS,EAAC,GAAG,EAAE;YACb,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE;gBAC3C,IAAG,KAAK,IAAI,QAAQ,EAAC,CAAC;oBACpB,QAAQ,EAAE,CAAA;gBACZ,CAAC;YACH,CAAC,CAAC,CAAA;QACJ,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAA;QAEd,OAAO,QAAQ,CAAA;IACjB,CAAC;CACF;AAGD;;;;;;;;GAQG;AACH,SAAgB,OAAO,CAAoB,IAAY,EAAE,GAAM;IAC7D,MAAM,IAAI,GAAG,IAAA,eAAO,EAAC,GAAG,EAAE;QACxB,OAAO,IAAI,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IAC5B,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAA;IAGV,OAAO,IAAI,CAAA;AACb,CAAC;AAED;;;;;GAKG;AACH,SAAgB,IAAI,CAAsB,GAAG,IAA6C;IACxF,OAAO,IAAI,IAAI,CAAC,GAAG,IAAI,CAAC,CAAA;AAC1B,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fieldFactory = fieldFactory;
4
+ /**
5
+ * Wraps a `Field` subclass constructor in a factory function, eliminating the
6
+ * need to call `new` at the call site and improving inference of the
7
+ * `validate` option's generic type. The returned factory accepts the same
8
+ * options as the class constructor and produces a fully typed `Field` instance.
9
+ *
10
+ * All entries in the `fields` namespace are built with this helper.
11
+ */
12
+ function fieldFactory(cls) {
13
+ return (options) => {
14
+ // @ts-expect-error
15
+ return new cls(options !== null && options !== void 0 ? options : {});
16
+ };
17
+ }
18
+ //# sourceMappingURL=factories.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factories.js","sourceRoot":"","sources":["../../src/lib/factories.tsx"],"names":[],"mappings":";;AAcA,oCAUC;AAlBD;;;;;;;GAOG;AACH,SAAgB,YAAY,CAI1B,GAAM;IACN,OAAO,CAAwC,OAAW,EAAgC,EAAE;QAE1F,mBAAmB;QACnB,OAAO,IAAI,GAAG,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC,CAAA;IAC/B,CAAC,CAAA;AACH,CAAC"}
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.Field = void 0;
18
+ __exportStar(require("./Form"), exports);
19
+ var Field_1 = require("./Field");
20
+ Object.defineProperty(exports, "Field", { enumerable: true, get: function () { return Field_1.Field; } });
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,yCAAsB;AACtB,iCAA6B;AAArB,8FAAA,KAAK,OAAA"}