From d2e4d53cbebecab198371a1c871a6147722f0ff9 Mon Sep 17 00:00:00 2001 From: Abhishiv Saxena Date: Thu, 27 Jun 2024 07:38:05 +0300 Subject: [PATCH] fixed implementation --- examples/kitchen-sink/src/index.tsx | 6 +++--- package.json | 2 +- src/dom/api.ts | 6 +----- src/stdlib/Each/index.tsx | 10 ++++++++++ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/examples/kitchen-sink/src/index.tsx b/examples/kitchen-sink/src/index.tsx index 5af77af..c934141 100644 --- a/examples/kitchen-sink/src/index.tsx +++ b/examples/kitchen-sink/src/index.tsx @@ -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(); }} @@ -128,7 +128,7 @@ export const Todos = component("Todos", (props, { signal, wire, store }) => { export const Layout = component<{}>( "Layout", (props, { signal, wire, api }) => { - return ; + // return ; const $count = signal("count", 0); const $doubleCount = wire(($) => $count($) * 2); // explicit subscription diff --git a/package.json b/package.json index 0bc3c21..38d2a57 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alfama", - "version": "0.3.24", + "version": "0.9.0", "author": "Abhishiv Saxena", "license": "MIT", "description": "Fine-grained reactive library with no compiler, no magic, and no virtual DOM", diff --git a/src/dom/api.ts b/src/dom/api.ts index 32c0ee6..ff7fc4c 100644 --- a/src/dom/api.ts +++ b/src/dom/api.ts @@ -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); diff --git a/src/stdlib/Each/index.tsx b/src/stdlib/Each/index.tsx index 9f4b2fb..3da9dc5 100644 --- a/src/stdlib/Each/index.tsx +++ b/src/stdlib/Each/index.tsx @@ -97,6 +97,7 @@ export const Each: ( // 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, @@ -106,6 +107,15 @@ export const Each: ( ); 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); }); }