@asteby/metacore-runtime-react 49.10.0 → 49.12.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 (56) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +40 -0
  3. package/dist/column-filter-field.d.ts +21 -0
  4. package/dist/column-filter-field.d.ts.map +1 -0
  5. package/dist/column-filter-field.js +51 -0
  6. package/dist/dialogs/dynamic-record.d.ts +5 -2
  7. package/dist/dialogs/dynamic-record.d.ts.map +1 -1
  8. package/dist/dialogs/dynamic-record.js +96 -14
  9. package/dist/dynamic-columns.d.ts.map +1 -1
  10. package/dist/dynamic-columns.js +11 -1
  11. package/dist/dynamic-form.d.ts.map +1 -1
  12. package/dist/dynamic-form.js +10 -2
  13. package/dist/dynamic-icon.d.ts +16 -1
  14. package/dist/dynamic-icon.d.ts.map +1 -1
  15. package/dist/dynamic-icon.js +36 -5
  16. package/dist/dynamic-multi-select-field.d.ts +13 -1
  17. package/dist/dynamic-multi-select-field.d.ts.map +1 -1
  18. package/dist/dynamic-multi-select-field.js +19 -4
  19. package/dist/dynamic-table.d.ts.map +1 -1
  20. package/dist/dynamic-table.js +2 -1
  21. package/dist/field-widget-registry.d.ts +35 -0
  22. package/dist/field-widget-registry.d.ts.map +1 -0
  23. package/dist/field-widget-registry.js +66 -0
  24. package/dist/icon-aliases.d.ts +2 -0
  25. package/dist/icon-aliases.d.ts.map +1 -0
  26. package/dist/icon-aliases.js +594 -0
  27. package/dist/icon-resolvers.d.ts +13 -0
  28. package/dist/icon-resolvers.d.ts.map +1 -0
  29. package/dist/icon-resolvers.js +36 -0
  30. package/dist/icon-spec.d.ts +17 -0
  31. package/dist/icon-spec.d.ts.map +1 -0
  32. package/dist/icon-spec.js +165 -0
  33. package/dist/index.d.ts +4 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +4 -0
  36. package/dist/types.d.ts +1 -1
  37. package/dist/types.d.ts.map +1 -1
  38. package/package.json +1 -1
  39. package/src/__tests__/column-filter-field.test.ts +105 -0
  40. package/src/__tests__/dynamic-record-field-types.test.tsx +221 -0
  41. package/src/__tests__/field-widget-registry.test.tsx +165 -0
  42. package/src/__tests__/icon-component.test.tsx +145 -0
  43. package/src/__tests__/icon-spec.test.ts +155 -0
  44. package/src/column-filter-field.ts +66 -0
  45. package/src/dialogs/dynamic-record.tsx +145 -13
  46. package/src/dynamic-columns.tsx +22 -1
  47. package/src/dynamic-form.tsx +12 -1
  48. package/src/dynamic-icon.tsx +68 -3
  49. package/src/dynamic-multi-select-field.tsx +29 -4
  50. package/src/dynamic-table.tsx +2 -1
  51. package/src/field-widget-registry.tsx +98 -0
  52. package/src/icon-aliases.ts +602 -0
  53. package/src/icon-resolvers.ts +59 -0
  54. package/src/icon-spec.ts +169 -0
  55. package/src/index.ts +12 -0
  56. package/src/types.ts +3 -0
@@ -2,14 +2,30 @@
2
2
  // set into the shared chunk. It draws through the shared glyph registry of
3
3
  // @asteby/metacore-ui: a glyph seen before renders in the first frame, and an
4
4
  // unseen one holds its box until it loads, so the label next to it stays put.
5
+ import { useState, type ReactNode } from 'react'
5
6
  import dynamicIconImports from 'lucide-react/dynamicIconImports'
6
7
  import { Glyph } from '@asteby/metacore-ui/icons'
8
+ import { resolveIconSpec } from './icon-resolvers'
7
9
 
8
10
  export interface DynamicIconProps {
9
11
  name: string
10
12
  className?: string
11
13
  }
12
14
 
15
+ export interface IconProps {
16
+ /** Any icon value: Lucide name, FontAwesome classes, emoji, image URL... */
17
+ name: unknown
18
+ className?: string
19
+ /** Accessible name. Without it the icon is decorative (aria-hidden). */
20
+ label?: string
21
+ /** Drawn when the value cannot be interpreted. Default: nothing. */
22
+ fallback?: ReactNode
23
+ /** Maps an image `src` before it reaches <img> (e.g. host `getImageUrl`). */
24
+ resolveImageSrc?: (src: string) => string
25
+ /** Render image sources (default true). DynamicIcon turns it off. */
26
+ images?: boolean
27
+ }
28
+
13
29
  type IconName = keyof typeof dynamicIconImports
14
30
 
15
31
  function pascalToKebab(pascal: string): string {
@@ -35,10 +51,59 @@ function loaderFor(pascal: string) {
35
51
  )
36
52
  }
37
53
 
54
+ function IconImage({ src, className, label }: { src: string; className?: string; label?: string }) {
55
+ const [failed, setFailed] = useState<string | null>(null)
56
+ if (failed === src) return null
57
+ return (
58
+ <img
59
+ src={src}
60
+ alt={label ?? ''}
61
+ loading="lazy"
62
+ decoding="async"
63
+ className={className}
64
+ onError={() => setFailed(src)}
65
+ />
66
+ )
67
+ }
68
+
69
+ // Icon — renders any icon value the SDK can interpret (see parseIconSpec and
70
+ // registerIconResolver): Lucide glyph, emoji, image or a host-rendered node.
71
+ // A value nobody understands renders `fallback` (default nothing), never the
72
+ // raw text. Decorative (aria-hidden) unless `label` is given.
73
+ export function Icon({ name, className, label, fallback = null, resolveImageSrc, images = true }: IconProps) {
74
+ const spec = resolveIconSpec(name)
75
+ const a11y = label ? ({ role: 'img', 'aria-label': label } as const) : ({ 'aria-hidden': true } as const)
76
+ switch (spec.kind) {
77
+ case 'lucide':
78
+ return <Glyph name={spec.name} className={className} {...a11y} />
79
+ case 'emoji':
80
+ return (
81
+ <span className={className} {...a11y}>
82
+ {spec.value}
83
+ </span>
84
+ )
85
+ case 'image':
86
+ if (!images) return <>{fallback}</>
87
+ return (
88
+ <IconImage
89
+ src={resolveImageSrc ? resolveImageSrc(spec.src) : spec.src}
90
+ className={className}
91
+ label={label}
92
+ />
93
+ )
94
+ case 'node':
95
+ return <>{spec.node}</>
96
+ default:
97
+ return <>{fallback}</>
98
+ }
99
+ }
100
+
101
+ // DynamicIcon keeps its { name, className } contract: Lucide names resolve
102
+ // exactly as before; FontAwesome classes, emoji and host resolvers now work
103
+ // too. Image paths still draw nothing here (icon slots in menus/buttons never
104
+ // showed <img>); use <Icon> or a `type: 'icon'` column for images.
38
105
  export function DynamicIcon({ name, className }: DynamicIconProps) {
39
- const resolved = resolveLucideIconName(name)
40
- if (!resolved) return null
41
- return <Glyph name={resolved} className={className} />
106
+ return <Icon name={name} className={className} images={false} />
42
107
  }
43
108
 
44
109
  // resolveLucideIconName — canonical PascalCase lucide name for a value that is
@@ -36,32 +36,57 @@ export interface DynamicMultiSelectFieldProps {
36
36
  /** Plain array of selected target ids. Absent/non-array value → treated as empty. */
37
37
  value: unknown
38
38
  onChange: (value: string[]) => void
39
+ /**
40
+ * Static options (DefineOptions served inline on the field). When the field
41
+ * has no `ref`/`searchEndpoint` and this list is non-empty, it is used as the
42
+ * option set and no request is made. Absent → remote resolution as before.
43
+ */
44
+ staticOptions?: { value: string; label: string; color?: string; icon?: string; image?: string }[]
39
45
  }
40
46
 
41
- export function DynamicMultiSelectField({ field, value, onChange }: DynamicMultiSelectFieldProps) {
47
+ export function DynamicMultiSelectField({ field, value, onChange, staticOptions }: DynamicMultiSelectFieldProps) {
42
48
  const ref = getFieldRef(field)
43
49
  const [query, setQuery] = useState('')
44
- const { options, loading, meta, error } = useOptionsResolver({
50
+ const useStatic = !ref && !field.searchEndpoint && !!staticOptions?.length
51
+ const remote = useOptionsResolver({
45
52
  modelKey: '',
46
53
  fieldKey: 'id',
47
54
  ref,
48
55
  endpoint: !ref && field.searchEndpoint ? field.searchEndpoint : undefined,
49
56
  limit: 200,
50
57
  optionFilter: getOptionFilter(field),
58
+ enabled: !useStatic,
51
59
  })
60
+ const staticResolved = useMemo<ResolvedOption[]>(
61
+ () =>
62
+ (staticOptions ?? []).map((o) => ({
63
+ id: o.value,
64
+ value: o.value,
65
+ label: o.label,
66
+ name: o.label,
67
+ color: o.color,
68
+ icon: o.icon,
69
+ image: o.image,
70
+ })),
71
+ [staticOptions],
72
+ )
73
+ const options = useStatic ? staticResolved : remote.options
74
+ const loading = useStatic ? false : remote.loading
75
+ const meta = useStatic ? ({ type: 'static', count: staticResolved.length } as const) : remote.meta
76
+ const error = useStatic ? null : remote.error
52
77
 
53
78
  const selected = useMemo(() => (Array.isArray(value) ? value.map(String) : []), [value])
54
79
  const endpoint = !ref && field.searchEndpoint ? field.searchEndpoint : undefined
55
80
  // Ask for the ids the loaded page does not cover — only once it is in.
56
81
  const pageSettled = !loading && (meta !== null || error !== null)
57
82
  const unresolved = pageSettled ? selected.filter((id) => !options.some((o) => String(o.id) === id)) : []
58
- const { resolved } = useResolveOptionIds({ ref, endpoint, field: 'id', ids: unresolved, enabled: unresolved.length > 0 })
83
+ const { resolved } = useResolveOptionIds({ ref, endpoint, field: 'id', ids: unresolved, enabled: !useStatic && unresolved.length > 0 })
59
84
  const selectedItems = selected.map((id): ResolvedOption => {
60
85
  const loaded = options.find((o) => String(o.id) === id)
61
86
  if (loaded) return loaded
62
87
  const r = pageSettled ? resolved.get(id) : undefined
63
88
  if (r?.status === 'found') return r.option
64
- const label = !r ? 'Cargando…' : r.status === 'missing' ? DELETED_RECORD_LABEL : id
89
+ const label = useStatic ? id : !r ? 'Cargando…' : r.status === 'missing' ? DELETED_RECORD_LABEL : id
65
90
  return { id, value: id, label, name: label }
66
91
  })
67
92
  const shown = useMemo(() => {
@@ -73,6 +73,7 @@ import type { ColumnFilterConfig, GetDynamicColumns, RowActionPredicate } from '
73
73
  import { defaultGetDynamicColumns, DATE_CELL_TYPES, aggregateOf, formatAggregateTotal } from './dynamic-columns'
74
74
  import { useFacetLoaders, isLongTextColumn } from './use-facet-loaders'
75
75
  import { translateOptionLabels } from './filter-chips'
76
+ import { applyColumnFilterFields } from './column-filter-field'
76
77
  import { dedupeById, useInfiniteScrollSentinel } from './use-infinite-scroll'
77
78
  import {
78
79
  DYNAMIC_TABLE_CARD_ESTIMATE_PX,
@@ -1387,7 +1388,7 @@ export function DynamicTable({
1387
1388
  : undefined,
1388
1389
  })
1389
1390
  }
1390
- return map
1391
+ return applyColumnFilterFields(metadata.columns, map, dynamicFilters)
1391
1392
  }, [metadata, filterOptionsMap, dynamicFilters, handleDynamicFilterChange, facetsBase, getFacetLoader, facetOptions, t, defaultFilters, effectiveHiddenColumns])
1392
1393
 
1393
1394
  // Prewarm every facet field once the configs settle, so a text column's
@@ -0,0 +1,98 @@
1
+ // Field widget registry. The kernel serves `FieldDef.Widget` (wire: `widget`)
2
+ // on modal/form fields; the SDK knows a closed set of built-ins (upload, icon,
3
+ // dynamic_select…). A host-specific widget — a map pin for latitude/longitude,
4
+ // a weekly schedule editor — plugs in by name, so `DefineModal` can declare
5
+ // `Widget: "map-pin"` and both `<DynamicRecordDialog>` and `<DynamicForm>`
6
+ // render the host's component:
7
+ //
8
+ // import { registerFieldWidget } from '@asteby/metacore-runtime-react'
9
+ //
10
+ // const dispose = registerFieldWidget('map-pin', MapPinWidget)
11
+ //
12
+ // The widget owns the field's value: it receives `value` and reports edits
13
+ // through `onChange`, with no access to the form internals. A name with no
14
+ // registration is ignored and the renderer falls back to its `type`-based
15
+ // default, exactly as before.
16
+ //
17
+ // Module-level singleton like the model-extension / agent-result registries,
18
+ // and observable so a federated addon that registers after first paint
19
+ // re-renders the fields that were waiting for it.
20
+ import * as React from 'react'
21
+ import type { ActionFieldDef } from './types'
22
+
23
+ export interface FieldWidgetProps {
24
+ /** The served field definition (key, label, type, widget, options…). */
25
+ field: ActionFieldDef
26
+ /** Current value of the field. */
27
+ value: any
28
+ /** Report a new value for the field. */
29
+ onChange: (value: any) => void
30
+ /** The field is not editable right now (readonly / locked). */
31
+ disabled?: boolean
32
+ /** Validation message for the field, when there is one. */
33
+ error?: string
34
+ /**
35
+ * Record the widget can read sibling fields from. In `DynamicForm` these are the
36
+ * LIVE form values (they update as the user types, also in create mode); in the
37
+ * record dialog (`EditField`) it is the loaded/edited record.
38
+ */
39
+ record?: Record<string, any>
40
+ }
41
+
42
+ export type FieldWidgetComponent = React.ComponentType<FieldWidgetProps>
43
+
44
+ const widgets = new Map<string, FieldWidgetComponent>()
45
+ const listeners = new Set<() => void>()
46
+ let version = 0
47
+
48
+ function emit(): void {
49
+ version++
50
+ listeners.forEach((l) => l())
51
+ }
52
+
53
+ /**
54
+ * Registers the component that renders fields declaring `widget: <name>`.
55
+ * Returns a disposer; a later registration for the same name replaces the
56
+ * earlier one, and the earlier disposer then leaves the newer one alone.
57
+ */
58
+ export function registerFieldWidget(name: string, component: FieldWidgetComponent): () => void {
59
+ widgets.set(name, component)
60
+ emit()
61
+ return () => {
62
+ if (widgets.get(name) === component) {
63
+ widgets.delete(name)
64
+ emit()
65
+ }
66
+ }
67
+ }
68
+
69
+ /** The component registered for `name`, or undefined (never throws). */
70
+ export function getFieldWidget(name?: string | null): FieldWidgetComponent | undefined {
71
+ if (!name) return undefined
72
+ return widgets.get(name)
73
+ }
74
+
75
+ /** Registered widget names, for diagnostics. */
76
+ export function listFieldWidgets(): string[] {
77
+ return [...widgets.keys()]
78
+ }
79
+
80
+ export function clearFieldWidgets(): void {
81
+ if (widgets.size === 0) return
82
+ widgets.clear()
83
+ emit()
84
+ }
85
+
86
+ function subscribe(listener: () => void): () => void {
87
+ listeners.add(listener)
88
+ return () => listeners.delete(listener)
89
+ }
90
+
91
+ /** Re-renders the caller whenever a widget is (un)registered. */
92
+ export function useFieldWidgetRegistryVersion(): number {
93
+ return React.useSyncExternalStore(
94
+ subscribe,
95
+ () => version,
96
+ () => version,
97
+ )
98
+ }