Skip to content

Commit

Permalink
feat: add keep
Browse files Browse the repository at this point in the history
keep the children of the element after first render.
  • Loading branch information
sebkolind committed Jun 8, 2024
1 parent e591ca0 commit 29d9e68
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/__tests__/attributes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ describe('attributes.ts', () => {

expect(el.hasAttribute('mounted')).toBe(false);
});

test("doesn't add `keep` as an attribute", () => {
const el = document.createElement('div');

addAttribute(el, 'keep', true);

expect(el.hasAttribute('keep')).toBe(false);
});
});
38 changes: 37 additions & 1 deletion src/__tests__/component.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { mount } from '../mount';
import { tags } from '../tags';
import { type Component } from '../types';
import { getByText, getByTestId, fireEvent } from '@testing-library/dom';
import {
getByText,
getByTestId,
fireEvent,
getByRole,
queryByText,
} from '@testing-library/dom';

const { div, p, button } = tags;

Expand Down Expand Up @@ -76,4 +82,34 @@ describe('components', () => {

expect(el).toBeDefined();
});

test('keep', () => {
const KeepComponent: Component<{ count: number }> = {
state: { count: 0 },
view({ state }) {
return div(
div(
[
`Don't change me ${state.count}`,
button('Increment', {
onclick: () => state.count++,
}),
],
{ keep: true },
),
);
},
};

mount(document.body, KeepComponent);

const btn = getByRole(document.body, 'button');

expect(btn).toBeDefined();

fireEvent.click(btn);

expect(queryByText(document.body, /Don't change me 0/)).toBeTruthy();
expect(queryByText(document.body, /Don't change me 1/)).toBeNull();
});
});
2 changes: 1 addition & 1 deletion src/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function addAttribute<A extends Attrs>(
key: string,
value: TagAttrsValues,
) {
if (key === 'mounted') {
if (key === 'mounted' || key === 'keep') {
return;
}

Expand Down
13 changes: 11 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@ export type TentNode<A extends Attrs = undefined> = Node &
Element &
HTMLElement & {
$tent: {
attributes: object;
attributes: Record<string, TagAttrsValues> & {
mounted?: ({ el }: { el: TentNode }) => void;
keep?: boolean;
};
};
dataset: A & DOMStringMap;
children: TentNode<A>[];
};

export type Children = string | number | TentNode | (Node | string | Context)[];
export type Children =
| string
| number
| TentNode
| Element
| (Node | string | Context)[];
export type Context = [string, Children, TagAttrs | undefined];

export type TagAttrsValues = string | boolean | number | Function;
type TagAttrs = Record<string, TagAttrsValues> & {
mounted?: ({ el }: { el: TentNode }) => void;
keep?: boolean;
};
export type Tags = Record<
string,
Expand Down
1 change: 1 addition & 0 deletions src/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function walker<A extends Attrs>(oldNode: TentNode<A>, newNode: TentNode<A>) {
}

if (oc.length === 0 && nc.length === 0) return;
if (oldNode.$tent.attributes.keep) return;

if (oc.length < nc.length) {
for (let i = 0; i < nc.length; i++) {
Expand Down

0 comments on commit 29d9e68

Please sign in to comment.