@sincpro/web-framework 3.1.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/core/copy.d.ts +2 -0
  2. package/dist/core/copy.js +2 -0
  3. package/dist/core/list.d.ts +3 -0
  4. package/dist/react/context.d.ts +3 -0
  5. package/dist/react/context.js +5 -0
  6. package/dist/web/index.d.ts +4 -2
  7. package/dist/web/index.js +4 -2
  8. package/dist/web/list/EntityList.d.ts +4 -1
  9. package/dist/web/list/EntityList.js +21 -12
  10. package/dist/web/list/EntitySearchBar.d.ts +6 -2
  11. package/dist/web/list/EntitySearchBar.js +11 -3
  12. package/dist/web/list/columns.js +29 -2
  13. package/dist/web/list/presentation.d.ts +30 -0
  14. package/dist/web/list/presentation.js +24 -0
  15. package/dist/web/list/search.d.ts +14 -5
  16. package/dist/web/list/search.js +29 -14
  17. package/dist/web/record/ActionButton.d.ts +26 -4
  18. package/dist/web/record/ActionButton.js +62 -3
  19. package/dist/web/record/CommandButton.d.ts +13 -1
  20. package/dist/web/record/CommandButton.js +24 -5
  21. package/dist/web/record/ServiceButton.d.ts +22 -4
  22. package/dist/web/record/ServiceButton.js +31 -6
  23. package/dist/web/record/ask.d.ts +72 -0
  24. package/dist/web/record/ask.js +63 -0
  25. package/dist/web/record/control.d.ts +2 -2
  26. package/dist/web/record/control.js +7 -3
  27. package/dist/web/record/edit.d.ts +1 -1
  28. package/dist/web/record/edit.js +11 -3
  29. package/dist/web/record/lines.d.ts +4 -0
  30. package/dist/web/record/lines.js +18 -3
  31. package/dist/web/record/record.d.ts +25 -1
  32. package/dist/web/record/record.js +38 -4
  33. package/dist/web/workspace/composer.d.ts +7 -2
  34. package/dist/web/workspace/composer.js +24 -4
  35. package/dist/web/workspace/record.d.ts +1 -1
  36. package/package.json +3 -3
@@ -2,8 +2,50 @@
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
3
  import { frameworkCopy } from "../../core/copy.js";
4
4
  import { Refused } from "../Refused.js";
5
- import { Button, Dialog } from "@sincpro/web-ui";
5
+ import { Button, Dialog, Field, FormSection } from "@sincpro/web-ui";
6
6
  import { useState } from "react";
7
+ /**
8
+ * The words of an action's window. A button that opens one says so with «…» — «Agregar a un
9
+ * tenant…» —; the window is already open, so its title says it once, without them.
10
+ */
11
+ function wordsOf(label, dialog) {
12
+ return {
13
+ title: dialog?.title ?? label.replace(/\s*(?:…|\.\.\.)$/u, ""),
14
+ ...(dialog?.subtitle === undefined ? {} : { subtitle: dialog.subtitle }),
15
+ };
16
+ }
17
+ /** The fields of an action that asks, its refusal over them and what its dry run said. */
18
+ function AskDialog({ action, form, words, onClose, onDone, }) {
19
+ const copy = frameworkCopy().command;
20
+ const [values, setValues] = useState(() => ({ ...form.initial }));
21
+ const rehearsing = action.rehearse !== undefined;
22
+ const confirm = async () => {
23
+ if (rehearsing && action.rehearsal === null) {
24
+ await action.rehearse?.(values);
25
+ return;
26
+ }
27
+ const answer = await action.run(values);
28
+ if (answer !== undefined)
29
+ onDone(answer);
30
+ };
31
+ return (_jsxs(Dialog, { open: true, ...words, onClose: onClose, footer: _jsxs("div", { className: "flex justify-end gap-2", children: [_jsx(Button, { size: "sm", role: "secondary", onClick: onClose, children: copy.cancel }), _jsx(Button, { size: "sm", role: "primary", disabled: action.busy, onClick: () => void confirm(), children: rehearsing && action.rehearsal !== null ? copy.confirm : copy.send })] }), children: [action.error === null ? null : _jsx(Refused, { error: action.error }), _jsx(FormSection, { children: form.fields.map((field) => {
32
+ const control = {
33
+ value: values[field.name],
34
+ set: (next) => {
35
+ setValues((now) => ({ ...now, [field.name]: next }));
36
+ // What the dry run said was about the values before this one.
37
+ if (action.rehearsal !== null || action.error !== null)
38
+ action.reset();
39
+ },
40
+ readonly: false,
41
+ invalid: false,
42
+ label: field.label,
43
+ };
44
+ // A control that names itself is not wrapped in a `Field` (its name would double):
45
+ // what is expected goes under it.
46
+ return field.labelled ? (_jsxs("div", { className: "flex flex-col gap-1", children: [field.draw(control), field.hint === undefined ? null : (_jsx("p", { className: "text-meta text-muted", children: field.hint }))] }, field.name)) : (_jsx(Field, { label: field.label, ...(field.hint === undefined ? {} : { hint: field.hint }), children: field.draw(control) }, field.name));
47
+ }) }), action.rehearsal === null ? null : _jsx("p", { className: "text-ink", children: action.rehearsal.text })] }));
48
+ }
7
49
  /**
8
50
  * An action as a button — the gesture `CommandButton` and `ServiceButton` share.
9
51
  *
@@ -13,11 +55,28 @@ import { useState } from "react";
13
55
  * confirmation is the consequence, in the server's words, not a question. A refusal is shown
14
56
  * where it happened, with what to do. Off while it runs: a click twice is one action.
15
57
  *
58
+ * One that needs input (`ask`) opens its fields first, filled from the record, and sends them on
59
+ * «Confirmar»; when it can rehearse, the first «Confirmar» asks what it would do with them and
60
+ * the second does it. A refusal stays in the dialog over the fields, so they can be put right.
61
+ *
62
+ * Its window is titled by the label without its «…», and says more only when the screen does
63
+ * (`dialog`: its own title, a sentence under it).
64
+ *
16
65
  * Internal: the gesture is drawn here once; what runs behind is each button's own.
17
66
  */
18
- export function ActionButton({ action, label, role = "secondary", disabled = false, onDone, }) {
67
+ export function ActionButton({ action, label, role = "secondary", disabled = false, ask: form, dialog, onDone, }) {
19
68
  const copy = frameworkCopy().command;
20
69
  const [open, setOpen] = useState(false);
70
+ const words = wordsOf(label, dialog);
71
+ if (form !== undefined)
72
+ return (_jsxs(_Fragment, { children: [_jsx(Button, { size: "sm", role: role, disabled: disabled || action.busy, onClick: () => setOpen(true), children: label }), open ? (_jsx(AskDialog, { action: action, form: form, words: words, onClose: () => {
73
+ setOpen(false);
74
+ action.reset();
75
+ }, onDone: (answer) => {
76
+ setOpen(false);
77
+ action.reset();
78
+ onDone?.(answer);
79
+ } })) : null] }));
21
80
  const destructive = action.rehearse !== undefined;
22
81
  const run = async () => {
23
82
  const answer = await action.run();
@@ -37,5 +96,5 @@ export function ActionButton({ action, label, role = "secondary", disabled = fal
37
96
  setOpen(false);
38
97
  action.reset();
39
98
  };
40
- return (_jsxs(_Fragment, { children: [_jsx(Button, { size: "sm", role: destructive ? "secondary" : role, disabled: disabled || action.busy, onClick: () => void (destructive ? ask() : run()), children: label }), open ? (_jsx(Dialog, { open: true, title: label, onClose: close, footer: destructive && action.error === null ? (_jsxs("div", { className: "flex justify-end gap-2", children: [_jsx(Button, { size: "sm", role: "secondary", onClick: close, children: copy.cancel }), _jsx(Button, { size: "sm", role: "primary", disabled: action.busy || action.rehearsal === null, onClick: () => void run(), children: copy.confirm })] })) : undefined, children: action.error !== null ? (_jsx(Refused, { error: action.error })) : action.rehearsal === null ? (_jsx("p", { className: "text-meta text-muted", children: copy.rehearsing })) : (_jsx("p", { className: "text-ink", children: action.rehearsal.text })) })) : null] }));
99
+ return (_jsxs(_Fragment, { children: [_jsx(Button, { size: "sm", role: destructive ? "secondary" : role, disabled: disabled || action.busy, onClick: () => void (destructive ? ask() : run()), children: label }), open ? (_jsx(Dialog, { open: true, ...words, onClose: close, footer: destructive && action.error === null ? (_jsxs("div", { className: "flex justify-end gap-2", children: [_jsx(Button, { size: "sm", role: "secondary", onClick: close, children: copy.cancel }), _jsx(Button, { size: "sm", role: "primary", disabled: action.busy || action.rehearsal === null, onClick: () => void run(), children: copy.confirm })] })) : undefined, children: action.error !== null ? (_jsx(Refused, { error: action.error })) : action.rehearsal === null ? (_jsx("p", { className: "text-meta text-muted", children: copy.rehearsing })) : (_jsx("p", { className: "text-ink", children: action.rehearsal.text })) })) : null] }));
41
100
  }
@@ -1,6 +1,8 @@
1
1
  import { type CommandAnswers, type CommandBodies, type Entity } from "@sincpro/client-framework";
2
2
  import type { AnyRecord } from "@sincpro/criteria";
3
3
  import { type AnswerOf } from "../../react/useCommand.js";
4
+ import { type ActionDialog } from "./ActionButton.js";
5
+ import { type AskField } from "./ask.js";
4
6
  /**
5
7
  * An entity's Command as a button: sent as it is, over the screens' transport.
6
8
  *
@@ -8,8 +10,12 @@ import { type AnswerOf } from "../../react/useCommand.js";
8
10
  * server's dry run first and show what it would do; a refusal where it happened. An action that is
9
11
  * one of the application's own use cases — logic of its own, composed, traced as
10
12
  * `<context>/<Service>` — is a `ServiceButton`.
13
+ *
14
+ * One that needs input names the fields it asks for (`ask`): a dialog draws them as the
15
+ * record's fields of the same name, filled from `record`, and what is filled goes in the body
16
+ * beside `body`.
11
17
  */
12
- export declare function CommandButton<T extends AnyRecord, R = unknown, C extends CommandBodies = CommandBodies, N extends keyof C & string = keyof C & string, A extends CommandAnswers = CommandAnswers>({ entity, command, id, body, label, destructive, role, disabled, onDone, }: {
18
+ export declare function CommandButton<T extends AnyRecord, R = unknown, C extends CommandBodies = CommandBodies, N extends keyof C & string = keyof C & string, A extends CommandAnswers = CommandAnswers>({ entity, command, id, body, label, destructive, role, disabled, ask, record, dialog, onDone, }: {
13
19
  entity: Entity<T, C, A>;
14
20
  /** One the entity declares; a typed entity takes only its own names. */
15
21
  command: N;
@@ -22,5 +28,11 @@ export declare function CommandButton<T extends AnyRecord, R = unknown, C extend
22
28
  role?: "primary" | "secondary" | "ghost";
23
29
  /** Not offered right now — «Mover» with no other tenant chosen. The server still decides. */
24
30
  disabled?: boolean;
31
+ /** The fields it asks for before it runs, sent in its body: `["areas"]`. */
32
+ ask?: readonly AskField[];
33
+ /** The record it acts on: what the asked fields start with. */
34
+ record?: AnyRecord;
35
+ /** What its window says — its title (the label without «…» by default), a sentence under it. */
36
+ dialog?: ActionDialog;
25
37
  onDone?: (answer: AnswerOf<R, A, N>) => void;
26
38
  }): import("react").JSX.Element;
@@ -1,8 +1,11 @@
1
1
  "use client";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
3
  import {} from "@sincpro/client-framework";
4
+ import { entityFor } from "../../core/relation.js";
5
+ import { useFramework } from "../../react/context.js";
4
6
  import { useCommand } from "../../react/useCommand.js";
5
- import { ActionButton } from "./ActionButton.js";
7
+ import { ActionButton, } from "./ActionButton.js";
8
+ import { askFormOf } from "./ask.js";
6
9
  /**
7
10
  * An entity's Command as a button: sent as it is, over the screens' transport.
8
11
  *
@@ -10,12 +13,28 @@ import { ActionButton } from "./ActionButton.js";
10
13
  * server's dry run first and show what it would do; a refusal where it happened. An action that is
11
14
  * one of the application's own use cases — logic of its own, composed, traced as
12
15
  * `<context>/<Service>` — is a `ServiceButton`.
16
+ *
17
+ * One that needs input names the fields it asks for (`ask`): a dialog draws them as the
18
+ * record's fields of the same name, filled from `record`, and what is filled goes in the body
19
+ * beside `body`.
13
20
  */
14
- export function CommandButton({ entity, command, id, body, label, destructive = false, role = "secondary", disabled = false, onDone, }) {
21
+ export function CommandButton({ entity, command, id, body, label, destructive = false, role = "secondary", disabled = false, ask, record, dialog, onDone, }) {
15
22
  const sending = useCommand(entity, command);
16
- return (_jsx(ActionButton, { action: {
17
- run: () => sending.run(id, body),
18
- ...(destructive ? { rehearse: () => sending.rehearse(id, body) } : {}),
23
+ const { metas, entities, locale } = useFramework();
24
+ const form = ask === undefined
25
+ ? undefined
26
+ : askFormOf(ask, {
27
+ meta: metas.get(entity.key),
28
+ locale,
29
+ record,
30
+ entityFor: (target) => entityFor(entities, target, metas.get),
31
+ });
32
+ const bodyWith = (asked) => (asked === undefined ? body : { ...body, ...asked });
33
+ return (_jsx(ActionButton, { ...(form === undefined ? {} : { ask: form }), ...(dialog === undefined ? {} : { dialog }), action: {
34
+ run: (asked) => sending.run(id, bodyWith(asked)),
35
+ ...(destructive
36
+ ? { rehearse: (asked) => sending.rehearse(id, bodyWith(asked)) }
37
+ : {}),
19
38
  busy: sending.busy,
20
39
  error: sending.error,
21
40
  rehearsal: sending.rehearsal,
@@ -1,4 +1,8 @@
1
+ import type { Entity } from "@sincpro/client-framework";
2
+ import type { AnyRecord } from "@sincpro/criteria";
1
3
  import type { Rehearsal } from "../../react/useCommand.js";
4
+ import { type ActionDialog, type Asked } from "./ActionButton.js";
5
+ import { type AskField } from "./ask.js";
2
6
  /**
3
7
  * One of the application's use cases as a button — its Feature or ApplicationService, run on its
4
8
  * bounded context.
@@ -17,15 +21,29 @@ import type { Rehearsal } from "../../react/useCommand.js";
17
21
  * and shows what it would do. What it changed, every screen hears by itself: the use case's
18
22
  * client announces its write (`processChanges`), so `onDone` is for what only this screen does
19
23
  * next — a toast, closing a window —, never to read again.
24
+ *
25
+ * A use case that needs input names the fields it asks for (`ask`), read as `entity`'s fields of
26
+ * the same name and filled from `record`; `run` and `rehearse` receive what was filled.
27
+ *
28
+ * <ServiceButton label="Delegar áreas" entity={cuentas} record={cuenta} ask={["areas"]}
29
+ * run={(asked) => cuentaBoundedContext.execute(new CommandDelegarAreas(cuenta.id, asked.areas))} />
20
30
  */
21
- export declare function ServiceButton<R>({ label, run, rehearse, role, disabled, onDone, }: {
31
+ export declare function ServiceButton<R>({ label, run, rehearse, role, disabled, ask, entity, record, dialog, onDone, }: {
22
32
  label: string;
23
- /** The use case, executed on its bounded context. */
24
- run: () => Promise<R>;
33
+ /** The use case, executed on its bounded context — with what was asked, when it asks. */
34
+ run: (asked: Asked) => Promise<R>;
25
35
  /** The same use case run dry — what it would do, nothing touched —, asked before `run`. */
26
- rehearse?: () => Promise<Rehearsal>;
36
+ rehearse?: (asked: Asked) => Promise<Rehearsal>;
27
37
  role?: "primary" | "secondary" | "ghost";
28
38
  /** Not offered right now. The use case still decides. */
29
39
  disabled?: boolean;
40
+ /** The fields it asks for before it runs: `["areas"]`. */
41
+ ask?: readonly AskField[];
42
+ /** Whose fields `ask` names: their labels, choices and controls. */
43
+ entity?: Entity;
44
+ /** The record it acts on: what the asked fields start with. */
45
+ record?: AnyRecord;
46
+ /** What its window says — its title (the label without «…» by default), a sentence under it. */
47
+ dialog?: ActionDialog;
30
48
  onDone?: (answer: R) => void;
31
49
  }): import("react").JSX.Element;
@@ -1,9 +1,14 @@
1
1
  "use client";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { entityFor } from "../../core/relation.js";
4
+ import { useFrameworkIfAny } from "../../react/context.js";
3
5
  import { useService } from "../../react/useService.js";
4
- import { ActionButton } from "./ActionButton.js";
6
+ import { ActionButton, } from "./ActionButton.js";
7
+ import { askFormOf } from "./ask.js";
5
8
  /** What the button runs when there is no dry run: nothing. */
6
- const NO_REHEARSAL = async () => undefined;
9
+ const NO_REHEARSAL = async (_asked) => undefined;
10
+ /** What a use case that asks nothing receives. */
11
+ const NOTHING_ASKED = {};
7
12
  /**
8
13
  * One of the application's use cases as a button — its Feature or ApplicationService, run on its
9
14
  * bounded context.
@@ -22,11 +27,31 @@ const NO_REHEARSAL = async () => undefined;
22
27
  * and shows what it would do. What it changed, every screen hears by itself: the use case's
23
28
  * client announces its write (`processChanges`), so `onDone` is for what only this screen does
24
29
  * next — a toast, closing a window —, never to read again.
30
+ *
31
+ * A use case that needs input names the fields it asks for (`ask`), read as `entity`'s fields of
32
+ * the same name and filled from `record`; `run` and `rehearse` receive what was filled.
33
+ *
34
+ * <ServiceButton label="Delegar áreas" entity={cuentas} record={cuenta} ask={["areas"]}
35
+ * run={(asked) => cuentaBoundedContext.execute(new CommandDelegarAreas(cuenta.id, asked.areas))} />
25
36
  */
26
- export function ServiceButton({ label, run, rehearse, role = "secondary", disabled = false, onDone, }) {
27
- const running = useService(run);
28
- const rehearsing = useService(rehearse ?? NO_REHEARSAL);
29
- return (_jsx(ActionButton, { action: {
37
+ export function ServiceButton({ label, run, rehearse, role = "secondary", disabled = false, ask, entity, record, dialog, onDone, }) {
38
+ const running = useService((asked) => run(asked ?? NOTHING_ASKED));
39
+ const rehearsing = useService((asked) => (rehearse ?? NO_REHEARSAL)(asked ?? NOTHING_ASKED));
40
+ // Outside a provider it still runs: the asked fields are then drawn by what `ask` says.
41
+ const framework = useFrameworkIfAny();
42
+ const form = ask === undefined
43
+ ? undefined
44
+ : askFormOf(ask, {
45
+ meta: entity === undefined ? undefined : framework?.metas.get(entity.key),
46
+ locale: framework?.locale,
47
+ record,
48
+ ...(framework === null
49
+ ? {}
50
+ : {
51
+ entityFor: (target) => entityFor(framework.entities, target, framework.metas.get),
52
+ }),
53
+ });
54
+ return (_jsx(ActionButton, { ...(form === undefined ? {} : { ask: form }), ...(dialog === undefined ? {} : { dialog }), action: {
30
55
  run: running.run,
31
56
  ...(rehearse === undefined ? {} : { rehearse: rehearsing.run }),
32
57
  busy: running.busy || rehearsing.busy,
@@ -0,0 +1,72 @@
1
+ import type { Entity } from "@sincpro/client-framework";
2
+ import { type AnyRecord, type Meta } from "@sincpro/criteria";
3
+ import { type FieldControl } from "./control.js";
4
+ import type { ReactNode } from "react";
5
+ /**
6
+ * A field an action asks for before it runs: a field of the entity by its name — `"areas"` —,
7
+ * or an object that says more about it — its starting value, a hint, its own control —, or
8
+ * describes one the entity does not have.
9
+ *
10
+ * <CommandButton entity={cuentas} command="delegar_areas" id={cuenta.id} record={cuenta}
11
+ * label="Delegar áreas…" ask={["areas"]} />
12
+ * ask={[{ name: "motivo", label: "Motivo", widget: "textarea" }]}
13
+ * ask={[{ name: "role", label: "Rol", choices: ROLES, initial: "member", hint: "Lo que podrá hacer" }]}
14
+ *
15
+ * Context: a Command that needs input could not live among a record's actions, so a screen drew
16
+ * its fields in the record's body with a button of their own. What the dialog shows is the
17
+ * screen's: the backend adds nothing for it. A field is read as the entity's field of that name
18
+ * when it has one — its label, its choices in their words, its control, its help — and what the
19
+ * object says goes OVER that, key by key: `{ name: "areas", hint }` is the entity's field with a
20
+ * hint, `{ name: "motivo", label, widget }` a field the entity lacks. A control of the
21
+ * application's goes in with `as`, never a widget named in a string (`widget` takes only the
22
+ * framework's own).
23
+ */
24
+ export type AskField = string | {
25
+ name: string;
26
+ /** Its word on screen — the entity's label for a field of that name when not given. */
27
+ label?: string;
28
+ /** The type of its value as the model writes it — `text` (the default), `number`, `date`,
29
+ * `boolean`, `text[]`. */
30
+ type?: string;
31
+ /** The values it takes, each with its word: a select, or checks for a `text[]`. */
32
+ choices?: readonly {
33
+ value: unknown;
34
+ label: string;
35
+ }[];
36
+ /** One of the framework's own widgets: `textarea`, several lines of prose. */
37
+ widget?: "textarea";
38
+ /** The application's own control. */
39
+ as?: (control: FieldControl) => ReactNode;
40
+ /** What it starts with when the record holds nothing under its name: a role that starts at
41
+ * «Miembro». What the record holds still wins — the action starts from the record. */
42
+ initial?: unknown;
43
+ /** What is expected, said by the field — over the backend's own help for it. */
44
+ hint?: string;
45
+ };
46
+ /** A field ready to draw: its name, its label, its control. */
47
+ export type AskedField = {
48
+ name: string;
49
+ label: string;
50
+ /** What is expected — the action's hint, or the model's help for the field. */
51
+ hint?: string | undefined;
52
+ /** It draws its own label — a yes/no, a list of checks —: not wrapped in a `Field`. */
53
+ labelled: boolean;
54
+ draw: (control: FieldControl) => ReactNode;
55
+ };
56
+ /** What a form of asked fields needs to be drawn and filled. */
57
+ export type AskForm = {
58
+ fields: readonly AskedField[];
59
+ initial: Readonly<Record<string, unknown>>;
60
+ };
61
+ /**
62
+ * The asked fields, drawn from the entity's model with what each says over it, and what they
63
+ * start with: the record's own values, else the field's `initial`. Its key is never asked by
64
+ * name — the action already knows whose record it is.
65
+ */
66
+ export declare function askFormOf(fields: readonly AskField[], options: {
67
+ meta: Meta | null | undefined;
68
+ locale?: string | undefined;
69
+ record?: AnyRecord | undefined;
70
+ /** The declared entity a relation points at, to choose it with its picker. */
71
+ entityFor?: (target: string) => Entity | undefined;
72
+ }): AskForm;
@@ -0,0 +1,63 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { choicesOf } from "@sincpro/criteria";
4
+ import { fieldMetaOf, viewOf } from "../../core/present.js";
5
+ import { relationFieldOf } from "../../core/relation.js";
6
+ import { controlFor, RelationPicker, } from "./control.js";
7
+ /** Whether a field's control names itself: a checkbox and a list of checks carry their label. */
8
+ function labels(type, choices) {
9
+ return type === "boolean" || (type.endsWith("[]") && choices.length > 0);
10
+ }
11
+ /**
12
+ * The asked fields, drawn from the entity's model with what each says over it, and what they
13
+ * start with: the record's own values, else the field's `initial`. Its key is never asked by
14
+ * name — the action already knows whose record it is.
15
+ */
16
+ export function askFormOf(fields, options) {
17
+ const { meta, locale, record } = options;
18
+ const key = meta?.get_id || meta?.identity || "id";
19
+ const asked = [];
20
+ const initial = {};
21
+ for (const one of fields) {
22
+ if (typeof one === "string" && (one === key || one === "id"))
23
+ continue;
24
+ const said = typeof one === "string" ? { name: one } : one;
25
+ const field = fieldMetaOf(meta, said.name);
26
+ // A field of the entity is read from the model; one it lacks only as the action says.
27
+ const view = field === undefined ? undefined : viewOf(meta, said.name, locale);
28
+ const relation = field === undefined ? undefined : relationFieldOf(meta, said.name);
29
+ // A many2one is chosen by its key, with the picker of the entity it points at — unless the
30
+ // action says how it is written: then it writes the name it gave.
31
+ const target = relation === undefined ||
32
+ relation.many ||
33
+ said.as !== undefined ||
34
+ said.type !== undefined ||
35
+ said.choices !== undefined
36
+ ? undefined
37
+ : options.entityFor?.(relation.target);
38
+ const name = target === undefined || relation === undefined ? said.name : relation.key;
39
+ const type = said.type ?? view?.type ?? "text";
40
+ const choices = said.choices ?? (field === undefined ? [] : choicesOf(meta, said.name, locale));
41
+ const widget = said.widget ?? field?.widget;
42
+ const label = said.label ?? view?.label ?? said.name;
43
+ const hint = said.hint ?? view?.help;
44
+ asked.push({
45
+ name,
46
+ label,
47
+ hint,
48
+ labelled: said.as === undefined && target === undefined && labels(type, [...choices]),
49
+ draw: said.as ??
50
+ (target === undefined
51
+ ? (control) => controlFor(control, type, field?.exact === true, [...choices], widget)
52
+ : (control) => (_jsx(RelationPicker, { target: target, value: control.value === null || control.value === undefined
53
+ ? ""
54
+ : String(control.value), onChange: (id) => control.set(id), label: control.label, onClear: () => control.set(null) }))),
55
+ });
56
+ // What the record holds wins; a field it leaves empty starts at what the action says.
57
+ const held = record?.[name];
58
+ const start = held === undefined || held === null ? (said.initial ?? held) : held;
59
+ if (start !== undefined)
60
+ initial[name] = start;
61
+ }
62
+ return { fields: asked, initial };
63
+ }
@@ -13,8 +13,8 @@ export type FieldControl = {
13
13
  standalone?: boolean;
14
14
  };
15
15
  /**
16
- * The control a field's TYPE asks for — a choice, a number, a date, a date and time, a yes/no, a
17
- * list of words, a text — or the one its `widget` names when the model names one of the
16
+ * The control a field's TYPE asks for — a choice, several of a choice (checks), a number, a
17
+ * date, a date and time, a yes/no, a list of words, a text — or the one its `widget` names when the model names one of the
18
18
  * framework's own: `textarea`, several lines of prose.
19
19
  * Shared by a record's field (`RecordField`) and a line's cell and dialog (`RecordLinesEditor`):
20
20
  * one rule for what a value is edited with.
@@ -2,10 +2,10 @@
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
3
  import { dayOf, instantOf, timeZoneNow, wallTimeOf, } from "@sincpro/client-framework";
4
4
  import { EntityPicker } from "./EntityPicker.js";
5
- import { Checkbox, DatePicker, DateTimePicker, NumberInput, Select, TagsField, TextArea, TextInput, } from "@sincpro/web-ui";
5
+ import { Checkbox, CheckList, DatePicker, DateTimePicker, NumberInput, Select, TagsField, TextArea, TextInput, } from "@sincpro/web-ui";
6
6
  /**
7
- * The control a field's TYPE asks for — a choice, a number, a date, a date and time, a yes/no, a
8
- * list of words, a text — or the one its `widget` names when the model names one of the
7
+ * The control a field's TYPE asks for — a choice, several of a choice (checks), a number, a
8
+ * date, a date and time, a yes/no, a list of words, a text — or the one its `widget` names when the model names one of the
9
9
  * framework's own: `textarea`, several lines of prose.
10
10
  * Shared by a record's field (`RecordField`) and a line's cell and dialog (`RecordLinesEditor`):
11
11
  * one rule for what a value is edited with.
@@ -19,6 +19,10 @@ import { Checkbox, DatePicker, DateTimePicker, NumberInput, Select, TagsField, T
19
19
  export function controlFor(control, type, exact, choices, widget) {
20
20
  const { value, set } = control;
21
21
  const named = control.standalone === true ? { "aria-label": control.label } : {};
22
+ // Several of the values at once — a list whose values are a choice: each one a check, the
23
+ // list's label above them.
24
+ if (choices.length > 0 && type.endsWith("[]"))
25
+ return (_jsx(CheckList, { label: control.label, items: choices.map((one) => ({ value: String(one.value), label: one.label })), value: Array.isArray(value) ? value.map(String) : [], onChange: set }));
22
26
  if (choices.length > 0)
23
27
  return (_jsx(Select, { value: value === null || value === undefined ? "" : String(value), options: choices.map((one) => ({ value: String(one.value), label: one.label })), onChange: set, ...named }));
24
28
  switch (type) {
@@ -47,7 +47,7 @@ export declare function RecordField<T extends Record<string, unknown>>({ record,
47
47
  /** A one2many of a declared entity, the screen's way: anything `EntityList` takes — its
48
48
  * cells, row actions, commands, faces of its own. What it passes wins over the defaults. */
49
49
  list?: Omit<EntityListProps<AnyRecord>, "listing" | "embedded">;
50
- }): import("react").JSX.Element | null;
50
+ }): string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | import("react").JSX.Element | null | undefined;
51
51
  /**
52
52
  * The record's «save / discard», for `<Record dirty={…}>`: present only while something changed,
53
53
  * off while saving, and off with the reason while a required field is empty.
@@ -108,14 +108,14 @@ export function RecordField({ record, name, as, bare = false, lines: look, list:
108
108
  // A page of them stays read only, as a one2many's: written back whole it would drop what
109
109
  // it did not bring.
110
110
  const whole = editableLines(edit.values[name]);
111
- const lines = whole === null ? (_jsx(RecordLines, { record: record.record ?? {}, meta: meta, name: name, locale: locale })) : (_jsx(RecordLinesEditor, { record: record, name: name, lines: whole, ...(look === undefined ? {} : { look }) }));
111
+ const lines = whole === null ? (_jsx(RecordLines, { record: record.record ?? {}, meta: meta, name: name, entity: record.entity, locale: locale, ...(look?.columns === undefined ? {} : { columns: look.columns }), ...(look?.tones === undefined ? {} : { tones: look.tones }) })) : (_jsx(RecordLinesEditor, { record: record, name: name, lines: whole, ...(look === undefined ? {} : { look }) }));
112
112
  return bare ? lines : _jsx(FormSection, { title: view.label, children: lines });
113
113
  }
114
114
  if (as === undefined && relation?.field.type === "one2many" && target === undefined) {
115
115
  // Editable when the record holds ALL its lines; a page of them stays read only, and so do
116
116
  // the records it only references — nothing it saves writes them.
117
117
  const whole = editableLines(edit.values[name], relation.field);
118
- const lines = whole === null ? (_jsx(RecordLines, { record: record.record ?? {}, meta: meta, name: name, locale: locale })) : (_jsx(RecordLinesEditor, { record: record, name: name, lines: whole, ...(look === undefined ? {} : { look }) }));
118
+ const lines = whole === null ? (_jsx(RecordLines, { record: record.record ?? {}, meta: meta, name: name, entity: record.entity, locale: locale, ...(look?.columns === undefined ? {} : { columns: look.columns }), ...(look?.tones === undefined ? {} : { tones: look.tones }) })) : (_jsx(RecordLinesEditor, { record: record, name: name, lines: whole, ...(look === undefined ? {} : { look }) }));
119
119
  return bare ? lines : _jsx(FormSection, { title: view.label, children: lines });
120
120
  }
121
121
  if (as === undefined && relation?.field.type === "one2many" && target !== undefined) {
@@ -181,9 +181,17 @@ export function RecordField({ record, name, as, bare = false, lines: look, list:
181
181
  },
182
182
  }) }));
183
183
  }
184
+ // A list of checks names itself, as a checkbox does: inside a `Field` it would read twice.
185
+ const choices = choicesOf(place.meta, place.name, locale);
186
+ if (as === undefined &&
187
+ !state.readonly &&
188
+ view.type.endsWith("[]") &&
189
+ choices.length > 0 &&
190
+ relation === undefined)
191
+ return controlFor(control, view.type, false, choices, field?.widget);
184
192
  if (view.type === "boolean" && as === undefined)
185
193
  return (_jsx(Checkbox, { checked: value === true, onChange: control.set, label: view.label, disabled: state.readonly }));
186
- return (_jsx(Field, { label: view.label, required: state.required && !state.readonly, ...(view.help === undefined ? {} : { hint: view.help }), ...(error === undefined ? {} : { error }), children: as !== undefined ? (as(control)) : relation !== undefined && !relation.many && target !== undefined ? (manyToOne()) : state.readonly ? (_jsx(ReadValue, { value: textOf(meta, name, value, locale) ?? "—" })) : (controlFor(control, view.type, field?.exact === true, choicesOf(place.meta, place.name, locale), field?.widget)) }));
194
+ return (_jsx(Field, { label: view.label, required: state.required && !state.readonly, ...(view.help === undefined ? {} : { hint: view.help }), ...(error === undefined ? {} : { error }), children: as !== undefined ? (as(control)) : relation !== undefined && !relation.many && target !== undefined ? (manyToOne()) : state.readonly ? (_jsx(ReadValue, { value: textOf(meta, name, value, locale) ?? "—" })) : (controlFor(control, view.type, field?.exact === true, choices, field?.widget)) }));
187
195
  }
188
196
  /**
189
197
  * The record's «save / discard», for `<Record dirty={…}>`: present only while something changed,
@@ -1,6 +1,7 @@
1
1
  import { type AnyRecord } from "@sincpro/criteria";
2
2
  import { type LineAdding } from "../../core/lines.js";
3
3
  import type { RecordReading } from "../../react/useRecord.js";
4
+ import type { ChoiceTones } from "../list/presentation.js";
4
5
  import { type FieldControl } from "./control.js";
5
6
  import { type LineEditorState } from "@sincpro/web-ui";
6
7
  import { type ReactNode } from "react";
@@ -39,6 +40,9 @@ export type LinesLook = {
39
40
  columns?: readonly string[];
40
41
  /** A field's cell drawn the screen's way, while it reads. */
41
42
  cells?: Readonly<Partial<Record<string, (line: AnyRecord) => ReactNode>>>;
43
+ /** The colour of each value of a choice, read as a tag — over what the record's entity
44
+ * declared for these lines (`presented(…, { parts })`). */
45
+ tones?: ChoiceTones<AnyRecord>;
42
46
  /** A field written with the screen's own widget — in its cell and in the dialog. */
43
47
  controls?: Readonly<Partial<Record<string, (control: LineControl) => ReactNode>>>;
44
48
  /** Columns that are not fields, after the fields. */
@@ -8,7 +8,8 @@ import { entityFor, relationFieldOf, } from "../../core/relation.js";
8
8
  import { useEntityFor, useFramework } from "../../react/context.js";
9
9
  import { controlFor, RelationPicker, } from "./control.js";
10
10
  import { RecordOpeningProvider, useRecordOpening, } from "./opening.js";
11
- import { Field, LineItems, ReadValue, } from "@sincpro/web-ui";
11
+ import { usePartLook } from "./record.js";
12
+ import { Field, LineItems, ReadValue, Tag, } from "@sincpro/web-ui";
12
13
  import { useRef } from "react";
13
14
  /** How a field of a line reads: a many2one's key by the name of what it points at. */
14
15
  function textIn(definition, field, relation, line, locale) {
@@ -105,7 +106,13 @@ export function RecordLinesEditor({ record, name, lines, look = {}, }) {
105
106
  ? (definition?.identity ?? "")
106
107
  : definition?.identity || "id";
107
108
  const fields = lineFields(definition);
108
- const shown = look.columns === undefined ? fields : look.columns.filter((one) => fields.includes(one));
109
+ // What the record's entity declared for these lines, under what the screen says.
110
+ const part = usePartLook(meta, name, {
111
+ entity: record.entity,
112
+ columns: look.columns,
113
+ tones: look.tones,
114
+ });
115
+ const shown = part.columns === undefined ? fields : part.columns.filter((one) => fields.includes(one));
109
116
  const controls = look.controls ?? {};
110
117
  const own = new Set(Object.keys(controls));
111
118
  const declared = (target) => entityFor(entities, target, metas.get) !== undefined;
@@ -139,12 +146,20 @@ export function RecordLinesEditor({ record, name, lines, look = {}, }) {
139
146
  definition.fields[field]?.readonly !== true &&
140
147
  (own.has(field) || writtenInCell(definition, field, declared));
141
148
  const ownCell = look.cells?.[field];
149
+ const tones = part.tones?.[field];
150
+ const toned = tones !== undefined && (definition?.fields[field]?.choices?.length ?? 0) > 0
151
+ ? (line) => {
152
+ const word = textIn(definition, field, relation, line, locale);
153
+ return word === "" ? null : (_jsx(Tag, { tone: tones[String(line[field])] ?? "neutral", children: word }));
154
+ }
155
+ : undefined;
142
156
  return {
143
157
  id: field,
144
158
  header: view.label,
145
159
  value: (line) => line[field],
146
160
  // Drawn as read: «Cemento 50 kg», not «CEM-50»; «1.552,00», not «1552.00».
147
- cell: ownCell ?? ((line) => textIn(definition, field, relation, line, locale)),
161
+ // A choice the record's entity coloured reads as its tag, as in a list.
162
+ cell: ownCell ?? toned ?? ((line) => textIn(definition, field, relation, line, locale)),
148
163
  text: (line) => textIn(definition, field, relation, line, locale),
149
164
  numeric: view.type === "number" || view.type === "integer",
150
165
  ...(inCell
@@ -1,4 +1,6 @@
1
+ import type { Entity } from "@sincpro/client-framework";
1
2
  import { type AnyRecord, type Meta } from "@sincpro/criteria";
3
+ import { type ChoiceTones, type PartLook } from "../list/presentation.js";
2
4
  import { type RowAction, type SheetField } from "@sincpro/web-ui";
3
5
  import type { ReactNode } from "react";
4
6
  export declare function factsOf<T extends AnyRecord>(record: T, meta: Meta | null | undefined, names: readonly string[], options?: {
@@ -7,6 +9,19 @@ export declare function factsOf<T extends AnyRecord>(record: T, meta: Meta | nul
7
9
  }): SheetField[];
8
10
  /** How many records a relation of this record holds — the TOTAL, not the page it brought. */
9
11
  export declare function countOf(record: AnyRecord, name: string): number;
12
+ /**
13
+ * How the rows of `name` look, layer over layer: the related entity's own presentation (when the
14
+ * relation points at a declared one), the parts the record's entity declared
15
+ * (`presented(issues, { parts: { runs: … } })`), and what the screen passes — each over the one
16
+ * before, so a screen changes one colour and keeps the rest.
17
+ *
18
+ * The record's entity is the one given, or the provider's entity of the model's aggregate.
19
+ */
20
+ export declare function usePartLook<T extends AnyRecord>(meta: Meta | null | undefined, name: string, own: {
21
+ entity?: Entity | undefined;
22
+ columns?: readonly string[] | undefined;
23
+ tones?: ChoiceTones<T> | undefined;
24
+ }): PartLook<T>;
10
25
  /**
11
26
  * A one2many / many2many of the record, as an embedded list.
12
27
  *
@@ -14,6 +29,10 @@ export declare function countOf(record: AnyRecord, name: string): number;
14
29
  * read off the rows otherwise. When the record brought only one page of them, it says so —
15
30
  * «se muestran 50 de 1000» — instead of drawing 50 as if they were all.
16
31
  *
32
+ * A choice of a row — a run's state, a member's role — is a tag in the colour declared ONCE
33
+ * with the record's entity (`presented(issues, { parts: { runs: { tones } } })`); `tones` here
34
+ * changes it for this screen, value by value.
35
+ *
17
36
  * `rowActions` acts on a part from its row — a group's member, a project's share — with a
18
37
  * Command of the record (`useCommand(projects, "unshareProject")`): the parts have no route of
19
38
  * their own, so what removes one is the record's, and the screen names it.
@@ -22,12 +41,17 @@ export declare function countOf(record: AnyRecord, name: string): number;
22
41
  * rowActions={(share) => [{ id: "unshare", label: "Quitar", icon: X, tone: "danger",
23
42
  * onSelect: () => unshare.run(project.id, { tenant_id: share.tenant_id }) }]} />
24
43
  */
25
- export declare function RecordLines<T extends AnyRecord>({ record, meta, name, columns, cells, locale, onOpen, rowActions, }: {
44
+ export declare function RecordLines<T extends AnyRecord>({ record, meta, name, entity, columns, cells, tones, locale, onOpen, rowActions, }: {
26
45
  record: AnyRecord;
27
46
  meta: Meta | null | undefined;
28
47
  name: string;
48
+ /** The record's entity, whose presentation says how these rows look (`parts`). Without it,
49
+ * the provider's entity of the model's aggregate. */
50
+ entity?: Entity | undefined;
29
51
  columns?: readonly string[];
30
52
  cells?: Readonly<Partial<Record<string, (row: T) => ReactNode>>>;
53
+ /** The colour of each value of a choice of a row, over what the entities declared. */
54
+ tones?: ChoiceTones<T> | undefined;
31
55
  locale?: string | undefined;
32
56
  onOpen?: (row: T) => void;
33
57
  /** What a row's part can be acted on with — a Command of the record. */