@sincpro/web-framework 0.2.0 → 0.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 (64) hide show
  1. package/README.md +56 -19
  2. package/dist/core/copy.d.ts +2 -0
  3. package/dist/core/copy.js +2 -0
  4. package/dist/core/edit.d.ts +35 -3
  5. package/dist/core/edit.js +49 -5
  6. package/dist/core/entity.d.ts +14 -2
  7. package/dist/core/entity.js +33 -1
  8. package/dist/core/index.d.ts +3 -2
  9. package/dist/core/index.js +3 -2
  10. package/dist/core/lines.d.ts +53 -0
  11. package/dist/core/lines.js +120 -0
  12. package/dist/core/list.d.ts +1 -1
  13. package/dist/core/lookup.d.ts +2 -0
  14. package/dist/core/lookup.js +3 -1
  15. package/dist/core/rest.d.ts +5 -9
  16. package/dist/core/rest.js +14 -6
  17. package/dist/core/transport.d.ts +9 -1
  18. package/dist/react/context.d.ts +8 -0
  19. package/dist/react/index.d.ts +2 -1
  20. package/dist/react/index.js +1 -0
  21. package/dist/react/useGroups.d.ts +13 -1
  22. package/dist/react/useGroups.js +48 -7
  23. package/dist/react/useListing.d.ts +8 -4
  24. package/dist/react/useListing.js +11 -5
  25. package/dist/react/usePivot.d.ts +33 -0
  26. package/dist/react/usePivot.js +59 -0
  27. package/dist/react/useRecord.d.ts +13 -9
  28. package/dist/react/useRecord.js +55 -13
  29. package/dist/web/index.d.ts +7 -1
  30. package/dist/web/index.js +7 -1
  31. package/dist/web/list/EntityList.d.ts +46 -7
  32. package/dist/web/list/EntityList.js +132 -21
  33. package/dist/web/list/EntitySearchBar.js +19 -1
  34. package/dist/web/list/columns.d.ts +0 -13
  35. package/dist/web/list/columns.js +20 -1
  36. package/dist/web/list/groups.d.ts +3 -1
  37. package/dist/web/list/groups.js +8 -1
  38. package/dist/web/list/openIn.d.ts +41 -0
  39. package/dist/web/list/openIn.js +55 -0
  40. package/dist/web/list/pivot.d.ts +23 -0
  41. package/dist/web/list/pivot.js +64 -0
  42. package/dist/web/list/presentation.d.ts +51 -0
  43. package/dist/web/list/presentation.js +20 -0
  44. package/dist/web/record/EntityPicker.d.ts +3 -1
  45. package/dist/web/record/EntityPicker.js +15 -5
  46. package/dist/web/record/EntityTags.js +16 -5
  47. package/dist/web/record/RelatedList.d.ts +6 -0
  48. package/dist/web/record/RelatedList.js +26 -2
  49. package/dist/web/record/control.d.ts +42 -0
  50. package/dist/web/record/control.js +60 -0
  51. package/dist/web/record/edit.d.ts +16 -8
  52. package/dist/web/record/edit.js +57 -49
  53. package/dist/web/record/lines.d.ts +84 -0
  54. package/dist/web/record/lines.js +190 -0
  55. package/dist/web/record/opening.d.ts +8 -0
  56. package/dist/web/record/summary.js +14 -6
  57. package/dist/web/workspace/composer.d.ts +2 -1
  58. package/dist/web/workspace/composer.js +27 -16
  59. package/dist/web/workspace/open.d.ts +2 -14
  60. package/dist/web/workspace/open.js +2 -13
  61. package/dist/web/workspace/record.js +6 -3
  62. package/dist/web/workspace/workspace.d.ts +4 -2
  63. package/dist/web/workspace/workspace.js +10 -4
  64. package/package.json +9 -9
@@ -0,0 +1,84 @@
1
+ import { type AnyRecord } from "@sincpro/criteria";
2
+ import { type LineAdding } from "../../core/lines.js";
3
+ import type { RecordReading } from "../../react/useRecord.js";
4
+ import { type FieldControl } from "./control.js";
5
+ import { type LineEditorState } from "@sincpro/web-ui";
6
+ import { type ReactNode } from "react";
7
+ /** What a control of the screen's own receives for a field of a line: the field's value and
8
+ * how to change it, as any field control, plus the whole line — a widget may write several
9
+ * fields at once (a product and its price). */
10
+ export type LineControl = FieldControl & {
11
+ line: AnyRecord;
12
+ setLine: (next: AnyRecord) => void;
13
+ };
14
+ /** A column that is not a field of the line — its delivery, its buttons: read only. */
15
+ export type LineColumn = {
16
+ /** Must not be the name of a field of the line. */
17
+ id: string;
18
+ header: string;
19
+ cell: (line: AnyRecord) => ReactNode;
20
+ /** What the lines' search reads in it. */
21
+ text?: (line: AnyRecord) => string;
22
+ numeric?: boolean;
23
+ };
24
+ /**
25
+ * How the lines look and are written, when the screen wants it its way — every piece optional,
26
+ * every one falling back to the model's.
27
+ *
28
+ * Context: «defaults, never cages». The model decides the columns and their controls; a screen
29
+ * can choose and order the columns, draw a cell its way, write a field with a widget of its
30
+ * own (in the cell and in the dialog), add columns that are not fields (a line's picking with a
31
+ * button that opens it), or write the whole dialog. What it does not get to do is break where
32
+ * a line is written: a field with its own control fits a cell, and a required field left out of
33
+ * the columns sends the line to the dialog (`addingOf`).
34
+ */
35
+ export type LinesLook = {
36
+ /** Where a line is written: the table when it can be (the default), or the dialog. */
37
+ adding?: LineAdding;
38
+ /** The fields drawn as columns, in order. The dialog still holds every field. */
39
+ columns?: readonly string[];
40
+ /** A field's cell drawn the screen's way, while it reads. */
41
+ cells?: Readonly<Partial<Record<string, (line: AnyRecord) => ReactNode>>>;
42
+ /** A field written with the screen's own widget — in its cell and in the dialog. */
43
+ controls?: Readonly<Partial<Record<string, (control: LineControl) => ReactNode>>>;
44
+ /** Columns that are not fields, after the fields. */
45
+ extra?: readonly LineColumn[];
46
+ /** The whole line's dialog, the screen's way. */
47
+ editor?: (draft: AnyRecord, set: (next: AnyRecord) => void, state: LineEditorState) => ReactNode;
48
+ };
49
+ /**
50
+ * The lines of an aggregate, edited inside its record: web-ui's `LineItems` over the record's
51
+ * edit — what the screen holds is the whole list, and every change writes it back with
52
+ * `edit.set(name, next)`, so it is dirty, previewed, saved and discarded like any field.
53
+ *
54
+ * Context: a one2many of no entity of its own (the lines of an invoice) used to be drawn read
55
+ * only (`RecordLines`). sincpro_framework writes such lines in the aggregate's own command, as
56
+ * the whole list in order; a new line goes without an id (`core/lines`). What `LineItems` does
57
+ * — add, edit, remove with «Deshacer», reorder — is the screen's: only the list that results
58
+ * travels. Each column is a field of the line model (`meta.fields[name].definition`, its
59
+ * scalars: `lineFields`), its control the one its type asks for — a many2one, the picker of its
60
+ * entity (`LineValue`) — its state the line model's hints over the line refined by the server's
61
+ * `items[i]` (`edit.cell`). A server error or warning that points into a cell
62
+ * (`#/values/lines/1/qty`, `["lines", 1, "price"]`) is said IN that cell (web-ui 1.6
63
+ * `cellState`); one about the line as a whole, on the line (`flag`).
64
+ *
65
+ * WHERE a line is written is one decision (`addingOf`): in the TABLE — «Agregar una línea» is
66
+ * its last row, a row is written where it is, the pencil opens the whole line in its dialog —
67
+ * whenever every column a person writes fits a cell; in the DIALOG otherwise, or when the
68
+ * screen asks (`look.adding="editor"`), and then the cells only read. The screen may draw it
69
+ * its way (`LinesLook`) without breaking that rule. A row that cannot be finished where
70
+ * it was opened is a trap, so «inline» is never forced. On a narrow box web-ui draws the lines
71
+ * as cards that open the dialog.
72
+ *
73
+ * Only a WHOLE list is editable: a record that brought one page of 1 000 lines draws them read
74
+ * only (`RecordLines`, «se muestran 50 de 1000»), because a page written back whole deletes
75
+ * every line it did not bring.
76
+ */
77
+ export declare function RecordLinesEditor<T extends AnyRecord>({ record, name, lines, look, }: {
78
+ record: RecordReading<T>;
79
+ name: string;
80
+ /** The list as on screen (`editableLines(edit.values[name])`), never a page. */
81
+ lines: readonly AnyRecord[];
82
+ /** The screen's way, piece by piece. */
83
+ look?: LinesLook;
84
+ }): import("react").JSX.Element;
@@ -0,0 +1,190 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { choicesOf } from "@sincpro/criteria";
4
+ import { adviceAt, defaultsOf, errorAt, pathOf } from "../../core/edit.js";
5
+ import { addingOf, blankLine, lineFields, lineKey, moved, sameLine, writtenInCell, } from "../../core/lines.js";
6
+ import { textOf, viewOf } from "../../core/present.js";
7
+ import { entityFor, relationFieldOf, } from "../../core/relation.js";
8
+ import { useEntityFor, useFramework } from "../../react/context.js";
9
+ import { controlFor, RelationPicker, } from "./control.js";
10
+ import { Field, LineItems, ReadValue, } from "@sincpro/web-ui";
11
+ import { useRef } from "react";
12
+ /** How a field of a line reads: a many2one's key by the name of what it points at. */
13
+ function textIn(definition, field, relation, line, locale) {
14
+ const named = relation === undefined
15
+ ? null
16
+ : textOf(definition, relation.relation, line[relation.relation], locale);
17
+ return named ?? textOf(definition, field, line[field], locale) ?? "";
18
+ }
19
+ /**
20
+ * One field of a line, written — in its cell or in its dialog: a many2one of a declared entity
21
+ * is its picker, anything else the control its type asks for.
22
+ *
23
+ * Context: every change is a COPY of the line that keeps its local key (`sameLine`): a new
24
+ * line has no id, and a row whose key changed is another row to the table — it would lose the
25
+ * focus mid-word. A chosen record is kept beside its key so the cell names it at once.
26
+ */
27
+ function LineValue({ definition, field, line, set, label, invalid, locale, inCell, own, }) {
28
+ const relation = relationFieldOf(definition, field);
29
+ const target = useEntityFor(relation?.target);
30
+ const write = (changes) => set(sameLine(line, { ...line, ...changes }));
31
+ if (own !== undefined)
32
+ return own({
33
+ value: line[field],
34
+ set: (next) => write({ [field]: next }),
35
+ readonly: false,
36
+ invalid,
37
+ label,
38
+ standalone: inCell,
39
+ line,
40
+ setLine: (next) => set(sameLine(line, next)),
41
+ });
42
+ if (relation !== undefined && !relation.many && target !== undefined) {
43
+ const key = line[relation.key];
44
+ return (_jsx(RelationPicker, { target: target, value: key === null || key === undefined ? "" : String(key), label: label, ownChooser: true, standalone: inCell, onChange: (id, chosen) => write({ [relation.key]: id, [relation.relation]: chosen ?? null }) }));
45
+ }
46
+ return controlFor({
47
+ value: line[field],
48
+ set: (next) => write({ [field]: next }),
49
+ readonly: false,
50
+ invalid,
51
+ label,
52
+ standalone: inCell,
53
+ }, viewOf(definition, field, locale).type, definition?.fields[field]?.exact === true, choicesOf(definition, field, locale));
54
+ }
55
+ /**
56
+ * The lines of an aggregate, edited inside its record: web-ui's `LineItems` over the record's
57
+ * edit — what the screen holds is the whole list, and every change writes it back with
58
+ * `edit.set(name, next)`, so it is dirty, previewed, saved and discarded like any field.
59
+ *
60
+ * Context: a one2many of no entity of its own (the lines of an invoice) used to be drawn read
61
+ * only (`RecordLines`). sincpro_framework writes such lines in the aggregate's own command, as
62
+ * the whole list in order; a new line goes without an id (`core/lines`). What `LineItems` does
63
+ * — add, edit, remove with «Deshacer», reorder — is the screen's: only the list that results
64
+ * travels. Each column is a field of the line model (`meta.fields[name].definition`, its
65
+ * scalars: `lineFields`), its control the one its type asks for — a many2one, the picker of its
66
+ * entity (`LineValue`) — its state the line model's hints over the line refined by the server's
67
+ * `items[i]` (`edit.cell`). A server error or warning that points into a cell
68
+ * (`#/values/lines/1/qty`, `["lines", 1, "price"]`) is said IN that cell (web-ui 1.6
69
+ * `cellState`); one about the line as a whole, on the line (`flag`).
70
+ *
71
+ * WHERE a line is written is one decision (`addingOf`): in the TABLE — «Agregar una línea» is
72
+ * its last row, a row is written where it is, the pencil opens the whole line in its dialog —
73
+ * whenever every column a person writes fits a cell; in the DIALOG otherwise, or when the
74
+ * screen asks (`look.adding="editor"`), and then the cells only read. The screen may draw it
75
+ * its way (`LinesLook`) without breaking that rule. A row that cannot be finished where
76
+ * it was opened is a trap, so «inline» is never forced. On a narrow box web-ui draws the lines
77
+ * as cards that open the dialog.
78
+ *
79
+ * Only a WHOLE list is editable: a record that brought one page of 1 000 lines draws them read
80
+ * only (`RecordLines`, «se muestran 50 de 1000»), because a page written back whole deletes
81
+ * every line it did not bring.
82
+ */
83
+ export function RecordLinesEditor({ record, name, lines, look = {}, }) {
84
+ const { meta, locale, edit } = record;
85
+ const { entities, metas } = useFramework();
86
+ const definition = meta?.fields[name]?.definition ?? null;
87
+ const identity = definition?.identity || "id";
88
+ const fields = lineFields(definition);
89
+ const shown = look.columns === undefined ? fields : look.columns.filter((one) => fields.includes(one));
90
+ const controls = look.controls ?? {};
91
+ const own = new Set(Object.keys(controls));
92
+ const declared = (target) => entityFor(entities, target, metas.get) !== undefined;
93
+ const adding = addingOf(definition, {
94
+ declared,
95
+ shown,
96
+ own,
97
+ ...(look.adding === undefined ? {} : { asked: look.adding }),
98
+ });
99
+ const writing = !edit.state(name).readonly;
100
+ // Once per render, not per cell: 2 000 lines × 8 fields asked the model 16 000 times.
101
+ const relations = new Map(fields.map((field) => [field, relationFieldOf(definition, field)]));
102
+ const keyOf = (line) => lineKey(line, identity);
103
+ // Where each line is, by its key — once per render: a `findIndex` per line drawn made the
104
+ // flags of 2 000 lines cost 50 ms (quadratic); with the map, a fraction of one.
105
+ const positions = new Map(lines.map((one, i) => [keyOf(one), i]));
106
+ const indexOf = (line) => positions.get(keyOf(line)) ?? lines.length;
107
+ // A callback of `LineItems` can run long after its render — «Deshacer» in a toast, seconds
108
+ // after the line went — so every change starts from the list on screen NOW, never the one of
109
+ // the render that made the callback.
110
+ const now = useRef(lines);
111
+ now.current = lines;
112
+ const write = (change) => edit.set(name, change(now.current));
113
+ const drawn = new Set(shown);
114
+ const fieldColumns = shown.map((field) => {
115
+ const view = viewOf(definition, field, locale);
116
+ const relation = relations.get(field);
117
+ const inCell = writing &&
118
+ adding === "inline" &&
119
+ definition !== null &&
120
+ definition.fields[field]?.readonly !== true &&
121
+ (own.has(field) || writtenInCell(definition, field, declared));
122
+ const ownCell = look.cells?.[field];
123
+ return {
124
+ id: field,
125
+ header: view.label,
126
+ value: (line) => line[field],
127
+ // Drawn as read: «Cemento 50 kg», not «CEM-50»; «1.552,00», not «1552.00».
128
+ cell: ownCell ?? ((line) => textIn(definition, field, relation, line, locale)),
129
+ text: (line) => textIn(definition, field, relation, line, locale),
130
+ numeric: view.type === "number" || view.type === "integer",
131
+ ...(inCell
132
+ ? {
133
+ edit: (line, set) => (_jsx(LineValue, { definition: definition, field: field, line: line, set: set, label: view.label, invalid: false, locale: locale, inCell: true, own: controls[field] })),
134
+ }
135
+ : {}),
136
+ };
137
+ });
138
+ const columns = [
139
+ ...fieldColumns,
140
+ ...(look.extra ?? []).map((one) => ({
141
+ id: one.id,
142
+ header: one.header,
143
+ value: () => null,
144
+ cell: one.cell,
145
+ ...(one.text === undefined ? {} : { text: one.text }),
146
+ ...(one.numeric === undefined ? {} : { numeric: one.numeric }),
147
+ })),
148
+ ];
149
+ return (_jsx(LineItems, { lines: lines, columns: columns, getLineId: keyOf, mode: writing ? "edit" : "read", adding: adding, onChange: (key, next) => write((all) => all.map((one) => (keyOf(one) === key ? sameLine(one, next) : one))), newLine: () => blankLine(defaultsOf(definition), identity), onAdd: (line) => write((all) => [...all, line]), onRemove: (key) => write((all) => all.filter((one) => keyOf(one) !== key)), onRestore: (line, index) => write((all) => [...all.slice(0, index), line, ...all.slice(index)]), onReorder: (key, before) => write((all) => moved(all, identity, key, before)),
150
+ // A cell says its own (web-ui 1.6 `cellState`): the line model's hints refined by the
151
+ // server's `items[i]`, the error pointing at it (under its value) and its warning.
152
+ cellState: (line, column) => {
153
+ const index = indexOf(line);
154
+ const at = `${name}/${index}/${column}`;
155
+ const state = edit.cell(name, index, column);
156
+ const error = errorAt(edit.errors, at);
157
+ const warned = adviceAt(edit.advice, at)[0];
158
+ return {
159
+ readonly: state.readonly,
160
+ required: state.required,
161
+ hidden: !state.visible,
162
+ ...(error === undefined ? {} : { error }),
163
+ ...(warned === undefined ? {} : { flag: { tone: "warn", label: warned.message } }),
164
+ };
165
+ },
166
+ // The line's own flag: what points at the line and at no cell drawn — the line as a
167
+ // whole, or a field it has no column for (an id the server does not know).
168
+ flag: (line) => {
169
+ const at = `${name}/${indexOf(line)}`;
170
+ const onLine = (key) => key === at ||
171
+ (key.startsWith(`${at}/`) && !drawn.has(key.slice(at.length + 1).split("/")[0]));
172
+ const error = Object.entries(edit.errors).find(([key]) => onLine(key))?.[1];
173
+ if (error !== undefined)
174
+ return { tone: "bad", label: error };
175
+ const warned = edit.advice.find((one) => onLine(pathOf(one.path ?? [one.field ?? ""])));
176
+ return warned === undefined ? null : { tone: "warn", label: warned.message };
177
+ }, editor: (draft, set, state) => {
178
+ if (look.editor !== undefined)
179
+ return look.editor(draft, set, state);
180
+ const index = indexOf(draft);
181
+ return (_jsx("div", { className: "grid gap-3", children: fields.map((field) => {
182
+ const state = edit.cell(name, index, field);
183
+ if (!state.visible)
184
+ return null;
185
+ const view = viewOf(definition, field, locale);
186
+ const error = edit.errors[`${name}/${index}/${field}`];
187
+ return (_jsx(Field, { label: view.label, required: state.required && !state.readonly, ...(error === undefined ? {} : { error }), children: state.readonly ? (_jsx(ReadValue, { value: textIn(definition, field, relations.get(field), draft, locale) || "—" })) : (_jsx(LineValue, { definition: definition, field: field, line: draft, set: set, label: view.label, invalid: error !== undefined, locale: locale, inCell: false, own: controls[field] })) }, field));
188
+ }) }));
189
+ } }));
190
+ }
@@ -1,3 +1,4 @@
1
+ import type { AnyRecord } from "@sincpro/criteria";
1
2
  import type { Entity, EntityRef } from "../../core/entity.js";
2
3
  import type { OpenTarget } from "@sincpro/web-ui";
3
4
  import { type ReactNode } from "react";
@@ -15,9 +16,16 @@ export type RecordOpening = {
15
16
  open: (ref: EntityRef, to: OpenTarget) => void;
16
17
  /** Where it may open from here, the default first. */
17
18
  targets: readonly OpenTarget[];
19
+ /** The form is not on a page — a panel, a peek, a modal: a list inside it starts as rows
20
+ * (a table of seven columns in 760 px breaks every number and date into three lines). */
21
+ narrow?: boolean;
18
22
  /** «Buscar más…»: the entity's own list opened to choose (in composer, a modal that answers),
19
23
  * on what was typed. Without it the picker brings its own chooser. */
20
24
  choose?: (entity: Entity, answer: (id: string) => void, query: string) => void;
25
+ /** «Agregar» on a one2many: the form of a new record of `entity`, starting with `values`
26
+ * (the key that points back here), opened where it answers; `created` once it exists.
27
+ * Without it a record's one2many only reads. */
28
+ create?: (entity: Entity, values: AnyRecord, created: () => void) => void;
21
29
  };
22
30
  export declare function RecordOpeningProvider({ value, children, }: {
23
31
  value: RecordOpening;
@@ -8,15 +8,27 @@ function lastChange(summary) {
8
8
  return copy.updatedLine(updated.at, updated.by);
9
9
  return created.at === null ? null : copy.createdLine(created.at, created.by);
10
10
  }
11
+ /** Its picture: a person's or a company's face is round, with the initials when there is no
12
+ * photo (`person`); anything else is its square photo, or nothing. The same in every size. */
13
+ function mediaOf(summary) {
14
+ if (summary.person)
15
+ return {
16
+ person: true,
17
+ alt: summary.name,
18
+ ...(summary.image === null ? {} : { src: summary.image }),
19
+ };
20
+ return summary.image === null ? undefined : { src: summary.image, alt: summary.name };
21
+ }
11
22
  /** The board's card: the state above, the name, what it is, its description, its last change. */
12
23
  export function cardOf(summary) {
13
24
  const footer = lastChange(summary);
25
+ const media = mediaOf(summary);
14
26
  return {
15
27
  title: summary.name,
16
28
  ...(summary.type === null ? {} : { subtitle: summary.type }),
17
29
  ...(summary.state === null ? {} : { tags: _jsx(Tag, { tone: "neutral", children: summary.state }) }),
18
30
  ...(summary.description === null ? {} : { meta: summary.description }),
19
- ...(summary.image === null ? {} : { media: { src: summary.image, alt: summary.name } }),
31
+ ...(media === undefined ? {} : { media }),
20
32
  ...(footer === null
21
33
  ? {}
22
34
  : { footer: _jsx("span", { className: "text-micro text-muted", children: footer }) }),
@@ -36,11 +48,7 @@ export function relationOf(summary) {
36
48
  ? []
37
49
  : [{ label: copy.updated, value: copy.when(summary.updated.at, summary.updated.by) }]),
38
50
  ];
39
- const media = summary.person
40
- ? { person: true, ...(summary.image === null ? {} : { src: summary.image }) }
41
- : summary.image === null
42
- ? undefined
43
- : { src: summary.image, alt: summary.name };
51
+ const media = mediaOf(summary);
44
52
  const secondary = summary.type ?? (summary.id === summary.name ? null : summary.id);
45
53
  const card = {
46
54
  ...(summary.state === null ? {} : { tag: { label: summary.state } }),
@@ -16,7 +16,8 @@ import { type ComponentType } from "react";
16
16
  *
17
17
  * A reference with no id is the list — opened to choose one when it carries `pick` — and one
18
18
  * with `new` is the record's own view with no id: the form of a record that does not exist yet
19
- * (`entity.newRef()`), which `useRecord(entity, "")` fills with the model's defaults.
19
+ * (`entity.newRef(values)`), which `useRecord(entity, resource)` fills with the model's defaults
20
+ * and what the reference carries.
20
21
  *
21
22
  * ⌘K asks the server with `matching` — 8 records, identity and display — the same question a
22
23
  * many2one asks. The model definition is asked for once, on the first search, unless `meta`
@@ -5,26 +5,31 @@ import { NEW_PARAM } from "../../core/entity.js";
5
5
  import { lookupOf } from "../../core/lookup.js";
6
6
  import { useListing } from "../../react/useListing.js";
7
7
  import { EntityList } from "../list/EntityList.js";
8
+ import { presentationOf } from "../list/presentation.js";
8
9
  import { RecordOpeningProvider, } from "../record/opening.js";
9
10
  import { openIn, RECORD_SIZE, targetsFor } from "./open.js";
10
11
  import { EntityRecord } from "./record.js";
11
12
  import { useMemo } from "react";
12
13
  /**
13
14
  * The fields of a view open their records in the workspace: a modal over this surface by
14
- * default, beside it from inside a modal, and «Buscar más…» as the entity's list that answers.
15
+ * default, beside it from inside a modal, «Buscar más…» as the entity's list that answers, and
16
+ * a one2many's «Agregar» as the new record's form that answers once it exists.
15
17
  */
16
18
  function InWorkspace({ api, children }) {
17
19
  const targets = targetsFor(api);
18
- const opening = useMemo(() => ({
19
- open: (ref, to) => openIn(api, ref, to),
20
- targets,
21
- choose: (entity, answer, query) => api.pick(entity.chooserRef(query), (ref) => ref.id === undefined || answer(ref.id), {
22
- // Never a modal over a modal: from inside one, the chooser docks beside it.
23
- ...(targets[0] === "modal"
24
- ? { to: "modal", size: RECORD_SIZE }
25
- : { to: "side" }),
26
- }),
27
- }), [api, targets]);
20
+ const opening = useMemo(() => {
21
+ // Never a modal over a modal: from inside one, what answers docks beside it.
22
+ const answering = targets[0] === "modal"
23
+ ? { to: "modal", size: RECORD_SIZE }
24
+ : { to: "side" };
25
+ return {
26
+ open: (ref, to) => openIn(api, ref, to),
27
+ targets,
28
+ narrow: api.surface !== "page",
29
+ choose: (entity, answer, query) => api.pick(entity.chooserRef(query), (ref) => ref.id === undefined || answer(ref.id), answering),
30
+ create: (entity, values, created) => api.pick(entity.newRef(values), () => created(), answering),
31
+ };
32
+ }, [api, targets]);
28
33
  return _jsx(RecordOpeningProvider, { value: opening, children: children });
29
34
  }
30
35
  /** The entity's list when the application writes none. */
@@ -65,11 +70,17 @@ export function entityView(entity, options) {
65
70
  ...matching(meta, query),
66
71
  meta: false,
67
72
  });
68
- return lookupOf(meta, page.rows).map((one) => ({
69
- ref: entity.ref(one.id),
70
- label: one.label,
71
- ...(one.detail === undefined ? {} : { detail: one.detail }),
72
- }));
73
+ // ⌘K writes text: of the entity's look of a choice, its name and its meta.
74
+ const look = presentationOf(entity).option;
75
+ return lookupOf(meta, page.rows, entity.summary).map((one) => {
76
+ const own = look === undefined || one.record === undefined ? {} : look(one.record);
77
+ const detail = own.meta ?? one.detail;
78
+ return {
79
+ ref: entity.ref(one.id),
80
+ label: own.name ?? one.label,
81
+ ...(detail === undefined ? {} : { detail }),
82
+ };
83
+ });
73
84
  },
74
85
  load: async () => EntityView,
75
86
  };
@@ -1,5 +1,5 @@
1
1
  import type { AnyRecord, Meta } from "@sincpro/criteria";
2
- import type { OpenOptions, ResourceRef, WorkspaceApi } from "@sincpro/web-composer";
2
+ import type { ResourceRef, WorkspaceApi } from "@sincpro/web-composer";
3
3
  import type { Entity } from "../../core/entity.js";
4
4
  import { type OpenTarget } from "@sincpro/web-ui";
5
5
  import type { ReactNode } from "react";
@@ -19,19 +19,7 @@ import type { ReactNode } from "react";
19
19
  *
20
20
  * The phone is composer's: a modal becomes a sheet, a side panel the page. No view asks.
21
21
  */
22
- /** A record's form fits; a short task (register a payment) passes its own size. */
23
- export declare const RECORD_SIZE: {
24
- readonly w: 760;
25
- readonly h: 560;
26
- };
27
- /** Where a related record may open from this view, the default first. */
28
- export declare function targetsFor(api: Pick<WorkspaceApi, "over">): readonly OpenTarget[];
29
- /** web-ui's target as composer's options. Destinations share their names, so only the size is
30
- * added — to the two that have one. */
31
- export declare function openOptions(to: OpenTarget, size?: {
32
- w: number;
33
- h: number;
34
- }): OpenOptions;
22
+ export { openOptions, RECORD_SIZE, targetsFor } from "../list/openIn.js";
35
23
  /**
36
24
  * Open a record where it was asked, or where it belongs from here.
37
25
  *
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
3
  import { useSummaryOf } from "../../react/useSummary.js";
4
+ import { openOptions, targetsFor } from "../list/openIn.js";
4
5
  import { relationOf } from "../record/summary.js";
5
6
  import { RelationValue } from "@sincpro/web-ui";
6
7
  /**
@@ -19,19 +20,7 @@ import { RelationValue } from "@sincpro/web-ui";
19
20
  *
20
21
  * The phone is composer's: a modal becomes a sheet, a side panel the page. No view asks.
21
22
  */
22
- /** A record's form fits; a short task (register a payment) passes its own size. */
23
- export const RECORD_SIZE = { w: 760, h: 560 };
24
- const IN_PLACE = ["modal", "peek", "side", "page"];
25
- const FROM_A_MODAL = ["side", "peek", "page"];
26
- /** Where a related record may open from this view, the default first. */
27
- export function targetsFor(api) {
28
- return api.over === null ? IN_PLACE : FROM_A_MODAL;
29
- }
30
- /** web-ui's target as composer's options. Destinations share their names, so only the size is
31
- * added — to the two that have one. */
32
- export function openOptions(to, size = RECORD_SIZE) {
33
- return to === "modal" || to === "window" ? { to, size } : { to };
34
- }
23
+ export { openOptions, RECORD_SIZE, targetsFor } from "../list/openIn.js";
35
24
  /**
36
25
  * Open a record where it was asked, or where it belongs from here.
37
26
  *
@@ -13,7 +13,7 @@ import { useSummaryOf } from "../../react/useSummary.js";
13
13
  import { dirtyOf, RecordField } from "../record/edit.js";
14
14
  import { Refused } from "../Refused.js";
15
15
  import { useRecordInWorkspace } from "./workspace.js";
16
- import { Button, Callout, Record, Skeleton } from "@sincpro/web-ui";
16
+ import { Button, Callout, FormSection, Record, Skeleton } from "@sincpro/web-ui";
17
17
  import { useState } from "react";
18
18
  /**
19
19
  * The record of an entity, drawn from its model — the screen `entityView` opens when the
@@ -32,7 +32,7 @@ import { useState } from "react";
32
32
  * actions={(invoice) => <CommandButton entity={invoices} command="post" … />} /> })
33
33
  */
34
34
  export function EntityRecord({ api, resource, entity, fields, pages, actions, }) {
35
- const reading = useRecord(entity, resource.id ?? "");
35
+ const reading = useRecord(entity, resource);
36
36
  const crumbs = useCrumbs(api);
37
37
  useRecordInWorkspace(api, reading);
38
38
  const history = useHistory(entity, reading.isNew ? "" : reading.id, [reading.record]);
@@ -62,6 +62,7 @@ export function EntityRecord({ api, resource, entity, fields, pages, actions, })
62
62
  const { meta, locale, edit } = reading;
63
63
  const layout = formOf(meta, entity.summary);
64
64
  const shown = fields ?? layout.fields;
65
+ const ownActions = actions?.(reading) ?? null;
65
66
  const tabs = pages ?? layout.pages;
66
67
  const summary = reading.isNew ? undefined : summarize(reading.record, meta);
67
68
  const dirty = dirtyOf(reading);
@@ -76,5 +77,7 @@ export function EntityRecord({ api, resource, entity, fields, pages, actions, })
76
77
  title: summary?.name ?? copy.creating(nameOf(meta, locale) || ""),
77
78
  ...(summary?.type == null ? {} : { subtitle: summary.type }),
78
79
  ...(summary?.state == null ? {} : { tag: { label: summary.state } }),
79
- }, ...(dirty === undefined ? {} : { dirty }), ...(edit.failure === null ? {} : { error: String(edit.failure.message) }), children: [_jsxs(Record.Rail, { children: [crumbs.length === 0 ? null : _jsx(Record.Crumbs, { crumbs: crumbs }), place === null ? null : (_jsx(Record.Pager, { index: place.index, total: place.total, onPrev: go(-1), onNext: go(1) })), _jsx(Record.Actions, { children: actions?.(reading) ?? null })] }), _jsxs(Record.Paper, { layout: "horizontal", children: [_jsxs("div", { className: "flex flex-col gap-3 p-4", children: [edit.conflict ? (_jsx(Callout, { tone: "warn", title: copy.conflict, children: _jsx(Button, { size: "sm", role: "secondary", onClick: reading.reload, children: copy.reload }) })) : null, edit.restored ? (_jsx(Callout, { tone: "info", title: copy.restored, children: null })) : null, shown.map((name) => (_jsx(RecordField, { record: reading, name: name }, name))), edit.advice.map((one) => (_jsx(Callout, { tone: "info", title: one.text, children: null }, one.text)))] }), tabs.length === 0 || reading.isNew ? null : (_jsx(Record.Pages, { current: page ?? tabs[0], onChange: setPage, children: tabs.map((name) => (_jsx(Record.Page, { id: name, label: labelOf(meta, name, locale), children: _jsx(RecordField, { record: reading, name: name, bare: true }) }, name))) })), createdStamp === undefined && updatedStamp === undefined ? null : (_jsx(Record.Audit, { ...(createdStamp === undefined ? {} : { created: createdStamp }), ...(updatedStamp === undefined ? {} : { updated: updatedStamp }) }))] }), history === undefined ? null : _jsx(Record.Activity, { events: history })] }));
80
+ }, ...(dirty === undefined ? {} : { dirty }), ...(edit.failure === null ? {} : { error: String(edit.failure.message) }), children: [crumbs.length === 0 && place === null && ownActions === null ? null : (_jsxs(Record.Rail, { children: [crumbs.length === 0 ? null : _jsx(Record.Crumbs, { crumbs: crumbs }), place === null ? null : (_jsx(Record.Pager, { index: place.index, total: place.total, onPrev: go(-1), onNext: go(1) })), ownActions === null ? null : _jsx(Record.Actions, { children: ownActions })] })), _jsxs(Record.Paper, { layout: "horizontal", children: [edit.conflict || edit.restored ? (_jsxs("div", { className: "flex flex-col gap-3", children: [edit.conflict ? (_jsx(Callout, { tone: "warn", title: copy.conflict, children: _jsx(Button, { size: "sm", role: "secondary", onClick: reading.reload, children: copy.reload }) })) : null, edit.restored ? (_jsx(Callout, { tone: "info", title: copy.restored, children: null })) : null] })) : null, _jsx(FormSection, { columns: api.surface === "page" || api.surface === "modal" ? 2 : 1, children: shown.map((name) => (_jsx(RecordField, { record: reading, name: name }, name))) }), edit.advice
81
+ .filter((one) => (one.path?.length ?? 0) <= 1)
82
+ .map((one) => (_jsx(Callout, { tone: "info", title: one.message, children: null }, one.message))), tabs.length === 0 || reading.isNew ? null : (_jsx(Record.Pages, { current: page ?? tabs[0], onChange: setPage, children: tabs.map((name) => (_jsx(Record.Page, { id: name, label: labelOf(meta, name, locale), children: _jsx(RecordField, { record: reading, name: name, bare: true }) }, name))) })), createdStamp === undefined && updatedStamp === undefined ? null : (_jsx(Record.Audit, { ...(createdStamp === undefined ? {} : { created: createdStamp }), ...(updatedStamp === undefined ? {} : { updated: updatedStamp }) }))] }), history === undefined ? null : _jsx(Record.Activity, { events: history })] }));
80
83
  }
@@ -13,9 +13,11 @@ import type { RecordReading } from "../../react/useRecord.js";
13
13
  *
14
14
  * A NEW record (opened with `entity.newRef()`) reads «Facturas · nuevo» until it exists; the
15
15
  * moment its create answers, the surface is REPLACED by the record it became (`api.replace`):
16
- * no crumb leads back to an empty form, and the window, peek or page it was in stays.
16
+ * no crumb leads back to an empty form, and the window, peek or page it was in stays. Opened
17
+ * to ANSWER (`api.pick(entity.newRef(…))` — «Agregar» on a record's one2many), it answers the
18
+ * record it became instead, and the surface closes: whoever asked reads it again.
17
19
  *
18
- * const invoice = useRecord(invoices, resource.id ?? "");
20
+ * const invoice = useRecord(invoices, resource);
19
21
  * useRecordInWorkspace(api, invoice);
20
22
  */
21
23
  export declare function useRecordInWorkspace<T extends AnyRecord>(api: WorkspaceApi, record: RecordReading<T>): void;
@@ -15,9 +15,11 @@ import { useEffect } from "react";
15
15
  *
16
16
  * A NEW record (opened with `entity.newRef()`) reads «Facturas · nuevo» until it exists; the
17
17
  * moment its create answers, the surface is REPLACED by the record it became (`api.replace`):
18
- * no crumb leads back to an empty form, and the window, peek or page it was in stays.
18
+ * no crumb leads back to an empty form, and the window, peek or page it was in stays. Opened
19
+ * to ANSWER (`api.pick(entity.newRef(…))` — «Agregar» on a record's one2many), it answers the
20
+ * record it became instead, and the surface closes: whoever asked reads it again.
19
21
  *
20
- * const invoice = useRecord(invoices, resource.id ?? "");
22
+ * const invoice = useRecord(invoices, resource);
21
23
  * useRecordInWorkspace(api, invoice);
22
24
  */
23
25
  export function useRecordInWorkspace(api, record) {
@@ -30,10 +32,14 @@ export function useRecordInWorkspace(api, record) {
30
32
  const dirty = record.edit.dirty;
31
33
  const opened = api.self.id ?? "";
32
34
  const { entity, id } = record;
33
- // Created here: show what it became, in place of the form.
35
+ // Created here: show what it became in place of the form — or hand it to whoever asked.
34
36
  useEffect(() => {
35
- if (opened === "" && id !== "")
37
+ if (opened !== "" || id === "")
38
+ return;
39
+ if (api.choosing === null)
36
40
  api.replace(entity.ref(id));
41
+ else
42
+ api.answer(entity.ref(id));
37
43
  }, [api, opened, entity, id]);
38
44
  useEffect(() => {
39
45
  api.setTitle(title);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sincpro/web-framework",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "The web client of sincpro_framework: entities, lists and records bound to the metadata the server publishes, drawn with @sincpro/web-ui and arranged with @sincpro/web-composer.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -23,19 +23,19 @@
23
23
  "printWidth": 94
24
24
  },
25
25
  "peerDependencies": {
26
- "@sincpro/criteria": "^0.3.0",
27
- "@sincpro/criteria-react": "^0.3.0",
28
- "@sincpro/web-composer": "^1.4.0",
29
- "@sincpro/web-ui": "^1.1.0",
26
+ "@sincpro/criteria": "^0.3.1",
27
+ "@sincpro/criteria-react": "^0.3.1",
28
+ "@sincpro/web-composer": "^1.5.0",
29
+ "@sincpro/web-ui": "^1.7.2",
30
30
  "react": ">=19",
31
31
  "react-dom": ">=19"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@eslint/js": "^10.0.1",
35
- "@sincpro/criteria": "^0.3.0",
36
- "@sincpro/criteria-react": "^0.3.0",
37
- "@sincpro/web-composer": "^1.4.0",
38
- "@sincpro/web-ui": "^1.1.0",
35
+ "@sincpro/criteria": "^0.3.1",
36
+ "@sincpro/criteria-react": "^0.3.1",
37
+ "@sincpro/web-composer": "^1.5.0",
38
+ "@sincpro/web-ui": "^1.7.2",
39
39
  "@storybook/addon-a11y": "10.6.5",
40
40
  "@storybook/addon-docs": "10.6.5",
41
41
  "@storybook/addon-mcp": "10.6.5",