Skip to content

Commit 5ee0281

Browse files
committed
update api
1 parent 8c88abd commit 5ee0281

File tree

7 files changed

+284
-16
lines changed

7 files changed

+284
-16
lines changed

docs/v0.1.0/en/api/global/component.mdx

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,111 @@ impl FatherView{
243243
}
244244
}
245245
</script>
246+
```
247+
248+
## `#[computed]`
249+
250+
Computed properties are values ​​dynamically calculated based on existing properties. In GenUI, computed properties do not store state, but are automatically updated based on changes in their dependencies.
251+
252+
| Features | Computed | Two-way Binding |
253+
| ------------------ | ---------------------------------- | --------------------- |
254+
| Store state? | ❌ No | ✅ Yes |
255+
| Mutable? | ✅ Update when dependencies change | ✅ Yes |
256+
| Manually editable? | ✅ But cannot use set_xxx method | ✅ Modify via set_xxx |
257+
| Dependent data | ✅ Requires dependencies | ✅ Component state |
258+
259+
### Usage
260+
261+
1. `#[computed]` needs to be declared in the method
262+
2. `#[computed]` depends on the fields declared in the component, which can be multiple
263+
3. The return value of the calculated property needs to be consistent with the component binding
264+
4. The calculated property is equivalent to handing over the value processing to the developer, so `set_xxx()` cannot be used in the calculated property method to change the component field. Instead, a more flexible assignment change is used
265+
266+
```rust
267+
impl Hello{
268+
#[computed([$(arg)*])]
269+
fn $computed_fn($[&self|&mut self]?) -> $return_value_type{
270+
// ...
271+
}
272+
}
273+
```
274+
275+
> - `$(arg)*`: Depends on the parameter list, indicating that there can be one or more
276+
> - `$computed_fn`: Calculated attribute processing function
277+
> - `$[&self|&mut self]?`: Indicates that it can be `&self` or `&mut self`, which is up to you to decide
278+
> - `$return_value_type`: Return value type
279+
280+
### Example
281+
282+
The way to write a calculated property is to use a method to declare it when binding the property:
283+
284+
```html
285+
<template>
286+
<component name="Hello" class="hello_view" spacing="12.0">
287+
<label :text="fmt_count()"></label>
288+
</component>
289+
</template>
290+
291+
<script>
292+
#[component]
293+
pub struct Hello{
294+
count: u32,
295+
}
296+
297+
impl Hello{
298+
#[computed([count])]
299+
fn fmt_count(&self) -> String{
300+
format!("count: {}", self.count)
301+
}
302+
}
303+
</script>
304+
```
305+
306+
## `nav_to!`
307+
308+
Used to jump from a sub-page of routing management to other sub-pages.
309+
310+
### Usage
311+
312+
```rust
313+
nav_to!($page_id);
314+
```
315+
316+
> - `$page_id`: id of the page
317+
318+
```toml title="router.toml"
319+
[bar_pages]
320+
login = { path = "crate::views::login::*", component = "Login" }
321+
```
322+
323+
The login key configured in `router.toml` is the page id
324+
325+
### Example
326+
327+
```rust
328+
impl Hello{
329+
pub fn to_register(&mut self) {
330+
nav_to!(register);
331+
}
332+
}
333+
```
334+
335+
## `nav_back!`
336+
337+
Return the route to the previous page by pushing the stack, if any.
338+
339+
### Usage
340+
341+
```rust
342+
nav_back!();
343+
```
344+
345+
### Example
346+
347+
```
348+
impl Hello{
349+
pub fn back(&mut self) {
350+
nav_back!();
351+
}
352+
}
246353
```

docs/v0.1.0/en/api/option/lifetime.mdx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Badge } from '@theme';
22

3-
# Life cycle <Badge text="experiment" type="danger" />
3+
# Life cycle <Badge text="experiment" type="danger" />
44

55
> [!WARNING]
66
>
7-
> Currently, the complete life cycle has not been fully implemented. It is an experimental function. Open `#[before_mount]`, `#mounted`
7+
> Currently, the complete life cycle has not been fully implemented. It is an experimental function.
8+
>
9+
> Open `#[before_mount]`, `#mounted`, `#[before_update]`, `#[updated]`
810
911
## `#[before_mount]`
1012

@@ -38,8 +40,23 @@ impl MyView{
3840

3941
## `#[before_update]`
4042

41-
Called before instance update
43+
Called after component properties are updated but before re-rendering the component
44+
45+
### Usage
4246

4347
## `#[updated]`
4448

45-
Called after instance update
49+
Called after the component properties have been updated and the component has been re-rendered.
50+
51+
### Usage
52+
53+
```rust
54+
impl Hello{
55+
#[updated]
56+
fn do_updated(&mut self){
57+
println!("do updated => hello.gen!");
58+
let new_val = self.get_count() + 1;
59+
self.set_count(new_val);
60+
}
61+
}
62+
```

docs/v0.1.0/en/components/feedback/dropdown.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Use dialog as drop-down option
3636
| `offset_x` | X-axis offset | `F32` |
3737
| `offset_y` | Y-axis offset | `F32` |
3838
| `visible` | Visible or not | `bool` |
39-
| `proportionv | Proportion | `F32` |
39+
| `proportion` | Proportion | `F32` |
4040
| `event_key` | Event identifier | `bool` |
4141

4242
### Events Table

docs/v0.1.0/en/examples/overview.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ All official examples are stored in [made_with_GenUI](https://github.com/Privoce
44

55
## Official Examples
66

7-
- [Source Code: Quickstart](https://github.com/Privoce/made_with_GenUI/tree/main/quickstart)
8-
- [Source Code: component ref(`c_ref!`) example](https://github.com/Privoce/made_with_GenUI/tree/main/c_ref_test)
9-
- [Source Code: for-syntax-sugar example](https://github.com/Privoce/made_with_GenUI/tree/main/for_test)
10-
- [Source Code: todo app example](https://github.com/Privoce/made_with_GenUI/tree/main/todo/todo_front)
7+
- [Source Code: Quickstart](https://github.com/Privoce/made_with_GenUI/tree/main/tests/quickstart)
8+
- [Source Code: component ref (`c_ref!`)](https://github.com/Privoce/made_with_GenUI/tree/main/tests/c_ref)
9+
- [Source Code: `for` sugar](https://github.com/Privoce/made_with_GenUI/tree/main/tests/for_sugar)
10+
- [Source Code: `if_else` sugar](https://github.com/Privoce/made_with_GenUI/tree/main/tests/if_sugar)
11+
- [Source Code: lifecycle](https://github.com/Privoce/made_with_GenUI/tree/main/tests/lifecycle)
12+
- [Sourec Code: router component](https://github.com/Privoce/made_with_GenUI/tree/main/tests/router)
13+
- [Source Code: router with tabbar](https://github.com/Privoce/made_with_GenUI/tree/main/tests/router_tabbar)
14+
- [Source Code: settings](https://github.com/Privoce/made_with_GenUI/blob/main/tests/settings/views/home.gen)
15+
- [Source Code: todo app (`network` plugin)](https://github.com/Privoce/made_with_GenUI/blob/main/tests/todo)
1116

1217
## Share Examples
1318

docs/v0.1.0/zh/api/global/component.mdx

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,112 @@ impl FatherView{
246246
}
247247
}
248248
</script>
249+
```
250+
251+
## `#[computed]`
252+
253+
计算属性(Computed Property)是基于现有属性动态计算出的值,在 GenUI 中,计算属性不会存储状态,而是根据其依赖项的变化自动更新。
254+
255+
| 特性 | 计算属性 (Computed) | 双向绑定 (Two-way Binding) |
256+
| ------------- | -------------------------- | ------------------------- |
257+
| 是否存储状态 | ❌ 否 | ✅ 是 |
258+
| 是否可变 | ✅ 依赖项变化时更新 | ✅ 是 |
259+
| 是否可手动修改 | ✅ 但不能使用 set_xxx 方法 | ✅ 通过 set_xxx 修改 |
260+
| 依赖数据 | ✅ 需要依赖项 | ✅ 组件状态 |
261+
262+
### 用法
263+
264+
1. `#[computed]`需要被声明在方法上
265+
2. `#[computed]`依赖于组件中声明的字段,可以是多个
266+
3. 计算属性的返回值需要和组件绑定一致
267+
4. 计算属性相当于将对值的处理交给开发者,所以在计算属性的方法中不可以使用`set_xxx()`对组件字段进行更改,取而代之的是更灵活的赋值更改
268+
269+
```rust
270+
impl Hello{
271+
#[computed([$(arg)*])]
272+
fn $computed_fn($[&self|&mut self]?) -> $return_value_type{
273+
// ...
274+
}
275+
}
276+
```
277+
278+
> - `$(arg)*`: 依赖参数列表, 表示可以有一个或多个
279+
> - `$computed_fn`: 计算属性处理函数
280+
> - `$[&self|&mut self]?`: 表示可以是`&self``&mut self`这由您来决定
281+
> - `$return_value_type`: 返回值类型
282+
283+
284+
### 示例
285+
286+
计算属性的写法为在属性绑定时使用方法进行声明:
287+
288+
```html
289+
<template>
290+
<component name="Hello" class="hello_view" spacing="12.0">
291+
<label :text="fmt_count()"></label>
292+
</component>
293+
</template>
294+
295+
<script>
296+
#[component]
297+
pub struct Hello{
298+
count: u32,
299+
}
300+
301+
impl Hello{
302+
#[computed([count])]
303+
fn fmt_count(&self) -> String{
304+
format!("count: {}", self.count)
305+
}
306+
}
307+
</script>
308+
```
309+
310+
## `nav_to!`
311+
312+
用于从路由管理的子页面中跳转至其他子页面中。
313+
314+
### 用法
315+
316+
```rust
317+
nav_to!($page_id);
318+
```
319+
320+
> - `$page_id`: 页面id
321+
322+
```toml title="router.toml"
323+
[bar_pages]
324+
login = { path = "crate::views::login::*", component = "Login" }
325+
```
326+
327+
`router.toml`中配置的login这个key就是页面id
328+
329+
### 示例
330+
331+
```rust
332+
impl Hello{
333+
pub fn to_register(&mut self) {
334+
nav_to!(register);
335+
}
336+
}
337+
```
338+
339+
## `nav_back!`
340+
341+
通过压栈的方式让路由返回上一个页面,如果有的话。
342+
343+
### 用法
344+
345+
```rust
346+
nav_back!();
347+
```
348+
349+
### 示例
350+
351+
```
352+
impl Hello{
353+
pub fn back(&mut self) {
354+
nav_back!();
355+
}
356+
}
249357
```

docs/v0.1.0/zh/api/option/lifetime.mdx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { Badge } from '@theme';
44

55
> [!WARNING]
66
>
7-
> 当前完整的生命周期还未全部实现,属于实验性功能,开放`#[before_mount]`, `#[mounted]`
7+
> 当前完整的生命周期还未全部实现,属于实验性功能
8+
>
9+
> 开放`#[before_mount]`, `#[mounted]`, `#[before_update]`, `#[updated]`
810
911
## `#[before_mount]`
1012

@@ -38,9 +40,33 @@ impl MyView{
3840

3941
## `#[before_update]`
4042

41-
在实例更新前调用
43+
在组件属性更新后,但还未重新渲染组件前被调用
44+
45+
### 写法
46+
47+
```rust
48+
impl Hello{
49+
#[before_update]
50+
fn do_before_update(&mut self) -> (){
51+
println!("do before update => hello.gen!");
52+
self.set_my_text(format!("{} => handle before", &self.my_text));
53+
}
54+
}
55+
```
4256

4357
## `#[updated]`
4458

45-
在实例更新后调用
59+
在组件属性更新后并已经重新对组件进行了渲染之后进行调用
60+
61+
### 写法
4662

63+
```rust
64+
impl Hello{
65+
#[updated]
66+
fn do_updated(&mut self){
67+
println!("do updated => hello.gen!");
68+
let new_val = self.get_count() + 1;
69+
self.set_count(new_val);
70+
}
71+
}
72+
```

docs/v0.1.0/zh/examples/overview.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
## 官方示例
66

7-
- [Quickstart源码](https://github.com/Privoce/made_with_GenUI/tree/main/quickstart)
8-
- [组件引用示例(`c_ref!`)源码](https://github.com/Privoce/made_with_GenUI/tree/main/c_ref_test)
9-
- [for循环语法糖示例源码](https://github.com/Privoce/made_with_GenUI/tree/main/for_test)
10-
- [待办事项示例源码](https://github.com/Privoce/made_with_GenUI/tree/main/todo/todo_front)
7+
- [Quickstart源码](https://github.com/Privoce/made_with_GenUI/tree/main/tests/quickstart)
8+
- [组件引用示例(`c_ref!`)源码](https://github.com/Privoce/made_with_GenUI/tree/main/tests/c_ref)
9+
- [for循环语法糖示例源码](https://github.com/Privoce/made_with_GenUI/tree/main/tests/for_sugar)
10+
- [if条件语法糖示例源码](https://github.com/Privoce/made_with_GenUI/tree/main/tests/if_sugar)
11+
- [lifecycle示例源码](https://github.com/Privoce/made_with_GenUI/tree/main/tests/lifecycle)
12+
- [路由组件示例源码](https://github.com/Privoce/made_with_GenUI/tree/main/tests/router)
13+
- [路由+Tabbar示例源码](https://github.com/Privoce/made_with_GenUI/tree/main/tests/router_tabbar)
14+
- [settings页面源码](https://github.com/Privoce/made_with_GenUI/blob/main/tests/settings/views/home.gen)
15+
- [待办事项示例源码](https://github.com/Privoce/made_with_GenUI/blob/main/tests/todo)
1116

1217
## 分享示例
1318

0 commit comments

Comments
 (0)