@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,64 @@
1
+ import { labelOf } from "@sincpro/criteria";
2
+ import { frameworkCopy } from "../../core/copy.js";
3
+ import { textOf } from "../../core/present.js";
4
+ /**
5
+ * web-ui's `Pivot` (`folded`), from what the SERVER counted (`usePivot`): the rows axis is the
6
+ * outer groups, the columns axis every inner value that appeared, each crossing an inner group,
7
+ * each row's total its outer group, each column's total the grouping by the columns alone.
8
+ * Nothing is added up here — a margin is read from the base, never summed from cells.
9
+ *
10
+ * A key is how its value READS («Pagada», «Efectivo»): the grid draws keys, and the server's
11
+ * value («paid») is not a word for a person.
12
+ */
13
+ export function pivotOf({ crossed, byColumn, rows, columns, measures, meta, locale, }) {
14
+ const none = frameworkCopy().list.noValue;
15
+ const keyOf = (bucket) => bucket.value === null || bucket.value === undefined || bucket.value === ""
16
+ ? none
17
+ : (textOf(meta, bucket.field, bucket.value, locale) ?? String(bucket.value));
18
+ const cellOf = (bucket) => ({
19
+ count: bucket.count,
20
+ values: Object.fromEntries(measures.map((name) => {
21
+ const value = bucket.measures?.[name];
22
+ return [name, value === null || value === undefined ? null : Number(value)];
23
+ })),
24
+ });
25
+ const rowKeys = crossed.map((bucket) => [keyOf(bucket)]);
26
+ // Columns in the server's order of the columns alone; any value only a crossing has, after.
27
+ const seen = new Set(byColumn.map(keyOf));
28
+ const columnKeys = [...seen];
29
+ for (const outer of crossed)
30
+ for (const inner of outer.groups ?? []) {
31
+ const key = keyOf(inner);
32
+ if (!seen.has(key)) {
33
+ seen.add(key);
34
+ columnKeys.push(key);
35
+ }
36
+ }
37
+ const at = new Map();
38
+ const rowTotals = new Map();
39
+ for (const outer of crossed) {
40
+ const row = keyOf(outer);
41
+ rowTotals.set(row, cellOf(outer));
42
+ for (const inner of outer.groups ?? [])
43
+ at.set(`${row}\u0000${keyOf(inner)}`, cellOf(inner));
44
+ }
45
+ const columnTotals = new Map(byColumn.map((bucket) => [keyOf(bucket), cellOf(bucket)]));
46
+ return {
47
+ rows: {
48
+ levels: [{ ...rows, label: labelOf(meta, rows.field, locale) }],
49
+ keys: rowKeys,
50
+ },
51
+ columns: {
52
+ levels: [{ ...columns, label: labelOf(meta, columns.field, locale) }],
53
+ keys: columnKeys.map((key) => [key]),
54
+ },
55
+ measures: measures.map((name) => ({
56
+ id: name,
57
+ header: labelOf(meta, name, locale),
58
+ format: (value) => textOf(meta, name, value, locale) ?? String(value),
59
+ })),
60
+ cell: (row, column) => at.get(`${row[0] ?? ""}\u0000${column[0] ?? ""}`),
61
+ rowTotal: (row) => rowTotals.get(row[0] ?? ""),
62
+ columnTotal: (column) => columnTotals.get(column[0] ?? ""),
63
+ };
64
+ }
@@ -0,0 +1,51 @@
1
+ import type { AnyRecord } from "@sincpro/criteria";
2
+ import type { Entity } from "../../core/entity.js";
3
+ import { type ListDefinition } from "../../core/list.js";
4
+ import type { PickerItem, RecordCard, RowOf } from "@sincpro/web-ui";
5
+ import type { ComponentProps } from "react";
6
+ /**
7
+ * How a record of an entity looks at each size — a choice, a row, a card — declared ONCE with
8
+ * the entity and used wherever the framework draws one of its records.
9
+ *
10
+ * Context: the name of a record is the backend's (`meta.display`, Odoo's `display_name`) and
11
+ * its parts are the summary's (`entity(…, { summary })`); what was missing is how it is DRAWN.
12
+ * Every place built its own: the picker's option was always name + key + face, the rows face
13
+ * name + three facts, the board's card whatever the row said. Now an application says it once:
14
+ *
15
+ * export const customers = presented(entity<Customer>("sales:customer", { list: "/customers" }), {
16
+ * option: (c) => ({ meta: c.tax_id, aside: <Saldo value={c.balance} /> }),
17
+ * row: (c) => ({ tags: c.blocked ? <Tag tone="bad">Bloqueado</Tag> : undefined }),
18
+ * card: (c) => ({ subtitle: c.city, figure: <Saldo value={c.balance} /> }),
19
+ * });
20
+ *
21
+ * Each size answers the PROPS of web-ui's piece for it (`PickerItem`, `RowOf`, `RecordCard`),
22
+ * merged OVER the default: give only what changes, and what is not given stays as it was.
23
+ * Props and not components for the reason `factsOf`/`columnsOf` give: web-ui keeps the
24
+ * keyboard, the highlight of what was typed, the press rule and the accessibility of the
25
+ * piece, and finds its slots by type. A whole component of the application in an option's
26
+ * place needs web-ui to take one (`ROADMAP.md`).
27
+ *
28
+ * A function around `entity()` and not a field of it: `core/` draws nothing and cannot name a
29
+ * React node; the presentation is the web's, so it is added here. The result IS the entity —
30
+ * every place that takes one takes it — with its presentation on board.
31
+ */
32
+ /** A choice in a many2one, a many2many and ⌘K. ⌘K writes only text: `name` and `meta`. */
33
+ export type OptionLook = Partial<Omit<PickerItem, "id">>;
34
+ /** A row of the «rows» face — and of the board's card when there is no `card`. */
35
+ export type RowLook = Partial<RowOf>;
36
+ /** The board's card. Opening it stays the list's: `onOpen` is not the presentation's. */
37
+ export type CardLook = Partial<Omit<ComponentProps<typeof RecordCard>, "onOpen">>;
38
+ export type Presentation<T> = {
39
+ option?: (record: T) => OptionLook;
40
+ row?: (record: T) => RowLook;
41
+ card?: (record: T) => CardLook;
42
+ };
43
+ export type Presented<T extends AnyRecord> = Entity<T> & {
44
+ readonly presentation: Presentation<T>;
45
+ };
46
+ /** The entity with how its records look: pass it wherever an entity goes. */
47
+ export declare function presented<T extends AnyRecord>(entity: Entity<T>, presentation: Presentation<T>): Presented<T>;
48
+ /** The presentation an entity — or the entity of a named list — was declared with; none is `{}`. */
49
+ export declare function presentationOf<T extends AnyRecord>(source: Entity<T> | ListDefinition<T>): Presentation<T>;
50
+ /** `look` over `base`, leaving out what `look` sets to `undefined`: «no tags» is not a hole. */
51
+ export declare function over<B extends object, L extends object>(base: B, look: L | undefined): B;
@@ -0,0 +1,20 @@
1
+ import { isList } from "../../core/list.js";
2
+ /** The entity with how its records look: pass it wherever an entity goes. */
3
+ export function presented(entity, presentation) {
4
+ return { ...entity, presentation };
5
+ }
6
+ /** The presentation an entity — or the entity of a named list — was declared with; none is `{}`. */
7
+ export function presentationOf(source) {
8
+ const base = isList(source) ? source.entity : source;
9
+ return "presentation" in base ? base.presentation : {};
10
+ }
11
+ /** `look` over `base`, leaving out what `look` sets to `undefined`: «no tags» is not a hole. */
12
+ export function over(base, look) {
13
+ if (look === undefined)
14
+ return base;
15
+ const out = { ...base };
16
+ for (const [key, value] of Object.entries(look))
17
+ if (value !== undefined)
18
+ out[key] = value;
19
+ return out;
20
+ }
@@ -41,7 +41,9 @@ export declare function EntityPicker<T extends AnyRecord>({ entity, value, onCha
41
41
  /** Which records may be chosen at all: applied by the server under what is typed. */
42
42
  criteria?: Criteria<T>;
43
43
  value?: string;
44
- onChange: (id: string) => void;
44
+ /** The key chosen, and its record when the choice brought it — what a line keeps beside the
45
+ * key so its cell names the choice without asking again. */
46
+ onChange: (id: string, chosen?: T) => void;
45
47
  /** Open the chosen record where the person asked — in composer,
46
48
  * `(id, to) => openIn(api, entity.ref(id), to)`. */
47
49
  onOpen?: (id: string, to: OpenTarget) => void;
@@ -6,6 +6,7 @@ import { isList } from "../../core/list.js";
6
6
  import { useListing } from "../../react/useListing.js";
7
7
  import { useLookup } from "../../react/useLookup.js";
8
8
  import { EntityList } from "../list/EntityList.js";
9
+ import { over, presentationOf } from "../list/presentation.js";
9
10
  import { Dialog, IconButton, Picker } from "@sincpro/web-ui";
10
11
  import { TextSearch } from "lucide-react";
11
12
  import { useState } from "react";
@@ -44,19 +45,28 @@ export function EntityPicker({ entity, value, onChange, onOpen, openTargets, onB
44
45
  ...(value === undefined ? {} : { value }),
45
46
  });
46
47
  const person = (isList(entity) ? entity.entity : entity).summary.person === true;
48
+ const look = presentationOf(entity).option;
47
49
  const [choosing, setChoosing] = useState(false);
48
50
  const browseAll = onBrowseAll ?? (() => setChoosing(true));
49
- const picker = (_jsx(Picker, { items: lookup.options.map((one) => ({
51
+ const picker = (_jsx(Picker, { items: lookup.options.map((one) => over({
50
52
  id: one.id,
51
53
  name: one.label,
52
54
  ...(one.detail === undefined ? {} : { meta: one.detail, haystack: one.detail }),
53
55
  // A person or a company is chosen by their face: the photo, or their initials.
54
56
  ...(person ? { avatar: one.image ?? true } : {}),
55
- })), ...(value === undefined ? {} : { value }), ...(lookup.chosen === undefined ? {} : { valueLabel: lookup.chosen.label }), onChange: onChange, ...(lookup.searchable ? { onSearch: lookup.search } : {}),
57
+ },
58
+ // The entity's own look of a choice, over the default (`presented`).
59
+ look === undefined || one.record === undefined ? undefined : look(one.record))), ...(value === undefined ? {} : { value }), ...(lookup.chosen === undefined
60
+ ? {}
61
+ : {
62
+ valueLabel: (lookup.chosen.record === undefined || look === undefined
63
+ ? undefined
64
+ : look(lookup.chosen.record).name) ?? lookup.chosen.label,
65
+ }), onChange: (id) => onChange(id, lookup.options.find((one) => one.id === id)?.record), ...(lookup.searchable ? { onSearch: lookup.search } : {}),
56
66
  // «Loading» only before there is anything: refining keeps the ten on screen.
57
67
  loading: lookup.loading && lookup.options.length === 0, ...(onOpen === undefined ? {} : { onOpen }), ...(openTargets === undefined ? {} : { openTargets }), onBrowseAll: browseAll, ...(placeholder === undefined ? {} : { placeholder }), ...(disabled === undefined ? {} : { disabled }), ...(ariaLabel === undefined ? {} : { "aria-label": ariaLabel }) }));
58
- return (_jsxs(_Fragment, { children: [browse ? (_jsxs("span", { className: "flex min-w-0 items-start gap-1", children: [_jsx("span", { className: "min-w-0 flex-1", children: picker }), _jsx(IconButton, { icon: TextSearch, label: frameworkCopy().picker.browse, onClick: () => browseAll(lookup.text), ...(disabled === undefined ? {} : { disabled }) })] })) : (picker), choosing ? (_jsx(Chooser, { entity: entity, ...(columns === undefined ? {} : { columns }), ...(criteria === undefined ? {} : { criteria }), onChoose: (id) => {
59
- onChange(id);
68
+ return (_jsxs(_Fragment, { children: [browse ? (_jsxs("span", { className: "flex min-w-0 items-start gap-1", children: [_jsx("span", { className: "min-w-0 flex-1", children: picker }), _jsx(IconButton, { icon: TextSearch, label: frameworkCopy().picker.browse, onClick: () => browseAll(lookup.text), ...(disabled === undefined ? {} : { disabled }) })] })) : (picker), choosing ? (_jsx(Chooser, { entity: entity, ...(columns === undefined ? {} : { columns }), ...(criteria === undefined ? {} : { criteria }), onChoose: (id, row) => {
69
+ onChange(id, row);
60
70
  setChoosing(false);
61
71
  }, onClose: () => setChoosing(false) })) : null] }));
62
72
  }
@@ -69,5 +79,5 @@ function Chooser({ entity, columns, criteria, onChoose, onClose, }) {
69
79
  });
70
80
  const key = listing.meta?.get_id || listing.meta?.identity || "id";
71
81
  return (_jsx(Dialog, { open: true, width: "lg", title: (isList(entity) ? entity.title : nameOf(listing.meta, listing.locale)) ||
72
- listing.entity.kind, onClose: onClose, children: _jsx(EntityList, { listing: listing, embedded: true, search: "box", ...(columns === undefined ? {} : { columns }), onOpen: (row) => onChoose(String(row[key])) }) }));
82
+ listing.entity.kind, onClose: onClose, children: _jsx(EntityList, { listing: listing, embedded: true, ...(columns === undefined ? {} : { columns }), onOpen: (row) => onChoose(String(row[key]), row) }) }));
73
83
  }
@@ -2,6 +2,7 @@
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
3
  import { isList } from "../../core/list.js";
4
4
  import { useLookup } from "../../react/useLookup.js";
5
+ import { presentationOf } from "../list/presentation.js";
5
6
  import { TagsField } from "@sincpro/web-ui";
6
7
  /**
7
8
  * A many2many: put SEVERAL records of an entity on this one — the followers of an invoice,
@@ -26,11 +27,21 @@ export function EntityTags({ entity, value, onChange, criteria, onOpen, openTarg
26
27
  values: value,
27
28
  });
28
29
  const person = (isList(entity) ? entity.entity : entity).summary.person === true;
29
- const asTag = (one) => ({
30
- value: one.id,
31
- label: one.label,
32
- ...(person ? { avatar: one.image ?? true } : {}),
33
- });
30
+ const look = presentationOf(entity).option;
31
+ // A chip says a name and shows a face; the list under it also the datum that tells two apart
32
+ // (web-ui 1.5 `TagOption.meta`, never on the chip). Of the entity's look of a choice, those
33
+ // three — as text: a tag option is plain.
34
+ const asTag = (one) => {
35
+ const own = look === undefined || one.record === undefined ? {} : look(one.record);
36
+ const avatar = own.avatar ?? (person ? (one.image ?? true) : undefined);
37
+ const meta = typeof own.meta === "string" ? own.meta : one.detail;
38
+ return {
39
+ value: one.id,
40
+ label: own.name ?? one.label,
41
+ ...(avatar === undefined ? {} : { avatar }),
42
+ ...(meta === undefined || meta === "" ? {} : { meta }),
43
+ };
44
+ };
34
45
  const offered = new Set(lookup.options.map((one) => one.id));
35
46
  // The chosen ride along for their labels; the field never offers them again.
36
47
  const options = [
@@ -10,6 +10,12 @@ import { type EntityListProps } from "../list/EntityList.js";
10
10
  * entity's own search template: the lines of an order are searched by product, the invoices of
11
11
  * a customer by number. The column that points back to this record is left out — it is the
12
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.
13
19
  */
14
20
  export declare function RelatedList<P extends AnyRecord, T extends AnyRecord>({ record, name, entity, pageSize, ...list }: {
15
21
  record: RecordReading<P>;
@@ -1,8 +1,11 @@
1
1
  "use client";
2
- import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { frameworkCopy } from "../../core/copy.js";
3
4
  import { defaultFields } from "../../core/present.js";
4
5
  import { useRelated } from "../../react/useRelated.js";
5
6
  import { EntityList } from "../list/EntityList.js";
7
+ import { useRecordOpening } from "./opening.js";
8
+ import { Button } from "@sincpro/web-ui";
6
9
  import { useMemo } from "react";
7
10
  /**
8
11
  * A relation of a record as a list inside it — searched, ordered and paged by the server.
@@ -12,9 +15,16 @@ import { useMemo } from "react";
12
15
  * entity's own search template: the lines of an order are searched by product, the invoices of
13
16
  * a customer by number. The column that points back to this record is left out — it is the
14
17
  * same on every row. Use `RecordLines` when the page the record brought is all there is.
18
+ *
19
+ * «Agregar» opens the form of a new related record already pointing back here, where the view
20
+ * says (`RecordOpening.create`: a modal that answers, in a workspace), and the list is read
21
+ * again once it exists. Never a row of the table: each one is an aggregate with its own create,
22
+ * rules and preview. There is none while this record does not exist yet — there is nothing to
23
+ * point back to — nor when the relation is read only or the entity cannot be created.
15
24
  */
16
25
  export function RelatedList({ record, name, entity, pageSize, ...list }) {
17
26
  const listing = useRelated(record, name, entity, pageSize === undefined ? {} : { pageSize });
27
+ const opening = useRecordOpening();
18
28
  // Every row points back to THIS record: the column that says so is the same on all of them.
19
29
  const back = record.meta?.fields[name]?.identified_by;
20
30
  const own = list.columns;
@@ -22,5 +32,19 @@ export function RelatedList({ record, name, entity, pageSize, ...list }) {
22
32
  (listing.meta === null || !back
23
33
  ? undefined
24
34
  : defaultFields(listing.meta).filter((field) => field !== back)), [own, listing.meta, back]);
25
- return (_jsx(EntityList, { listing: listing, embedded: true, search: "box", ...list, ...(columns === undefined ? {} : { columns }) }));
35
+ const create = opening?.create;
36
+ // The value the rows point back with — the same key `useRelated` scopes the list by.
37
+ const identity = record.meta?.get_id || record.meta?.identity || "id";
38
+ const here = record.record === null ? undefined : record.record[identity];
39
+ const adding = create === undefined ||
40
+ entity.create === undefined ||
41
+ !back ||
42
+ record.isNew ||
43
+ here === undefined ||
44
+ record.edit.state(name).readonly ? null : (_jsx(Button, { size: "sm", role: "secondary", onClick: () => create(entity, { [back]: here }, listing.reload), children: frameworkCopy().record.add }));
45
+ return (_jsx(EntityList, { listing: listing, embedded: true, ...list, ...(columns === undefined ? {} : { columns }), ...(adding === null && list.actions === undefined
46
+ ? {}
47
+ : {
48
+ actions: (_jsxs(_Fragment, { children: [list.actions, adding] })),
49
+ }) }));
26
50
  }
@@ -0,0 +1,42 @@
1
+ import type { AnyRecord } from "@sincpro/criteria";
2
+ import type { Entity } from "../../core/entity.js";
3
+ import type { ReactNode } from "react";
4
+ /** What a control of a field receives: its value, how to change it, and its state now. */
5
+ export type FieldControl = {
6
+ value: unknown;
7
+ set: (value: unknown) => void;
8
+ readonly: boolean;
9
+ invalid: boolean;
10
+ label: string;
11
+ /** No `Field` names it — a cell of a table: the control carries its label itself. Inside a
12
+ * `Field` it must not, or React Aria reads both and names it «Precio Precio». */
13
+ standalone?: boolean;
14
+ };
15
+ /**
16
+ * The control a field's TYPE asks for — a choice, a number, a date, a yes/no, a list of words,
17
+ * a text.
18
+ * Shared by a record's field (`RecordField`) and a line's cell and dialog (`RecordLinesEditor`):
19
+ * one rule for what a value is edited with.
20
+ */
21
+ export declare function controlFor(control: FieldControl, type: string, exact: boolean, choices: {
22
+ value: unknown;
23
+ label: string;
24
+ }[]): ReactNode;
25
+ /**
26
+ * A many2one's control: the picker of the entity it points at — the server's literal search,
27
+ * the first ten, «Buscar más…» — opening the chosen one where the view says.
28
+ *
29
+ * Context: shared by a record's field and a line. Inside a line (`ownChooser`: its row or its
30
+ * dialog) «Buscar más…» is the picker's own chooser, never the workspace's: the workspace opens
31
+ * it outside the line — a row commits when the focus leaves it, and the answer would land on a
32
+ * line no longer being written; over the line's dialog it would be a modal over a modal.
33
+ */
34
+ export declare function RelationPicker({ target, value, onChange, label, ownChooser, standalone, }: {
35
+ target: Entity;
36
+ value: string;
37
+ onChange: (id: string, chosen?: AnyRecord) => void;
38
+ label: string;
39
+ ownChooser?: boolean;
40
+ /** No `Field` names it (a cell): it carries `label` itself — as `FieldControl.standalone`. */
41
+ standalone?: boolean;
42
+ }): import("react").JSX.Element;
@@ -0,0 +1,60 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { EntityPicker } from "./EntityPicker.js";
4
+ import { useRecordOpening } from "./opening.js";
5
+ import { Checkbox, DatePicker, NumberInput, Select, TagsField, TextInput, } from "@sincpro/web-ui";
6
+ /**
7
+ * The control a field's TYPE asks for — a choice, a number, a date, a yes/no, a list of words,
8
+ * a text.
9
+ * Shared by a record's field (`RecordField`) and a line's cell and dialog (`RecordLinesEditor`):
10
+ * one rule for what a value is edited with.
11
+ */
12
+ export function controlFor(control, type, exact, choices) {
13
+ const { value, set } = control;
14
+ const named = control.standalone === true ? { "aria-label": control.label } : {};
15
+ if (choices.length > 0)
16
+ return (_jsx(Select, { value: value === null || value === undefined ? "" : String(value), options: choices.map((one) => ({ value: String(one.value), label: one.label })), onChange: set, ...named }));
17
+ switch (type) {
18
+ case "boolean":
19
+ return _jsx(Checkbox, { checked: value === true, onChange: set, label: control.label });
20
+ case "integer":
21
+ case "number":
22
+ return (_jsx(NumberInput, { value: value === null || value === undefined || value === "" ? "" : Number(value),
23
+ // A decimal travels as text so no digit is lost; the server reads it that way back.
24
+ onChange: (next) => set(next === "" ? null : exact ? String(next) : next), ...named }));
25
+ case "date":
26
+ return (_jsx(DatePicker, { value: typeof value === "string" && value !== "" ? value : null, onChange: set, ...named }));
27
+ case "text[]": {
28
+ // A list of words is chips: each word is its own key, and typing a new one adds it.
29
+ const words = Array.isArray(value) ? value.map(String) : [];
30
+ return (_jsx(TagsField, { value: words, options: words.map((word) => ({ value: word, label: word })), onChange: set, onCreate: (word) => word, ...named }));
31
+ }
32
+ default:
33
+ return (_jsx(TextInput, { value: value === null || value === undefined ? "" : String(value), onChange: set, ...named }));
34
+ }
35
+ }
36
+ /**
37
+ * A many2one's control: the picker of the entity it points at — the server's literal search,
38
+ * the first ten, «Buscar más…» — opening the chosen one where the view says.
39
+ *
40
+ * Context: shared by a record's field and a line. Inside a line (`ownChooser`: its row or its
41
+ * dialog) «Buscar más…» is the picker's own chooser, never the workspace's: the workspace opens
42
+ * it outside the line — a row commits when the focus leaves it, and the answer would land on a
43
+ * line no longer being written; over the line's dialog it would be a modal over a modal.
44
+ */
45
+ export function RelationPicker({ target, value, onChange, label, ownChooser = false, standalone = false, }) {
46
+ const opening = useRecordOpening();
47
+ return (_jsx(EntityPicker, { entity: target, value: value, onChange: onChange,
48
+ // «Buscar más…» is an option of the picker already: no second button beside it.
49
+ browse: false, ...(standalone ? { "aria-label": label } : {}), ...(opening === null
50
+ ? {}
51
+ : {
52
+ onOpen: (chosen, to) => opening.open(target.ref(chosen), to),
53
+ openTargets: opening.targets,
54
+ ...(ownChooser || opening.choose === undefined
55
+ ? {}
56
+ : {
57
+ onBrowseAll: (query) => opening.choose?.(target, (id) => onChange(id), query),
58
+ }),
59
+ }) }));
60
+ }
@@ -1,4 +1,9 @@
1
+ import { type AnyRecord } from "@sincpro/criteria";
1
2
  import type { RecordReading } from "../../react/useRecord.js";
3
+ import type { EntityListProps } from "../list/EntityList.js";
4
+ import { type FieldControl } from "./control.js";
5
+ import { type LinesLook } from "./lines.js";
6
+ export type { FieldControl } from "./control.js";
2
7
  import { type DirtyState } from "@sincpro/web-ui";
3
8
  import type { ReactNode } from "react";
4
9
  /**
@@ -18,22 +23,25 @@ import type { ReactNode } from "react";
18
23
  * search, the ten first, «Buscar más…» — and, read-only, the record's name as a link. Where it
19
24
  * opens is the view's (`RecordOpening`: a modal in a workspace, nothing outside one).
20
25
  *
26
+ * A one2many is a block: its lines (`lines`) or its related list (`list`), each with every piece
27
+ * replaceable the screen's way.
28
+ *
21
29
  * <RecordField record={invoice} name="due_date" />
22
30
  * <RecordField record={invoice} name="customer_id" />
31
+ * <RecordField record={invoice} name="lines" lines={{ controls: { qty: Stepper } }} />
23
32
  */
24
- export type FieldControl = {
25
- value: unknown;
26
- set: (value: unknown) => void;
27
- readonly: boolean;
28
- invalid: boolean;
29
- label: string;
30
- };
31
- export declare function RecordField<T extends Record<string, unknown>>({ record, name, as, bare, }: {
33
+ export declare function RecordField<T extends Record<string, unknown>>({ record, name, as, bare, lines: look, list: listed, }: {
32
34
  record: RecordReading<T>;
33
35
  name: string;
34
36
  as?: (control: FieldControl) => ReactNode;
35
37
  /** The control without its label — inside a page or a section that already names it. */
36
38
  bare?: boolean;
39
+ /** Lines (a one2many of no entity of its own), the screen's way: where a line is written,
40
+ * which columns, a cell or a control of its own, columns that are not fields, the dialog. */
41
+ lines?: LinesLook;
42
+ /** A one2many of a declared entity, the screen's way: anything `EntityList` takes — its
43
+ * cells, row actions, commands, faces of its own. What it passes wins over the defaults. */
44
+ list?: Omit<EntityListProps<AnyRecord>, "listing" | "embedded">;
37
45
  }): import("react").JSX.Element | null;
38
46
  /**
39
47
  * The record's «save / discard», for `<Record dirty={…}>`: present only while something changed,
@@ -2,15 +2,42 @@
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
3
  import { choicesOf } from "@sincpro/criteria";
4
4
  import { frameworkCopy } from "../../core/copy.js";
5
+ import { errorAt } from "../../core/edit.js";
6
+ import { editableLines } from "../../core/lines.js";
5
7
  import { textOf, viewOf } from "../../core/present.js";
6
8
  import { relationFieldOf } from "../../core/relation.js";
7
9
  import { useEntityFor, useFramework } from "../../react/context.js";
8
- import { EntityPicker } from "./EntityPicker.js";
10
+ import { controlFor, RelationPicker, } from "./control.js";
11
+ import { RecordLinesEditor } from "./lines.js";
9
12
  import { useRecordOpening } from "./opening.js";
10
13
  import { RecordLines } from "./record.js";
11
14
  import { RelatedList } from "./RelatedList.js";
12
- import { Checkbox, DatePicker, Field, NumberInput, ReadValue, RelationValue, Select, TagsField, TextInput, } from "@sincpro/web-ui";
13
- export function RecordField({ record, name, as, bare = false, }) {
15
+ import { Checkbox, Field, FormSection, ReadValue, RelationValue, } from "@sincpro/web-ui";
16
+ /**
17
+ * One field of a record being edited, from the model: its word, whether it is required or
18
+ * read-only now (the hints, evaluated over what is on screen), the server's error for it, and a
19
+ * control chosen by its type.
20
+ *
21
+ * Context: forge's forms wrote `<Field label="Nombre" required><TextInput … /></Field>` once per
22
+ * field, with the label, the «required» and the value plumbing by hand, while the server already
23
+ * said all three. Here a field is a NAME. The control by type is a default, never the only way:
24
+ * `as` draws any control — a rich text, a domain widget — and still receives the value, the
25
+ * setter and the state.
26
+ *
27
+ * A many2one needs no `as`: the model says what it points at (`relation: "Cliente"`, the key
28
+ * `customer_id` it is written by), the application declared that entity
29
+ * (`SincproApp entities`), so the field is an `EntityPicker` of it — the server's literal
30
+ * search, the ten first, «Buscar más…» — and, read-only, the record's name as a link. Where it
31
+ * opens is the view's (`RecordOpening`: a modal in a workspace, nothing outside one).
32
+ *
33
+ * A one2many is a block: its lines (`lines`) or its related list (`list`), each with every piece
34
+ * replaceable the screen's way.
35
+ *
36
+ * <RecordField record={invoice} name="due_date" />
37
+ * <RecordField record={invoice} name="customer_id" />
38
+ * <RecordField record={invoice} name="lines" lines={{ controls: { qty: Stepper } }} />
39
+ */
40
+ export function RecordField({ record, name, as, bare = false, lines: look, list: listed, }) {
14
41
  const { meta, locale, edit } = record;
15
42
  const view = viewOf(meta, name, locale);
16
43
  const state = edit.state(name);
@@ -20,28 +47,43 @@ export function RecordField({ record, name, as, bare = false, }) {
20
47
  const { metas } = useFramework();
21
48
  if (!state.visible)
22
49
  return null;
50
+ // A one2many is a BLOCK, named by a section — never a `Field`: web-ui's controls read the
51
+ // id and the label of the `Field` around them, and every cell and the list's search bar
52
+ // took the same id and the relation's name.
23
53
  // A one2many of a declared entity is that entity's own list, filtered to this record by
24
54
  // the server — never the page the record happened to bring — and a row opens its record.
25
- // A one2many of no declared entity (the lines of an invoice): the page the record brought,
26
- // with its real total.
55
+ // A one2many of no declared entity (the lines of an invoice): edited inside the record as a
56
+ // whole list (`RecordLinesEditor`) — or, when the record brought only a page, that page read
57
+ // only with its real total.
27
58
  if (as === undefined && relation?.field.type === "one2many" && target === undefined) {
28
- const lines = (_jsx(RecordLines, { record: record.record ?? {}, meta: meta, name: name, locale: locale }));
29
- return bare ? lines : _jsx(Field, { label: view.label, children: lines });
59
+ // Editable when the record holds ALL its lines; a page of them stays read only.
60
+ const whole = editableLines(edit.values[name]);
61
+ 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 }) }));
62
+ return bare ? lines : _jsx(FormSection, { title: view.label, children: lines });
30
63
  }
31
64
  if (as === undefined && relation?.field.type === "one2many" && target !== undefined) {
32
65
  const list = (_jsx(RelatedList, { record: record, name: name, entity: target, ...(opening === null
33
66
  ? {}
34
- : {
35
- onOpen: (row) => {
67
+ : (() => {
68
+ const refOf = (row) => {
36
69
  const known = metas.get(target.key);
37
70
  const key = known?.get_id || known?.identity || "id";
38
- opening.open(target.ref(String(row[key])), opening.targets[0] ?? "page");
39
- },
40
- }) }));
41
- return bare ? list : _jsx(Field, { label: view.label, children: list });
71
+ return target.ref(String(row[key]));
72
+ };
73
+ // A press opens where the view says first; «Abrir en…» offers every other place.
74
+ return {
75
+ onOpen: (row) => opening.open(refOf(row), opening.targets[0] ?? "page"),
76
+ onOpenIn: (row, to) => opening.open(refOf(row), to),
77
+ openTargets: opening.targets,
78
+ // Not on a page, a list inside the form starts as rows; the table stays a click
79
+ // away.
80
+ ...(opening.narrow === true ? { views: ["rows", "table"] } : {}),
81
+ };
82
+ })()), ...listed }));
83
+ return bare ? list : _jsx(FormSection, { title: view.label, children: list });
42
84
  }
43
85
  const value = edit.values[name];
44
- const error = edit.errors[name];
86
+ const error = errorAt(edit.errors, name);
45
87
  const control = {
46
88
  value,
47
89
  set: (next) => edit.set(name, next),
@@ -64,46 +106,12 @@ export function RecordField({ record, name, as, bare = false, }) {
64
106
  onOpenIn: (to) => opening.open(target.ref(id), to),
65
107
  openTargets: opening.targets,
66
108
  }) }));
67
- const answer = (next) => edit.set(relation.key, next);
68
- return (_jsx(EntityPicker, { entity: target, value: id, onChange: answer,
69
- // «Buscar más…» is an option of the picker already: no second button beside it.
70
- browse: false, "aria-label": view.label, ...(opening === null
71
- ? {}
72
- : {
73
- onOpen: (chosen, to) => opening.open(target.ref(chosen), to),
74
- openTargets: opening.targets,
75
- ...(opening.choose === undefined
76
- ? {}
77
- : {
78
- onBrowseAll: (query) => opening.choose?.(target, answer, query),
79
- }),
80
- }) }));
109
+ return (_jsx(RelationPicker, { target: target, value: id, onChange: (next) => edit.set(relation.key, next), label: view.label }));
81
110
  }
82
111
  if (view.type === "boolean" && as === undefined)
83
112
  return (_jsx(Checkbox, { checked: value === true, onChange: control.set, label: view.label, disabled: state.readonly }));
84
113
  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(meta, name, locale))) }));
85
114
  }
86
- function controlFor(control, type, exact, choices) {
87
- const { value, set } = control;
88
- if (choices.length > 0)
89
- return (_jsx(Select, { value: value === null || value === undefined ? "" : String(value), options: choices.map((one) => ({ value: String(one.value), label: one.label })), onChange: set }));
90
- switch (type) {
91
- case "integer":
92
- case "number":
93
- return (_jsx(NumberInput, { value: value === null || value === undefined || value === "" ? "" : Number(value),
94
- // A decimal travels as text so no digit is lost; the server reads it that way back.
95
- onChange: (next) => set(next === "" ? null : exact ? String(next) : next) }));
96
- case "date":
97
- return (_jsx(DatePicker, { value: typeof value === "string" && value !== "" ? value : null, onChange: set }));
98
- case "text[]": {
99
- // A list of words is chips: each word is its own key, and typing a new one adds it.
100
- const words = Array.isArray(value) ? value.map(String) : [];
101
- return (_jsx(TagsField, { value: words, options: words.map((word) => ({ value: word, label: word })), onChange: set, onCreate: (word) => word }));
102
- }
103
- default:
104
- return (_jsx(TextInput, { value: value === null || value === undefined ? "" : String(value), onChange: set }));
105
- }
106
- }
107
115
  /**
108
116
  * The record's «save / discard», for `<Record dirty={…}>`: present only while something changed,
109
117
  * off while saving, and off with the reason while a required field is empty.