@nustackdev/ui-core 0.2.3 → 0.2.4

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/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "url": "git+https://github.com/nustackdev/nu.git",
7
7
  "directory": "pkgs/ts/ui-core"
8
8
  },
9
- "version": "0.2.3",
9
+ "version": "0.2.4",
10
10
  "type": "module",
11
11
  "main": "./src/index.ts",
12
12
  "types": "./src/index.ts",
package/src/tree.test.ts CHANGED
@@ -74,6 +74,18 @@ describe("write", () => {
74
74
  expect(segments(store.getState().getIn(["sec"]))).toEqual(["t", "u"]);
75
75
  });
76
76
 
77
+ it("keeps the declared props as the node's seed, untouched by later writes", () => {
78
+ const store = createTreeStore();
79
+ store.getState().write([step("f", "Form"), step("title", "InputRef", { value: "seed" })]);
80
+ store.getState().setProps(["f", "title"], { value: "typed" });
81
+ store.getState().write([step("f", "Form"), step("title", "InputRef", { value: "seed" })], {
82
+ value: "written",
83
+ });
84
+ const node = store.getState().getIn(["f", "title"]);
85
+ expect(node?.props.value).toBe("written");
86
+ expect(node?.seed).toEqual({ value: "seed" });
87
+ });
88
+
77
89
  it("keeps a prop set at runtime when a deep write rides through", () => {
78
90
  const store = createTreeStore();
79
91
  const chain = [
@@ -147,6 +159,7 @@ describe("write", () => {
147
159
  type: "InputRef",
148
160
  props: { value: "y" },
149
161
  children: new Map(),
162
+ seed: { value: "x" },
150
163
  });
151
164
  });
152
165
 
@@ -296,6 +309,7 @@ describe("dispatch", () => {
296
309
  type: "InputRef",
297
310
  props: { label: "L", value: "typed" },
298
311
  children: new Map(),
312
+ seed: { label: "L" },
299
313
  });
300
314
  });
301
315
 
package/src/tree.ts CHANGED
@@ -22,9 +22,9 @@
22
22
  // the component registry live in the kit.
23
23
 
24
24
  import { current, enableMapSet, isDraft } from "immer";
25
- import { type ChainLevel, type ErrorPayload, type Frame, OP_ERROR } from "./protocol";
26
- import { createStore, type StoreApi } from "zustand/vanilla";
27
25
  import { immer } from "zustand/middleware/immer";
26
+ import { createStore, type StoreApi } from "zustand/vanilla";
27
+ import { type ChainLevel, type ErrorPayload, type Frame, OP_ERROR } from "./protocol";
28
28
 
29
29
  // `children` is a Map, and immer only drafts those once this is on.
30
30
  enableMapSet();
@@ -45,6 +45,10 @@ export type Node = {
45
45
  props: Props;
46
46
  children: Map<string, Node>;
47
47
  hidden?: boolean;
48
+ // The props the node's slot declared, as they came on the chain that
49
+ // created it. Never written after that, so a reset (an input back to its
50
+ // declared value) has something to go back to once the live props moved.
51
+ seed?: Props;
48
52
  };
49
53
 
50
54
  /** Alias for consumers where the DOM's `Node` is also in scope. */
@@ -266,14 +270,20 @@ export function createTreeStore(options: TreeStoreOptions = {}): TreeStore {
266
270
  let child: Node | undefined = node.children.get(segment);
267
271
  if (!child) {
268
272
  child = emptyNode(type);
269
- if (stepProps) Object.assign(child.props, stepProps);
273
+ if (stepProps) {
274
+ Object.assign(child.props, stepProps);
275
+ child.seed = { ...stepProps };
276
+ }
270
277
  node.children.set(segment, child);
271
278
  } else if (child.type === null && type !== null) {
272
279
  // Autovivified earlier by something below it, so it
273
280
  // never got its declared props. This write names its
274
281
  // type, which is when it really comes into being.
275
282
  child.type = type;
276
- if (stepProps) Object.assign(child.props, stepProps);
283
+ if (stepProps) {
284
+ Object.assign(child.props, stepProps);
285
+ child.seed = { ...stepProps };
286
+ }
277
287
  }
278
288
  // Otherwise: the level is already there with the right
279
289
  // type. The chain says nothing new about it.