@nustackdev/ui-core 0.2.1 → 0.2.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/package.json +1 -1
- package/src/tree.test.ts +84 -0
- package/src/tree.ts +34 -4
package/package.json
CHANGED
package/src/tree.test.ts
CHANGED
|
@@ -408,6 +408,90 @@ describe("dispatch", () => {
|
|
|
408
408
|
});
|
|
409
409
|
});
|
|
410
410
|
|
|
411
|
+
describe("visibility", () => {
|
|
412
|
+
it("hides a node and keeps its props and children", () => {
|
|
413
|
+
const store = createTreeStore();
|
|
414
|
+
store.getState().write([step("a", "Card", { title: "T" }), step("b", "TextRef")], { value: "x" });
|
|
415
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "Card", { title: "T" })]));
|
|
416
|
+
const a = store.getState().getIn(["a"]);
|
|
417
|
+
expect(a?.hidden).toBe(true);
|
|
418
|
+
expect(a?.props).toEqual({ title: "T" });
|
|
419
|
+
expect(store.getState().getIn(["a", "b"])?.props).toEqual({ value: "x" });
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it("shows it again on display", () => {
|
|
423
|
+
const store = createTreeStore();
|
|
424
|
+
store.getState().write([step("a", "TextRef")]);
|
|
425
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "TextRef")]));
|
|
426
|
+
store.getState().dispatch(frame(OPS.display, ["a"], null, [step("a", "TextRef")]));
|
|
427
|
+
expect(store.getState().getIn(["a"])?.hidden).toBeUndefined();
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it("brings a node into being hidden when the hide is the first frame", () => {
|
|
431
|
+
const store = createTreeStore();
|
|
432
|
+
store.getState().dispatch(
|
|
433
|
+
frame(OPS.hide, ["a", "b"], null, [step("a", "Column"), step("b", "TextRef", { value: "seed" })]),
|
|
434
|
+
);
|
|
435
|
+
const b = store.getState().getIn(["a", "b"]);
|
|
436
|
+
expect(b?.type).toBe("TextRef");
|
|
437
|
+
expect(b?.props).toEqual({ value: "seed" });
|
|
438
|
+
expect(b?.hidden).toBe(true);
|
|
439
|
+
expect(store.getState().getIn(["a"])?.hidden).toBeUndefined();
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it("keeps the flag through prop merges and a type's own write handler", () => {
|
|
443
|
+
const behaviours: Record<string, Behaviour> = {
|
|
444
|
+
HeadingRef: {
|
|
445
|
+
handlers: {
|
|
446
|
+
write: (ctx, payload) =>
|
|
447
|
+
ctx.update((props) => {
|
|
448
|
+
props.label = String(payload);
|
|
449
|
+
}),
|
|
450
|
+
},
|
|
451
|
+
},
|
|
452
|
+
};
|
|
453
|
+
const store = createTreeStore({ resolve: (t) => behaviours[t] });
|
|
454
|
+
store.getState().write([step("h", "HeadingRef"), step("t", "TextRef")]);
|
|
455
|
+
store.getState().dispatch(frame(OPS.hide, ["h"], null, [step("h", "HeadingRef")]));
|
|
456
|
+
store.getState().dispatch(frame(OPS.hide, ["h", "t"], null, [step("h", "HeadingRef"), step("t", "TextRef")]));
|
|
457
|
+
store.getState().dispatch(frame(OPS.write, ["h"], "hello", [step("h", "HeadingRef")]));
|
|
458
|
+
store.getState().dispatch(
|
|
459
|
+
frame(OPS.write, ["h", "t"], { value: 1, hidden: false }, [step("h", "HeadingRef"), step("t", "TextRef")]),
|
|
460
|
+
);
|
|
461
|
+
expect(store.getState().getIn(["h"])?.hidden).toBe(true);
|
|
462
|
+
expect(store.getState().getIn(["h"])?.props.label).toBe("hello");
|
|
463
|
+
expect(store.getState().getIn(["h", "t"])?.hidden).toBe(true);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it("never asks a type's handlers", () => {
|
|
467
|
+
const errors: string[] = [];
|
|
468
|
+
const hide = vi.fn();
|
|
469
|
+
const behaviours: Record<string, Behaviour> = { TextRef: { handlers: { hide } } };
|
|
470
|
+
const store = createTreeStore({ resolve: (t) => behaviours[t], onError: (m) => errors.push(m) });
|
|
471
|
+
store.getState().write([step("a", "TextRef")]);
|
|
472
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "TextRef")]));
|
|
473
|
+
expect(hide).not.toHaveBeenCalled();
|
|
474
|
+
expect(errors).toEqual([]);
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it("leaves an untouched node's identity alone on a repeat", () => {
|
|
478
|
+
const store = createTreeStore();
|
|
479
|
+
store.getState().write([step("a", "TextRef")]);
|
|
480
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "TextRef")]));
|
|
481
|
+
const root = store.getState().root;
|
|
482
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "TextRef")]));
|
|
483
|
+
expect(store.getState().root).toBe(root);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
it("still removes a hidden node", () => {
|
|
487
|
+
const store = createTreeStore();
|
|
488
|
+
store.getState().write([step("a", "TextRef")]);
|
|
489
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "TextRef")]));
|
|
490
|
+
store.getState().dispatch(frame(OPS.remove, ["a"]));
|
|
491
|
+
expect(store.getState().getIn(["a"])).toBeNull();
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
|
|
411
495
|
describe("send", () => {
|
|
412
496
|
it("drops frames until a sender is set, then delivers", () => {
|
|
413
497
|
const sent: TreeFrame[] = [];
|
package/src/tree.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// The browser store, as a tree.
|
|
2
2
|
//
|
|
3
|
-
// One node is
|
|
4
|
-
// keyed by segment
|
|
5
|
-
//
|
|
6
|
-
// own -- a node's current value
|
|
3
|
+
// One node is three things: a `type`, a bag of `props`, and `children`
|
|
4
|
+
// keyed by segment (plus the `hidden` flag the visibility ops set). `type` is
|
|
5
|
+
// null for a node that exists only because something below it was written.
|
|
6
|
+
// `value` is just a prop, not a field of its own -- a node's current value
|
|
7
|
+
// lives at `props.value` like everything else.
|
|
7
8
|
//
|
|
8
9
|
// Writes autovivify. A frame carries a `chain`, root-first, one triple per
|
|
9
10
|
// level, so a write to `a.b.c` creates `a` and `b` with the type and props
|
|
@@ -31,10 +32,14 @@ export type Props = Record<string, unknown>;
|
|
|
31
32
|
// A Map and not a plain object: render order is insertion order, and an
|
|
32
33
|
// object hoists integer-like keys to the front, so a segment like "0" or an
|
|
33
34
|
// index from a dynamic ref would silently jump the queue.
|
|
35
|
+
//
|
|
36
|
+
// `hidden` lives on the node and not in its props, so no prop merge and no
|
|
37
|
+
// type's own write handler can clobber it. Unset means shown.
|
|
34
38
|
export type Node = {
|
|
35
39
|
type: string | null;
|
|
36
40
|
props: Props;
|
|
37
41
|
children: Map<string, Node>;
|
|
42
|
+
hidden?: boolean;
|
|
38
43
|
};
|
|
39
44
|
|
|
40
45
|
/** Alias for consumers where the DOM's `Node` is also in scope. */
|
|
@@ -63,6 +68,11 @@ export const OPS = {
|
|
|
63
68
|
// write to it. What a boot batch is made of.
|
|
64
69
|
init: "init",
|
|
65
70
|
remove: "remove",
|
|
71
|
+
// Chain only, no payload, like init. Hide keeps the node, its props and
|
|
72
|
+
// its children (state survives, the browser just draws nothing); display
|
|
73
|
+
// shows it again. Every type takes them, none handles them.
|
|
74
|
+
hide: "hide",
|
|
75
|
+
display: "display",
|
|
66
76
|
read: "read",
|
|
67
77
|
notify: "notify",
|
|
68
78
|
} as const;
|
|
@@ -101,6 +111,8 @@ export type TreeActions = {
|
|
|
101
111
|
getIn: (path: Path) => Node | null;
|
|
102
112
|
/** Merge props into an existing node. No-op when the node is not there. */
|
|
103
113
|
setProps: (path: Path, props: Props) => void;
|
|
114
|
+
/** Hide or show an existing node. No-op when the node is not there. */
|
|
115
|
+
setHidden: (path: Path, hidden: boolean) => void;
|
|
104
116
|
/** Route an inbound frame to its node, by node type and frame op. */
|
|
105
117
|
dispatch: (frame: TreeFrame) => void;
|
|
106
118
|
/** Outbound. No-op until a sender is set. */
|
|
@@ -266,6 +278,17 @@ export function createTreeStore(options: TreeStoreOptions = {}): TreeStore {
|
|
|
266
278
|
Object.assign(node.props, props);
|
|
267
279
|
}),
|
|
268
280
|
|
|
281
|
+
setHidden: (path, hidden) => {
|
|
282
|
+
const node = getNode(get().root, path);
|
|
283
|
+
if (!node || Boolean(node.hidden) === hidden) return;
|
|
284
|
+
set((draft) => {
|
|
285
|
+
const target = getNode(draft.root as Node, path);
|
|
286
|
+
if (!target) return;
|
|
287
|
+
if (hidden) target.hidden = true;
|
|
288
|
+
else delete target.hidden;
|
|
289
|
+
});
|
|
290
|
+
},
|
|
291
|
+
|
|
269
292
|
send: (frame) => {
|
|
270
293
|
if (outbound) outbound(frame);
|
|
271
294
|
},
|
|
@@ -291,6 +314,13 @@ export function createTreeStore(options: TreeStoreOptions = {}): TreeStore {
|
|
|
291
314
|
api.write(chain);
|
|
292
315
|
return;
|
|
293
316
|
}
|
|
317
|
+
if (frame.op === OPS.hide || frame.op === OPS.display) {
|
|
318
|
+
// Structure first (the node may not exist yet), then the
|
|
319
|
+
// flag. Generic for every type, never a handler's call.
|
|
320
|
+
api.write(chain);
|
|
321
|
+
api.setHidden(frame.ref, frame.op === OPS.hide);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
294
324
|
const handlerFor = (node: Node | null) => {
|
|
295
325
|
const behaviour = node?.type ? resolve(node.type) : undefined;
|
|
296
326
|
return behaviour?.handlers?.[frame.op];
|