@punica/form-mcp 0.4.0-next.0 → 0.4.0-next.1
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/bundled/form/controller/index.d.ts +6 -0
- package/bundled/form/define/Form.d.ts +2 -2
- package/bundled/form/model/form/index.d.ts +11 -2
- package/bundled/form/model/item/index.d.ts +7 -0
- package/bundled/form/package.json +1 -1
- package/bundled/form/utils/deepCopy.d.ts +2 -1
- package/bundled/form-kit/decorators.d.ts +36 -1
- package/bundled/form-kit/json/index.d.ts +1 -1
- package/bundled/form-kit/package.json +1 -1
- package/bundled/form-kit/react/FieldList.d.ts +33 -0
- package/bundled/form-kit/react/Rows.d.ts +9 -0
- package/bundled/form-kit/react/fields.d.ts +5 -11
- package/bundled/form-kit/react/index.d.ts +2 -3
- package/bundled/form-kit/types.d.ts +45 -3
- package/bundled/form-kit/validation.d.ts +4 -0
- package/bundled/form-react/index.d.ts +1 -0
- package/bundled/form-react/package.json +1 -1
- package/bundled/form-react/useFieldArray.d.ts +34 -0
- package/bundled/form-schema/decorator.d.ts +26 -0
- package/bundled/form-schema/index.d.ts +11 -0
- package/bundled/form-schema/issues.d.ts +22 -0
- package/bundled/form-schema/package.json +1 -0
- package/bundled/form-schema/schema.d.ts +17 -0
- package/bundled/form-schema/service.d.ts +27 -0
- package/bundled/form-schema/standard.d.ts +30 -0
- package/bundled/form-service/fieldArray/index.d.ts +47 -0
- package/bundled/form-service/formValidation.d.ts +36 -0
- package/bundled/form-service/index.d.ts +7 -0
- package/bundled/form-service/package.json +1 -1
- package/bundled/form-service/rows.d.ts +67 -0
- package/bundled/form-service/setValue/index.d.ts +1 -0
- package/bundled/form-service/validate/index.d.ts +2 -1
- package/bundled/form-service/valuesOf.d.ts +3 -0
- package/package.json +6 -5
- package/src/create.mjs +2 -2
- package/src/guide.md +83 -6
- package/src/main.mjs +2 -1
|
@@ -4,6 +4,12 @@ export type FormControllerOptions = {
|
|
|
4
4
|
name?: string;
|
|
5
5
|
/** The register whose form item types the form reports. */
|
|
6
6
|
register?: FormItemRegister;
|
|
7
|
+
/**
|
|
8
|
+
* The services of a form that lists none, with `@Services` or in the form
|
|
9
|
+
* given to `fromForm`. The rows of a list get the services of the form
|
|
10
|
+
* they belong to this way.
|
|
11
|
+
*/
|
|
12
|
+
services?: Array<IService | (new () => IService)>;
|
|
7
13
|
};
|
|
8
14
|
/** A class whose instances are services. */
|
|
9
15
|
type ServiceClass<S extends IService> = new (...args: any[]) => S;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FormPropertyDecorator } from './decorator';
|
|
2
2
|
/** A property of a form defined with `defineForm`. */
|
|
3
3
|
export type FormField<T> = {
|
|
4
4
|
/** The value a new form starts with. */
|
|
@@ -38,4 +38,4 @@ export declare const field: <T>(value: T, ...decorators: FormPropertyDecorator[]
|
|
|
38
38
|
* @returns A class whose constructor takes values that replace the initial
|
|
39
39
|
* ones; a value that is `undefined` leaves the initial one in place
|
|
40
40
|
*/
|
|
41
|
-
export declare const defineForm: <S extends Record<string, FormField<unknown>>>(fields: S, ...decorators:
|
|
41
|
+
export declare const defineForm: <S extends Record<string, FormField<unknown>>>(fields: S, ...decorators: Array<(target: new (values?: Partial<FormValues<S>>) => FormValues<S>, context?: ClassDecoratorContext) => void>) => new (values?: Partial<FormValues<S>>) => FormValues<S>;
|
|
@@ -7,7 +7,10 @@ export type FormState = {
|
|
|
7
7
|
dirty: boolean;
|
|
8
8
|
/** Whether the user has left a field. */
|
|
9
9
|
touched: boolean;
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Whether no item is in error, and the form as a whole is valid when it
|
|
12
|
+
* has validations of its own.
|
|
13
|
+
*/
|
|
11
14
|
valid: boolean;
|
|
12
15
|
/** Whether an item is being validated. */
|
|
13
16
|
validating: boolean;
|
|
@@ -19,7 +22,13 @@ export type FormState = {
|
|
|
19
22
|
errors: string[];
|
|
20
23
|
};
|
|
21
24
|
/** The part of the state services write; the rest follows from the items. */
|
|
22
|
-
export type FormStatePatch = Partial<Pick<FormState, 'submitting' | 'submitCount' | 'errors'
|
|
25
|
+
export type FormStatePatch = Partial<Pick<FormState, 'submitting' | 'submitCount' | 'errors'>> & {
|
|
26
|
+
/**
|
|
27
|
+
* Whether the form as a whole is not valid, whatever its items say: a
|
|
28
|
+
* validation of the form found something. It stays until it is set again.
|
|
29
|
+
*/
|
|
30
|
+
invalid?: boolean;
|
|
31
|
+
};
|
|
23
32
|
/**
|
|
24
33
|
* The state of a form. Readers and starters may attach their own properties
|
|
25
34
|
* (a title, a layout, …), which are read back as `unknown`.
|
|
@@ -34,6 +34,13 @@ export type FormItem<E> = {
|
|
|
34
34
|
validateDebounce?: number;
|
|
35
35
|
/** Whether the last validation of the item failed. */
|
|
36
36
|
error?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Makes the item a list of forms, such as the lines of an invoice: returns
|
|
39
|
+
* the entity of a new row, an instance of a form class. The value of the
|
|
40
|
+
* item is then the array of the values of its rows. The rows are created
|
|
41
|
+
* and changed by the `FieldArray` service, which the form has to list.
|
|
42
|
+
*/
|
|
43
|
+
row?(): object;
|
|
37
44
|
/** The messages of the last failed validation. */
|
|
38
45
|
errorMessages?: string[];
|
|
39
46
|
/** Runs once while the form is created; the returned items are written to the form. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"2.2.0-next.
|
|
1
|
+
{"version":"2.2.0-next.1"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates a deep copy of the given value.
|
|
3
3
|
* Functions are kept by reference, prototypes are preserved and
|
|
4
|
-
* circular references are supported.
|
|
4
|
+
* circular references are supported. Files (`File`, `Blob`) are kept by
|
|
5
|
+
* reference.
|
|
5
6
|
* @param value - The value to copy
|
|
6
7
|
* @returns The copied value
|
|
7
8
|
*/
|
|
@@ -9,7 +9,7 @@ export type FormKitOptions = {
|
|
|
9
9
|
columns?: 1 | 2 | 3 | 4;
|
|
10
10
|
/**
|
|
11
11
|
* Services in addition to GetValues, SetValue, Validate, Trigger, Reset,
|
|
12
|
-
* Touch and
|
|
12
|
+
* Touch, Submit and FieldArray.
|
|
13
13
|
*/
|
|
14
14
|
services?: Array<IService | (new () => IService)>;
|
|
15
15
|
/** Readers in addition to the kit's. */
|
|
@@ -30,6 +30,12 @@ export declare const KIT_TYPES: {
|
|
|
30
30
|
readonly select: "kit:select";
|
|
31
31
|
readonly checkbox: "kit:checkbox";
|
|
32
32
|
readonly switch: "kit:switch";
|
|
33
|
+
readonly radio: "kit:radio";
|
|
34
|
+
readonly date: "kit:date";
|
|
35
|
+
readonly multiselect: "kit:multiselect";
|
|
36
|
+
readonly password: "kit:password";
|
|
37
|
+
readonly file: "kit:file";
|
|
38
|
+
readonly array: "kit:array";
|
|
33
39
|
};
|
|
34
40
|
/** A single-line text field. */
|
|
35
41
|
export declare const TextField: (props: FieldProps) => FormPropertyDecorator;
|
|
@@ -43,3 +49,32 @@ export declare const SelectField: (props: FieldProps) => FormPropertyDecorator;
|
|
|
43
49
|
export declare const CheckboxField: (props: FieldProps) => FormPropertyDecorator;
|
|
44
50
|
/** A switch; its value is a boolean. */
|
|
45
51
|
export declare const SwitchField: (props: FieldProps) => FormPropertyDecorator;
|
|
52
|
+
/** A set of radio buttons for `options`, of which one is chosen. */
|
|
53
|
+
export declare const RadioGroupField: (props: FieldProps) => FormPropertyDecorator;
|
|
54
|
+
/**
|
|
55
|
+
* A date field; its value is the date as `yyyy-mm-dd`, or `undefined` while
|
|
56
|
+
* it is empty. `minDate` and `maxDate` limit it.
|
|
57
|
+
*/
|
|
58
|
+
export declare const DateField: (props: FieldProps) => FormPropertyDecorator;
|
|
59
|
+
/** A select that takes several of its `options`; its value is an array. */
|
|
60
|
+
export declare const MultiSelectField: (props: FieldProps) => FormPropertyDecorator;
|
|
61
|
+
/** A text field that hides what is typed. */
|
|
62
|
+
export declare const PasswordField: (props: FieldProps) => FormPropertyDecorator;
|
|
63
|
+
/**
|
|
64
|
+
* A file field; its value is a `File`, or `undefined` while it is empty.
|
|
65
|
+
* With `multiple` it is an array of files. `accept` and `maxFileSize` limit
|
|
66
|
+
* what it takes.
|
|
67
|
+
*/
|
|
68
|
+
export declare const FileField: (props: FieldProps) => FormPropertyDecorator;
|
|
69
|
+
/**
|
|
70
|
+
* A list of rows, such as the lines of an invoice. Every row is a form of
|
|
71
|
+
* the class `row` returns, with the fields of that class:
|
|
72
|
+
*
|
|
73
|
+
* @ArrayField({ label: 'Lines', row: () => new LineForm(), minItems: 1 })
|
|
74
|
+
* lines: LineForm[] = [];
|
|
75
|
+
*
|
|
76
|
+
* The row class needs no `@FormKit`; with it, `columns` lays out its fields.
|
|
77
|
+
*/
|
|
78
|
+
export declare const ArrayField: (props: FieldProps & {
|
|
79
|
+
row: () => object;
|
|
80
|
+
}) => FormPropertyDecorator;
|
|
@@ -3,7 +3,7 @@ import { FromSchemaOptions, JsonFormValues } from '@punica/form-json';
|
|
|
3
3
|
import { type FormViewProps } from '../react';
|
|
4
4
|
/**
|
|
5
5
|
* The types a form definition may use with the kit, and the form item type
|
|
6
|
-
* of the kit each one stands for.
|
|
6
|
+
* of the kit each one stands for: every type of the kit but the list.
|
|
7
7
|
*/
|
|
8
8
|
export declare const KIT_JSON_TYPES: Record<string, string>;
|
|
9
9
|
export type JsonFormProps = Omit<FormViewProps<JsonFormValues>, 'controller'> & {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"0.3.0-next.
|
|
1
|
+
{"version":"0.3.0-next.1"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import '@punica/components/components/field/field.js';
|
|
3
|
+
import { FormController } from '@punica/form';
|
|
4
|
+
import { KitItem } from '../types';
|
|
5
|
+
/** The types whose component shows the label itself. */
|
|
6
|
+
export declare const SELF_LABELLED: string[];
|
|
7
|
+
/** What a component that renders a form item type receives. */
|
|
8
|
+
export type FieldComponentProps = {
|
|
9
|
+
item: KitItem<any>;
|
|
10
|
+
/** Sets the value of the item. */
|
|
11
|
+
onChange: (value: unknown) => void;
|
|
12
|
+
/** The controller of the form the item belongs to. */
|
|
13
|
+
controller: FormController<any, KitItem<any>>;
|
|
14
|
+
/** The components the form is rendered with, by form item type. */
|
|
15
|
+
components: Record<string, FieldComponent>;
|
|
16
|
+
/** Reports what went wrong, as the form does. */
|
|
17
|
+
onError: (error: unknown) => void;
|
|
18
|
+
};
|
|
19
|
+
export type FieldComponent = (props: FieldComponentProps) => ReactNode;
|
|
20
|
+
type FieldsProps<E extends object> = {
|
|
21
|
+
controller: FormController<E, KitItem<E>>;
|
|
22
|
+
items: Array<KitItem<E>>;
|
|
23
|
+
components: Record<string, FieldComponent>;
|
|
24
|
+
onError: (error: unknown) => void;
|
|
25
|
+
/** Whether every field is disabled, as the rows of a disabled list are. */
|
|
26
|
+
disabled?: boolean;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The fields of a form: one `punica-field` for every item that is not
|
|
30
|
+
* hidden, around the component of its type.
|
|
31
|
+
*/
|
|
32
|
+
export declare function Fields<E extends object>({ controller, items, components, onError, disabled }: FieldsProps<E>): ReactNode;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import '@punica/components/components/button/button.js';
|
|
2
|
+
import '@punica/components/components/formLayout/formLayout.js';
|
|
3
|
+
import { FieldComponent } from './FieldList';
|
|
4
|
+
/**
|
|
5
|
+
* The rows of a list, each with the fields of its form, a button that
|
|
6
|
+
* removes it and one that adds a row. The rows of a disabled list are
|
|
7
|
+
* disabled as well.
|
|
8
|
+
*/
|
|
9
|
+
export declare const Rows: FieldComponent;
|
|
@@ -1,19 +1,13 @@
|
|
|
1
|
-
import type { ReactNode } from 'react';
|
|
2
1
|
import '@punica/components/components/checkbox/checkbox.js';
|
|
2
|
+
import '@punica/components/components/datePicker/datePicker.js';
|
|
3
|
+
import '@punica/components/components/fileInput/fileInput.js';
|
|
3
4
|
import '@punica/components/components/input/input.js';
|
|
5
|
+
import '@punica/components/components/radio/radio.js';
|
|
6
|
+
import '@punica/components/components/radio/radioGroup.js';
|
|
4
7
|
import '@punica/components/components/select/option.js';
|
|
5
8
|
import '@punica/components/components/select/select.js';
|
|
6
9
|
import '@punica/components/components/switch/switch.js';
|
|
7
10
|
import '@punica/components/components/textarea/textarea.js';
|
|
8
|
-
import {
|
|
9
|
-
/** What a component that renders a form item type receives. */
|
|
10
|
-
export type FieldComponentProps = {
|
|
11
|
-
item: KitItem<any>;
|
|
12
|
-
/** Sets the value of the item. */
|
|
13
|
-
onChange: (value: unknown) => void;
|
|
14
|
-
};
|
|
15
|
-
export type FieldComponent = (props: FieldComponentProps) => ReactNode;
|
|
11
|
+
import { FieldComponent } from './FieldList';
|
|
16
12
|
/** The component of every form item type of the kit. */
|
|
17
13
|
export declare const fieldComponents: Record<string, FieldComponent>;
|
|
18
|
-
/** The types whose component shows the label itself. */
|
|
19
|
-
export declare const SELF_LABELLED: string[];
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { type ReactNode } from 'react';
|
|
2
2
|
import '@punica/components/components/button/button.js';
|
|
3
|
-
import '@punica/components/components/field/field.js';
|
|
4
3
|
import '@punica/components/components/formLayout/formLayout.js';
|
|
5
4
|
import { FormController, FormControllerOptions } from '@punica/form';
|
|
6
5
|
import { KitItem } from '../types';
|
|
7
|
-
import { FieldComponent } from './
|
|
8
|
-
export type { FieldComponent, FieldComponentProps } from './
|
|
6
|
+
import { FieldComponent } from './FieldList';
|
|
7
|
+
export type { FieldComponent, FieldComponentProps } from './FieldList';
|
|
9
8
|
export { fieldComponents } from './fields';
|
|
10
9
|
type ViewProps<E extends object> = {
|
|
11
10
|
/** Called with the values of a valid form when it is submitted. */
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { ErrorDetail, FormItem } from '@punica/form';
|
|
2
|
-
/** A choice
|
|
2
|
+
/** A choice: its value, or its value and the text to show. */
|
|
3
3
|
export type Option = string | {
|
|
4
4
|
value: string;
|
|
5
5
|
label: string;
|
|
6
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* The choices of a field: a list, or a function that loads one. The field
|
|
9
|
+
* shows that it is loading and calls the function once, also when several
|
|
10
|
+
* forms use it.
|
|
11
|
+
*/
|
|
12
|
+
export type Options = Option[] | (() => Promise<Option[]>);
|
|
7
13
|
/** How many columns of the form a field takes. */
|
|
8
14
|
export type Span = 1 | 2 | 3 | 4 | 'full';
|
|
9
15
|
/** The rules the kit validates by itself. */
|
|
@@ -18,6 +24,16 @@ export type Rules = {
|
|
|
18
24
|
min?: number;
|
|
19
25
|
/** Highest number allowed. */
|
|
20
26
|
max?: number;
|
|
27
|
+
/** Earliest date allowed, as `yyyy-mm-dd`. */
|
|
28
|
+
minDate?: string;
|
|
29
|
+
/** Latest date allowed, as `yyyy-mm-dd`. */
|
|
30
|
+
maxDate?: string;
|
|
31
|
+
/** Fewest choices, files or rows allowed. */
|
|
32
|
+
minItems?: number;
|
|
33
|
+
/** Most choices, files or rows allowed. */
|
|
34
|
+
maxItems?: number;
|
|
35
|
+
/** The largest file allowed, in bytes. */
|
|
36
|
+
maxFileSize?: number;
|
|
21
37
|
};
|
|
22
38
|
/**
|
|
23
39
|
* The form item of the kit: a form item of Punica Form with what the kit's
|
|
@@ -30,8 +46,27 @@ export type KitItem<E> = FormItem<E> & Rules & {
|
|
|
30
46
|
placeholder?: string;
|
|
31
47
|
/** The type of a text field, `text` by default. */
|
|
32
48
|
inputType?: 'text' | 'email' | 'password' | 'tel' | 'url' | 'date';
|
|
33
|
-
/**
|
|
34
|
-
|
|
49
|
+
/**
|
|
50
|
+
* What the browser may fill a text or password field in with, as the
|
|
51
|
+
* `autocomplete` of an input (`email`, `new-password`, `off`).
|
|
52
|
+
*/
|
|
53
|
+
autocomplete?: string;
|
|
54
|
+
/** The choices of a select, a multi-select or a radio group. */
|
|
55
|
+
options?: Options;
|
|
56
|
+
/** Shown in place of the choices while they are loaded, `Loading…` by default. */
|
|
57
|
+
loadingText?: string;
|
|
58
|
+
/** How the choices of a radio group are laid out, `vertical` by default. */
|
|
59
|
+
orientation?: 'horizontal' | 'vertical';
|
|
60
|
+
/** Whether a file field takes several files; its value is an array then. */
|
|
61
|
+
multiple?: boolean;
|
|
62
|
+
/** The files a file field takes, as the `accept` of a file input (`image/*,.pdf`). */
|
|
63
|
+
accept?: string;
|
|
64
|
+
/** The language a date field is typed and shown in, the one of the page by default. */
|
|
65
|
+
locale?: string;
|
|
66
|
+
/** The text of the button that adds a row to a list, `Add` by default. */
|
|
67
|
+
addLabel?: string;
|
|
68
|
+
/** The text of the button that removes a row from a list, `Remove` by default. */
|
|
69
|
+
removeLabel?: string;
|
|
35
70
|
/** Visible lines of a text area. */
|
|
36
71
|
rows?: number;
|
|
37
72
|
/** Columns the field takes in a form with several columns. */
|
|
@@ -49,6 +84,13 @@ export type Messages = {
|
|
|
49
84
|
pattern: (label: string) => string;
|
|
50
85
|
min: (label: string, value: number) => string;
|
|
51
86
|
max: (label: string, value: number) => string;
|
|
87
|
+
minDate: (label: string, date: string) => string;
|
|
88
|
+
maxDate: (label: string, date: string) => string;
|
|
89
|
+
minItems: (label: string, count: number) => string;
|
|
90
|
+
maxItems: (label: string, count: number) => string;
|
|
91
|
+
maxFileSize: (label: string, bytes: number) => string;
|
|
92
|
+
date: (label: string) => string;
|
|
93
|
+
accept: (label: string) => string;
|
|
52
94
|
};
|
|
53
95
|
/** A validation of an item. */
|
|
54
96
|
export type Validation = NonNullable<FieldProps['validation']>;
|
|
@@ -4,6 +4,10 @@ import { FieldProps, Messages, Validation } from './types';
|
|
|
4
4
|
* translations. A field can still have its own with `messages`.
|
|
5
5
|
*/
|
|
6
6
|
export declare const setMessages: (custom: Partial<Messages>) => void;
|
|
7
|
+
/** The first rule the value breaks, as its message. */
|
|
8
|
+
export declare const check: (props: FieldProps & {
|
|
9
|
+
type?: string;
|
|
10
|
+
}, value: unknown) => string | undefined;
|
|
7
11
|
/**
|
|
8
12
|
* The validation of a field: its rules first, then its own `validation`
|
|
9
13
|
* function, which only runs for a value that passed the rules.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"0.2.0-next.
|
|
1
|
+
{"version":"0.2.0-next.1"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { FormController, FormItem } from '@punica/form';
|
|
2
|
+
import { FormRow, ListProperty, RowOf } from '@punica/form-service';
|
|
3
|
+
/** A list of a form and the functions that change it. */
|
|
4
|
+
export type FieldArrayState<R extends object, F extends FormItem<R> = FormItem<R>> = {
|
|
5
|
+
/**
|
|
6
|
+
* The rows of the list, each with a `key` for React and the `controller`
|
|
7
|
+
* of its form. The array is replaced when a row is added, removed or
|
|
8
|
+
* moved, not when a value changes.
|
|
9
|
+
*/
|
|
10
|
+
rows: ReadonlyArray<FormRow<R, F>>;
|
|
11
|
+
/** Adds a row at the end; the values replace the ones of the row class. */
|
|
12
|
+
append: (value?: Partial<R>) => Promise<FormRow<R>>;
|
|
13
|
+
/** Adds a row at an index. */
|
|
14
|
+
insert: (index: number, value?: Partial<R>) => Promise<FormRow<R>>;
|
|
15
|
+
/** Removes a row: the row itself or the one at an index. */
|
|
16
|
+
remove: (row: FormRow<R> | number) => Promise<void>;
|
|
17
|
+
/** Moves the row at an index to another index. */
|
|
18
|
+
move: (from: number, to: number) => Promise<void>;
|
|
19
|
+
/** Replaces the rows by rows for the values. */
|
|
20
|
+
replace: (values: Array<Partial<R>>) => Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* A list of rows of a form, such as the lines of an invoice. The component
|
|
24
|
+
* renders again when a row is added, removed or moved; a value that changes
|
|
25
|
+
* renders the components that follow the form of that row, with `useForm`.
|
|
26
|
+
*
|
|
27
|
+
* const { rows, append, remove } = useFieldArray(controller, 'lines');
|
|
28
|
+
*
|
|
29
|
+
* rows.map((row) => <Line key={row.key} controller={row.controller} />)
|
|
30
|
+
*
|
|
31
|
+
* The form has to list the `FieldArray` service.
|
|
32
|
+
* @param property - The property of the list
|
|
33
|
+
*/
|
|
34
|
+
export declare const useFieldArray: <E extends object, F extends FormItem<E>, K extends ListProperty<E>, RF extends FormItem<RowOf<E, K>> = FormItem<RowOf<E, K>>>(controller: FormController<E, F>, property: K) => FieldArrayState<RowOf<E, K>, RF>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { InputOf, StandardSchema } from './standard';
|
|
2
|
+
/**
|
|
3
|
+
* What a form class may hold for a schema: the properties of the schema it
|
|
4
|
+
* has are of the type the schema takes. A form may leave properties out and
|
|
5
|
+
* start with empty values, which is what the schema is there to report.
|
|
6
|
+
*/
|
|
7
|
+
type Fits<T> = T extends object ? {
|
|
8
|
+
[K in keyof T]?: T[K] | null;
|
|
9
|
+
} : unknown;
|
|
10
|
+
/**
|
|
11
|
+
* Validates a form with a schema of its values:
|
|
12
|
+
*
|
|
13
|
+
* @Schema(z.object({ email: z.string().email(), age: z.number().min(18) }))
|
|
14
|
+
* @Services([SchemaValidation, Validate, Submit, …])
|
|
15
|
+
* class UserForm { … }
|
|
16
|
+
*
|
|
17
|
+
* The form has to list the `SchemaValidation` service, which shows every
|
|
18
|
+
* issue of the schema on the item it is about. A class whose properties do
|
|
19
|
+
* not have the types the schema takes does not compile.
|
|
20
|
+
* @param standard - A schema of Zod, Valibot, ArkType or another library
|
|
21
|
+
* that implements Standard Schema
|
|
22
|
+
*/
|
|
23
|
+
export declare const Schema: <S extends StandardSchema>(standard: S) => (target: abstract new (...args: any[]) => Fits<InputOf<S>>, context?: ClassDecoratorContext) => void;
|
|
24
|
+
/** The schema `@Schema` gave the class of an entity. */
|
|
25
|
+
export declare const schemaOf: (entity: unknown) => StandardSchema | undefined;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { FormItem } from '@punica/form';
|
|
2
|
+
import type { SchemaValidation } from './service';
|
|
3
|
+
export { Schema } from './decorator';
|
|
4
|
+
export { schema } from './schema';
|
|
5
|
+
export { SchemaValidation } from './service';
|
|
6
|
+
export type { StandardIssue, StandardResult, StandardSchema } from './standard';
|
|
7
|
+
declare module '@punica/form' {
|
|
8
|
+
interface ServiceRegistry<E, F extends FormItem<E>> {
|
|
9
|
+
schemaValidation: SchemaValidation<E, F>;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { FormItem } from '@punica/form';
|
|
2
|
+
import { StandardIssue, StandardSchema } from './standard';
|
|
3
|
+
/** The messages of the issues of a value; none for a valid one. */
|
|
4
|
+
export declare const validate: (schema: StandardSchema, value: unknown) => Promise<ReadonlyArray<StandardIssue>>;
|
|
5
|
+
/**
|
|
6
|
+
* What a form would be submitted as, as a plain object: the entity it was
|
|
7
|
+
* created from, with the values of its items.
|
|
8
|
+
*/
|
|
9
|
+
export declare const valuesOf: <E>(entity: E, items: Array<FormItem<E>>) => Record<string, unknown>;
|
|
10
|
+
/** The issues of a form, sorted by the item they belong to. */
|
|
11
|
+
export type Sorted<E> = {
|
|
12
|
+
/** The messages of an item, by its property. */
|
|
13
|
+
items: Map<keyof E, string[]>;
|
|
14
|
+
/** The messages that belong to no item, with the path they are about. */
|
|
15
|
+
form: string[];
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Sorts issues by the item they are about: the item whose value holds the
|
|
19
|
+
* path of the issue. An issue about `address.city` belongs to the item of
|
|
20
|
+
* the path `address/city`, or else to the one of `address`.
|
|
21
|
+
*/
|
|
22
|
+
export declare const sort: <E>(issues: ReadonlyArray<StandardIssue>, items: Array<FormItem<E>>) => Sorted<E>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"0.1.0-next.0"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ErrorDetail } from '@punica/form';
|
|
2
|
+
import { StandardSchema } from './standard';
|
|
3
|
+
/**
|
|
4
|
+
* The validation of a form item from a schema of its value:
|
|
5
|
+
*
|
|
6
|
+
* @TextField({ label: 'Email', validation: schema(z.string().email()) })
|
|
7
|
+
* email = '';
|
|
8
|
+
*
|
|
9
|
+
* The messages of the item are the messages of the issues of the schema.
|
|
10
|
+
* @param standard - A schema of Zod, Valibot, ArkType or another library
|
|
11
|
+
* that implements Standard Schema
|
|
12
|
+
*/
|
|
13
|
+
export declare const schema: (standard: StandardSchema) => ({ formItem }: {
|
|
14
|
+
formItem: {
|
|
15
|
+
value?: unknown;
|
|
16
|
+
};
|
|
17
|
+
}) => Promise<ErrorDetail>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceInitialize } from '@punica/form';
|
|
2
|
+
import { StandardSchema } from './standard';
|
|
3
|
+
/**
|
|
4
|
+
* Validates a form with the schema `@Schema` gave its class. Every issue of
|
|
5
|
+
* the schema is shown on the item it is about; issues that belong to no
|
|
6
|
+
* item, such as a rule about two values, become errors of the form
|
|
7
|
+
* (`state.errors`) when it is validated as a whole or submitted.
|
|
8
|
+
*
|
|
9
|
+
* A validation function of an item still runs, before the schema: the
|
|
10
|
+
* schema is asked about an item that passed it. Hidden and disabled items
|
|
11
|
+
* are not validated, as always.
|
|
12
|
+
*
|
|
13
|
+
* For a form that is not made from a class, give the schema to the service:
|
|
14
|
+
* `services: [SchemaValidation.of(schema), …]`.
|
|
15
|
+
*/
|
|
16
|
+
export declare class SchemaValidation<E, F extends FormItem<E> = FormItem<E>> implements IServiceInitialize<E, F> {
|
|
17
|
+
#private;
|
|
18
|
+
/** The schema of the forms of a service made with `of`. */
|
|
19
|
+
protected static schema?: StandardSchema;
|
|
20
|
+
/**
|
|
21
|
+
* A service for the forms of one schema, for forms without a class that
|
|
22
|
+
* `@Schema` could be written above.
|
|
23
|
+
*/
|
|
24
|
+
static of(standard: StandardSchema): typeof SchemaValidation;
|
|
25
|
+
get name(): string;
|
|
26
|
+
initialize(command: CommandService<E, F>): void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Standard Schema interface (https://standardschema.dev), version 1, as
|
|
3
|
+
* far as this package uses it. Zod, Valibot, ArkType and others implement
|
|
4
|
+
* it, so their schemas are accepted as they are.
|
|
5
|
+
*/
|
|
6
|
+
export interface StandardSchema<Input = unknown, Output = Input> {
|
|
7
|
+
readonly '~standard': {
|
|
8
|
+
readonly version: 1;
|
|
9
|
+
readonly vendor: string;
|
|
10
|
+
readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
|
|
11
|
+
readonly types?: {
|
|
12
|
+
readonly input: Input;
|
|
13
|
+
readonly output: Output;
|
|
14
|
+
} | undefined;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export type StandardResult<Output> = {
|
|
18
|
+
readonly value: Output;
|
|
19
|
+
readonly issues?: undefined;
|
|
20
|
+
} | {
|
|
21
|
+
readonly issues: ReadonlyArray<StandardIssue>;
|
|
22
|
+
};
|
|
23
|
+
export type StandardIssue = {
|
|
24
|
+
readonly message: string;
|
|
25
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
26
|
+
readonly key: PropertyKey;
|
|
27
|
+
}> | undefined;
|
|
28
|
+
};
|
|
29
|
+
/** What a schema accepts. */
|
|
30
|
+
export type InputOf<S extends StandardSchema> = NonNullable<S['~standard']['types']>['input'];
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceInitialize } from '@punica/form';
|
|
2
|
+
import { FormRow } from '../rows';
|
|
3
|
+
/** The properties of an entity that hold a list of rows. */
|
|
4
|
+
export type ListProperty<E> = {
|
|
5
|
+
[K in keyof E]-?: NonNullable<E[K]> extends ReadonlyArray<object> ? K : never;
|
|
6
|
+
}[keyof E];
|
|
7
|
+
/** The entity of the rows of a list. */
|
|
8
|
+
export type RowOf<E, K extends keyof E> = NonNullable<E[K]> extends ReadonlyArray<infer R extends object> ? R : never;
|
|
9
|
+
/**
|
|
10
|
+
* Lists of rows, such as the lines of an invoice. Every row is a form of its
|
|
11
|
+
* own, created from the entity the `row` function of the form item returns:
|
|
12
|
+
*
|
|
13
|
+
* const { rows, controller } = ...
|
|
14
|
+
* const lines = controller.getService(FieldArray);
|
|
15
|
+
* await lines.append('lines', { name: 'Paper' });
|
|
16
|
+
* lines.rows('lines')[0].controller.getService(SetValue).run('price', 5);
|
|
17
|
+
*
|
|
18
|
+
* The value of the item is the array of the values of its rows. The item is
|
|
19
|
+
* dirty, touched and in error when a row is, and validating the form
|
|
20
|
+
* validates the rows. A row class without `@Services` gets the services of
|
|
21
|
+
* the form around it.
|
|
22
|
+
*/
|
|
23
|
+
export declare class FieldArray<E, F extends FormItem<E> = FormItem<E>> implements IServiceInitialize<E, F> {
|
|
24
|
+
#private;
|
|
25
|
+
get name(): string;
|
|
26
|
+
initialize(command: CommandService<E, F>): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* The rows of a list. The array is replaced when a row is added, removed
|
|
29
|
+
* or moved and stays the same while only values change.
|
|
30
|
+
* @throws When the item of the property is not a list
|
|
31
|
+
*/
|
|
32
|
+
rows<K extends ListProperty<E>, RF extends FormItem<RowOf<E, K>> = FormItem<RowOf<E, K>>>(property: K): ReadonlyArray<FormRow<RowOf<E, K>, RF>>;
|
|
33
|
+
/**
|
|
34
|
+
* Adds a row at the end of a list.
|
|
35
|
+
* @param value - Values of the row that replace the ones of its class
|
|
36
|
+
* @returns The row
|
|
37
|
+
*/
|
|
38
|
+
append<K extends ListProperty<E>>(property: K, value?: Partial<RowOf<E, K>>): Promise<FormRow<RowOf<E, K>>>;
|
|
39
|
+
/** Adds a row at an index of a list. */
|
|
40
|
+
insert<K extends ListProperty<E>>(property: K, index: number, value?: Partial<RowOf<E, K>>): Promise<FormRow<RowOf<E, K>>>;
|
|
41
|
+
/** Removes a row from a list: the row itself or the one at an index. */
|
|
42
|
+
remove<K extends ListProperty<E>>(property: K, row: FormRow<RowOf<E, K>> | number): Promise<void>;
|
|
43
|
+
/** Moves the row at an index of a list to another index. */
|
|
44
|
+
move<K extends ListProperty<E>>(property: K, from: number, to: number): Promise<void>;
|
|
45
|
+
/** Replaces the rows of a list by rows for the values. */
|
|
46
|
+
replace<K extends ListProperty<E>>(property: K, values: Array<Partial<RowOf<E, K>>>): Promise<void>;
|
|
47
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { CommandService, FormItem } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* A validation of a form as a whole: returns the messages of what is wrong,
|
|
4
|
+
* none for a valid form.
|
|
5
|
+
*/
|
|
6
|
+
export type FormValidation = () => string[] | Promise<string[]>;
|
|
7
|
+
/**
|
|
8
|
+
* Adds a validation of the form as a whole, for what does not belong to one
|
|
9
|
+
* item, such as two dates that have to be in order. A service adds it when
|
|
10
|
+
* it is initialized. `Validate` and `Submit` run it after the items: its
|
|
11
|
+
* messages are added to the errors of the form (`state.errors`), and a form
|
|
12
|
+
* that has some is not valid (`state.valid`) and not submitted. `Reset` and
|
|
13
|
+
* `ClearErrors` remove them.
|
|
14
|
+
* @param command - What the service received when it was initialized
|
|
15
|
+
*/
|
|
16
|
+
export declare const addFormValidation: <E, F extends FormItem<E>>(command: CommandService<E, F>, validation: FormValidation) => void;
|
|
17
|
+
/**
|
|
18
|
+
* What tells a validation of the form that the form was reset since:
|
|
19
|
+
* read it before validating the items and give it to `validateForm`.
|
|
20
|
+
*/
|
|
21
|
+
export declare const formEpoch: (command: object) => number;
|
|
22
|
+
/**
|
|
23
|
+
* Runs the validations of the form as a whole and writes their messages
|
|
24
|
+
* among the errors of the form; the other errors, such as the one of a
|
|
25
|
+
* rejected submit, stay. A form without such validations is left alone.
|
|
26
|
+
* @param epoch - `formEpoch` from before the items were validated
|
|
27
|
+
* @returns Whether there is no message. `false` as well when the form was
|
|
28
|
+
* reset meanwhile
|
|
29
|
+
*/
|
|
30
|
+
export declare const validateForm: <E, F extends FormItem<E>>(command: CommandService<E, F>, epoch?: number) => Promise<boolean>;
|
|
31
|
+
/**
|
|
32
|
+
* Removes the messages of the validations of the form as a whole.
|
|
33
|
+
* @param reset - The form is reset: validations that are running do not
|
|
34
|
+
* count any more
|
|
35
|
+
*/
|
|
36
|
+
export declare const clearFormValidation: <E, F extends FormItem<E>>(command: CommandService<E, F>, reset?: boolean) => void;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { FormItem } from '@punica/form';
|
|
2
2
|
import type { ClearError } from './clearError';
|
|
3
3
|
import type { ClearErrors } from './clearErrors';
|
|
4
|
+
import type { FieldArray } from './fieldArray';
|
|
4
5
|
import type { GetValues } from './getValues';
|
|
5
6
|
import type { Reset } from './reset';
|
|
6
7
|
import type { ResetItem } from './resetItem';
|
|
@@ -11,6 +12,11 @@ import type { Trigger } from './trigger';
|
|
|
11
12
|
import type { Validate } from './validate';
|
|
12
13
|
export * from './clearError';
|
|
13
14
|
export * from './clearErrors';
|
|
15
|
+
export * from './fieldArray';
|
|
16
|
+
export { addFormValidation } from './formValidation';
|
|
17
|
+
export { wrapValidation } from './rows';
|
|
18
|
+
export type { FormValidation } from './formValidation';
|
|
19
|
+
export type { FormRow } from './rows';
|
|
14
20
|
export * from './getValues';
|
|
15
21
|
export * from './reset';
|
|
16
22
|
export * from './resetItem';
|
|
@@ -24,6 +30,7 @@ declare module '@punica/form' {
|
|
|
24
30
|
interface ServiceRegistry<E, F extends FormItem<E>> {
|
|
25
31
|
clearError: ClearError<E, F>;
|
|
26
32
|
clearErrors: ClearErrors<E, F>;
|
|
33
|
+
fieldArray: FieldArray<E, F>;
|
|
27
34
|
getValues: GetValues<E, F>;
|
|
28
35
|
reset: Reset<E, F>;
|
|
29
36
|
resetItem: ResetItem<E, F>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"2.2.0-next.
|
|
1
|
+
{"version":"2.2.0-next.1"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { CommandService, FormController, FormItem, IService } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* A row of a list: a form of its own. `key` identifies the row for as long
|
|
4
|
+
* as it is part of the list, wherever it is moved to.
|
|
5
|
+
*/
|
|
6
|
+
export type FormRow<R extends object = object, F extends FormItem<R> = FormItem<R>> = {
|
|
7
|
+
key: string;
|
|
8
|
+
controller: FormController<R, F>;
|
|
9
|
+
};
|
|
10
|
+
type AnyRow = FormRow<any, any>;
|
|
11
|
+
/** Whether the item of a property is a list whose rows were created. */
|
|
12
|
+
export declare const hasRows: (command: object, property: PropertyKey) => boolean;
|
|
13
|
+
/** The properties of the lists of a form. */
|
|
14
|
+
export declare const listProperties: (command: object) => PropertyKey[];
|
|
15
|
+
/**
|
|
16
|
+
* The rows of a list.
|
|
17
|
+
* @throws When the item of the property is not a list
|
|
18
|
+
*/
|
|
19
|
+
export declare const rowsOf: (command: object, property: PropertyKey) => readonly AnyRow[];
|
|
20
|
+
/**
|
|
21
|
+
* Creates the rows of every list of a form from the values of their items.
|
|
22
|
+
* @param validate - The class of the service a row is validated with: the
|
|
23
|
+
* validation of the item of a list also validates its rows. A row whose
|
|
24
|
+
* class lists services of its own, without that one, counts as valid
|
|
25
|
+
*/
|
|
26
|
+
export declare const createLists: <E, F extends FormItem<E>>(command: CommandService<E, F>, validate: new () => IService & {
|
|
27
|
+
run: () => Promise<boolean>;
|
|
28
|
+
}) => Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Wraps the validations of the items of a form, for a service that adds a
|
|
31
|
+
* validation of its own to them. It works with lists whatever the order of
|
|
32
|
+
* the services: the validation of a list item is wrapped inside the
|
|
33
|
+
* validation of its rows, so what it finds stays while the rows change.
|
|
34
|
+
* @param wrap - Returns the new validation of an item for its current one
|
|
35
|
+
*/
|
|
36
|
+
export declare const wrapValidation: <E, F extends FormItem<E>>(command: CommandService<E, F>, wrap: (item: F, validation: F["validation"]) => F["validation"]) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Adds a row to a list.
|
|
39
|
+
* @param index - Where; at the end when it is left out
|
|
40
|
+
*/
|
|
41
|
+
export declare const insertRow: <E, F extends FormItem<E>>(command: CommandService<E, F>, property: keyof E, value?: object, index?: number) => Promise<AnyRow>;
|
|
42
|
+
/** Removes a row from a list: the row itself or the one at an index. */
|
|
43
|
+
export declare const removeRow: <E, F extends FormItem<E>>(command: CommandService<E, F>, property: keyof E, row: AnyRow | number) => Promise<void>;
|
|
44
|
+
/** Moves the row at an index of a list to another one. */
|
|
45
|
+
export declare const moveRow: <E, F extends FormItem<E>>(command: CommandService<E, F>, property: keyof E, from: number, to: number) => Promise<void>;
|
|
46
|
+
/** Replaces the rows of a list by rows for the values. */
|
|
47
|
+
export declare const replaceRows: <E, F extends FormItem<E>>(command: CommandService<E, F>, property: keyof E, values: unknown) => Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Brings the rows of a list, or of every list, back to the ones the form
|
|
50
|
+
* was started with. The items of the lists are not written until that is
|
|
51
|
+
* done; the caller resets them meanwhile.
|
|
52
|
+
* @returns `undefined`, at once, for a form without lists
|
|
53
|
+
*/
|
|
54
|
+
export declare const resetRows: <E, F extends FormItem<E>>(command: CommandService<E, F>, property?: keyof E) => Promise<void> | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* The service of a class of the form of a row; `undefined` when the row
|
|
57
|
+
* does not have it.
|
|
58
|
+
*/
|
|
59
|
+
export declare const serviceOf: <S extends IService>(row: AnyRow, service: new () => S) => S | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Clears the errors of the rows of a list, or of every list, of a form and
|
|
62
|
+
* forgets the result of the validation of the items of the lists.
|
|
63
|
+
* @param clear - Clears the errors of a row
|
|
64
|
+
* @returns `undefined`, at once, for a form without lists
|
|
65
|
+
*/
|
|
66
|
+
export declare const clearRows: <E, F extends FormItem<E>>(command: CommandService<E, F>, property: keyof E | undefined, clear: (row: AnyRow) => unknown) => Promise<void> | undefined;
|
|
67
|
+
export {};
|
|
@@ -10,6 +10,7 @@ export declare class SetValue<E, F extends FormItem<E> = FormItem<E>> implements
|
|
|
10
10
|
* Fires UPDATE_ITEM with the changed item and the items its control returned.
|
|
11
11
|
* When the property is set again, or the form is reset, while the control
|
|
12
12
|
* function is still running, the result of that older run is dropped.
|
|
13
|
+
* The value of a list replaces its rows.
|
|
13
14
|
* @throws When the form has no item for the property
|
|
14
15
|
*/
|
|
15
16
|
run(property: keyof E, value: unknown): Promise<void>;
|
|
@@ -7,7 +7,8 @@ export declare class Validate<E, F extends FormItem<E> = FormItem<E>> implements
|
|
|
7
7
|
get name(): string;
|
|
8
8
|
initialize(command: CommandService<E, F>): void;
|
|
9
9
|
/**
|
|
10
|
-
* @returns Whether every validated item is valid
|
|
10
|
+
* @returns Whether every validated item is valid, and the form as a
|
|
11
|
+
* whole when it has validations of its own (`addFormValidation`)
|
|
11
12
|
*/
|
|
12
13
|
run(): Promise<boolean>;
|
|
13
14
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@punica/form-mcp",
|
|
3
|
-
"version": "0.4.0-next.
|
|
3
|
+
"version": "0.4.0-next.1",
|
|
4
4
|
"description": "MCP server for Punica Form: look up the exact API of its packages, read the rules and check code that uses them.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mcp",
|
|
@@ -40,10 +40,11 @@
|
|
|
40
40
|
"zod": "^4.6.7"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@punica/form": "^2.2.0-next.
|
|
44
|
-
"@punica/form-service": "^2.2.0-next.
|
|
45
|
-
"@punica/form-react": "^0.2.0-next.
|
|
46
|
-
"@punica/form-kit": "^0.3.0-next.
|
|
43
|
+
"@punica/form": "^2.2.0-next.1",
|
|
44
|
+
"@punica/form-service": "^2.2.0-next.1",
|
|
45
|
+
"@punica/form-react": "^0.2.0-next.1",
|
|
46
|
+
"@punica/form-kit": "^0.3.0-next.1",
|
|
47
|
+
"@punica/form-schema": "^0.1.0-next.0"
|
|
47
48
|
},
|
|
48
49
|
"scripts": {
|
|
49
50
|
"build": "node build.mjs",
|
package/src/create.mjs
CHANGED
|
@@ -13,7 +13,7 @@ const { version } = JSON.parse(readFileSync(new URL('../package.json', import.me
|
|
|
13
13
|
const text = (value) => ({ content: [{ type: 'text', text: value }] });
|
|
14
14
|
|
|
15
15
|
/** The types a form definition may use when the form kit renders it. */
|
|
16
|
-
const KIT_TYPES = ['text', 'textarea', 'number', 'select', 'checkbox', 'switch'];
|
|
16
|
+
const KIT_TYPES = ['text', 'textarea', 'number', 'select', 'checkbox', 'switch', 'radio', 'date', 'multiselect', 'password', 'file'];
|
|
17
17
|
|
|
18
18
|
/** A tool argument that is JSON: given as an object, or as its text. */
|
|
19
19
|
const parse = (value) => (typeof value === 'string' ? JSON.parse(value) : value);
|
|
@@ -62,7 +62,7 @@ export function createPunicaFormServer({ packages, guide }) {
|
|
|
62
62
|
{
|
|
63
63
|
title: 'Find Punica Form exports',
|
|
64
64
|
description:
|
|
65
|
-
'Finds exports of the Punica Form packages (@punica/form, @punica/form-service, @punica/form-react, @punica/form-kit, @punica/form-json) by name or by words in their documentation ("validate", "reader", "field"). Without a query: every export.',
|
|
65
|
+
'Finds exports of the Punica Form packages (@punica/form, @punica/form-service, @punica/form-react, @punica/form-kit, @punica/form-json, @punica/form-schema) by name or by words in their documentation ("validate", "reader", "field"). Without a query: every export.',
|
|
66
66
|
inputSchema: { query: z.string().optional().describe('Words to look for') },
|
|
67
67
|
annotations: { readOnlyHint: true }
|
|
68
68
|
},
|
package/src/guide.md
CHANGED
|
@@ -12,8 +12,11 @@ Punica Form is a headless form engine. `@punica/form` holds the engine,
|
|
|
12
12
|
`deepCopy`, the DevTools switches and the types. Use `search_api` for the
|
|
13
13
|
exact list.
|
|
14
14
|
- `@punica/form-json`: forms as data; see "Forms as data" below.
|
|
15
|
+
- `@punica/form-schema`: validation with a schema of Zod, Valibot, ArkType
|
|
16
|
+
or Yup; see "Validation with a schema" below.
|
|
15
17
|
- `@punica/form-service`: `Validate`, `GetValues`, `SetValue`, `Reset`,
|
|
16
|
-
`ResetItem`, `Trigger`, `ClearError`, `ClearErrors`, `
|
|
18
|
+
`ResetItem`, `Trigger`, `ClearError`, `ClearErrors`, `Touch`, `Submit`,
|
|
19
|
+
`FieldArray`, `Store`.
|
|
17
20
|
- NOT provided: `@Title`, `@Description`, `@Layout`, `@FormItemInputText` or
|
|
18
21
|
any other form item decorator, a `Reader` class, a base form item. These
|
|
19
22
|
belong to the application's own form system. Build them with the `define*`
|
|
@@ -51,9 +54,13 @@ Punica Form is a headless form engine. `@punica/form` holds the engine,
|
|
|
51
54
|
|
|
52
55
|
`@punica/form-kit` provides what the application would otherwise build:
|
|
53
56
|
`@FormKit({ title, columns })` on the class, the field decorators `TextField`,
|
|
54
|
-
`NumberField`, `TextAreaField`, `SelectField`, `CheckboxField
|
|
55
|
-
`SwitchField
|
|
56
|
-
`
|
|
57
|
+
`NumberField`, `TextAreaField`, `SelectField`, `CheckboxField`,
|
|
58
|
+
`SwitchField`, `RadioGroupField`, `MultiSelectField`, `DateField` (its value
|
|
59
|
+
is `yyyy-mm-dd`), `PasswordField`, `FileField` and `ArrayField` (a list of
|
|
60
|
+
rows: `@ArrayField({ label, row: () => new LineForm() })`, the kit renders a
|
|
61
|
+
form for every row) with the rules `required`, `minLength`, `maxLength`,
|
|
62
|
+
`pattern`, `min`, `max`, `minDate`, `maxDate`, `minItems`, `maxItems` and
|
|
63
|
+
`maxFileSize`; `options` may be a function that loads the choices. And from `@punica/form-kit/react` the component
|
|
57
64
|
`<KitForm entity={() => new UserForm()} onSubmit={…} />`, which renders the
|
|
58
65
|
form with `@punica/components`, shows what `onSubmit` throws above the
|
|
59
66
|
fields and reports it to `onError`. Use it when the project has it installed;
|
|
@@ -74,7 +81,7 @@ and runs it.
|
|
|
74
81
|
refuse a definition that is not valid with a `FormDefinitionError` whose
|
|
75
82
|
`problems` have a `path` and a `message`.
|
|
76
83
|
- Types with the kit: `text`, `textarea`, `number`, `select`, `checkbox`,
|
|
77
|
-
`switch`. Kit options such as `inputType`, `rows` and `span` go into `props`.
|
|
84
|
+
`switch`, `radio`, `date`, `multiselect`, `password`, `file`. Kit options such as `inputType`, `rows` and `span` go into `props`.
|
|
78
85
|
- Never put functions, expressions or HTML into a definition, and never
|
|
79
86
|
build one by string concatenation: produce the object and validate it.
|
|
80
87
|
- In React a definition is read when the component mounts; give `JsonForm` a
|
|
@@ -143,6 +150,7 @@ entity of the form, so property names and results are type-checked.
|
|
|
143
150
|
| `ClearErrors` | `run(): Promise<void>` |
|
|
144
151
|
| `Touch` | `run(property): Promise<void>`, marks the item as touched; call it when the user leaves the field (since 2.2) |
|
|
145
152
|
| `Submit` | `run(handler?): Promise<boolean>`, validates, then calls `handler(values)`; sets `state.submitting`, ignores a second submit meanwhile, and turns what the handler throws into `state.errors` instead of rejecting (since 2.2) |
|
|
153
|
+
| `FieldArray` | no `run`: `rows(property)`, `append`, `insert`, `remove`, `move`, `replace`; see "Lists of rows" (since 2.2) |
|
|
146
154
|
| `Store` | no `run`: it adds `readStoreItem`, `writeStoreItem`, `deleteStoreItem` to the command passed to item functions (`CommandItem<E, F, StoreCommand>`) |
|
|
147
155
|
|
|
148
156
|
```ts
|
|
@@ -171,11 +179,53 @@ if (await controller.getService(Validate).run()) {
|
|
|
171
179
|
- To submit a form prefer `Submit` (since 2.2) over calling `Validate` and
|
|
172
180
|
`GetValues` by hand. Do not keep your own `submitting` or `dirty` flags.
|
|
173
181
|
|
|
182
|
+
## Lists of rows
|
|
183
|
+
|
|
184
|
+
For repeated rows (the lines of an invoice, a list of phone numbers) do not
|
|
185
|
+
keep an array in a single item and do not build property names such as
|
|
186
|
+
`lines.0.price`. Since 2.2 a form item with a `row` function is a list: every
|
|
187
|
+
row is a form of its own, created from the entity `row` returns.
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
class LineForm {
|
|
191
|
+
@FormItemInputText({ label: 'Product' })
|
|
192
|
+
product = '';
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
@Services([FieldArray, GetValues, SetValue, Validate, Trigger, Submit])
|
|
196
|
+
class InvoiceForm {
|
|
197
|
+
// Any form item decorator of the application; `row` makes it a list
|
|
198
|
+
@FormItemList({ label: 'Lines', row: () => new LineForm() })
|
|
199
|
+
lines: LineForm[] = [];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const list = controller.getService(FieldArray);
|
|
203
|
+
|
|
204
|
+
const row = await list.append('lines', { product: 'Paper' });
|
|
205
|
+
await row.controller.getService(SetValue).run('product', 'Ink');
|
|
206
|
+
await list.remove('lines', row); // or an index
|
|
207
|
+
list.rows('lines'); // [{ key, controller }], a new array only when rows change
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
- The form has to list `FieldArray`. A row class without `@Services` gets the
|
|
211
|
+
services of the form around it; one with `@Services` keeps its own.
|
|
212
|
+
- The value of the list item is the array of the values of its rows.
|
|
213
|
+
`GetValues` and `Submit` return it; never collect the rows by hand.
|
|
214
|
+
- `Validate` and `Submit` validate the rows. The list item is `dirty`,
|
|
215
|
+
`touched` and in `error` when a row is, and `Reset` brings back the rows the
|
|
216
|
+
form was started with. `SetValue` on the list replaces its rows.
|
|
217
|
+
- A `validation` of the list item itself counts besides the rows, for
|
|
218
|
+
example "add at least one line".
|
|
219
|
+
- In React: `const { rows, append, remove, move } = useFieldArray(controller, "lines")`
|
|
220
|
+
from `@punica/form-react`. Render `rows.map((row) => <Line key={row.key} controller={row.controller} />)`
|
|
221
|
+
and call `useForm(row.controller)` inside `Line`, so typing into a row
|
|
222
|
+
renders that row only.
|
|
223
|
+
|
|
174
224
|
## Form items
|
|
175
225
|
|
|
176
226
|
A form item is a plain object: `property`, `label`, `type`, `value`,
|
|
177
227
|
`required`, `disabled`, `hidden`, `error`, `errorMessages`, `dirty`,
|
|
178
|
-
`touched`, `validating
|
|
228
|
+
`touched`, `validating`, and `row` for a list of rows. Add the
|
|
179
229
|
properties of your form system with a type of your own:
|
|
180
230
|
`type AppItem<E> = FormItem<E> & { layout?: Layout }`, then use
|
|
181
231
|
`FormController<UserForm, AppItem<UserForm>>`.
|
|
@@ -196,6 +246,33 @@ getItem }` holding copies, and return items instead of changing them:
|
|
|
196
246
|
returned items are written to the form
|
|
197
247
|
- `initialize(command): Promise<FormItem[]>`, run once while the form is created
|
|
198
248
|
|
|
249
|
+
## Validation with a schema
|
|
250
|
+
|
|
251
|
+
When the project has a schema library (Zod, Valibot, ArkType, Yup: anything
|
|
252
|
+
that implements Standard Schema) and `@punica/form-schema`, do not write the
|
|
253
|
+
same rules again as validation functions.
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
import { Schema, SchemaValidation, schema } from "@punica/form-schema";
|
|
257
|
+
|
|
258
|
+
const User = z.object({ email: z.email(), age: z.number().min(18) });
|
|
259
|
+
|
|
260
|
+
@Schema(User) // the class does not compile when its properties do not fit
|
|
261
|
+
@Services([SchemaValidation, GetValues, SetValue, Validate, Trigger, Submit])
|
|
262
|
+
class UserForm { … }
|
|
263
|
+
|
|
264
|
+
// Or one item only, without the service:
|
|
265
|
+
// validation: schema(z.email("Enter an email"))
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
- `SchemaValidation` has to be listed. It shows every issue on the item its
|
|
269
|
+
path is about; issues that belong to no item become `state.errors`, make
|
|
270
|
+
`state.valid` false and keep the form from being submitted.
|
|
271
|
+
- For a form without a class: `services: [SchemaValidation.of(User), …]`.
|
|
272
|
+
- A rule about the form as a whole without a schema: a service that calls
|
|
273
|
+
`addFormValidation(command, () => messages)` of `@punica/form-service` in
|
|
274
|
+
its `initialize` (since 2.2).
|
|
275
|
+
|
|
199
276
|
## Events
|
|
200
277
|
|
|
201
278
|
`controller.on(event, handler)` returns the function that removes the
|
package/src/main.mjs
CHANGED
|
@@ -12,7 +12,8 @@ const packages = [
|
|
|
12
12
|
loadPackage('@punica/form-kit'),
|
|
13
13
|
loadPackage('@punica/form-kit', process.cwd(), 'react'),
|
|
14
14
|
loadPackage('@punica/form-kit', process.cwd(), 'json'),
|
|
15
|
-
loadPackage('@punica/form-json')
|
|
15
|
+
loadPackage('@punica/form-json'),
|
|
16
|
+
loadPackage('@punica/form-schema')
|
|
16
17
|
].filter(Boolean);
|
|
17
18
|
const guide = readFileSync(new URL('./guide.md', import.meta.url), 'utf8');
|
|
18
19
|
|