@xmachines/json-render-react 0.19.0-xm.2 → 0.20.0-xm.1

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.
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  elementTreeSchema,
3
3
  schema
4
- } from "./chunk-WYDS23XB.mjs";
4
+ } from "./chunk-SDI5YI5X.mjs";
5
5
 
6
6
  // ../react-state/dist/index.mjs
7
7
  import {
@@ -249,8 +249,10 @@ function ValidationProvider({
249
249
  const [fieldStates, setFieldStates] = useState({});
250
250
  const fieldStatesRef = useRef2({});
251
251
  const [fieldConfigs, setFieldConfigs] = useState({});
252
+ const registrationCounts = useRef2({});
252
253
  const registerField = useCallback2(
253
254
  (path, config) => {
255
+ registrationCounts.current[path] = (registrationCounts.current[path] ?? 0) + 1;
254
256
  setFieldConfigs((prev) => {
255
257
  const existing = prev[path];
256
258
  if (existing && validationConfigEqual(existing, config)) {
@@ -261,6 +263,20 @@ function ValidationProvider({
261
263
  },
262
264
  []
263
265
  );
266
+ const unregisterField = useCallback2((path) => {
267
+ const next = (registrationCounts.current[path] ?? 0) - 1;
268
+ if (next > 0) {
269
+ registrationCounts.current[path] = next;
270
+ return;
271
+ }
272
+ delete registrationCounts.current[path];
273
+ delete fieldStatesRef.current[path];
274
+ setFieldConfigs((prev) => {
275
+ if (!(path in prev)) return prev;
276
+ const { [path]: _removed, ...rest } = prev;
277
+ return rest;
278
+ });
279
+ }, []);
264
280
  const validate = useCallback2(
265
281
  (path, config) => {
266
282
  const currentState = getSnapshot();
@@ -332,7 +348,8 @@ function ValidationProvider({
332
348
  touch,
333
349
  clear,
334
350
  validateAll,
335
- registerField
351
+ registerField,
352
+ unregisterField
336
353
  }),
337
354
  [
338
355
  customFunctions,
@@ -343,7 +360,8 @@ function ValidationProvider({
343
360
  touch,
344
361
  clear,
345
362
  validateAll,
346
- registerField
363
+ registerField,
364
+ unregisterField
347
365
  ]
348
366
  );
349
367
  return /* @__PURE__ */ jsx3(ValidationContext.Provider, { value, children });
@@ -364,13 +382,20 @@ function useFieldValidation(path, config) {
364
382
  validate: validateField,
365
383
  touch: touchField,
366
384
  clear: clearField,
367
- registerField
385
+ registerField,
386
+ unregisterField
368
387
  } = useValidation();
388
+ const configKey = config === void 0 ? void 0 : JSON.stringify(config);
389
+ const configRef = React2.useRef(config);
390
+ configRef.current = config;
369
391
  React2.useEffect(() => {
370
- if (path && config) {
371
- registerField(path, config);
392
+ const current = configRef.current;
393
+ if (path && current) {
394
+ registerField(path, current);
395
+ return () => unregisterField(path);
372
396
  }
373
- }, [path, config, registerField]);
397
+ return void 0;
398
+ }, [path, configKey, registerField, unregisterField]);
374
399
  const state = fieldStates[path] ?? {
375
400
  touched: false,
376
401
  validated: false,
@@ -564,9 +589,8 @@ function ActionProvider({
564
589
  handler,
565
590
  setState: set,
566
591
  navigate,
567
- executeAction: async (name) => {
568
- const subBinding = { action: name };
569
- await execute(subBinding);
592
+ executeAction: async (binding2) => {
593
+ await execute(binding2);
570
594
  }
571
595
  });
572
596
  } finally {
@@ -585,9 +609,8 @@ function ActionProvider({
585
609
  handler,
586
610
  setState: set,
587
611
  navigate,
588
- executeAction: async (name) => {
589
- const subBinding = { action: name };
590
- await execute(subBinding);
612
+ executeAction: async (binding2) => {
613
+ await execute(binding2);
591
614
  }
592
615
  });
593
616
  } finally {
@@ -787,6 +810,9 @@ import {
787
810
  resolveElementProps,
788
811
  resolveBindings,
789
812
  resolveActionParam,
813
+ resolveRepeatItemStatePath,
814
+ resolveRepeatStatePath,
815
+ splitRepeatVisibility,
790
816
  evaluateVisibility as evaluateVisibility2,
791
817
  getByPath as getByPath2,
792
818
  isDevtoolsActive,
@@ -794,6 +820,18 @@ import {
794
820
  createDirectiveRegistry
795
821
  } from "@xmachines/json-render-core";
796
822
  import { Fragment, jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
823
+ var registryMetadata = /* @__PURE__ */ new WeakMap();
824
+ function withRenderErrorHandler(registry, onRenderError) {
825
+ const next = { ...registry };
826
+ Object.defineProperty(next, "onRenderError", {
827
+ value: onRenderError,
828
+ enumerable: false,
829
+ configurable: true
830
+ });
831
+ const metadata = registryMetadata.get(registry);
832
+ if (metadata) registryMetadata.set(next, metadata);
833
+ return next;
834
+ }
797
835
  function reportRenderError(registry, elementType, error, componentStack) {
798
836
  const onRenderError = registry?.onRenderError;
799
837
  if (typeof onRenderError === "function") {
@@ -873,7 +911,9 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
873
911
  base.directives = directives;
874
912
  return base;
875
913
  }, [ctx, repeatScope, functions, directives]);
876
- const isVisible = element.visible === void 0 ? true : evaluateVisibility2(element.visible, fullCtx);
914
+ const repeatVisibility = element.repeat !== void 0 && repeatScope == null ? splitRepeatVisibility(element.visible) : { container: element.visible, itemFilter: void 0 };
915
+ const repeatItemFilter = repeatVisibility.itemFilter;
916
+ const isVisible = repeatVisibility.container === void 0 ? true : evaluateVisibility2(repeatVisibility.container, fullCtx);
877
917
  const onBindings = element.on;
878
918
  const emit = useCallback4(
879
919
  async (eventName) => {
@@ -986,21 +1026,28 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
986
1026
  console.warn(`No renderer for component type: ${resolvedElement.type}`);
987
1027
  return null;
988
1028
  }
989
- const children = resolvedElement.repeat ? /* @__PURE__ */ jsx6(
990
- RepeatChildren,
991
- {
992
- element: resolvedElement,
993
- spec,
994
- registry,
995
- loading,
996
- fallback
1029
+ const metadata = registryMetadata.get(registry)?.[resolvedElement.type];
1030
+ if (resolvedElement.slots && metadata?.slots) {
1031
+ const availableSlots = new Set(metadata.slots);
1032
+ for (const slotName of Object.keys(resolvedElement.slots)) {
1033
+ if (slotName === "default") {
1034
+ console.warn(
1035
+ `[json-render] Component "${resolvedElement.type}" uses slots.default. Use "children" for default slot content.`
1036
+ );
1037
+ } else if (!availableSlots.has(slotName)) {
1038
+ console.warn(
1039
+ `[json-render] Unknown slot "${slotName}" on component "${resolvedElement.type}". Available slots: ${metadata.slots.join(", ")}`
1040
+ );
1041
+ }
997
1042
  }
998
- ) : resolvedElement.children?.map((childKey) => {
1043
+ }
1044
+ const renderChildKeys = (childKeys, slotName) => childKeys.map((childKey) => {
999
1045
  const childElement = spec.elements[childKey];
1000
1046
  if (!childElement) {
1001
1047
  if (!loading) {
1048
+ const location = slotName ? `in slot "${slotName}" of "${resolvedElement.type}"` : `as child of "${resolvedElement.type}"`;
1002
1049
  console.warn(
1003
- `[json-render] Missing element "${childKey}" referenced as child of "${resolvedElement.type}". This element will not render.`
1050
+ `[json-render] Missing element "${childKey}" referenced ${location}. This element will not render.`
1004
1051
  );
1005
1052
  }
1006
1053
  return null;
@@ -1018,10 +1065,28 @@ var ElementRenderer = React5.memo(function ElementRenderer2({
1018
1065
  childKey
1019
1066
  );
1020
1067
  });
1068
+ const children = resolvedElement.repeat ? /* @__PURE__ */ jsx6(
1069
+ RepeatChildren,
1070
+ {
1071
+ element: resolvedElement,
1072
+ spec,
1073
+ registry,
1074
+ loading,
1075
+ fallback,
1076
+ itemFilter: repeatItemFilter
1077
+ }
1078
+ ) : resolvedElement.children ? renderChildKeys(resolvedElement.children) : void 0;
1079
+ const slots = resolvedElement.slots ? Object.fromEntries(
1080
+ Object.entries(resolvedElement.slots).map(([slotName, childKeys]) => [
1081
+ slotName,
1082
+ renderChildKeys(childKeys, slotName)
1083
+ ])
1084
+ ) : void 0;
1021
1085
  const rendered = /* @__PURE__ */ jsx6(
1022
1086
  Component,
1023
1087
  {
1024
1088
  element: resolvedElement,
1089
+ slots,
1025
1090
  emit,
1026
1091
  on,
1027
1092
  bindings: elementBindings,
@@ -1044,13 +1109,32 @@ function RepeatChildren({
1044
1109
  spec,
1045
1110
  registry,
1046
1111
  loading,
1047
- fallback
1112
+ fallback,
1113
+ itemFilter
1048
1114
  }) {
1049
1115
  const { state } = useStateStore();
1116
+ const { ctx } = useVisibility();
1117
+ const parentScope = useRepeatScope();
1050
1118
  const repeat = element.repeat;
1051
- const statePath = repeat.statePath;
1119
+ const statePath = resolveRepeatStatePath(
1120
+ repeat.statePath,
1121
+ parentScope?.basePath
1122
+ );
1123
+ if (statePath === void 0) {
1124
+ console.warn(
1125
+ "[json-render] $item in repeat.statePath used outside of a repeat scope"
1126
+ );
1127
+ return null;
1128
+ }
1052
1129
  const items = getByPath2(state, statePath) ?? [];
1053
- return /* @__PURE__ */ jsx6(Fragment, { children: items.map((itemValue, index) => {
1130
+ const entries = items.map((itemValue, index) => ({ itemValue, index })).filter(
1131
+ ({ itemValue, index }) => itemFilter === void 0 || evaluateVisibility2(itemFilter, {
1132
+ ...ctx,
1133
+ repeatItem: itemValue,
1134
+ repeatIndex: index
1135
+ })
1136
+ );
1137
+ return /* @__PURE__ */ jsx6(Fragment, { children: entries.map(({ itemValue, index }) => {
1054
1138
  const key = repeat.key && typeof itemValue === "object" && itemValue !== null ? String(
1055
1139
  itemValue[repeat.key] ?? index
1056
1140
  ) : String(index);
@@ -1059,7 +1143,7 @@ function RepeatChildren({
1059
1143
  {
1060
1144
  item: itemValue,
1061
1145
  index,
1062
- basePath: `${statePath}/${index}`,
1146
+ basePath: resolveRepeatItemStatePath(statePath, index),
1063
1147
  children: element.children?.map((childKey) => {
1064
1148
  const childElement = spec.elements[childKey];
1065
1149
  if (!childElement) {
@@ -1095,16 +1179,10 @@ function Renderer({
1095
1179
  fallback,
1096
1180
  onRenderError
1097
1181
  }) {
1098
- const rendererRegistry = useMemo5(() => {
1099
- if (!onRenderError) return registry;
1100
- const next = { ...registry };
1101
- Object.defineProperty(next, "onRenderError", {
1102
- value: onRenderError,
1103
- enumerable: false,
1104
- configurable: true
1105
- });
1106
- return next;
1107
- }, [registry, onRenderError]);
1182
+ const rendererRegistry = useMemo5(
1183
+ () => onRenderError ? withRenderErrorHandler(registry, onRenderError) : registry,
1184
+ [registry, onRenderError]
1185
+ );
1108
1186
  if (!spec || !spec.root) {
1109
1187
  return null;
1110
1188
  }
@@ -1167,13 +1245,14 @@ function ConfirmationDialogManager() {
1167
1245
  }
1168
1246
  );
1169
1247
  }
1170
- function defineRegistry(_catalog, options) {
1248
+ function defineRegistry(catalog, options) {
1171
1249
  const registry = {};
1172
1250
  if (options.components) {
1173
1251
  for (const [name, componentFn] of Object.entries(options.components)) {
1174
1252
  registry[name] = ({
1175
1253
  element,
1176
1254
  children,
1255
+ slots,
1177
1256
  emit,
1178
1257
  on,
1179
1258
  bindings,
@@ -1182,6 +1261,7 @@ function defineRegistry(_catalog, options) {
1182
1261
  return componentFn({
1183
1262
  props: element.props,
1184
1263
  children,
1264
+ slots,
1185
1265
  emit,
1186
1266
  on,
1187
1267
  bindings,
@@ -1190,6 +1270,10 @@ function defineRegistry(_catalog, options) {
1190
1270
  };
1191
1271
  }
1192
1272
  }
1273
+ const catalogComponents = catalog.data.components;
1274
+ if (catalogComponents) {
1275
+ registryMetadata.set(registry, catalogComponents);
1276
+ }
1193
1277
  const actionMap = options.actions ? Object.entries(options.actions) : [];
1194
1278
  const handlers = (getSetState, getState) => {
1195
1279
  const result = {};
@@ -1223,6 +1307,10 @@ function defineRegistry(_catalog, options) {
1223
1307
  }
1224
1308
  function createRenderer(catalog, components) {
1225
1309
  const registry = components;
1310
+ const catalogComponents = catalog.data.components;
1311
+ if (catalogComponents) {
1312
+ registryMetadata.set(registry, catalogComponents);
1313
+ }
1226
1314
  return function CatalogRenderer({
1227
1315
  spec,
1228
1316
  store,
@@ -1239,16 +1327,10 @@ function createRenderer(catalog, components) {
1239
1327
  () => directives ? createDirectiveRegistry(directives) : void 0,
1240
1328
  [directives]
1241
1329
  );
1242
- const rendererRegistry = useMemo5(() => {
1243
- if (!onRenderError) return registry;
1244
- const next = { ...registry };
1245
- Object.defineProperty(next, "onRenderError", {
1246
- value: onRenderError,
1247
- enumerable: false,
1248
- configurable: true
1249
- });
1250
- return next;
1251
- }, [onRenderError]);
1330
+ const rendererRegistry = useMemo5(
1331
+ () => onRenderError ? withRenderErrorHandler(registry, onRenderError) : registry,
1332
+ [onRenderError]
1333
+ );
1252
1334
  const actionHandlers = onAction ? new Proxy(
1253
1335
  {},
1254
1336
  {