@fluixi/forms-core 0.2.1-alpha.0 → 0.2.1-alpha.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/dist/array.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import type { ArrayControl, ArrayOptions, Control } from './types.js';
2
2
  /** Stable key for a control (for keyed list rendering). */
3
3
  export declare const keyOf: (control: object) => number;
4
- export declare function createFormArray<TItem, TCtrl extends Control<TItem> = Control<TItem>>(initial?: TCtrl[], options?: ArrayOptions<TItem>): ArrayControl<TItem, TCtrl>;
4
+ export declare function createFormArray<TItem, TCtrl extends Control<TItem> = Control<TItem>>(initial?: TCtrl[], options?: ArrayOptions<TItem, TCtrl>): ArrayControl<TItem, TCtrl>;
package/dist/array.js CHANGED
@@ -7,6 +7,7 @@
7
7
  * exposed via `keyOf` for keyed rendering. Aggregate state is derived with memos.
8
8
  */
9
9
  import { createSignal, createMemo, untrack, batch } from '@fluixi/reactive/signal';
10
+ import { createField } from './field.js';
10
11
  let _key = 0;
11
12
  const keys = new WeakMap();
12
13
  /** Stable key for a control (for keyed list rendering). */
@@ -18,6 +19,9 @@ export const keyOf = (control) => {
18
19
  };
19
20
  export function createFormArray(initial = [], options = {}) {
20
21
  const [list, setList] = createSignal([...initial]);
22
+ // Wrap a plain value into a child control for pushValue/insertValue. Defaults to
23
+ // a field; a `control` factory builds groups/nested arrays for structured rows.
24
+ const makeControl = options.control ?? ((v) => createField(v));
21
25
  const [disabled, setDisabledSig] = createSignal(!!options.disabled);
22
26
  const [submitCount] = createSignal(0);
23
27
  const syncValidators = options.validators ?? [];
@@ -84,6 +88,8 @@ export function createFormArray(initial = [], options = {}) {
84
88
  wire(c);
85
89
  mutate((arr) => (arr.splice(i, 0, c), arr));
86
90
  },
91
+ pushValue: (v) => self.push(makeControl(v)),
92
+ insertValue: (i, v) => self.insert(i, makeControl(v)),
87
93
  removeAt: (i) => mutate((arr) => (arr.splice(i, 1), arr)),
88
94
  move: (from, to) => mutate((arr) => {
89
95
  const [c] = arr.splice(from, 1);
package/dist/index.d.ts CHANGED
@@ -11,4 +11,5 @@ export { createField } from './field.js';
11
11
  export { createGroup, createForm } from './group.js';
12
12
  export { createFormArray, keyOf } from './array.js';
13
13
  export * from './validators.js';
14
+ export * from './standard.js';
14
15
  export type { Accessor, ValidationError, ValidatorContext, Validator, AsyncValidator, UpdateOptions, ControlKind, Control, AnyControl, FieldOptions, FieldControl, ControlsRecord, GroupValue, GroupOptions, GroupControl, ArrayOptions, ArrayControl, FormControl, PartialDeep, Path, } from './types.js';
package/dist/index.js CHANGED
@@ -11,3 +11,4 @@ export { createField } from './field.js';
11
11
  export { createGroup, createForm } from './group.js';
12
12
  export { createFormArray, keyOf } from './array.js';
13
13
  export * from './validators.js';
14
+ export * from './standard.js';
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Standard Schema (https://standardschema.dev): one adapter for every library that implements
3
+ * it, Zod 4, Valibot, ArkType among them. Only the interface is declared here, so nothing is
4
+ * installed for it.
5
+ */
6
+ import type { AsyncValidator, ValidationError, Validator } from './types.js';
7
+ /** A path segment: a key, or an object carrying one. */
8
+ export type StandardPathSegment = PropertyKey | {
9
+ readonly key: PropertyKey;
10
+ };
11
+ export interface StandardIssue {
12
+ readonly message: string;
13
+ readonly path?: ReadonlyArray<StandardPathSegment>;
14
+ }
15
+ export type StandardResult<Output> = {
16
+ readonly value: Output;
17
+ readonly issues?: undefined;
18
+ } | {
19
+ readonly issues: ReadonlyArray<StandardIssue>;
20
+ };
21
+ /** The Standard Schema v1 interface, as far as validation needs it. */
22
+ export interface StandardSchema<Input = unknown, Output = Input> {
23
+ readonly '~standard': {
24
+ readonly version: 1;
25
+ readonly vendor: string;
26
+ readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
27
+ readonly types?: {
28
+ readonly input: Input;
29
+ readonly output: Output;
30
+ };
31
+ };
32
+ }
33
+ /** The output type a Standard Schema validates to. */
34
+ export type InferStandard<S> = S extends StandardSchema<unknown, infer O> ? O : never;
35
+ /** `a.0.b`. Through String(): a symbol key cannot be joined directly. */
36
+ export declare function standardPath(path?: ReadonlyArray<StandardPathSegment>): string;
37
+ /**
38
+ * A validator from a Standard Schema, for a field or a whole form. Its first issue becomes the
39
+ * error; `meta.path` says where it belongs. A schema that validates asynchronously belongs in
40
+ * `asyncValidators` through `standardAsyncValidator`: here it is reported as an error rather
41
+ * than passing unchecked.
42
+ */
43
+ export declare function standardValidator<T>(schema: StandardSchema<unknown, T>): Validator<T>;
44
+ /** The asynchronous form, for `asyncValidators`: debounced and aborted like any other. */
45
+ export declare function standardAsyncValidator<T>(schema: StandardSchema<unknown, T>): AsyncValidator<T>;
46
+ /** Every issue of a failed validation, grouped by dotted path, for routing to fields. */
47
+ export declare function standardErrorsByPath(schema: StandardSchema, value: unknown): Promise<Record<string, ValidationError[]>>;
@@ -0,0 +1,40 @@
1
+ /** `a.0.b`. Through String(): a symbol key cannot be joined directly. */
2
+ export function standardPath(path = []) {
3
+ return path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.');
4
+ }
5
+ const toError = (issue) => ({
6
+ type: 'schema',
7
+ message: issue.message,
8
+ meta: { path: standardPath(issue.path) },
9
+ });
10
+ const firstError = (result) => result.issues?.length ? toError(result.issues[0]) : null;
11
+ function isPromise(value) {
12
+ return typeof value?.then === 'function';
13
+ }
14
+ /**
15
+ * A validator from a Standard Schema, for a field or a whole form. Its first issue becomes the
16
+ * error; `meta.path` says where it belongs. A schema that validates asynchronously belongs in
17
+ * `asyncValidators` through `standardAsyncValidator`: here it is reported as an error rather
18
+ * than passing unchecked.
19
+ */
20
+ export function standardValidator(schema) {
21
+ return (value) => {
22
+ const result = schema['~standard'].validate(value);
23
+ if (isPromise(result)) {
24
+ return { type: 'schema', message: 'This schema validates asynchronously: use standardAsyncValidator.' };
25
+ }
26
+ return firstError(result);
27
+ };
28
+ }
29
+ /** The asynchronous form, for `asyncValidators`: debounced and aborted like any other. */
30
+ export function standardAsyncValidator(schema) {
31
+ return async (value) => firstError(await schema['~standard'].validate(value));
32
+ }
33
+ /** Every issue of a failed validation, grouped by dotted path, for routing to fields. */
34
+ export async function standardErrorsByPath(schema, value) {
35
+ const result = await schema['~standard'].validate(value);
36
+ const out = {};
37
+ for (const issue of result.issues ?? [])
38
+ (out[standardPath(issue.path)] ??= []).push(toError(issue));
39
+ return out;
40
+ }
package/dist/types.d.ts CHANGED
@@ -115,10 +115,16 @@ export interface GroupControl<C extends ControlsRecord> extends Control<GroupVal
115
115
  /** Remove a control at runtime. */
116
116
  removeControl(key: keyof C): void;
117
117
  }
118
- export interface ArrayOptions<T> {
118
+ export interface ArrayOptions<T, TCtrl extends Control<T> = Control<T>> {
119
119
  validators?: Validator<T[]>[];
120
120
  asyncValidators?: AsyncValidator<T[]>[];
121
121
  disabled?: boolean;
122
+ /**
123
+ * How to wrap a plain value into a child control for `pushValue`/`insertValue`.
124
+ * Defaults to `createField(value)`; supply this to build groups/nested arrays
125
+ * for structured rows.
126
+ */
127
+ control?: (value: T) => TCtrl;
122
128
  }
123
129
  export interface ArrayControl<TItem, TCtrl extends Control<TItem> = Control<TItem>> extends Control<TItem[]> {
124
130
  readonly kind: 'array';
@@ -128,6 +134,10 @@ export interface ArrayControl<TItem, TCtrl extends Control<TItem> = Control<TIte
128
134
  at(index: number): TCtrl | undefined;
129
135
  push(control: TCtrl): void;
130
136
  insert(index: number, control: TCtrl): void;
137
+ /** Wrap a plain value into a child control (see {@link ArrayOptions.control}) and append it. */
138
+ pushValue(value: TItem): void;
139
+ /** Wrap a plain value into a child control and insert it at `index`. */
140
+ insertValue(index: number, value: TItem): void;
131
141
  removeAt(index: number): void;
132
142
  move(from: number, to: number): void;
133
143
  clear(): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluixi/forms-core",
3
- "version": "0.2.1-alpha.0",
3
+ "version": "0.2.1-alpha.2",
4
4
  "license": "MIT",
5
5
  "description": "Reactive forms core (FormControl/FormGroup/FormArray) for fine-grained signal frameworks.",
6
6
  "type": "module",