Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support collection type and nesting templates diff #70

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/basic/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { reactive, customElement, attribute, html } from 'pwc';

@customElement('child-element')
class Child extends HTMLElement {
name = 'Child';
@reactive
accessor data = {};

@reactive
@attribute('data-class-name')
Expand Down
21 changes: 21 additions & 0 deletions examples/condition/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Basic</title>
<style>
.red {
color: red;
}
.green {
color: green;
}
</style>
</head>
<body>
<custom-element>
</custom-element>
<script type="module" src="/main.js"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions examples/condition/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { reactive, customElement, html } from 'pwc';

@customElement('child-element')
class ChildElement extends HTMLElement {
@reactive
accessor data = { foo: 0 }
get template() {
console.log('>>>');
return html`<div>${this.data.foo}</div>`;
}
}

@customElement('custom-element')
class CustomElement extends HTMLElement {
@reactive
accessor #condition = true;

@reactive
accessor #icon = '';

@reactive
accessor #data = { foo: 1 }


handleClick() {
console.log('click');
this.#condition = !this.#condition;
this.#icon += '!';
}

get template() {
const result = html`<div>
<p @click=${this.handleClick}>Condition is ${this.#condition + ''}</p>
${this.#condition ? html`<p>True Condition${this.#icon}</p>` : html`<p>False Condition${this.#icon}</p>`}
<child-element data=${this.#data}></child-element>
</div>`;

console.log(result);
return result;
}
}
18 changes: 18 additions & 0 deletions examples/condition/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "basic",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"rollup": "^2.0.0",
"vite": "^2.7.2",
"vite-plugin-babel": "^1.0.0"
},
"dependencies": {
"pwc": "workspace:*"
},
"browserslist": "chrome > 60"
}
33 changes: 33 additions & 0 deletions examples/condition/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { defineConfig } from 'vite';
import babel from 'vite-plugin-babel';

export default defineConfig({
plugins: [
babel({
babelConfig: {
presets: [
[
'@babel/preset-env',
{
targets: {
chrome: 99,
},
modules: false,
},
],
],
plugins: [
[
'@babel/plugin-proposal-decorators',
{
version: '2021-12',
},
],
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-class-static-block',
'@babel/plugin-proposal-private-methods'
],
},
}),
],
});
26 changes: 13 additions & 13 deletions examples/list/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ class CustomElement extends HTMLElement {
}

get template() {
return html`<div @click=${this.onClick}>
${html`${this.#list.map((item, index) => {
if (item === 'item 2') {
return null;
}
if (item === 'item 3') {
return [1, 2, 3].map((insideItem) => {
return html`<div @click=${() => this.handleItemClick(index)}>inside list: ${insideItem}</div>`;
});
}
return html`<div @click=${() => this.handleItemClick(index)}>${item}</div>`;
})}`}
</div>`;
return html`<div @click=${this.handleToggle}>
${html`${this.#list.map((item, index) => {
if (item === 'item 2') {
return null;
}
if (item === 'item 3') {
return [1, 2, 3].map((insideItem) => {
return html`<div @click=${() => this.handleItemClick(index)}>inside list: ${insideItem}</div>`;
});
}
return html`<div @click=${() => this.handleItemClick(index)}>${item}</div>`;
})}`}
</div>`;
}
}
9 changes: 8 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
module.exports = {
coverageDirectory: './coverage/',
collectCoverage: true,
collectCoverageFrom: ['packages/pwc/src/**/*.{js,ts}', 'packages/pwc-compiler/esm/**/*.{js,ts}', '!packages/**/*.d.ts', '!packages/**/type.ts', '!packages/*/src/index.{js,ts}'],
collectCoverageFrom: [
'packages/pwc/src/**/*.{js,ts}',
'packages/pwc-compiler/esm/**/*.{js,ts}',
'!packages/**/*.d.ts',
'!packages/**/type.ts',
'!packages/*/src/index.{js,ts}',
'!packages/*/src/utils/*.{js,ts}',
],
coveragePathIgnorePatterns: ['<rootDir>/node_modules/'],
roots: ['<rootDir>/packages'],
testPathIgnorePatterns: ['/node_modules/', '/cjs/', '/esm/', '/es2017/', '/dist/', '.d.ts'],
Expand Down
2 changes: 1 addition & 1 deletion packages/pwc/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from '@ice/pkg';

export default defineConfig({
sourceMaps: 'inline',
sourceMaps: false,

transform: {
excludes: ['**/__tests__/**'],
Expand Down
3 changes: 2 additions & 1 deletion packages/pwc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"author": "Rax Team",
"homepage": "https://github.com/raxjs/pwc#readme",
"license": "MIT",
"main": "esm/index.js",
"main": "cjs/index.js",
"module": "",
"files": [
"esm/",
"es2017/",
Expand Down
2 changes: 2 additions & 0 deletions packages/pwc/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export const TEXT_COMMENT_DATA = '?pwc_t';
export const PLACEHOLDER_COMMENT_DATA = '?pwc_p';
export const enum ReactiveFlags {
RAW = '__p_raw__',
PROPERTY = '__p_property__',
IS_REACTIVE = '__p_is_reactive__',
}

export const TemplateString = 'templateString';
Expand Down
94 changes: 86 additions & 8 deletions packages/pwc/src/elements/__tests__/HTMLElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ describe('Render HTMLElement', () => {

const container = document.getElementById('reactive-container');
container.click();

// @ts-ignore
expect(element._getChangedProperties()).toEqual(new Set(['#data', '#text', '#className']));
await nextTick();
expect(element.innerHTML).toEqual(
'<!--?pwc_p--><div id="reactive-container" class="green">hello?<!--?pwc_t--> - jack!<!--?pwc_t--></div><!--?pwc_t-->',
Expand Down Expand Up @@ -264,32 +265,48 @@ describe('Render nested components', () => {

it('any reactive data should trigger the update of child components', async () => {
const parentBtn = document.getElementById('parent-btn');
const childElement = document.getElementById('child-container');
expect(childElement.innerHTML).toEqual(
const childContainer = document.getElementById('child-container');
expect(childContainer.innerHTML).toEqual(
'\n <!--?pwc_p--><div>Hello<!--?pwc_t--> - World<!--?pwc_t--> - <!--?pwc_t--></div>\n ',
);
expect(mockChildFn).toBeCalledTimes(1);

const parentElement = element;
const childElement = document.getElementsByTagName('child-element')[0];
// @ts-ignore
expect(parentElement._getChangedProperties()).toEqual(new Set());
// @ts-ignore
expect(childElement._getChangedProperties()).toEqual(new Set());

// primity type
parentBtn.click();
// @ts-ignore
expect(parentElement._getChangedProperties()).toEqual(new Set(['#title']));

await nextTick();
expect(childElement.innerHTML).toEqual(
expect(childContainer.innerHTML).toEqual(
'\n <!--?pwc_p--><div>Hello!<!--?pwc_t--> - World<!--?pwc_t--> - <!--?pwc_t--></div>\n ',
);
expect(mockChildFn).toBeCalledTimes(2);

// object type
parentBtn.click();
// @ts-ignore
expect(parentElement._getChangedProperties()).toEqual(new Set(['#data']));

await nextTick();
expect(childElement.innerHTML).toEqual(
expect(childContainer.innerHTML).toEqual(
'\n <!--?pwc_p--><div>Hello!<!--?pwc_t--> - World!<!--?pwc_t--> - <!--?pwc_t--></div>\n ',
);
expect(mockChildFn).toBeCalledTimes(3);

// array type
parentBtn.click();
// @ts-ignore
expect(parentElement._getChangedProperties()).toEqual(new Set(['#items']));

await nextTick();
expect(childElement.innerHTML).toEqual(
expect(childContainer.innerHTML).toEqual(
'\n <!--?pwc_p--><div>Hello!<!--?pwc_t--> - World!<!--?pwc_t--> - 2<!--?pwc_t--></div>\n ',
);
expect(mockChildFn).toBeCalledTimes(4);
Expand Down Expand Up @@ -323,6 +340,65 @@ describe('Render nested components', () => {
);
expect(mockChildFn).toBeCalledTimes(6);
});

it('keep track of changed properties', async () => {
const mockFn1 = jest.fn();
const mockFn2 = jest.fn();
@customElement('child-element-1')
class ChildElement1 extends HTMLElement {
@reactive
accessor data = {}
get template() {
return mockFn1();
}
}

@customElement('child-element-2')
class ChildElement2 extends HTMLElement {
@reactive
accessor data = {}
get template() {
return mockFn2();
}
}


@customElement('track-element')
class ParentElement extends HTMLElement {
@reactive
accessor #data1 = { foo: 1 }

@reactive
accessor #data2 = { foo: 2};

handleClick() {
this.#data2.foo = 3;
}

get template() {
return html`
<div id="track-btn" @click=${this.handleClick}>Click</div>
<child-element-1 data=${this.#data1}></child-element-1>
<child-element-2 data=${this.#data2}></child-element-2>
`
}
}

const element = document.createElement('track-element');
document.body.append(element);
const btn = document.getElementById('track-btn');

expect(mockFn1).toBeCalledTimes(1);
expect(mockFn2).toBeCalledTimes(1);

btn.click();
// @ts-ignore
expect(element._getChangedProperties()).toEqual(new Set(['#data2']));

await nextTick();
expect(mockFn1).toBeCalledTimes(1);
expect(mockFn2).toBeCalledTimes(2);
});
});

describe('render multiple kinds template', () => {
Expand Down Expand Up @@ -443,7 +519,7 @@ describe('render multiple kinds template', () => {
document.body.appendChild(el2);

expect(el2.innerHTML).toEqual(
'<span>1<!--?pwc_t--></span><span>3<!--?pwc_t--></span><span>2<!--?pwc_t--></span><!--?pwc_t-->',
'<span>1<!--?pwc_t--></span><span>3<!--?pwc_t--></span><span>2<!--?pwc_t--></span><!--?pwc_t--><!--?pwc_t-->',
);

@customElement('hybrid-list')
Expand Down Expand Up @@ -493,7 +569,7 @@ describe('render multiple kinds template', () => {
inside list: 2<!--?pwc_t-->
</div><!--?pwc_p--><div class=\"item3\">
inside list: 3<!--?pwc_t-->
</div><!--?pwc_p--><div class=\"item4\">item4<!--?pwc_t--></div><!--?pwc_t--><!--?pwc_t-->`);
</div><!--?pwc_t--><!--?pwc_p--><div class=\"item4\">item4<!--?pwc_t--></div><!--?pwc_t--><!--?pwc_t-->`);
const item3 = document.getElementsByClassName('item3')[0];
item3.click();
await nextTick();
Expand Down Expand Up @@ -585,3 +661,5 @@ describe('render with rax', () => {
);
});
});


Loading