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

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