Skip to content

Commit

Permalink
fixed <Each> implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishiv committed Jun 27, 2024
1 parent 65f29f8 commit d2e4d53
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions examples/kitchen-sink/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export const Todos = component("Todos", (props, { signal, wire, store }) => {
json.stats = { count: 0 };
console.log(json);
produce($todos.items, (items) => {
//items.splice(1, 0, json);
items.push(json);
items.splice(1, 0, json);
//items.push(json);
});
(e.target as HTMLFormElement).reset();
}}
Expand Down Expand Up @@ -128,7 +128,7 @@ export const Todos = component("Todos", (props, { signal, wire, store }) => {
export const Layout = component<{}>(
"Layout",
(props, { signal, wire, api }) => {
return <Todos />;
// return <Todos />;
const $count = signal("count", 0);
const $doubleCount = wire(($) => $count($) * 2); // explicit subscription

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alfama",
"version": "0.3.24",
"version": "0.9.0",
"author": "Abhishiv Saxena<[email protected]>",
"license": "MIT",
"description": "Fine-grained reactive library with no compiler, no magic, and no virtual DOM",
Expand Down
6 changes: 1 addition & 5 deletions src/dom/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ export const addNode = (
const elementsToInsert = node.dom;
if (before) {
const beforeIndex = parentStep.children.indexOf(before);
parentStep.children.splice(
beforeIndex === 0 ? 0 : beforeIndex - 1,
0,
node
);
parentStep.children.splice(beforeIndex, 0, node);

const refNode: HTMLElement = before.dom as HTMLElement;
refNode.before(elementsToInsert);
Expand Down
10 changes: 10 additions & 0 deletions src/stdlib/Each/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const Each: <T extends ArrayOrObject>(
// Add the new nodes being spliced in
items.forEach((item, i) => {
const index = startIndex + i;
const previousChildren = [...(pStep.children || [])];
const { treeStep, el } = renderArray(
pStep,
props.renderItem,
Expand All @@ -106,6 +107,15 @@ export const Each: <T extends ArrayOrObject>(
);
const { registry, root } = reifyTree(renderContext, el, pStep);
const before = previousChildren[startIndex + i] || null;
console.log(previousChildren);
console.log("before", {
startIndex,
i,
before,
pStep,
parentStep,
root,
});
addNode(renderContext, pStep, root, before);
});
}
Expand Down

0 comments on commit d2e4d53

Please sign in to comment.