Skip to content

Commit a6e768f

Browse files
owenchen1004JackLian
authored andcommitted
补充和更正docs中的functionsetter用法
1 parent 505a640 commit a6e768f

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: FunctionSetter
3+
---
4+
## 简介
5+
可以将function绑定在物料上
6+
7+
## 设置器返回
8+
9+
设置器返回一个Function对象,调用function()运行Function对象得到运行结果。
10+
11+
如下是一个典型的使用案例:
12+
13+
```javascript
14+
export type TestProps = React.ComponentProps<typeof Test> & {
15+
testFunction?: Function | undefined;
16+
};
17+
18+
const getTestData = () => {
19+
if(this.props.testFunction === undefined){
20+
return undefined;
21+
}else{
22+
return this.props.testFunction() // 返回testFunction()方法的运行结果;
23+
}
24+
}
25+
```
26+
27+
28+
## 参数设置
29+
30+
如果需要额外传参,需要将扩展参数设置打开,在代码面板中,编辑参数内容。
31+
32+
注意:
33+
34+
- 额外参数必须被包装成一个对象,如参数模板中所示
35+
- 可以使用动态变量例如 (this.items,this.state.xxx)
36+
```javascript
37+
{
38+
testKey: this.state.text,
39+
}
40+
```
41+
42+
- 该参数是额外参数,会加在原有参数后面,例如在 onClick 中加入扩展传参,最终函数消费的时候应该如下所示
43+
```javascript
44+
// e 为 onClick 原有函数传参,extParams 为自定义传参
45+
onClick(e, extParams) {
46+
this.setState({
47+
isShowDialog: extParams.isShowDialog,
48+
});
49+
}
50+
```
51+
52+
## 事件新建函数模板
53+
有时候我们创建的函数会有用到一些通用的函数模板,我们可以在物料协议的 meta.ts 中创建一个模板,如下
54+
55+
```TypeScript
56+
{
57+
name: 'onChange',
58+
title: {
59+
label: 'onChange',
60+
tip: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
61+
},
62+
propType: 'func',
63+
setter: [
64+
{
65+
componentName: 'FunctionSetter',
66+
props: {
67+
template: 'onTableChange(value,${extParams}){\n\n}',
68+
},
69+
},
70+
],
71+
}
72+
```
73+
74+
其中 ${extParams} 为扩展参数占位符,如果用户没有声明扩展参数,会移除对应的参数声明。

docs/docs/guide/appendix/setters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ sidebar_position: 4
1414
| DateYearSetter || 日期型 - 年数据设置器 | |
1515
| [EventSetter](./setterDetails/event) | function | 事件绑定设置器 | ![](https://img.alicdn.com/imgextra/i4/O1CN01qxIYiO1ksVknhTpnW_!!6000000004739-2-tps-1202-1014.png) |
1616
| [IconSetter](./setterDetails/icon) | string | 图标设置器 | ![](https://img.alicdn.com/imgextra/i3/O1CN01zsOMxo1TXaBmjHCRc_!!6000000002392-2-tps-1172-579.png) |
17-
| FunctionSetter | any | 函数型数据设置器 | ![](https://img.alicdn.com/imgextra/i4/O1CN01jLiJBo1ZIp7OmDLp0_!!6000000003172-2-tps-794-110.png) |
17+
| [FunctionSetter](./setterDetails/function) | function | 函数型数据设置器 | ![](https://img.alicdn.com/imgextra/i4/O1CN01jLiJBo1ZIp7OmDLp0_!!6000000003172-2-tps-794-110.png) |
1818
| JsonSetter | object | json 型数据设置器 | ![](https://img.alicdn.com/imgextra/i2/O1CN01mQTFjY1YiBQzWYj64_!!6000000003092-2-tps-1076-1068.png) |
1919
| [MixedSetter](./setterDetails/mixed) | any | 混合型数据设置器 | ![](https://img.alicdn.com/imgextra/i1/O1CN01ZxomFY1JW4j7wIGuQ_!!6000000001035-2-tps-1552-480.png) |
2020
| [NumberSetter](./setterDetails/number) | number | 数值型数据设置器 | ![](https://img.alicdn.com/imgextra/i3/O1CN01dSfSgg1WS2EpbqJIO_!!6000000002786-2-tps-1152-328.png) |

0 commit comments

Comments
 (0)