@punica/form-mcp 0.3.1 → 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.
Files changed (64) hide show
  1. package/README.md +8 -6
  2. package/bundled/form/controller/index.d.ts +6 -0
  3. package/bundled/form/define/Form.d.ts +41 -0
  4. package/bundled/form/define/index.d.ts +2 -0
  5. package/bundled/form/model/command/service.d.ts +7 -1
  6. package/bundled/form/model/constants/index.d.ts +4 -0
  7. package/bundled/form/model/form/index.d.ts +32 -0
  8. package/bundled/form/model/item/index.d.ts +35 -2
  9. package/bundled/form/package.json +1 -1
  10. package/bundled/form/utils/deepCopy.d.ts +2 -1
  11. package/bundled/form/utils/index.d.ts +1 -0
  12. package/bundled/form/utils/isEqual.d.ts +7 -0
  13. package/bundled/form-json/conditions.d.ts +8 -0
  14. package/bundled/form-json/definition/fromJsonSchema.d.ts +22 -0
  15. package/bundled/form-json/definition/index.d.ts +6 -0
  16. package/bundled/form-json/definition/limits.d.ts +31 -0
  17. package/bundled/form-json/definition/schema.d.ts +202 -0
  18. package/bundled/form-json/definition/types.d.ts +87 -0
  19. package/bundled/form-json/definition/validate.d.ts +15 -0
  20. package/bundled/form-json/fromSchema.d.ts +50 -0
  21. package/bundled/form-json/index.d.ts +4 -0
  22. package/bundled/form-json/package.json +1 -0
  23. package/bundled/form-json/rules.d.ts +19 -0
  24. package/bundled/form-kit/decorators.d.ts +39 -1
  25. package/bundled/form-kit/json/index.d.ts +33 -0
  26. package/bundled/form-kit/package.json +1 -1
  27. package/bundled/form-kit/react/FieldList.d.ts +33 -0
  28. package/bundled/form-kit/react/Rows.d.ts +9 -0
  29. package/bundled/form-kit/react/fields.d.ts +5 -11
  30. package/bundled/form-kit/react/index.d.ts +5 -4
  31. package/bundled/form-kit/types.d.ts +45 -3
  32. package/bundled/form-kit/validation.d.ts +4 -0
  33. package/bundled/form-react/index.d.ts +3 -0
  34. package/bundled/form-react/package.json +1 -1
  35. package/bundled/form-react/useController.d.ts +15 -0
  36. package/bundled/form-react/useFieldArray.d.ts +34 -0
  37. package/bundled/form-react/useFormController.d.ts +2 -7
  38. package/bundled/form-react/useFormState.d.ts +7 -0
  39. package/bundled/form-schema/decorator.d.ts +26 -0
  40. package/bundled/form-schema/index.d.ts +11 -0
  41. package/bundled/form-schema/issues.d.ts +22 -0
  42. package/bundled/form-schema/package.json +1 -0
  43. package/bundled/form-schema/schema.d.ts +17 -0
  44. package/bundled/form-schema/service.d.ts +27 -0
  45. package/bundled/form-schema/standard.d.ts +30 -0
  46. package/bundled/form-service/fieldArray/index.d.ts +47 -0
  47. package/bundled/form-service/formValidation.d.ts +36 -0
  48. package/bundled/form-service/index.d.ts +13 -0
  49. package/bundled/form-service/message.d.ts +2 -0
  50. package/bundled/form-service/package.json +1 -1
  51. package/bundled/form-service/readValues.d.ts +6 -0
  52. package/bundled/form-service/rows.d.ts +67 -0
  53. package/bundled/form-service/setValue/index.d.ts +1 -0
  54. package/bundled/form-service/submit/index.d.ts +20 -0
  55. package/bundled/form-service/touch/index.d.ts +13 -0
  56. package/bundled/form-service/trigger/index.d.ts +3 -0
  57. package/bundled/form-service/validate/index.d.ts +2 -1
  58. package/bundled/form-service/validateItem.d.ts +25 -9
  59. package/bundled/form-service/valuesOf.d.ts +3 -0
  60. package/package.json +7 -5
  61. package/src/create.mjs +78 -3
  62. package/src/guide.md +156 -8
  63. package/src/load.mjs +7 -2
  64. package/src/main.mjs +5 -2
@@ -0,0 +1,50 @@
1
+ import { FormController, FormItem, FormItemRegister, IService } from '@punica/form';
2
+ import { DefinitionProblem, OptionDefinition } from './definition';
3
+ import { Rule } from './rules';
4
+ /** The values of a form made from a definition, by the properties of its items. */
5
+ export type JsonFormValues = Record<string, unknown>;
6
+ /** An item of a form made from a definition. */
7
+ export type JsonFormItem = FormItem<JsonFormValues> & {
8
+ help?: string;
9
+ placeholder?: string;
10
+ options?: OptionDefinition[];
11
+ /** The `props` of the definition are properties of the item. */
12
+ [key: string]: unknown;
13
+ };
14
+ export type FromSchemaOptions = {
15
+ /**
16
+ * The types a definition may use, and the form item type each one stands
17
+ * for: `{ text: 'kit:text' }`. Without it a definition may use the types
18
+ * of the register, by their own names.
19
+ */
20
+ types?: Record<string, string>;
21
+ /** Rules of the application, by the names definitions use them with. */
22
+ rules?: Record<string, Rule>;
23
+ /**
24
+ * Services in addition to GetValues, SetValue, Validate, Trigger, Reset,
25
+ * Touch and Submit.
26
+ */
27
+ services?: Array<IService | (new () => IService)>;
28
+ /** The register whose form item types the form reports. */
29
+ register?: FormItemRegister;
30
+ /** Identifies the form in developer tools; the name of the definition by default. */
31
+ name?: string;
32
+ };
33
+ /**
34
+ * Thrown for a definition that is not valid. `problems` says what is wrong
35
+ * and where.
36
+ */
37
+ export declare class FormDefinitionError extends Error {
38
+ readonly problems: DefinitionProblem[];
39
+ constructor(problems: DefinitionProblem[]);
40
+ }
41
+ /**
42
+ * Creates the controller of a form that is described as data. The
43
+ * definition is checked first; whatever it is, nothing of it is executed.
44
+ * Start the controller as usual, or give this function to `useController`.
45
+ *
46
+ * const controller = await fromSchema(definition, { types: { text: 'app:text' } });
47
+ * await controller.start();
48
+ * @throws {FormDefinitionError} When the definition is not valid
49
+ */
50
+ export declare const fromSchema: (definition: unknown, options?: FromSchemaOptions) => Promise<FormController<JsonFormValues, JsonFormItem>>;
@@ -0,0 +1,4 @@
1
+ export * from './definition';
2
+ export * from './fromSchema';
3
+ export { builtInRules } from './rules';
4
+ export type { Rule, RuleContext } from './rules';
@@ -0,0 +1 @@
1
+ {"version":"0.1.0-next.0"}
@@ -0,0 +1,19 @@
1
+ import { JsonValue } from './definition';
2
+ /** What a rule knows about the item it checks. */
3
+ export type RuleContext = {
4
+ /** The `value` of the rule in the definition. */
5
+ value?: JsonValue;
6
+ /** The label of the item. */
7
+ label: string;
8
+ };
9
+ /**
10
+ * A validation rule: returns the message for a value that breaks it and
11
+ * `undefined` for one that does not. It may ask a server.
12
+ */
13
+ export type Rule = (value: unknown, context: RuleContext) => string | undefined | Promise<string | undefined>;
14
+ export declare const isEmpty: (value: unknown) => boolean;
15
+ /**
16
+ * The rules every form definition can use. Except for `required`, a rule is
17
+ * not asked about an empty value.
18
+ */
19
+ export declare const builtInRules: Record<string, Rule>;
@@ -7,7 +7,10 @@ export type FormKitOptions = {
7
7
  description?: string;
8
8
  /** Columns the fields are laid out in, 1 by default. */
9
9
  columns?: 1 | 2 | 3 | 4;
10
- /** Services in addition to GetValues, SetValue, Validate, Trigger and Reset. */
10
+ /**
11
+ * Services in addition to GetValues, SetValue, Validate, Trigger, Reset,
12
+ * Touch, Submit and FieldArray.
13
+ */
11
14
  services?: Array<IService | (new () => IService)>;
12
15
  /** Readers in addition to the kit's. */
13
16
  readers?: Array<IReader<any, any>>;
@@ -27,6 +30,12 @@ export declare const KIT_TYPES: {
27
30
  readonly select: "kit:select";
28
31
  readonly checkbox: "kit:checkbox";
29
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";
30
39
  };
31
40
  /** A single-line text field. */
32
41
  export declare const TextField: (props: FieldProps) => FormPropertyDecorator;
@@ -40,3 +49,32 @@ export declare const SelectField: (props: FieldProps) => FormPropertyDecorator;
40
49
  export declare const CheckboxField: (props: FieldProps) => FormPropertyDecorator;
41
50
  /** A switch; its value is a boolean. */
42
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;
@@ -0,0 +1,33 @@
1
+ import type { ReactNode } from 'react';
2
+ import { FromSchemaOptions, JsonFormValues } from '@punica/form-json';
3
+ import { type FormViewProps } from '../react';
4
+ /**
5
+ * The types a form definition may use with the kit, and the form item type
6
+ * of the kit each one stands for: every type of the kit but the list.
7
+ */
8
+ export declare const KIT_JSON_TYPES: Record<string, string>;
9
+ export type JsonFormProps = Omit<FormViewProps<JsonFormValues>, 'controller'> & {
10
+ /**
11
+ * The form definition, read when the component mounts. It is checked
12
+ * before anything is rendered, so it may come from a language model.
13
+ * Give the component a `key` to show another definition.
14
+ */
15
+ definition: unknown;
16
+ /** Rules, services and types in addition to the kit's. */
17
+ options?: FromSchemaOptions;
18
+ /** Shown while the form is being started. */
19
+ loading?: ReactNode;
20
+ /**
21
+ * Shown when the definition is not valid or the form could not be
22
+ * started; receives what was thrown, a `FormDefinitionError` for a
23
+ * definition that is not valid.
24
+ */
25
+ failed?: (error: unknown) => ReactNode;
26
+ };
27
+ /**
28
+ * A complete form from a form definition: checks the definition, creates
29
+ * its controller, starts it and renders it with `@punica/components`.
30
+ *
31
+ * <JsonForm definition={definition} onSubmit={(values) => save(values)} />
32
+ */
33
+ export declare function JsonForm({ definition, options, loading, failed, ...view }: JsonFormProps): ReactNode;
@@ -1 +1 @@
1
- {"version":"0.2.0"}
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 { KitItem } from '../types';
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,18 +1,18 @@
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 './fields';
8
- export type { FieldComponent, FieldComponentProps } from './fields';
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. */
12
11
  onSubmit?: (values: E, controller: FormController<E, KitItem<E>>) => void | Promise<void>;
13
12
  /**
14
13
  * Called with what a validation, a control function or `onSubmit` threw.
15
- * Without it the error is logged to the console.
14
+ * Without it the error is logged to the console. What a submit throws is
15
+ * also shown above the fields.
16
16
  */
17
17
  onError?: (error: unknown) => void;
18
18
  /** The text of the submit button, `Submit` by default. */
@@ -23,6 +23,7 @@ type ViewProps<E extends object> = {
23
23
  components?: Record<string, FieldComponent>;
24
24
  };
25
25
  export type FormViewProps<E extends object> = ViewProps<E> & {
26
+ /** A started controller of a `@FormKit` class, which brings the services the view needs. */
26
27
  controller: FormController<E, KitItem<E>>;
27
28
  };
28
29
  /**
@@ -1,9 +1,15 @@
1
1
  import { ErrorDetail, FormItem } from '@punica/form';
2
- /** A choice of a select: its value, or its value and the text to show. */
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
- /** The choices of a select. */
34
- options?: Option[];
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,2 +1,5 @@
1
1
  export * from './useForm';
2
+ export * from './useController';
2
3
  export * from './useFormController';
4
+ export * from './useFormState';
5
+ export * from './useFieldArray';
@@ -1 +1 @@
1
- {"version":"0.1.1"}
1
+ {"version":"0.2.0-next.1"}
@@ -0,0 +1,15 @@
1
+ import { FormController, FormItem } from '@punica/form';
2
+ export type FormControllerState<E extends object, F extends FormItem<E>> = {
3
+ /** The started controller; `undefined` while the form is being started. */
4
+ controller?: FormController<E, F>;
5
+ /** What was thrown while the form was created or started. */
6
+ error?: unknown;
7
+ };
8
+ /**
9
+ * Creates a controller when the component mounts and starts it. Use it for
10
+ * forms that are not made from an entity, such as a form described as data;
11
+ * for a form class there is `useFormController`.
12
+ * @param create - Returns the controller, not started; read when the
13
+ * component mounts. Give the component a `key` to create another form
14
+ */
15
+ export declare const useController: <E extends object, F extends FormItem<E> = FormItem<E>>(create: () => FormController<E, F> | Promise<FormController<E, F>>) => FormControllerState<E, F>;
@@ -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>;
@@ -1,10 +1,5 @@
1
- import { FormController, FormControllerOptions, FormItem } from '@punica/form';
2
- export type FormControllerState<E extends object, F extends FormItem<E>> = {
3
- /** The started controller; `undefined` while the form is being started. */
4
- controller?: FormController<E, F>;
5
- /** What a reader, a starter or a service threw while the form was started. */
6
- error?: unknown;
7
- };
1
+ import { FormControllerOptions, FormItem } from '@punica/form';
2
+ import { FormControllerState } from './useController';
8
3
  /**
9
4
  * Creates the controller of a form when the component mounts and starts it.
10
5
  * @param createEntity - Returns the entity of the form, for example `() => new UserForm()`
@@ -0,0 +1,7 @@
1
+ import { FormController, FormItem, FormState } from '@punica/form';
2
+ /**
3
+ * The state of a form as a whole: dirty, touched, valid, validating, submitting, its
4
+ * submit count and its errors. Unlike `useForm`, the component only renders
5
+ * again when this state changes, not after every change of an item.
6
+ */
7
+ export declare const useFormState: <E extends object, F extends FormItem<E>>(controller: FormController<E, F>) => FormState;
@@ -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
+ }