@sincpro/web-framework 3.0.0 → 3.2.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 (62) hide show
  1. package/README.md +29 -0
  2. package/dist/core/copy.d.ts +13 -1
  3. package/dist/core/copy.js +30 -0
  4. package/dist/core/explain.d.ts +12 -4
  5. package/dist/core/explain.js +18 -6
  6. package/dist/core/form.d.ts +7 -3
  7. package/dist/core/form.js +11 -5
  8. package/dist/core/groups.d.ts +17 -0
  9. package/dist/core/groups.js +31 -0
  10. package/dist/core/history.d.ts +41 -4
  11. package/dist/core/history.js +79 -12
  12. package/dist/core/index.d.ts +4 -3
  13. package/dist/core/index.js +4 -3
  14. package/dist/core/list.d.ts +3 -0
  15. package/dist/core/measure.d.ts +14 -5
  16. package/dist/core/measure.js +32 -7
  17. package/dist/core/present.d.ts +51 -3
  18. package/dist/core/present.js +162 -15
  19. package/dist/core/summary.d.ts +6 -2
  20. package/dist/react/context.d.ts +26 -2
  21. package/dist/react/context.js +56 -2
  22. package/dist/react/useGroups.js +15 -4
  23. package/dist/react/useHistory.d.ts +4 -2
  24. package/dist/react/useHistory.js +20 -5
  25. package/dist/react/usePivot.js +18 -3
  26. package/dist/react/useRecord.d.ts +3 -0
  27. package/dist/react/useRecord.js +14 -2
  28. package/dist/web/Refused.d.ts +3 -3
  29. package/dist/web/Refused.js +3 -3
  30. package/dist/web/index.d.ts +3 -2
  31. package/dist/web/index.js +2 -1
  32. package/dist/web/list/EntityList.d.ts +4 -1
  33. package/dist/web/list/EntityList.js +69 -21
  34. package/dist/web/list/EntitySearchBar.d.ts +6 -2
  35. package/dist/web/list/EntitySearchBar.js +11 -3
  36. package/dist/web/list/columns.d.ts +6 -1
  37. package/dist/web/list/columns.js +81 -9
  38. package/dist/web/list/presentation.d.ts +20 -1
  39. package/dist/web/list/search.d.ts +14 -5
  40. package/dist/web/list/search.js +29 -14
  41. package/dist/web/record/ActionButton.d.ts +14 -4
  42. package/dist/web/record/ActionButton.js +45 -2
  43. package/dist/web/record/CommandButton.d.ts +10 -1
  44. package/dist/web/record/CommandButton.js +23 -4
  45. package/dist/web/record/RelatedList.d.ts +0 -15
  46. package/dist/web/record/RelatedList.js +35 -6
  47. package/dist/web/record/ServiceButton.d.ts +20 -4
  48. package/dist/web/record/ServiceButton.js +30 -5
  49. package/dist/web/record/ask.d.ts +59 -0
  50. package/dist/web/record/ask.js +59 -0
  51. package/dist/web/record/control.d.ts +2 -2
  52. package/dist/web/record/control.js +7 -3
  53. package/dist/web/record/edit.d.ts +6 -1
  54. package/dist/web/record/edit.js +56 -8
  55. package/dist/web/record/record.js +5 -2
  56. package/dist/web/workspace/SincproApp.d.ts +10 -1
  57. package/dist/web/workspace/SincproApp.js +18 -4
  58. package/dist/web/workspace/composer.d.ts +7 -2
  59. package/dist/web/workspace/composer.js +24 -4
  60. package/dist/web/workspace/record.d.ts +7 -2
  61. package/dist/web/workspace/record.js +31 -6
  62. package/package.json +7 -7
@@ -1,7 +1,7 @@
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 ListDefinition } from "../../core/list.js";
4
- import type { PickerItem, RecordCard, RowOf } from "@sincpro/web-ui";
4
+ import type { PickerItem, RecordCard, RowOf, Tone } from "@sincpro/web-ui";
5
5
  import type { ComponentProps } from "react";
6
6
  /**
7
7
  * How a record of an entity looks at each size — a choice, a row, a card — declared ONCE with
@@ -35,10 +35,29 @@ export type OptionLook = Partial<Omit<PickerItem, "id">>;
35
35
  export type RowLook = Partial<RowOf>;
36
36
  /** The board's card. Opening it stays the list's: `onOpen` is not the presentation's. */
37
37
  export type CardLook = Partial<Omit<ComponentProps<typeof RecordCard>, "onOpen">>;
38
+ /** The colour of each value of a choice, by field and by value as text (the Meta's own keys):
39
+ * `{ estado: { APROBADO: "good", RECHAZADO: "bad" } }`. */
40
+ export type ChoiceTones<T> = Readonly<Partial<Record<keyof T & string, Readonly<Record<string, Tone>>>>>;
38
41
  export type Presentation<T> = {
39
42
  option?: (record: T) => OptionLook;
40
43
  row?: (record: T) => RowLook;
41
44
  card?: (record: T) => CardLook;
45
+ /**
46
+ * The colour of each value of a choice — a state, a priority. Declared once here and used
47
+ * wherever the framework draws the value: a table's cell, a row's and a card's tag, the tag of
48
+ * the record's header. A value with no tone is a neutral tag; the word is always the Meta's.
49
+ *
50
+ * Context: which colour «Rechazado» is, is the client's decision — the backend says what a
51
+ * value is called, never how it looks — and nothing here held it: every application kept a
52
+ * dictionary per state and passed it to its rows, its record and its cells by hand.
53
+ */
54
+ tones?: ChoiceTones<T>;
55
+ /** The colour of each kind of fact in its history, by its `name` — whole, or its last part
56
+ * (`pago_aprobado`) — over the framework's own (`FACT_TONES`). */
57
+ facts?: Readonly<Record<string, Tone>>;
58
+ /** The fields a table of its records shows when the screen names none — on its own list and
59
+ * inside another record (a one2many) alike. Without it, every field but the stamps. */
60
+ columns?: readonly (keyof T & string)[];
42
61
  };
43
62
  export type Presented<T extends AnyRecord, C extends CommandBodies = CommandBodies, A extends CommandAnswers = CommandAnswers> = Entity<T, C, A> & {
44
63
  readonly presentation: Presentation<T>;
@@ -11,11 +11,20 @@ import type { ModelDefinition, SearchFacet } from "@sincpro/web-ui";
11
11
  *
12
12
  * Known gap: web-ui's field has `choices` but no word for each choice, so the bar offers an
13
13
  * enum's raw values («paid») where a cell says «Pagada». Fixing it is web-ui's.
14
+ *
15
+ * `texts` keeps only those plain text fields: web-ui's bar offers a typed word in EVERY text
16
+ * field of the model it is handed (its `facets` are read only without one), so the bar is handed
17
+ * the model with the text fields a person types into (`facetsOf`) — and the criteria builder the
18
+ * whole one.
14
19
  */
15
- export declare function modelOf(meta: Meta, locale?: string): ModelDefinition;
20
+ export declare function modelOf(meta: Meta, locale?: string, texts?: readonly string[]): ModelDefinition;
16
21
  /**
17
- * Where a typed text can be looked for: the fields the entity's own search template names
18
- * first (what «search by name» means for THIS entity), then every other text field it can
19
- * filter on.
22
+ * Where a typed text can be looked for: the fields the screen names (`defineList facets`), else
23
+ * the ones the entity's search template names — what a typed text already means for it —, else
24
+ * its display field.
25
+ *
26
+ * Context: it used to append every text field that takes `like`, and «Excel» in a list of
27
+ * enrollments offered sixteen places to look, «ID» and a storage key among them. Which fields a
28
+ * person types into is the screen's decision; every other field is still in the criteria builder.
20
29
  */
21
- export declare function facetsOf(meta: Meta, locale?: string): SearchFacet[];
30
+ export declare function facetsOf(meta: Meta, locale?: string, named?: readonly string[]): SearchFacet[];
@@ -10,13 +10,25 @@ import { labelOf } from "@sincpro/criteria";
10
10
  *
11
11
  * Known gap: web-ui's field has `choices` but no word for each choice, so the bar offers an
12
12
  * enum's raw values («paid») where a cell says «Pagada». Fixing it is web-ui's.
13
+ *
14
+ * `texts` keeps only those plain text fields: web-ui's bar offers a typed word in EVERY text
15
+ * field of the model it is handed (its `facets` are read only without one), so the bar is handed
16
+ * the model with the text fields a person types into (`facetsOf`) — and the criteria builder the
17
+ * whole one.
13
18
  */
14
- export function modelOf(meta, locale) {
15
- const names = Object.keys(meta.fields);
19
+ export function modelOf(meta, locale, texts) {
20
+ const kept = (name) => {
21
+ const field = meta.fields[name];
22
+ const text = field?.type === "text" && !field.choices?.length;
23
+ return texts === undefined || !text || texts.includes(name);
24
+ };
25
+ const names = Object.keys(meta.fields).filter(kept);
16
26
  return {
17
27
  aggregate: meta.aggregate,
18
28
  identity: meta.identity,
19
- fields: Object.fromEntries(Object.entries(meta.fields).map(([name, field]) => [
29
+ fields: Object.fromEntries(Object.entries(meta.fields)
30
+ .filter(([name]) => kept(name))
31
+ .map(([name, field]) => [
20
32
  name,
21
33
  {
22
34
  type: field.type,
@@ -26,7 +38,9 @@ export function modelOf(meta, locale) {
26
38
  kind: field.kind,
27
39
  ...(field.many === undefined ? {} : { many: field.many }),
28
40
  ...(field.relation === undefined ? {} : { relation: field.relation }),
29
- ...(field.identified_by === undefined ? {} : { identifiedBy: field.identified_by }),
41
+ ...(field.identified_by === undefined
42
+ ? {}
43
+ : { identifiedBy: field.identified_by }),
30
44
  ...(field.choices && field.choices.length > 0 ? { choices: field.choices } : {}),
31
45
  },
32
46
  ])),
@@ -58,15 +72,16 @@ function searchedFields(meta) {
58
72
  return found;
59
73
  }
60
74
  /**
61
- * Where a typed text can be looked for: the fields the entity's own search template names
62
- * first (what «search by name» means for THIS entity), then every other text field it can
63
- * filter on.
75
+ * Where a typed text can be looked for: the fields the screen names (`defineList facets`), else
76
+ * the ones the entity's search template names — what a typed text already means for it —, else
77
+ * its display field.
78
+ *
79
+ * Context: it used to append every text field that takes `like`, and «Excel» in a list of
80
+ * enrollments offered sixteen places to look, «ID» and a storage key among them. Which fields a
81
+ * person types into is the screen's decision; every other field is still in the criteria builder.
64
82
  */
65
- export function facetsOf(meta, locale) {
66
- const first = searchedFields(meta);
67
- const texts = Object.entries(meta.fields)
68
- .filter(([, field]) => field.type === "text" && field.ops.includes("like") && !field.choices?.length)
69
- .map(([name]) => name);
70
- const order = [...new Set([...first, ...texts])];
71
- return order.map((name) => ({ id: name, label: labelOf(meta, name, locale) }));
83
+ export function facetsOf(meta, locale, named) {
84
+ const searched = [...new Set(searchedFields(meta))];
85
+ const fields = named ?? (searched.length > 0 ? searched : meta.display ? [meta.display] : []);
86
+ return fields.map((name) => ({ id: name, label: labelOf(meta, name, locale) }));
72
87
  }
@@ -1,13 +1,17 @@
1
1
  import type { Rehearsal } from "../../react/useCommand.js";
2
+ import type { AskForm } from "./ask.js";
3
+ /** What a person filled in before the action runs, by field name. */
4
+ export type Asked = Readonly<Record<string, unknown>>;
2
5
  /**
3
6
  * What a button needs of an action to draw it — whatever runs behind: an entity's Command
4
7
  * (`useCommand`) or one of the application's use cases (`useService`).
5
8
  */
6
9
  export type ButtonAction<R> = {
7
- /** Does it; `undefined` when it failed — the failure is in `error`. */
8
- run: () => Promise<R | undefined>;
10
+ /** Does it — with what was asked, when it asks —; `undefined` when it failed: the failure is
11
+ * in `error`. */
12
+ run: (asked?: Asked) => Promise<R | undefined>;
9
13
  /** Asks first what it would do — a dry run: the action destroys something. */
10
- rehearse?: () => Promise<Rehearsal | undefined>;
14
+ rehearse?: (asked?: Asked) => Promise<Rehearsal | undefined>;
11
15
  busy: boolean;
12
16
  error: unknown;
13
17
  /** What the last dry run said it would do. */
@@ -23,12 +27,18 @@ export type ButtonAction<R> = {
23
27
  * confirmation is the consequence, in the server's words, not a question. A refusal is shown
24
28
  * where it happened, with what to do. Off while it runs: a click twice is one action.
25
29
  *
30
+ * One that needs input (`ask`) opens its fields first, filled from the record, and sends them on
31
+ * «Confirmar»; when it can rehearse, the first «Confirmar» asks what it would do with them and
32
+ * the second does it. A refusal stays in the dialog over the fields, so they can be put right.
33
+ *
26
34
  * Internal: the gesture is drawn here once; what runs behind is each button's own.
27
35
  */
28
- export declare function ActionButton<R>({ action, label, role, disabled, onDone, }: {
36
+ export declare function ActionButton<R>({ action, label, role, disabled, ask: form, onDone, }: {
29
37
  action: ButtonAction<R>;
30
38
  label: string;
31
39
  role?: "primary" | "secondary" | "ghost";
32
40
  disabled?: boolean;
41
+ /** What it asks for before it runs — `askFormOf`. */
42
+ ask?: AskForm;
33
43
  onDone?: (answer: R) => void;
34
44
  }): import("react").JSX.Element;
@@ -2,8 +2,38 @@
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
+ /** The fields of an action that asks, its refusal over them and what its dry run said. */
8
+ function AskDialog({ action, form, label, onClose, onDone, }) {
9
+ const copy = frameworkCopy().command;
10
+ const [values, setValues] = useState(() => ({ ...form.initial }));
11
+ const rehearsing = action.rehearse !== undefined;
12
+ const confirm = async () => {
13
+ if (rehearsing && action.rehearsal === null) {
14
+ await action.rehearse?.(values);
15
+ return;
16
+ }
17
+ const answer = await action.run(values);
18
+ if (answer !== undefined)
19
+ onDone(answer);
20
+ };
21
+ return (_jsxs(Dialog, { open: true, title: label, 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) => {
22
+ const control = {
23
+ value: values[field.name],
24
+ set: (next) => {
25
+ setValues((now) => ({ ...now, [field.name]: next }));
26
+ // What the dry run said was about the values before this one.
27
+ if (action.rehearsal !== null || action.error !== null)
28
+ action.reset();
29
+ },
30
+ readonly: false,
31
+ invalid: false,
32
+ label: field.label,
33
+ };
34
+ return field.labelled ? (_jsx("div", { children: field.draw(control) }, field.name)) : (_jsx(Field, { label: field.label, children: field.draw(control) }, field.name));
35
+ }) }), action.rehearsal === null ? null : _jsx("p", { className: "text-ink", children: action.rehearsal.text })] }));
36
+ }
7
37
  /**
8
38
  * An action as a button — the gesture `CommandButton` and `ServiceButton` share.
9
39
  *
@@ -13,11 +43,24 @@ import { useState } from "react";
13
43
  * confirmation is the consequence, in the server's words, not a question. A refusal is shown
14
44
  * where it happened, with what to do. Off while it runs: a click twice is one action.
15
45
  *
46
+ * One that needs input (`ask`) opens its fields first, filled from the record, and sends them on
47
+ * «Confirmar»; when it can rehearse, the first «Confirmar» asks what it would do with them and
48
+ * the second does it. A refusal stays in the dialog over the fields, so they can be put right.
49
+ *
16
50
  * Internal: the gesture is drawn here once; what runs behind is each button's own.
17
51
  */
18
- export function ActionButton({ action, label, role = "secondary", disabled = false, onDone, }) {
52
+ export function ActionButton({ action, label, role = "secondary", disabled = false, ask: form, onDone, }) {
19
53
  const copy = frameworkCopy().command;
20
54
  const [open, setOpen] = useState(false);
55
+ if (form !== undefined)
56
+ 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, label: label, onClose: () => {
57
+ setOpen(false);
58
+ action.reset();
59
+ }, onDone: (answer) => {
60
+ setOpen(false);
61
+ action.reset();
62
+ onDone?.(answer);
63
+ } })) : null] }));
21
64
  const destructive = action.rehearse !== undefined;
22
65
  const run = async () => {
23
66
  const answer = await action.run();
@@ -1,6 +1,7 @@
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 AskField } from "./ask.js";
4
5
  /**
5
6
  * An entity's Command as a button: sent as it is, over the screens' transport.
6
7
  *
@@ -8,8 +9,12 @@ import { type AnswerOf } from "../../react/useCommand.js";
8
9
  * server's dry run first and show what it would do; a refusal where it happened. An action that is
9
10
  * one of the application's own use cases — logic of its own, composed, traced as
10
11
  * `<context>/<Service>` — is a `ServiceButton`.
12
+ *
13
+ * One that needs input names the fields it asks for (`ask`): a dialog draws them as the
14
+ * record's fields of the same name, filled from `record`, and what is filled goes in the body
15
+ * beside `body`.
11
16
  */
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, }: {
17
+ 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, onDone, }: {
13
18
  entity: Entity<T, C, A>;
14
19
  /** One the entity declares; a typed entity takes only its own names. */
15
20
  command: N;
@@ -22,5 +27,9 @@ export declare function CommandButton<T extends AnyRecord, R = unknown, C extend
22
27
  role?: "primary" | "secondary" | "ghost";
23
28
  /** Not offered right now — «Mover» with no other tenant chosen. The server still decides. */
24
29
  disabled?: boolean;
30
+ /** The fields it asks for before it runs, sent in its body: `["areas"]`. */
31
+ ask?: readonly AskField[];
32
+ /** The record it acts on: what the asked fields start with. */
33
+ record?: AnyRecord;
25
34
  onDone?: (answer: AnswerOf<R, A, N>) => void;
26
35
  }): 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
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, 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 }), 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,
@@ -2,21 +2,6 @@ import { type Entity } from "@sincpro/client-framework";
2
2
  import type { AnyRecord } from "@sincpro/criteria";
3
3
  import type { RecordReading } from "../../react/useRecord.js";
4
4
  import { type EntityListProps } from "../list/EntityList.js";
5
- /**
6
- * A relation of a record as a list inside it — searched, ordered and paged by the server.
7
- *
8
- * Context: the same `EntityList` a screen uses, `embedded` so it reads as a field of the record
9
- * (a one-line bar that does not stick), over `useRelated`. Its search box fills the RELATED
10
- * entity's own search template: the lines of an order are searched by product, the invoices of
11
- * a customer by number. The column that points back to this record is left out — it is the
12
- * same on every row. Use `RecordLines` when the page the record brought is all there is.
13
- *
14
- * «Agregar» opens the form of a new related record already pointing back here, where the view
15
- * says (`RecordOpening.create`: a modal that answers, in a workspace), and the list is read
16
- * again once it exists. Never a row of the table: each one is an aggregate with its own create,
17
- * rules and preview. There is none while this record does not exist yet — there is nothing to
18
- * point back to — nor when the relation is read only or the entity cannot be created.
19
- */
20
5
  export declare function RelatedList<P extends AnyRecord, T extends AnyRecord>({ record, name, entity, pageSize, ...list }: {
21
6
  record: RecordReading<P>;
22
7
  name: string;
@@ -2,13 +2,15 @@
2
2
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import {} from "@sincpro/client-framework";
4
4
  import { frameworkCopy } from "../../core/copy.js";
5
- import { defaultFields } from "../../core/present.js";
5
+ import { fieldStateOf } from "../../core/edit.js";
6
+ import { defaultFields, STAMPS } from "../../core/present.js";
6
7
  import { backReference, isKeyValue } from "../../core/relation.js";
7
8
  import { useRelated } from "../../react/useRelated.js";
8
9
  import { EntityList } from "../list/EntityList.js";
10
+ import { presentationOf } from "../list/presentation.js";
9
11
  import { useRecordOpening } from "./opening.js";
10
12
  import { Button } from "@sincpro/web-ui";
11
- import { useMemo } from "react";
13
+ import { useMemo, useRef } from "react";
12
14
  /**
13
15
  * A relation of a record as a list inside it — searched, ordered and paged by the server.
14
16
  *
@@ -18,12 +20,20 @@ import { useMemo } from "react";
18
20
  * a customer by number. The column that points back to this record is left out — it is the
19
21
  * same on every row. Use `RecordLines` when the page the record brought is all there is.
20
22
  *
23
+ * It reads as a part of the record, not as a screen: its columns are the related entity's own
24
+ * (`presented(entity, { columns })`), else its fields without the framework's stamps (created,
25
+ * updated, version) and without a key nobody reads (a UUID identity that is not its name); and a
26
+ * relation of ten records or fewer has no search bar — three dues of a payment are read, not
27
+ * searched. A screen that wants them passes `columns` / `search`.
28
+ *
21
29
  * «Agregar» opens the form of a new related record already pointing back here, where the view
22
30
  * says (`RecordOpening.create`: a modal that answers, in a workspace), and the list is read
23
31
  * again once it exists. Never a row of the table: each one is an aggregate with its own create,
24
32
  * rules and preview. There is none while this record does not exist yet — there is nothing to
25
33
  * point back to — nor when the relation is read only or the entity cannot be created.
26
34
  */
35
+ /** As many related records as are read at a glance — a picker opens with as many. */
36
+ const SHORT = 10;
27
37
  export function RelatedList({ record, name, entity, pageSize, ...list }) {
28
38
  const listing = useRelated(record, name, entity, pageSize === undefined ? {} : { pageSize });
29
39
  const opening = useRecordOpening();
@@ -33,10 +43,24 @@ export function RelatedList({ record, name, entity, pageSize, ...list }) {
33
43
  const pointing = backReference(record.record, record.meta?.fields[name], identity);
34
44
  const back = pointing?.field;
35
45
  const own = list.columns;
46
+ const look = presentationOf(entity).columns;
47
+ const meta = listing.meta;
36
48
  const columns = useMemo(() => own ??
37
- (listing.meta === null || !back
49
+ look?.filter((field) => field !== back) ??
50
+ (meta === null
38
51
  ? undefined
39
- : defaultFields(listing.meta).filter((field) => field !== back)), [own, listing.meta, back]);
52
+ : defaultFields(meta).filter((field) => field !== back &&
53
+ !STAMPS.has(field) &&
54
+ // The identity is a column only when it is the record's name (a code, a number).
55
+ (field !== meta.identity || meta.display === field || !meta.display))), [own, look, meta, back]);
56
+ // A relation of a few records is read whole; the bar comes once there is more to find, and
57
+ // stays: a search that narrowed it to one must not take away the bar that undoes it.
58
+ const long = listing.count !== null &&
59
+ (!listing.count.exact || listing.count.value > Math.min(SHORT, listing.page.size));
60
+ const searching = listing.text !== "" || listing.selection !== undefined;
61
+ const barred = useRef(false);
62
+ if (long || searching)
63
+ barred.current = true;
40
64
  const create = opening?.create;
41
65
  // The value the rows point back with.
42
66
  const here = pointing?.value;
@@ -45,8 +69,13 @@ export function RelatedList({ record, name, entity, pageSize, ...list }) {
45
69
  !back ||
46
70
  record.isNew ||
47
71
  !isKeyValue(here) ||
48
- record.edit.state(name).readonly ? null : (_jsx(Button, { size: "sm", role: "secondary", onClick: () => create(entity, { [back]: here }, listing.reload), children: frameworkCopy().record.add }));
49
- return (_jsx(EntityList, { listing: listing, embedded: true, ...list, ...(columns === undefined ? {} : { columns }), ...(adding === null && list.actions === undefined
72
+ // The relation's own hint — not whether THIS record can be written: adding stores another
73
+ // record (a customer only Commands move still gets new invoices).
74
+ fieldStateOf(record.meta, name, record.edit.values).readonly ? null : (_jsx(Button, { size: "sm", role: "secondary", onClick: () => create(entity, { [back]: here }, listing.reload), children: frameworkCopy().record.add }));
75
+ return (_jsx(EntityList, { listing: listing, embedded: true,
76
+ // The bar, not a box: a relation of records is searched like any list of records —
77
+ // named filters, the criteria builder, grouping. A screen that wants a box passes one.
78
+ search: barred.current ? "bar" : false, ...list, ...(columns === undefined ? {} : { columns }), ...(adding === null && list.actions === undefined
50
79
  ? {}
51
80
  : {
52
81
  actions: (_jsxs(_Fragment, { children: [list.actions, adding] })),
@@ -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 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,27 @@ 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, 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;
30
46
  onDone?: (answer: R) => void;
31
47
  }): 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
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, 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 }), action: {
30
55
  run: running.run,
31
56
  ...(rehearse === undefined ? {} : { rehearse: rehearsing.run }),
32
57
  busy: running.busy || rehearsing.busy,
@@ -0,0 +1,59 @@
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"` —, or
7
+ * one the entity does not have, described by the action.
8
+ *
9
+ * <CommandButton entity={cuentas} command="delegar_areas" id={cuenta.id} record={cuenta}
10
+ * label="Delegar áreas" ask={["areas"]} />
11
+ * ask={[{ name: "motivo", label: "Motivo", widget: "textarea" }]}
12
+ *
13
+ * Context: a Command that needs input could not live among a record's actions, so a screen drew
14
+ * its fields in the record's body with a button of their own. What the dialog shows is the
15
+ * screen's: the backend adds nothing for it. A field named by the entity reads as the record's
16
+ * field of that name — its label, its choices in their words, its control —; a control of the
17
+ * application's goes in with `as`, never a widget named in a string (`widget` takes only the
18
+ * framework's own).
19
+ */
20
+ export type AskField = string | {
21
+ name: string;
22
+ label: string;
23
+ /** The type of its value as the model writes it — `text` (the default), `number`, `date`,
24
+ * `boolean`, `text[]`. */
25
+ type?: string;
26
+ /** The values it takes, each with its word: a select, or checks for a `text[]`. */
27
+ choices?: readonly {
28
+ value: unknown;
29
+ label: string;
30
+ }[];
31
+ /** One of the framework's own widgets: `textarea`, several lines of prose. */
32
+ widget?: "textarea";
33
+ /** The application's own control. */
34
+ as?: (control: FieldControl) => ReactNode;
35
+ };
36
+ /** A field ready to draw: its name, its label, its control. */
37
+ export type AskedField = {
38
+ name: string;
39
+ label: string;
40
+ /** It draws its own label — a yes/no, a list of checks —: not wrapped in a `Field`. */
41
+ labelled: boolean;
42
+ draw: (control: FieldControl) => ReactNode;
43
+ };
44
+ /** What a form of asked fields needs to be drawn and filled. */
45
+ export type AskForm = {
46
+ fields: readonly AskedField[];
47
+ initial: Readonly<Record<string, unknown>>;
48
+ };
49
+ /**
50
+ * The asked fields, drawn from the entity's model, and what they start with: the record's own
51
+ * values. Its key is never asked — the action already knows whose record it is.
52
+ */
53
+ export declare function askFormOf(fields: readonly AskField[], options: {
54
+ meta: Meta | null | undefined;
55
+ locale?: string | undefined;
56
+ record?: AnyRecord | undefined;
57
+ /** The declared entity a relation points at, to choose it with its picker. */
58
+ entityFor?: (target: string) => Entity | undefined;
59
+ }): AskForm;
@@ -0,0 +1,59 @@
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, and what they start with: the record's own
13
+ * values. Its key is never asked — the action already knows whose record it is.
14
+ */
15
+ export function askFormOf(fields, options) {
16
+ const { meta, locale, record } = options;
17
+ const key = meta?.get_id || meta?.identity || "id";
18
+ const asked = [];
19
+ const initial = {};
20
+ for (const one of fields) {
21
+ if (typeof one !== "string") {
22
+ const choices = [...(one.choices ?? [])];
23
+ const type = one.type ?? "text";
24
+ asked.push({
25
+ name: one.name,
26
+ label: one.label,
27
+ labelled: one.as === undefined && labels(type, choices),
28
+ draw: (control) => one.as?.(control) ?? controlFor(control, type, false, choices, one.widget),
29
+ });
30
+ if (record !== undefined && record[one.name] !== undefined)
31
+ initial[one.name] = record[one.name];
32
+ continue;
33
+ }
34
+ if (one === key || one === "id")
35
+ continue;
36
+ const view = viewOf(meta, one, locale);
37
+ const field = fieldMetaOf(meta, one);
38
+ const relation = relationFieldOf(meta, one);
39
+ const target = relation === undefined || relation.many
40
+ ? undefined
41
+ : options.entityFor?.(relation.target);
42
+ // A many2one is chosen by its key, with the picker of the entity it points at.
43
+ const name = target === undefined || relation === undefined ? one : relation.key;
44
+ const choices = choicesOf(meta, one, locale);
45
+ asked.push({
46
+ name,
47
+ label: view.label,
48
+ labelled: target === undefined && labels(view.type, choices),
49
+ draw: target === undefined
50
+ ? (control) => controlFor(control, view.type, field?.exact === true, choices, field?.widget)
51
+ : (control) => (_jsx(RelationPicker, { target: target, value: control.value === null || control.value === undefined
52
+ ? ""
53
+ : String(control.value), onChange: (id) => control.set(id), label: control.label, onClear: () => control.set(null) })),
54
+ });
55
+ if (record !== undefined && record[name] !== undefined)
56
+ initial[name] = record[name];
57
+ }
58
+ return { fields: asked, initial };
59
+ }