@plasmicapp/react-web 0.2.159 → 0.2.161

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.
@@ -1,4 +1,15 @@
1
- export declare type InitFunc<T> = ($props: Record<string, any>, $state: $State, $ctx: Record<string, any>, indexes?: number[]) => T;
1
+ export declare type InitFuncEnv = {
2
+ $props: Record<string, any>;
3
+ $state: Record<string, any>;
4
+ $queries?: Record<string, any>;
5
+ $ctx?: Record<string, any>;
6
+ };
7
+ export declare type DollarStateEnv = Omit<InitFuncEnv, "$state">;
8
+ export declare type NoUndefinedField<T> = {
9
+ [P in keyof T]-?: T[P];
10
+ };
11
+ export declare type InitFunc<T> = (env: NoUndefinedField<InitFuncEnv>) => T;
12
+ export declare type DeprecatedInitFunc<T> = ($props: Record<string, any>, $state: $State, $ctx: Record<string, any>) => T;
2
13
  export declare type ObjectPath = (string | number)[];
3
14
  export interface $StateSpec<T> {
4
15
  path: string;
@@ -1,5 +1,3 @@
1
1
  import { $State, $StateSpec } from "./types";
2
- export declare function useDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any>, opts?: {
3
- inCanvas: boolean;
4
- }): $State;
2
+ export declare function useDollarState(specs: $StateSpec<any>[], ...rest: any[]): $State;
5
3
  export default useDollarState;
@@ -58,6 +58,9 @@ interface Person {
58
58
  export declare const FormBuilder: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {
59
59
  people: Person[];
60
60
  }>;
61
+ export declare const FormBuilderImplicitStates: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {
62
+ people: Person[];
63
+ }>;
61
64
  export declare const StateCellIsArray: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {
62
65
  people: Person[];
63
66
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plasmicapp/react-web",
3
- "version": "0.2.159",
3
+ "version": "0.2.161",
4
4
  "description": "plasmic library for rendering in the presentational style",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "prettier": {},
42
42
  "dependencies": {
43
- "@plasmicapp/data-sources": "0.1.38",
43
+ "@plasmicapp/data-sources": "0.1.39",
44
44
  "@plasmicapp/data-sources-context": "0.1.6",
45
45
  "@plasmicapp/host": "1.0.107",
46
46
  "@plasmicapp/query": "0.1.61",
@@ -109,5 +109,5 @@
109
109
  "react": ">=16.8.0",
110
110
  "react-dom": ">=16.8.0"
111
111
  },
112
- "gitHead": "c37c2f944f1fc0572fd11293d56313fb52a1720b"
112
+ "gitHead": "345916a2f99a5b4015f1415f26335d003d404715"
113
113
  }
@@ -1075,7 +1075,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
1075
1075
  stateAccess.add({ path: path, node: node });
1076
1076
  var spec = node.getSpec();
1077
1077
  if (spec.valueProp) {
1078
- return $$state.props[spec.valueProp];
1078
+ return $$state.env.$props[spec.valueProp];
1079
1079
  }
1080
1080
  else if (!node.hasState(path) && spec.initFunc) {
1081
1081
  node.createStateCell(path);
@@ -1090,11 +1090,12 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
1090
1090
  stateAccess.forEach(function (_a) {
1091
1091
  var node = _a.node, path = _a.path;
1092
1092
  node.addListener(path, function () {
1093
- var newValue = initialSpecNode.getSpec().initFunc($$state.props, $state, $$state.ctx);
1093
+ var newValue = invokeInitFuncBackwardsCompatible(initialSpecNode.getSpec().initFunc, __assign({ $state: $state }, $$state.env));
1094
1094
  set(proxyRoot, initialStatePath, newValue);
1095
1095
  });
1096
1096
  });
1097
- var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))($$state.props, $state, $$state.ctx);
1097
+ var initFunc = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath));
1098
+ var initialValue = invokeInitFuncBackwardsCompatible(initFunc, __assign({ $state: $state }, $$state.env));
1098
1099
  initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
1099
1100
  var initialSpec = initialSpecNode.getSpec();
1100
1101
  var value = initialSpec.isImmutable
@@ -1103,7 +1104,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
1103
1104
  set(proxyRoot, initialStatePath, value);
1104
1105
  //immediately fire onChange
1105
1106
  if (initialSpec.onChangeProp) {
1106
- (_b = (_a = $$state.props)[initialSpec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, initialValue);
1107
+ (_b = (_a = $$state.env.$props)[initialSpec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, initialValue);
1107
1108
  }
1108
1109
  return initialValue;
1109
1110
  }
@@ -1128,7 +1129,7 @@ function create$StateProxy($$state, leafHandlers) {
1128
1129
  //we are always in a leaf, since we only have two cases:
1129
1130
  // 1 - delete properties outside the state tree
1130
1131
  // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
1131
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get(proxyRoot, currPath.slice(spec.pathObj.length)));
1132
+ (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get(proxyRoot, currPath.slice(spec.pathObj.length)));
1132
1133
  }
1133
1134
  var nextPath = getNextPath(property);
1134
1135
  var nextNode = currNode.makeTransition(property);
@@ -1162,7 +1163,7 @@ function create$StateProxy($$state, leafHandlers) {
1162
1163
  return Reflect.get(target, property, receiver);
1163
1164
  },
1164
1165
  set: function (target, property, value, receiver) {
1165
- var _a, _b;
1166
+ var _a, _b, _c, _d;
1166
1167
  var nextPath = getNextPath(property);
1167
1168
  var nextNode = currNode.makeTransition(property);
1168
1169
  if (property === "registerInitFunc" && currPath.length === 0) {
@@ -1194,12 +1195,19 @@ function create$StateProxy($$state, leafHandlers) {
1194
1195
  else {
1195
1196
  Reflect.set(target, property, value, receiver);
1196
1197
  }
1197
- nextNode.getAllSpecs().forEach(function (spec) {
1198
- var _a, _b;
1198
+ if (currNode.isLeaf()) {
1199
1199
  if (spec.onChangeProp) {
1200
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value);
1200
+ (_d = (_c = $$state.env.$props)[spec.onChangeProp]) === null || _d === void 0 ? void 0 : _d.call(_c, target);
1201
1201
  }
1202
- });
1202
+ }
1203
+ else {
1204
+ nextNode.getAllSpecs().forEach(function (spec) {
1205
+ var _a, _b;
1206
+ if (spec.onChangeProp) {
1207
+ (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value);
1208
+ }
1209
+ });
1210
+ }
1203
1211
  var newValue = (isOutside || currNode.isLeaf()) && currNode.getSpec().isImmutable
1204
1212
  ? mkUntrackedValue(value)
1205
1213
  : value;
@@ -1236,22 +1244,77 @@ function create$StateProxy($$state, leafHandlers) {
1236
1244
  var mkUntrackedValue = function (o) {
1237
1245
  return o != null && typeof o === "object" ? ref(o) : o;
1238
1246
  };
1239
- function useDollarState(specs, props, $ctx, opts) {
1247
+ var envFieldsAreNonNill = function (env) {
1248
+ var _a, _b;
1249
+ return ({
1250
+ $props: env.$props,
1251
+ $ctx: (_a = env.$ctx) !== null && _a !== void 0 ? _a : {},
1252
+ $queries: (_b = env.$queries) !== null && _b !== void 0 ? _b : {}
1253
+ });
1254
+ };
1255
+ /**
1256
+ * We need to support two versions with different parameters to be backward compatible
1257
+ * {
1258
+ * specs: $StateSpec<any>[],
1259
+ * props: Record<string, any>,
1260
+ * $ctx?: Record<string, any>,
1261
+ * opts?: { inCanvas: boolean; }
1262
+ * }
1263
+ * {
1264
+ * specs: $StateSpec<any>[],
1265
+ * env: { $props; $queries; $ctx },
1266
+ * opts?: { inCanvas: boolean }
1267
+ * }
1268
+ */
1269
+ function extractDollarStateParametersBackwardCompatible() {
1270
+ var rest = [];
1271
+ for (var _i = 0; _i < arguments.length; _i++) {
1272
+ rest[_i] = arguments[_i];
1273
+ }
1274
+ if ("$props" in rest[0]) {
1275
+ // latest version
1276
+ return {
1277
+ env: rest[0],
1278
+ opts: rest[1]
1279
+ };
1280
+ }
1281
+ else {
1282
+ return {
1283
+ env: {
1284
+ $props: rest[0],
1285
+ $ctx: rest[1],
1286
+ $queries: {}
1287
+ },
1288
+ opts: rest[2]
1289
+ };
1290
+ }
1291
+ }
1292
+ function invokeInitFuncBackwardsCompatible(initFunc, env) {
1293
+ if (initFunc.length > 1) {
1294
+ return initFunc(env.$props, env.$state, env.$ctx);
1295
+ }
1296
+ else {
1297
+ return initFunc(env);
1298
+ }
1299
+ }
1300
+ function useDollarState(specs) {
1301
+ var rest = [];
1302
+ for (var _i = 1; _i < arguments.length; _i++) {
1303
+ rest[_i - 1] = arguments[_i];
1304
+ }
1305
+ var _a = extractDollarStateParametersBackwardCompatible.apply(void 0, __spreadArray([], __read(rest), false)), env = _a.env, opts = _a.opts;
1240
1306
  var $$state = React__default.useRef((function () {
1241
1307
  var rootSpecTree = buildTree(specs);
1242
1308
  return {
1243
1309
  rootSpecTree: rootSpecTree,
1244
1310
  specTreeLeaves: getStateCells(rootSpecTree),
1245
1311
  stateValues: proxy({}),
1246
- props: {},
1247
- ctx: {},
1312
+ env: envFieldsAreNonNill(env),
1248
1313
  specs: [],
1249
1314
  registrationsQueue: proxy([])
1250
1315
  };
1251
1316
  })()).current;
1252
- $$state.props = props;
1253
- $$state.ctx = $ctx !== null && $ctx !== void 0 ? $ctx : {};
1254
- $$state.specs = specs;
1317
+ ($$state.env = envFieldsAreNonNill(env)), ($$state.specs = specs);
1255
1318
  var create$State = function () {
1256
1319
  var $state = Object.assign(create$StateProxy($$state, function (node, path) {
1257
1320
  if (!node.hasState(path)) {
@@ -1268,7 +1331,7 @@ function useDollarState(specs, props, $ctx, opts) {
1268
1331
  get: function (target, property, receiver) {
1269
1332
  var spec = node.getSpec();
1270
1333
  if (spec.valueProp) {
1271
- return $$state.props[spec.valueProp];
1334
+ return $$state.env.$props[spec.valueProp];
1272
1335
  }
1273
1336
  else {
1274
1337
  return Reflect.get(target, property, receiver);
@@ -1281,7 +1344,7 @@ function useDollarState(specs, props, $ctx, opts) {
1281
1344
  if (!node.hasState(realPath)) {
1282
1345
  node.createStateCell(realPath);
1283
1346
  }
1284
- if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
1347
+ if (!deepEqual(node.getState(realPath).initialValue, f(__assign({ $state: $state }, $$state.env)))) {
1285
1348
  $$state.registrationsQueue.push(mkUntrackedValue({ node: node, path: realPath, f: f }));
1286
1349
  }
1287
1350
  }
@@ -1315,7 +1378,7 @@ function useDollarState(specs, props, $ctx, opts) {
1315
1378
  }
1316
1379
  node.createStateCell(spec.pathObj);
1317
1380
  var init = spec.valueProp
1318
- ? $$state.props[spec.valueProp]
1381
+ ? $$state.env.$props[spec.valueProp]
1319
1382
  : spec.initFunc
1320
1383
  ? initializeStateValue($$state, node, spec.pathObj, $state)
1321
1384
  : spec.initVal;
@@ -1331,7 +1394,7 @@ function useDollarState(specs, props, $ctx, opts) {
1331
1394
  var node = _a.node, stateCell = _a.stateCell;
1332
1395
  var initFunc = node.getInitFunc(stateCell);
1333
1396
  if (initFunc) {
1334
- var newInit = initFunc(props, $state, $ctx !== null && $ctx !== void 0 ? $ctx : {});
1397
+ var newInit = invokeInitFuncBackwardsCompatible(initFunc, __assign({ $state: $state }, envFieldsAreNonNill(env)));
1335
1398
  if (!deepEqual(newInit, stateCell.initialValue)) {
1336
1399
  resetSpecs.push({ stateCell: stateCell, node: node });
1337
1400
  }
@@ -1342,7 +1405,7 @@ function useDollarState(specs, props, $ctx, opts) {
1342
1405
  var newInit = initializeStateValue($$state, node, stateCell.path, $state);
1343
1406
  var spec = node.getSpec();
1344
1407
  if (spec.onChangeProp) {
1345
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, newInit);
1408
+ (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, newInit);
1346
1409
  }
1347
1410
  };
1348
1411
  useIsomorphicLayoutEffect(function () {
@@ -1350,7 +1413,7 @@ function useDollarState(specs, props, $ctx, opts) {
1350
1413
  var stateCell = _a.stateCell, node = _a.node;
1351
1414
  reInitializeState(node, stateCell);
1352
1415
  });
1353
- }, [props, resetSpecs]);
1416
+ }, [env.$props, resetSpecs]);
1354
1417
  useIsomorphicLayoutEffect(function () {
1355
1418
  while ($$state.registrationsQueue.length) {
1356
1419
  var _a = $$state.registrationsQueue.shift(), node = _a.node, path = _a.path, f = _a.f;