@plasmicapp/react-web 0.2.155 → 0.2.157

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.
@@ -28,6 +28,7 @@ export declare class StateSpecNode<T> {
28
28
  states(): StateCell<T>[];
29
29
  hasState(path: ObjectPath): boolean;
30
30
  createStateCell(path: ObjectPath): void;
31
+ deleteStateCell(prefixPath: ObjectPath): void;
31
32
  setInitialValue(path: ObjectPath, value: any): void;
32
33
  getInitialValue(path: ObjectPath): Symbol | T | undefined;
33
34
  addListener(path: ObjectPath, f: () => void): void;
@@ -1,4 +1,14 @@
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;
2
12
  export declare type ObjectPath = (string | number)[];
3
13
  export interface $StateSpec<T> {
4
14
  path: string;
@@ -1,5 +1,5 @@
1
- import { $State, $StateSpec } from "./types";
2
- export declare function useDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any>, opts?: {
1
+ import { $State, $StateSpec, DollarStateEnv } from "./types";
2
+ export declare function useDollarState(specs: $StateSpec<any>[], env: DollarStateEnv, opts?: {
3
3
  inCanvas: boolean;
4
4
  }): $State;
5
5
  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.155",
3
+ "version": "0.2.157",
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,9 +40,9 @@
40
40
  },
41
41
  "prettier": {},
42
42
  "dependencies": {
43
- "@plasmicapp/data-sources": "0.1.35",
43
+ "@plasmicapp/data-sources": "0.1.37",
44
44
  "@plasmicapp/data-sources-context": "0.1.6",
45
- "@plasmicapp/host": "1.0.104",
45
+ "@plasmicapp/host": "1.0.106",
46
46
  "@plasmicapp/query": "0.1.61",
47
47
  "@react-aria/checkbox": "^3.5.0",
48
48
  "@react-aria/focus": "^3.7.0",
@@ -109,5 +109,5 @@
109
109
  "react": ">=16.8.0",
110
110
  "react-dom": ">=16.8.0"
111
111
  },
112
- "gitHead": "ad015d481e656aa9235c5ac30cd83d184a8a11a9"
112
+ "gitHead": "64f5e28630820046b17012ad59e544061dade419"
113
113
  }
@@ -802,6 +802,15 @@ var StateSpecNode = /** @class */ (function () {
802
802
  path: path
803
803
  };
804
804
  };
805
+ StateSpecNode.prototype.deleteStateCell = function (prefixPath) {
806
+ var _this = this;
807
+ __spreadArray([], __read(Object.keys(this._state)), false).forEach(function (key) {
808
+ var path = JSON.parse(key);
809
+ if (shallowEqual(prefixPath, path.slice(0, prefixPath.length))) {
810
+ delete _this._state[key];
811
+ }
812
+ });
813
+ };
805
814
  StateSpecNode.prototype.setInitialValue = function (path, value) {
806
815
  var key = JSON.stringify(path);
807
816
  this._state[key].initialValue = value;
@@ -1063,7 +1072,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
1063
1072
  stateAccess.add({ path: path, node: node });
1064
1073
  var spec = node.getSpec();
1065
1074
  if (spec.valueProp) {
1066
- return $$state.props[spec.valueProp];
1075
+ return $$state.env.$props[spec.valueProp];
1067
1076
  }
1068
1077
  else if (!node.hasState(path) && spec.initFunc) {
1069
1078
  node.createStateCell(path);
@@ -1078,11 +1087,11 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
1078
1087
  stateAccess.forEach(function (_a) {
1079
1088
  var node = _a.node, path = _a.path;
1080
1089
  node.addListener(path, function () {
1081
- var newValue = initialSpecNode.getSpec().initFunc($$state.props, $state, $$state.ctx);
1090
+ var newValue = initialSpecNode.getSpec().initFunc(__assign({ $state: $state }, $$state.env));
1082
1091
  set(proxyRoot, initialStatePath, newValue);
1083
1092
  });
1084
1093
  });
1085
- var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))($$state.props, $state, $$state.ctx);
1094
+ var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))(__assign({ $state: $state }, $$state.env));
1086
1095
  initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
1087
1096
  var initialSpec = initialSpecNode.getSpec();
1088
1097
  var value = initialSpec.isImmutable
@@ -1091,7 +1100,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
1091
1100
  set(proxyRoot, initialStatePath, value);
1092
1101
  //immediately fire onChange
1093
1102
  if (initialSpec.onChangeProp) {
1094
- (_b = (_a = $$state.props)[initialSpec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, initialValue);
1103
+ (_b = (_a = $$state.env.$props)[initialSpec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, initialValue);
1095
1104
  }
1096
1105
  return initialValue;
1097
1106
  }
@@ -1116,7 +1125,14 @@ function create$StateProxy($$state, leafHandlers) {
1116
1125
  //we are always in a leaf, since we only have two cases:
1117
1126
  // 1 - delete properties outside the state tree
1118
1127
  // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
1119
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get(proxyRoot, currPath.slice(spec.pathObj.length)));
1128
+ (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get(proxyRoot, currPath.slice(spec.pathObj.length)));
1129
+ }
1130
+ var nextPath = getNextPath(property);
1131
+ var nextNode = currNode.makeTransition(property);
1132
+ if (nextNode) {
1133
+ getStateCells(nextNode).forEach(function (node) {
1134
+ node.deleteStateCell(nextPath);
1135
+ });
1120
1136
  }
1121
1137
  return Reflect.deleteProperty(target, property);
1122
1138
  },
@@ -1143,7 +1159,7 @@ function create$StateProxy($$state, leafHandlers) {
1143
1159
  return Reflect.get(target, property, receiver);
1144
1160
  },
1145
1161
  set: function (target, property, value, receiver) {
1146
- var _a, _b;
1162
+ var _a, _b, _c, _d;
1147
1163
  var nextPath = getNextPath(property);
1148
1164
  var nextNode = currNode.makeTransition(property);
1149
1165
  if (property === "registerInitFunc" && currPath.length === 0) {
@@ -1175,12 +1191,19 @@ function create$StateProxy($$state, leafHandlers) {
1175
1191
  else {
1176
1192
  Reflect.set(target, property, value, receiver);
1177
1193
  }
1178
- nextNode.getAllSpecs().forEach(function (spec) {
1179
- var _a, _b;
1194
+ if (currNode.isLeaf()) {
1180
1195
  if (spec.onChangeProp) {
1181
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value);
1196
+ (_d = (_c = $$state.env.$props)[spec.onChangeProp]) === null || _d === void 0 ? void 0 : _d.call(_c, target);
1182
1197
  }
1183
- });
1198
+ }
1199
+ else {
1200
+ nextNode.getAllSpecs().forEach(function (spec) {
1201
+ var _a, _b;
1202
+ if (spec.onChangeProp) {
1203
+ (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value);
1204
+ }
1205
+ });
1206
+ }
1184
1207
  var newValue = (isOutside || currNode.isLeaf()) && currNode.getSpec().isImmutable
1185
1208
  ? mkUntrackedValue(value)
1186
1209
  : value;
@@ -1217,22 +1240,27 @@ function create$StateProxy($$state, leafHandlers) {
1217
1240
  var mkUntrackedValue = function (o) {
1218
1241
  return o != null && typeof o === "object" ? ref(o) : o;
1219
1242
  };
1220
- function useDollarState(specs, props, $ctx, opts) {
1243
+ var envFieldsAreNonNill = function (env) {
1244
+ var _a, _b;
1245
+ return ({
1246
+ $props: env.$props,
1247
+ $ctx: (_a = env.$ctx) !== null && _a !== void 0 ? _a : {},
1248
+ $queries: (_b = env.$queries) !== null && _b !== void 0 ? _b : {}
1249
+ });
1250
+ };
1251
+ function useDollarState(specs, env, opts) {
1221
1252
  var $$state = React__default.useRef((function () {
1222
1253
  var rootSpecTree = buildTree(specs);
1223
1254
  return {
1224
1255
  rootSpecTree: rootSpecTree,
1225
1256
  specTreeLeaves: getStateCells(rootSpecTree),
1226
1257
  stateValues: proxy({}),
1227
- props: {},
1228
- ctx: {},
1258
+ env: envFieldsAreNonNill(env),
1229
1259
  specs: [],
1230
1260
  registrationsQueue: proxy([])
1231
1261
  };
1232
1262
  })()).current;
1233
- $$state.props = props;
1234
- $$state.ctx = $ctx !== null && $ctx !== void 0 ? $ctx : {};
1235
- $$state.specs = specs;
1263
+ ($$state.env = envFieldsAreNonNill(env)), ($$state.specs = specs);
1236
1264
  var create$State = function () {
1237
1265
  var $state = Object.assign(create$StateProxy($$state, function (node, path) {
1238
1266
  if (!node.hasState(path)) {
@@ -1249,7 +1277,7 @@ function useDollarState(specs, props, $ctx, opts) {
1249
1277
  get: function (target, property, receiver) {
1250
1278
  var spec = node.getSpec();
1251
1279
  if (spec.valueProp) {
1252
- return $$state.props[spec.valueProp];
1280
+ return $$state.env.$props[spec.valueProp];
1253
1281
  }
1254
1282
  else {
1255
1283
  return Reflect.get(target, property, receiver);
@@ -1262,7 +1290,7 @@ function useDollarState(specs, props, $ctx, opts) {
1262
1290
  if (!node.hasState(realPath)) {
1263
1291
  node.createStateCell(realPath);
1264
1292
  }
1265
- if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
1293
+ if (!deepEqual(node.getState(realPath).initialValue, f(__assign({ $state: $state }, $$state.env)))) {
1266
1294
  $$state.registrationsQueue.push(mkUntrackedValue({ node: node, path: realPath, f: f }));
1267
1295
  }
1268
1296
  }
@@ -1296,7 +1324,7 @@ function useDollarState(specs, props, $ctx, opts) {
1296
1324
  }
1297
1325
  node.createStateCell(spec.pathObj);
1298
1326
  var init = spec.valueProp
1299
- ? $$state.props[spec.valueProp]
1327
+ ? $$state.env.$props[spec.valueProp]
1300
1328
  : spec.initFunc
1301
1329
  ? initializeStateValue($$state, node, spec.pathObj, $state)
1302
1330
  : spec.initVal;
@@ -1312,7 +1340,7 @@ function useDollarState(specs, props, $ctx, opts) {
1312
1340
  var node = _a.node, stateCell = _a.stateCell;
1313
1341
  var initFunc = node.getInitFunc(stateCell);
1314
1342
  if (initFunc) {
1315
- var newInit = initFunc(props, $state, $ctx !== null && $ctx !== void 0 ? $ctx : {});
1343
+ var newInit = initFunc(__assign({ $state: $state }, envFieldsAreNonNill(env)));
1316
1344
  if (!deepEqual(newInit, stateCell.initialValue)) {
1317
1345
  resetSpecs.push({ stateCell: stateCell, node: node });
1318
1346
  }
@@ -1323,7 +1351,7 @@ function useDollarState(specs, props, $ctx, opts) {
1323
1351
  var newInit = initializeStateValue($$state, node, stateCell.path, $state);
1324
1352
  var spec = node.getSpec();
1325
1353
  if (spec.onChangeProp) {
1326
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, newInit);
1354
+ (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, newInit);
1327
1355
  }
1328
1356
  };
1329
1357
  useIsomorphicLayoutEffect(function () {
@@ -1331,7 +1359,7 @@ function useDollarState(specs, props, $ctx, opts) {
1331
1359
  var stateCell = _a.stateCell, node = _a.node;
1332
1360
  reInitializeState(node, stateCell);
1333
1361
  });
1334
- }, [props, resetSpecs]);
1362
+ }, [env.$props, resetSpecs]);
1335
1363
  useIsomorphicLayoutEffect(function () {
1336
1364
  while ($$state.registrationsQueue.length) {
1337
1365
  var _a = $$state.registrationsQueue.shift(), node = _a.node, path = _a.path, f = _a.f;