@asteby/metacore-runtime-react 49.4.0 → 49.6.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 (87) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/api-context.d.ts +9 -1
  3. package/dist/api-context.d.ts.map +1 -1
  4. package/dist/api-context.js +6 -4
  5. package/dist/business/customer-picker.d.ts +8 -2
  6. package/dist/business/customer-picker.d.ts.map +1 -1
  7. package/dist/business/customer-picker.js +30 -27
  8. package/dist/business/index.d.ts +1 -0
  9. package/dist/business/index.d.ts.map +1 -1
  10. package/dist/business/index.js +1 -0
  11. package/dist/business/line-product-cell.d.ts +8 -10
  12. package/dist/business/line-product-cell.d.ts.map +1 -1
  13. package/dist/business/line-product-cell.js +13 -90
  14. package/dist/business/product-options.d.ts +41 -0
  15. package/dist/business/product-options.d.ts.map +1 -0
  16. package/dist/business/product-options.js +58 -0
  17. package/dist/business/product-picker.d.ts +5 -1
  18. package/dist/business/product-picker.d.ts.map +1 -1
  19. package/dist/business/product-picker.js +26 -35
  20. package/dist/business/relate-documents.d.ts +1 -1
  21. package/dist/business/relate-documents.d.ts.map +1 -1
  22. package/dist/business/relate-documents.js +6 -8
  23. package/dist/business/vehicle-picker.d.ts +8 -2
  24. package/dist/business/vehicle-picker.d.ts.map +1 -1
  25. package/dist/business/vehicle-picker.js +28 -28
  26. package/dist/dialogs/dynamic-record.d.ts.map +1 -1
  27. package/dist/dialogs/dynamic-record.js +28 -43
  28. package/dist/dynamic-multi-select-field.d.ts.map +1 -1
  29. package/dist/dynamic-multi-select-field.js +17 -5
  30. package/dist/dynamic-select-field.d.ts +7 -23
  31. package/dist/dynamic-select-field.d.ts.map +1 -1
  32. package/dist/dynamic-select-field.js +67 -161
  33. package/dist/dynamic-table.d.ts +31 -1
  34. package/dist/dynamic-table.d.ts.map +1 -1
  35. package/dist/dynamic-table.js +37 -4
  36. package/dist/entity-select.d.ts +5 -2
  37. package/dist/entity-select.d.ts.map +1 -1
  38. package/dist/entity-select.js +23 -116
  39. package/dist/index.d.ts +7 -2
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +8 -1
  42. package/dist/query-batch.d.ts.map +1 -1
  43. package/dist/query-batch.js +17 -1
  44. package/dist/record-picker-actions.d.ts +40 -2
  45. package/dist/record-picker-actions.d.ts.map +1 -1
  46. package/dist/record-picker-actions.js +44 -4
  47. package/dist/record-picker-dialog.d.ts +23 -0
  48. package/dist/record-picker-dialog.d.ts.map +1 -0
  49. package/dist/record-picker-dialog.js +37 -0
  50. package/dist/record-picker-option.d.ts +25 -0
  51. package/dist/record-picker-option.d.ts.map +1 -0
  52. package/dist/record-picker-option.js +55 -0
  53. package/dist/record-picker.d.ts +149 -0
  54. package/dist/record-picker.d.ts.map +1 -0
  55. package/dist/record-picker.js +372 -0
  56. package/package.json +1 -1
  57. package/src/__tests__/api-batch-opt-out.test.tsx +83 -0
  58. package/src/__tests__/business-pickers.test.tsx +6 -3
  59. package/src/__tests__/document-editor-inline-selects.test.tsx +2 -1
  60. package/src/__tests__/document-form-dialog.test.tsx +4 -3
  61. package/src/__tests__/document-lines-grid.test.tsx +2 -1
  62. package/src/__tests__/dynamic-table-bulk-actions.test.tsx +140 -0
  63. package/src/__tests__/dynamic-table-load-error.test.tsx +97 -0
  64. package/src/__tests__/record-picker.test.tsx +469 -0
  65. package/src/api-context.tsx +19 -4
  66. package/src/business/customer-picker.tsx +87 -98
  67. package/src/business/index.ts +1 -0
  68. package/src/business/line-product-cell.tsx +55 -180
  69. package/src/business/product-options.tsx +119 -0
  70. package/src/business/product-picker.tsx +63 -126
  71. package/src/business/relate-documents.tsx +36 -45
  72. package/src/business/vehicle-picker.tsx +76 -99
  73. package/src/dialogs/dynamic-record.tsx +44 -97
  74. package/src/dynamic-multi-select-field.tsx +39 -13
  75. package/src/dynamic-select-field.tsx +143 -368
  76. package/src/dynamic-table.tsx +91 -4
  77. package/src/entity-select.tsx +53 -202
  78. package/src/index.ts +27 -2
  79. package/src/query-batch.ts +18 -1
  80. package/src/record-picker-actions.tsx +101 -8
  81. package/src/record-picker-dialog.tsx +80 -0
  82. package/src/record-picker-option.tsx +96 -0
  83. package/src/record-picker.tsx +971 -0
  84. package/dist/business/use-async-search.d.ts +0 -10
  85. package/dist/business/use-async-search.d.ts.map +0 -1
  86. package/dist/business/use-async-search.js +0 -38
  87. package/src/business/use-async-search.ts +0 -43
@@ -1,16 +1,21 @@
1
- // RecordPickerActions — create/edit affordances shared by every "pick a related
2
- // record" control (DynamicSelectField for declarative forms, EntitySelect for
3
- // hand-written screens). ONE implementation of the same contract:
1
+ // RecordPickerActions — create/edit affordances of the shared <RecordPicker>
2
+ // (record-picker.tsx), the ONE "pick a related record" control of the runtime.
3
+ // Every picker (DynamicSelectField, EntitySelect, the business pickers) gets
4
+ // the same contract from here:
4
5
  //
5
6
  // - nothing selected → "+" joined to the trigger: opens the model's CREATE
6
7
  // modal and the new record ends up selected;
7
8
  // - a record selected → pencil joined to the trigger: opens THAT record's
8
9
  // EDIT modal and the label refreshes on save;
9
10
  // - "Crear …" at the foot of the result list, so creating is one step away
10
- // from a search with no hits.
11
+ // from a search with no hits (prefilled with the searched text).
11
12
  //
12
13
  // The action renders as a segment of the trigger (shared border, no gap) so the
13
14
  // control reads as a single field instead of a select plus a loose button.
15
+ //
16
+ // The create/edit modals themselves are the HOST's (decoupled window events the
17
+ // host's RecordCreateBridge listens for) or, for hand-written screens, the
18
+ // in-tree <CreateRecordDialog> (see record-picker-dialog.tsx).
14
19
  import type { ReactNode } from 'react'
15
20
  import { Pencil, Plus } from 'lucide-react'
16
21
  import { Button, CommandGroup, CommandItem, CommandSeparator } from '@asteby/metacore-ui/primitives'
@@ -26,6 +31,13 @@ export interface RecordPickerActionProps {
26
31
  onCreate?: () => void
27
32
  onEdit?: () => void
28
33
  disabled?: boolean
34
+ /** Accessible name override for the "+" (default `Crear <label>`). */
35
+ createLabel?: string
36
+ /** Accessible name override for the pencil (default `Editar <label>`). */
37
+ editLabel?: string
38
+ /** `cell` → compact, borderless segment for table cells. */
39
+ variant?: 'field' | 'cell'
40
+ className?: string
29
41
  }
30
42
 
31
43
  /**
@@ -33,11 +45,22 @@ export interface RecordPickerActionProps {
33
45
  * otherwise (and `onCreate`). Renders nothing when neither applies — callers
34
46
  * should then drop {@link JOINED_TRIGGER_CLASS} from the trigger.
35
47
  */
36
- export function RecordPickerAction({ hasValue, label, onCreate, onEdit, disabled }: RecordPickerActionProps) {
48
+ export function RecordPickerAction({
49
+ hasValue,
50
+ label,
51
+ onCreate,
52
+ onEdit,
53
+ disabled,
54
+ createLabel,
55
+ editLabel,
56
+ variant = 'field',
57
+ className,
58
+ }: RecordPickerActionProps) {
37
59
  const edit = hasValue && !!onEdit
38
60
  const create = !hasValue && !!onCreate
39
61
  if (!edit && !create) return null
40
- const name = edit ? `Editar ${label}` : `Crear ${label}`
62
+ const name = edit ? editLabel ?? `Editar ${label}` : createLabel ?? `Crear ${label}`
63
+ const size = variant === 'cell' ? 'size-8 border-transparent bg-transparent shadow-none' : 'size-9'
41
64
  return (
42
65
  <Button
43
66
  type="button"
@@ -53,7 +76,10 @@ export function RecordPickerAction({ hasValue, label, onCreate, onEdit, disabled
53
76
  title={name}
54
77
  data-slot="record-picker-action"
55
78
  data-action={edit ? 'edit' : 'create'}
56
- className="-ml-px size-9 shrink-0 rounded-l-none text-muted-foreground hover:text-foreground focus-visible:z-10"
79
+ className={
80
+ `-ml-px ${size} shrink-0 rounded-l-none text-muted-foreground hover:text-foreground focus-visible:z-10` +
81
+ (className ? ` ${className}` : '')
82
+ }
57
83
  >
58
84
  {edit ? <Pencil className="size-4" /> : <Plus className="size-4" />}
59
85
  </Button>
@@ -65,7 +91,12 @@ export function hasRecordPickerAction(hasValue: boolean, onCreate?: unknown, onE
65
91
  return hasValue ? !!onEdit : !!onCreate
66
92
  }
67
93
 
68
- /** "Crear …" row at the foot of a Command list. */
94
+ /**
95
+ * "Crear …" row at the foot of a cmdk `<Command>` list.
96
+ *
97
+ * @deprecated <RecordPicker> renders its own create footer (with the searched
98
+ * text prefilled). Kept for hand-written cmdk lists in consumers.
99
+ */
69
100
  export function PickerCreateItem({ label, onSelect, children }: { label: string; onSelect: () => void; children?: ReactNode }) {
70
101
  return (
71
102
  <>
@@ -79,3 +110,65 @@ export function PickerCreateItem({ label, onSelect, children }: { label: string;
79
110
  </>
80
111
  )
81
112
  }
113
+
114
+ // ---------------------------------------------------------------------------
115
+ // Host create/edit modal — decoupled window events.
116
+ //
117
+ // The SDK never imports host code: a picker fires `metacore:create-record` /
118
+ // `metacore:edit-record` and the host's RecordCreateBridge opens the model's
119
+ // REAL modal (full fields from /metadata/modal), handing the saved record back
120
+ // through the callback. Works for ANY model.
121
+ // ---------------------------------------------------------------------------
122
+
123
+ /** Detail of the `metacore:create-record` event (contract with the host). */
124
+ export interface RecordCreateRequest {
125
+ model: string
126
+ /** Seed values for the new record. Keys that aren't fields of the model are ignored by the dialog. */
127
+ defaults?: Record<string, unknown>
128
+ /** Fields rendered locked in the create modal. */
129
+ lockedFields?: string[]
130
+ onCreated?: (record: any) => void
131
+ }
132
+
133
+ /** Detail of the `metacore:edit-record` event (contract with the host). */
134
+ export interface RecordEditRequest {
135
+ model: string
136
+ recordId: string
137
+ onSaved?: (record: any) => void
138
+ }
139
+
140
+ /** Ask the host to open `model`'s create modal. No-op outside a browser. */
141
+ export function requestRecordCreate(req: RecordCreateRequest): void {
142
+ if (typeof window === 'undefined') return
143
+ window.dispatchEvent(new CustomEvent('metacore:create-record', { detail: req }))
144
+ }
145
+
146
+ /** Ask the host to open the edit modal of `recordId`. No-op outside a browser. */
147
+ export function requestRecordEdit(req: RecordEditRequest): void {
148
+ if (typeof window === 'undefined') return
149
+ window.dispatchEvent(new CustomEvent('metacore:edit-record', { detail: req }))
150
+ }
151
+
152
+ /** Display label of a saved record: `labelField`, else name / label / title. */
153
+ export function recordLabel(rec: Record<string, unknown> | null | undefined, labelField = 'name'): string | undefined {
154
+ if (!rec) return undefined
155
+ for (const k of [labelField, 'name', 'label', 'title']) {
156
+ const v = rec[k]
157
+ if (v != null && v !== '') return String(v)
158
+ }
159
+ return undefined
160
+ }
161
+
162
+ /**
163
+ * Create defaults with the searched text seeded into `field` (the model's
164
+ * label column: name, plate…). Explicit `defaults` win over the prefill.
165
+ */
166
+ export function withSearchPrefill(
167
+ query: string | undefined,
168
+ field: string | null | undefined,
169
+ defaults?: Record<string, unknown>,
170
+ ): Record<string, unknown> | undefined {
171
+ const q = (query ?? '').trim()
172
+ if (!q || !field) return defaults
173
+ return { [field]: q, ...(defaults ?? {}) }
174
+ }
@@ -0,0 +1,80 @@
1
+ // useRecordPickerDialog — create/edit plumbing for pickers on hand-written
2
+ // screens that sit outside the host's RecordCreateBridge (POS, federated
3
+ // panels): the in-tree, metadata-driven <CreateRecordDialog> posting to the
4
+ // standard org-scoped CRUD (`/data/<model>/me`). Declarative pickers use the
5
+ // host events instead (requestRecordCreate / requestRecordEdit).
6
+ //
7
+ // One implementation for EntitySelect, CustomerPicker and VehiclePicker: the
8
+ // searched text prefills the new record's label field, the saved record is
9
+ // handed back to select it (create) or refresh its label (edit).
10
+ import { useState, type ReactNode } from 'react'
11
+ import { CreateRecordDialog } from './dialogs/create-record-dialog'
12
+ import { useApi } from './api-context'
13
+ import { withSearchPrefill } from './record-picker-actions'
14
+
15
+ export interface UseRecordPickerDialogOptions {
16
+ /** Kernel model key. */
17
+ model: string
18
+ /** CRUD base. Default `/data/<model>/me`; edit PUTs `<base>/<id>`. */
19
+ endpoint?: string
20
+ /** Field seeded with the searched text on create (null = none). Default "name". */
21
+ prefillField?: string | null
22
+ /** Seed values for create (win over the prefill). */
23
+ createDefaults?: Record<string, unknown>
24
+ /** Fields of `createDefaults` rendered locked on create. */
25
+ lockedCreateFields?: string[]
26
+ /** The saved record (`{ id, ...fields }`). */
27
+ onSaved: (record: Record<string, unknown>, kind: 'create' | 'edit') => void
28
+ }
29
+
30
+ export interface UseRecordPickerDialogResult {
31
+ openCreate: (query?: string) => void
32
+ openEdit: (id: string) => void
33
+ /** Render it next to the picker. */
34
+ dialog: ReactNode
35
+ }
36
+
37
+ const unwrap = (res: { data?: any }) => ((res.data?.data ?? res.data) ?? {}) as Record<string, unknown>
38
+
39
+ export function useRecordPickerDialog({
40
+ model,
41
+ endpoint,
42
+ prefillField = 'name',
43
+ createDefaults,
44
+ lockedCreateFields,
45
+ onSaved,
46
+ }: UseRecordPickerDialogOptions): UseRecordPickerDialogResult {
47
+ const api = useApi()
48
+ const base = endpoint ?? `/data/${model}/me`
49
+ const [state, setState] = useState<{ recordId?: string; defaults?: Record<string, unknown> } | null>(null)
50
+
51
+ const dialog = state ? (
52
+ <CreateRecordDialog
53
+ modelKey={model}
54
+ open
55
+ onOpenChange={(o) => {
56
+ if (!o) setState(null)
57
+ }}
58
+ recordId={state.recordId}
59
+ endpoint={base}
60
+ defaults={state.recordId ? undefined : state.defaults}
61
+ lockedFields={state.recordId ? undefined : lockedCreateFields}
62
+ onCreate={async (data) => {
63
+ const rec = unwrap(await api.post(base, data))
64
+ onSaved(rec, 'create')
65
+ return rec.id != null ? { id: String(rec.id) } : undefined
66
+ }}
67
+ onUpdate={async (id, data) => {
68
+ const rec = unwrap(await api.put(`${base}/${id}`, data))
69
+ onSaved({ ...rec, id: rec.id ?? id }, 'edit')
70
+ return { id: String(id) }
71
+ }}
72
+ />
73
+ ) : null
74
+
75
+ return {
76
+ openCreate: (query = '') => setState({ defaults: withSearchPrefill(query, prefillField, createDefaults) }),
77
+ openEdit: (id: string) => setState({ recordId: id }),
78
+ dialog,
79
+ }
80
+ }
@@ -0,0 +1,96 @@
1
+ // Option visuals shared by every RecordPicker configuration: a thumbnail
2
+ // (image / lucide icon name / initials fallback) and the leading visual of an
3
+ // option (image, else icon, else color dot, else initials).
4
+ import { useState } from 'react'
5
+ import { InitialsAvatar } from '@asteby/metacore-ui/primitives'
6
+ import { resolveColorCss } from '@asteby/metacore-ui/lib'
7
+ import { DynamicIcon, isLucideIconName } from './dynamic-icon'
8
+ import type { ResolvedOption } from './use-options-resolver'
9
+
10
+ /**
11
+ * Small square thumbnail for an option's `image`. When the option has no image
12
+ * it falls back to a deterministic initials avatar (shared `InitialsAvatar`)
13
+ * derived from `name`, so an imageless reference reads as a colored badge rather
14
+ * than an empty placeholder — and rows/triggers stay aligned. `size` is in
15
+ * pixels (kept small — 20–24px — so the picker reads as a list, not a gallery).
16
+ * Inline style for the box dimensions: arbitrary Tailwind classes from a
17
+ * federated addon don't always survive the host's class scan.
18
+ */
19
+ export function OptionThumb({
20
+ image,
21
+ name,
22
+ size = 20,
23
+ }: {
24
+ image?: string | null
25
+ name?: string | null
26
+ size?: number
27
+ }) {
28
+ const [broken, setBroken] = useState(false)
29
+ const box = { width: size, height: size }
30
+ // Missing or 404 image (bare avatar filenames often 404) → initials, never
31
+ // an invisible box that looks like "no avatar".
32
+ if (!image || broken) {
33
+ return <InitialsAvatar name={name} size={size} rounded="sm" tone="neutral" />
34
+ }
35
+ // A lucide icon name stored where an image url/path is expected (the `icon`
36
+ // form widget's icon mode) → render the glyph instead of a broken <img>.
37
+ if (isLucideIconName(image)) {
38
+ return (
39
+ <span className="flex shrink-0 items-center justify-center rounded-sm bg-muted" style={box} aria-hidden>
40
+ <DynamicIcon name={image} className="size-4" />
41
+ </span>
42
+ )
43
+ }
44
+ return (
45
+ <img
46
+ src={image}
47
+ alt=""
48
+ aria-hidden
49
+ loading="lazy"
50
+ className="shrink-0 rounded-sm object-cover"
51
+ style={box}
52
+ onError={() => setBroken(true)}
53
+ />
54
+ )
55
+ }
56
+
57
+ /**
58
+ * Leading visual for an option: a photo thumbnail (FK relations with an image),
59
+ * else a declared icon, else a color dot (enum/status options with a color).
60
+ * Returns null when the option carries none, so plain text options stay plain.
61
+ */
62
+ export function OptionLead({
63
+ option,
64
+ size = 20,
65
+ }: {
66
+ option?: Pick<ResolvedOption, 'image' | 'color' | 'icon' | 'label'> | null
67
+ size?: number
68
+ }) {
69
+ if (!option) return null
70
+ if (option.image) return <OptionThumb image={option.image} name={option.label} size={size} />
71
+ if (option.icon) {
72
+ return (
73
+ <span
74
+ className="flex shrink-0 items-center justify-center"
75
+ style={{ width: size, height: size, color: option.color ? resolveColorCss(option.color) : undefined }}
76
+ aria-hidden
77
+ >
78
+ <DynamicIcon name={option.icon} className="size-4" />
79
+ </span>
80
+ )
81
+ }
82
+ if (option.color) {
83
+ return (
84
+ <span
85
+ className="shrink-0 rounded-full"
86
+ style={{ width: Math.round(size * 0.5), height: Math.round(size * 0.5), background: resolveColorCss(option.color) }}
87
+ aria-hidden
88
+ />
89
+ )
90
+ }
91
+ // No image/icon/color: an imageless reference option. Show its initials
92
+ // (shared InitialsAvatar) so it stays visually aligned with the sibling
93
+ // options that DO carry a thumbnail, rather than a blank gap.
94
+ if (option.label) return <InitialsAvatar name={option.label} size={size} rounded="sm" tone="neutral" />
95
+ return null
96
+ }