Skip to content

Commit 478736b

Browse files
committed
init
1 parent bca8d18 commit 478736b

8 files changed

Lines changed: 226 additions & 2 deletions

File tree

doc/en/changeLog/index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
Although the version number uses the version number of Mettle, the content includes not only the updated content of Mettle, but also the updated content of other official tools in the same period.
55
:::
66

7-
## v0.3.1 (Latest)
7+
## v0.5.0 (Latest)
8+
9+
- Added two built-in properties, `$once` and `$memo`, to optimize rendering performance.
10+
11+
## v0.3.1
812

913
- Support TSX development applications. TSX provides type safety, better development experience and code maintainability in front-end projects.
1014

doc/en/essentials/api/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ function add() {
9696
}
9797
```
9898

99+
The third parameter is optional. Its type is `Symbol` and is used with the built-in attribute `$memo` to indicate updated data.
100+
99101
## html
100102

101103
` html`` ` is a tag function. The syntax of the tag function is to directly follow the function name with a template string. For example, you can write HTML tags directly in the template string.

doc/en/essentials/example/index.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,28 @@ export default () =>
631631
return () => <div style={toastStyles()}>{msg}</div>;
632632
});
633633
```
634+
635+
## Object to string
636+
637+
```jsx
638+
function useObjToStyle(obj: any) {
639+
const needUnit = (key: any) => !/[0-9]$/.test(key) && typeof obj[key] === 'number';
640+
641+
return Object.entries(obj)
642+
.map(([key, value]) => {
643+
const cssKey = key.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`);
644+
const cssValue = needUnit(key) ? `${value}px` : value;
645+
return `${cssKey}:${cssValue};`;
646+
})
647+
.join(' ');
648+
}
649+
650+
let isShow = true;
651+
function useShowStyle() {
652+
const style = {
653+
display: isShow ? 'block' : 'none',
654+
};
655+
656+
return useObjToStyle(style);
657+
}
658+
```

doc/en/essentials/usage/index.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,87 @@ defineComponent(() => {
323323
});
324324
```
325325

326+
### $once
327+
328+
Render the element only once, and skip future updates.
329+
330+
```jsx
331+
defineComponent(() => {
332+
let count = 0;
333+
334+
function add() {
335+
setData(() => {
336+
count++;
337+
});
338+
}
339+
340+
return () => (
341+
<fragment>
342+
<button onClick={add}>Add</button>
343+
<h1 $once>{count}</h1>
344+
<input value={count} />
345+
</fragment>
346+
);
347+
});
348+
```
349+
350+
### $memo
351+
352+
Cache a subtree of a template and skip the update of the subtree.
353+
354+
This property requires an array of fixed length. The value of the first item in the array is of type `Boolean`. If the value is `false`, the update of the entire subtree will be skipped. The value of the second item in the array is of type `Symbol`, which is used with `setData`.
355+
356+
```jsx
357+
defineComponent(({ setData }) => {
358+
const symbol1 = Symbol();
359+
let selected = 0;
360+
let arr = [
361+
{
362+
id: '1',
363+
val: 'A',
364+
},
365+
{
366+
id: '2',
367+
val: 'B',
368+
},
369+
{
370+
id: '3',
371+
val: 'C',
372+
},
373+
];
374+
375+
function handle(event) {
376+
const el = event.target;
377+
const id = Number(el.dataset.id);
378+
setData(
379+
() => {
380+
selected = id;
381+
},
382+
null,
383+
symbol1
384+
);
385+
return false;
386+
}
387+
388+
return () => (
389+
<fragment>
390+
<ul onClick={handle}>
391+
{arr.map((todo) => (
392+
<li
393+
$memo={[todo.id == selected, symbol1]}
394+
class={todo.id == selected ? 'danger' : ''}
395+
key={todo.id}
396+
data-id={todo.id}
397+
>
398+
{todo.val}
399+
</li>
400+
))}
401+
</ul>
402+
</fragment>
403+
);
404+
});
405+
```
406+
326407
## Built-in Tags
327408

328409
### component

doc/zh/changeLog/index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
版本号虽然取用的是 Mettle 的版本号,但是内容不仅仅含有 Mettle 更新的内容,还有同期的其他官方工具的更新内容。
55
:::
66

7-
## v0.3.1 (Latest)
7+
## v0.5.0 (Latest)
8+
9+
- 新增`$once``$memo`两个内置属性,用来优化渲染性能。
10+
11+
## v0.3.1
812

913
- 支持 TSX 开发应用,TSX 在前端项目中提供了类型安全、更好的开发体验和代码可维护性。
1014

doc/zh/essentials/api/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ function add() {
9696
}
9797
```
9898

99+
第三个参数非必传,参数的类型是`Symbol`,与内置属性`$memo`搭配使用,用于标明更新的数据。
100+
99101
## html
100102

101103
` html`` `是一个标签函数,标签函数的语法是直接在函数名后跟一个模板字符串。 例如,你可以直接在模板字符串中编写 HTML 标签。

doc/zh/essentials/example/index.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,28 @@ export default () =>
631631
return () => <div style={toastStyles()}>{msg}</div>;
632632
});
633633
```
634+
635+
## 对象转换为字符串
636+
637+
```jsx
638+
function useObjToStyle(obj: any) {
639+
const needUnit = (key: any) => !/[0-9]$/.test(key) && typeof obj[key] === 'number';
640+
641+
return Object.entries(obj)
642+
.map(([key, value]) => {
643+
const cssKey = key.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`);
644+
const cssValue = needUnit(key) ? `${value}px` : value;
645+
return `${cssKey}:${cssValue};`;
646+
})
647+
.join(' ');
648+
}
649+
650+
let isShow = true;
651+
function useShowStyle() {
652+
const style = {
653+
display: isShow ? 'block' : 'none',
654+
};
655+
656+
return useObjToStyle(style);
657+
}
658+
```

doc/zh/essentials/usage/index.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,87 @@ defineComponent(() => {
323323
});
324324
```
325325

326+
### $once
327+
328+
仅渲染元素一次,并跳过之后的更新。
329+
330+
```jsx
331+
defineComponent(({ setData }) => {
332+
let count = 0;
333+
334+
function add() {
335+
setData(() => {
336+
count++;
337+
});
338+
}
339+
340+
return () => (
341+
<fragment>
342+
<button onClick={add}>Add</button>
343+
<h1 $once>{count}</h1>
344+
<input value={count} />
345+
</fragment>
346+
);
347+
});
348+
```
349+
350+
### $memo
351+
352+
缓存一个模板的子树,跳过子树的更新。
353+
354+
该属性需要传入一个固定长度的数组。数组第一项的值的类型为 `Boolean`,如果值为`false`,那么整个子树的更新将被跳过。数组第二项值的类型为 `Symbol`,与 `setData` 搭配使用。
355+
356+
```jsx
357+
defineComponent(({ setData }) => {
358+
const symbol1 = Symbol();
359+
let selected = 0;
360+
let arr = [
361+
{
362+
id: '1',
363+
val: 'A',
364+
},
365+
{
366+
id: '2',
367+
val: 'B',
368+
},
369+
{
370+
id: '3',
371+
val: 'C',
372+
},
373+
];
374+
375+
function handle(event) {
376+
const el = event.target;
377+
const id = Number(el.dataset.id);
378+
setData(
379+
() => {
380+
selected = id;
381+
},
382+
null,
383+
symbol1
384+
);
385+
return false;
386+
}
387+
388+
return () => (
389+
<fragment>
390+
<ul onClick={handle}>
391+
{arr.map((todo) => (
392+
<li
393+
$memo={[todo.id == selected, symbol1]}
394+
class={todo.id == selected ? 'danger' : ''}
395+
key={todo.id}
396+
data-id={todo.id}
397+
>
398+
{todo.val}
399+
</li>
400+
))}
401+
</ul>
402+
</fragment>
403+
);
404+
});
405+
```
406+
326407
## 内置标签
327408

328409
### component

0 commit comments

Comments
 (0)