@hzab/list-render 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,34 +6,67 @@
6
6
 
7
7
  ```JSX
8
8
  // RemoteData
9
-
10
9
  import listSchema from "./list.schema.json";
11
10
 
12
11
  const schemaScope = {
13
- fetchMenuTree(field) {
14
- // field 目标组件的 配置参数
15
- console.log("fetchMenuTree field", field);
16
- // 通过 field.component[1] 可以动态配置目标组件的 props
17
- console.log("fetchMenuTree field.component[1]", field.component[1]);
18
- // 树选择器 数据源
19
- field.component[1].treeData = [{ value: 1, label: 1 }];
12
+ // 动态获取 select 数据
13
+ fetchSelectOptions(field, query) {
14
+ field.loading = true;
15
+ fetch(
16
+ `//some.domain/getSomething?test=${query.test}`
17
+ )
18
+ .then((response) => response.json())
19
+ .then(
20
+ ({ data }) => {
21
+ // field 目标组件的 配置参数
22
+ console.log("fetchSelectOptions field", field);
23
+ // 通过 select 可直接通过 field.dataSource 进行选项数据的配置
24
+ field.dataSource = data;
25
+ field.loading = false;
26
+ },
27
+ () => {
28
+ field.loading = false;
29
+ }
30
+ );
31
+ },
32
+ // 动态获取 treeSelect 数据
33
+ fetchMenuTree(field, query) {
34
+ field.loading = true;
35
+ fetch(
36
+ `//some.domain/getSomething?test=${query.test}`
37
+ )
38
+ .then((response) => response.json())
39
+ .then(
40
+ ({ data }) => {
41
+ // field 目标组件的 配置参数
42
+ console.log("fetchMenuTree field", field);
43
+ // 通过 field.component[1] 可以动态配置目标组件的 props
44
+ console.log("fetchMenuTree field.component[1]", field.component[1]);
45
+ // 树选择器 数据源,需要通过 field.component[1] 进行配置
46
+ field.component[1].treeData = data;
47
+ field.loading = false;
48
+ },
49
+ () => {
50
+ field.loading = false;
51
+ }
52
+ );
20
53
  },
21
54
  };
22
55
 
23
- function RemoteData () {
24
- const listDM = useMemo(()=>(new DataModule({
25
- getListApi: "/api/v1/user/list",
26
- getApi: "/api/v1/user/list/:id",
27
- createApi: "/api/v1/user/list",
28
- updateApi: "/api/v1/user/list/:id",
29
- deleteApi: "/api/v1/user/list/:id",
30
- })), []);
56
+ function RemoteData() {
57
+ const listDM = useMemo(
58
+ () =>
59
+ new DataModule({
60
+ getListApi: "/api/v1/user/list",
61
+ getApi: "/api/v1/user/list/:id",
62
+ createApi: "/api/v1/user/list",
63
+ updateApi: "/api/v1/user/list/:id",
64
+ deleteApi: "/api/v1/user/list/:id",
65
+ }),
66
+ []
67
+ );
31
68
  return (
32
- <ListRender
33
- model={listDM}
34
- schema={listSchema}
35
- schemaScope={schemaScope}
36
- />
69
+ <ListRender model={listDM} schema={listSchema} schemaScope={schemaScope} />
37
70
  );
38
71
  }
39
72
 
@@ -65,9 +98,26 @@ export default RemoteData;
65
98
  "name": "parentId",
66
99
  "x-designable-id": "i012z5nbd5z",
67
100
  "x-index": 0
101
+ },
102
+ "select": {
103
+ "title": "select",
104
+ "x-decorator": "FormItem",
105
+ "x-component": "Select",
106
+ "x-validator": [],
107
+ "x-component-props": {},
108
+ "x-decorator-props": {},
109
+ "name": "select",
110
+ "x-designable-id": "jvjf7fkpke9",
111
+ "x-index": 1,
112
+ "x-reactions": {
113
+ "dependencies": [],
114
+ "fulfill": {
115
+ "run": "$effect(() => {\n try {\n fetchSelectOptions && fetchSelectOptions($self);\n } catch (e) {}\n}, [])\n"
116
+ }
117
+ }
68
118
  }
69
119
  },
70
- "x-designable-id": "ivyw5onkhz8"
120
+ "x-designable-id": "ldzb3lu81dx"
71
121
  }
72
122
  }
73
123
 
package/docs/table.md ADDED
@@ -0,0 +1,86 @@
1
+ # 表格相关
2
+
3
+ ## query 相关
4
+
5
+ #### 设置过滤项
6
+
7
+ ```JSX
8
+ <ListRender
9
+ schema={schema}
10
+ model={listDM}
11
+ // 字段为 search 的 query
12
+ search="模糊搜索"
13
+ // 传入 schema 中所需表单项的 name
14
+ filters={["userId"]}
15
+ />
16
+ ```
17
+
18
+ #### 开启重置按钮
19
+
20
+ ```JSX
21
+ <ListRender
22
+ schema={schema}
23
+ model={listDM}
24
+ // 字段为 search 的 query
25
+ search="模糊搜索"
26
+ // 传入 schema 中所需表单项的 name
27
+ filters={["userId"]}
28
+ queryConf={{
29
+ hasReset: true
30
+ }}
31
+ />
32
+ ```
33
+
34
+ ## 顶部操作栏(新增按钮附近)
35
+
36
+ #### 新增按钮右侧添加其他组件
37
+
38
+ - 使用 Slots 实现
39
+
40
+ ```JSX
41
+ const Slots = {
42
+ headerActionSuffix() {
43
+ return (<div>headerActionPrefix</div>)
44
+ },
45
+ };
46
+
47
+ <ListRender schema={schema} model={listDM} Slots={Slots} />
48
+ ```
49
+
50
+ ## table 相关
51
+
52
+ #### 手动发起请求
53
+
54
+ ```JSX
55
+
56
+ // 手动发起请求
57
+ useEffect(() => {
58
+ // 重置 pageNum 为1,并发起请求
59
+ listRef.current?.onSearch();
60
+ // 请求当前页数据
61
+ listRef.current?.getList();
62
+ }, []);
63
+
64
+ <ListRender
65
+ ref={listRef}
66
+ schema={schema}
67
+ model={listDM}
68
+ // 关闭自动发起请求
69
+ closeAutoRequest={false}
70
+ />
71
+ ```
72
+
73
+ #### 编辑按钮左侧添加组件
74
+
75
+ ```JSX
76
+ const Slots = {
77
+ actionPrefixSlot(props) {
78
+ const { text, record, index, onEdit, onDel } = props;
79
+ return (<div>actionPrefixSlot</div>)
80
+ },
81
+ };
82
+
83
+ <ListRender schema={schema} model={listDM} Slots={Slots} />
84
+ ```
85
+
86
+ ## 新增、编辑表单相关
package/lib/data-model.js CHANGED
@@ -1 +1 @@
1
- import _ from"lodash";import axios from"axios";const _$Temp={};class DataModel{constructor(t){const{ctx:e,query:s,createApi:i,createMap:a,getApi:r,getMap:o,getListApi:n,getListMap:h,getListFunc:p,updateApi:l,updateMap:c,deleteApi:d,multipleDeleteApi:g,axios:u,axiosConf:m}=t;this.ctx=e||{},this.query=s||{},this.axios=u||_$Temp.axios||axios,this.axiosConf=m||{},this.createApi=i,this.createMap=a,this.getApi=r,this.getMap=o,this.getListApi=n,this.getListMap=h,this.getListFunc=p,this.updateApi=l,this.updateMap=c,this.deleteApi=d,this.multipleDeleteApi=g}getApiUrl(t,e,s=this.ctx){if(!t)throw new Error("Error api 不能为空",t,e,s);let i=t;const a=_.merge({},e,s);return _.each(a,((t,e)=>{_.isString(t)&&_.isNumber(t)&&!_.isBoolean(t)||(i=i.replace(`:${e}`,t))})),i}get(t={},e={}){let s=_.merge({},this.query,t);return s=_.pickBy(s,(t=>!_.isNil(t)&&""!==t)),new Promise(((t,i)=>{const a=this.getApiUrl(this.getApi,s,e);this.axios.get(a,{...this.axiosConf,params:s}).then((e=>{this.handleRes(e,(e=>{this.getMap&&(e=this.getMap(e)),t(e)}),i)})).catch((t=>this.errorHandler(t,i)))}))}async getList(t,e){let s=_.merge({},this.query,t);s=_.pickBy(s,(t=>!_.isNil(t)&&""!==t));let i=null;if(this.getListFunc)i=await this.getListFunc(s);else{const a=new Promise(((i,a)=>{const r=this.getApiUrl(this.getListApi,t,e);this.axios.get(r,{...this.axiosConf,params:s}).then((t=>{this.handleRes(t,(t=>{this.getListMap&&(t.list=t.list.map((t=>this.getListMap(t)))),i(t)}),a)})).catch((t=>this.errorHandler(t,a)))}));i=await a}return i}create(t,e){return new Promise(((s,i)=>{const a=this.getApiUrl(this.createApi,t,e),r={...this.axiosConf};t instanceof FormData&&(r.headers={"Content-Type":"multipart/form-data"}),this.axios.post(a,t,r).then((t=>{this.handleRes(t,s,i)})).catch((t=>this.errorHandler(t,i)))}))}update(t,e){return new Promise(((s,i)=>{const a=this.getApiUrl(this.updateApi,t,e),r={...this.axiosConf};t instanceof FormData&&(r.headers={"Content-Type":"multipart/form-data"}),this.axios.put(a,t,r).then((t=>{this.handleRes(t,s,i)})).catch((t=>this.errorHandler(t,i)))}))}delete(t,e){return new Promise(((s,i)=>{const a=this.getApiUrl(this.deleteApi,t,e);this.axios.delete(a,{...this.axiosConf,...t}).then((t=>{this.handleRes(t,s,i)})).catch((t=>this.errorHandler(t,i)))}))}multipleDelete(t,e){return new Promise(((s,i)=>{const a=this.getApiUrl(this.multipleDeleteApi,t,e);this.axios.delete(a,{...this.axiosConf,...t}).then((t=>{this.handleRes(t,s,i)})).catch((t=>this.errorHandler(t,i)))}))}handleRes(t,e,s){if("object"!=typeof t)return void s(new Error("response not object"));let i=t;i.data&&i.headers&&i.request&&"number"==typeof i.data?.code&&(i=i.data);const{code:a,message:r,data:o,msg:n}=i;if(200===a){let t=o??{};_.isObject(t)&&void 0===t.message&&(t._message=r||n),e(t)}else{const e=new Error(r||n);e.code=a,e.response=t,e._message=r||n,s(e)}}errorHandler(t,e){const s=t.response||t;if(s){const t=s.data&&(s.data.message||s.data.msg)||s.msg,i=new Error(t||s.statusText||"未知错误");return i.code=s.status,i.response=s,t&&(i._message=t),e(i)}return e(t)}}function setDefaultAxios(t){t&&(_$Temp.axios=t)}export{axios,DataModel,setDefaultAxios};export default DataModel;
1
+ export*from"@hzab/data-model";import DataModel from"@hzab/data-model";export default DataModel;