Skip to content

Commit

Permalink
[IMP] add withKey helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ged-odoo committed Aug 24, 2021
1 parent 7c73b9a commit bb2d61b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ export function remove(vnode: VNode) {
vnode.beforeRemove();
vnode.remove();
}

export function withKey(vnode: VNode, key: any) {
vnode.key = key;
return vnode;
}
14 changes: 4 additions & 10 deletions tests/list.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { list, mount, multi, patch, text, createBlock, VNode } from "../src";
import { list, mount, multi, patch, text, createBlock, VNode, withKey } from "../src";
import { makeTestFixture } from "./helpers";

//------------------------------------------------------------------------------
Expand All @@ -16,9 +16,7 @@ afterEach(() => {
});

function kText(str: string, key: any): VNode {
const block = text(str);
block.key = key;
return block;
return withKey(text(str), key);
}

function n(n: number) {
Expand All @@ -29,16 +27,12 @@ const span = createBlock("<span><block-text-0/></span>");
const p = createBlock("<p><block-text-0/></p>");

function kSpan(str: string, key: any): VNode {
const block = span([str]);
block.key = key;
return block;
return withKey(span([str]), key);
}

function kPair(n: number): VNode {
const bnodes = [p([String(n)]), p([String(n)])];
const block = multi(bnodes);
block.key = n;
return block;
return withKey(multi(bnodes), n);
}

describe("list node: misc", () => {
Expand Down

0 comments on commit bb2d61b

Please sign in to comment.