@hzab/list-render 1.10.12-beta → 1.10.12-beta3

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.
package/CHANGELOG.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # @hzab/list-render@1.10.12
2
2
 
3
+ fix: 首次列表加载携带queryFormInitialValues的参数
4
+ feat: schema title 支持传入 ReactNode,支持通过 schemaScope 传入
3
5
  fix: table 响应数据 去除 inTable == false 的字段
4
- feat: schema title 支持传入 ReactNode
5
- feat: schema clone 函数
6
+ fix: table 响应表单 FormilyField 环境变量 scenario: "tableReactionsForm"
7
+ fix: 行内编辑 清除上一次的数据,解决切换表单数据未清除问题
6
8
 
7
9
  # @hzab/list-render@1.10.11
8
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/list-render",
3
- "version": "1.10.12-beta",
3
+ "version": "1.10.12-beta3",
4
4
  "description": "",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -1,149 +1,174 @@
1
- import { observer } from "@formily/react";
2
- import { untracked, autorun, observable } from "@formily/reactive";
3
-
4
- export function handleTableProps(field, { scope, value, record }) {
5
- if (!field) {
6
- return field;
7
- }
8
- const opt = {
9
- scope: scope,
10
- $self: field,
11
- $values: value,
12
- $deps: record,
13
- };
14
- // inTable
15
- // x-table-props
16
- if (typeof field.inTable === "string") {
17
- field.inTable = getStateFunc(field.inTable, opt)(opt);
18
- }
19
- const tableProps = field["x-table-props"];
20
- if (tableProps) {
21
- if (typeof tableProps === "string") {
22
- field["x-table-props"] = getStateFunc(field.inTable, opt)(opt);
23
- } else if (tableProps && typeof tableProps === "object") {
24
- Object.keys(tableProps).forEach((key) => {
25
- if (typeof field["x-table-props"][key] === "string") {
26
- field["x-table-props"][key] = getStateFunc(field["x-table-props"][key], opt)(opt);
27
- }
28
- });
29
- }
30
- }
31
- return field;
32
- }
33
-
34
- /**
35
- * 解决 render 中无法响应数据的情况
36
- * @param {Function} render
37
- * @returns
38
- */
39
- export function getColRender(render) {
40
- return function (...args) {
41
- const Observer = observer(function () {
42
- return render(...args);
43
- });
44
- return <Observer />;
45
- };
46
- }
47
-
48
- /**
49
- * 根据 formily 函数字符串 {{xxx}},获取可执行函数
50
- * @param {string} funcStr
51
- * @param {Object} opt
52
- * @returns
53
- */
54
- export function getStateFunc(funcStr = "", opt) {
55
- const stateStr = funcStr.replace(/^\{\{/, "").replace(/\}\}$/, "");
56
- return getFunc(`return ${stateStr};`, opt);
57
- }
58
-
59
- /**
60
- * 根据函数字符串,获取可执行函数
61
- * @param {string} funcStr
62
- * @param {Object} opt
63
- * @returns
64
- */
65
- export function getFunc(
66
- funcStr = "",
67
- // opt = {"scope", "$self", "$values"},
68
- opt = {},
69
- ) {
70
- const _opt = {
71
- // opt = ["scope", "$self", "$deps", "$values", "$observable", "$memo", "$effect"],
72
- $observable: observable,
73
- $memo: autorun.memo,
74
- $effect: autorun.effect,
75
- scope: {},
76
- ...opt,
77
- };
78
- const fn = new Function(
79
- "opt",
80
- `const { ${Object.keys(_opt).join(", ")} } = opt || {}; const {${Object.keys(_opt.scope || {}).join(
81
- ", ",
82
- )}} = scope || {}; ${funcStr}`,
83
- );
84
-
85
- return function (opt) {
86
- return fn.call(this, {
87
- // opt = ["scope", "$self", "$deps", "$values", "$observable", "$memo", "$effect"],
88
- $observable: observable,
89
- $memo: autorun.memo,
90
- $effect: autorun.effect,
91
- ...opt,
92
- });
93
- };
94
- }
95
-
96
- /**
97
- * 根据 formily 函数字符串 {{xxx}},获取可执行的异步函数
98
- * @param {string} funcStr
99
- * @param {Object} opt
100
- * @returns
101
- */
102
- export function getStateSyncFunc(funcStr = "", opt) {
103
- const stateStr = funcStr.replace(/^\{\{/, "").replace(/\}\}$/, "");
104
- return getFuncSync(`return ${stateStr}`, opt);
105
- }
106
-
107
- /**
108
- * 根据函数字符串,获取可执行的异步函数
109
- * @param {string} funcStr
110
- * @param {Object} opt
111
- * @returns
112
- */
113
- export function getFuncSync(
114
- funcStr = "",
115
- // opt = {"scope", "$self", "$values"},
116
- opt = {},
117
- ) {
118
- const _opt = {
119
- // opt = ["scope", "$self", "$deps", "$values", "$observable", "$memo", "$effect"],
120
- $observable: observable,
121
- $memo: autorun.memo,
122
- $effect: autorun.effect,
123
- scope: {},
124
- ...opt,
125
- };
126
-
127
- // 把 effectFn 字符串转为可执行函数,并传入所需的参数,支持异步
128
- const getAsyncFunction = new Function(`return Object.getPrototypeOf(async function(){}).constructor;`);
129
- const AsyncFunction = getAsyncFunction();
130
- // 只能这样拿到 AsyncFunction
131
- // 直接写 Object.getPrototypeOf(async function(){}).constructor
132
- // 会被 babel 转成 Function
133
- const asyncFunc = new AsyncFunction(
134
- "opt",
135
- `try {const {${Object.keys(_opt).join(", ")}} = opt || {}; const {${Object.keys(_opt.scope || {}).join(
136
- ", ",
137
- )}} = scope || {}; ${funcStr}} catch(err) {console.error("Error Formily Reactive Function: ", err);}`,
138
- );
139
-
140
- return function (opt) {
141
- return asyncFunc.call(this, {
142
- // opt = ["scope", "$self", "$deps", "$values", "$observable", "$memo", "$effect"],
143
- $observable: observable,
144
- $memo: autorun.memo,
145
- $effect: autorun.effect,
146
- ...opt,
147
- });
148
- };
149
- }
1
+ import { observer } from "@formily/react";
2
+ import { autorun, observable } from "@formily/reactive";
3
+
4
+ export function handleTableProps(field, { scope, value, record }) {
5
+ if (!field) {
6
+ return field;
7
+ }
8
+ const opt = {
9
+ scope: scope,
10
+ $self: field,
11
+ $values: value,
12
+ $deps: record,
13
+ };
14
+ // inTable
15
+ // x-table-props
16
+ if (typeof field.inTable === "string") {
17
+ field.inTable = getStateFunc(field.inTable, opt)(opt);
18
+ }
19
+ const tableProps = field["x-table-props"];
20
+ if (tableProps) {
21
+ if (typeof tableProps === "string") {
22
+ field["x-table-props"] = getStateFunc(field.inTable, opt)(opt);
23
+ } else if (tableProps && typeof tableProps === "object") {
24
+ Object.keys(tableProps).forEach((key) => {
25
+ if (typeof field["x-table-props"][key] === "string") {
26
+ field["x-table-props"][key] = getStateFunc(field["x-table-props"][key], opt)(opt);
27
+ }
28
+ });
29
+ }
30
+ }
31
+ return field;
32
+ }
33
+
34
+ /**
35
+ * 解决 render 中无法响应数据的情况
36
+ * @param {Function} render
37
+ * @returns
38
+ */
39
+ export function getColRender(render) {
40
+ return function (...args) {
41
+ const Observer = observer(function () {
42
+ return render(...args);
43
+ });
44
+ return <Observer />;
45
+ };
46
+ }
47
+
48
+ /**
49
+ * 根据 formily 函数字符串 {{xxx}},获取变量或可执行函数
50
+ * @param {*} funcStr
51
+ * @param {*} opt
52
+ * @returns
53
+ */
54
+ export const getScopeVal = function (funcStr = "", opt = {}) {
55
+ if (typeof funcStr !== "string" || !funcStr.startsWith("{{")) {
56
+ return funcStr;
57
+ }
58
+ const stateStr = funcStr.replace(/^\{\{/, "").replace(/\}\}$/, "");
59
+ const res = opt?.scope?.[stateStr];
60
+ // 存在对应变量直接返回
61
+ if (res) {
62
+ return res;
63
+ }
64
+ return getFunc(stateStr, opt);
65
+ };
66
+
67
+ /**
68
+ * 根据 formily 函数字符串 {{xxx}},获取可执行函数
69
+ * @param {string} funcStr
70
+ * @param {Object} opt
71
+ * @returns
72
+ */
73
+ export function getStateFunc(funcStr = "", opt) {
74
+ if (typeof funcStr !== "string" || !funcStr.startsWith("{{")) {
75
+ return funcStr;
76
+ }
77
+ const stateStr = funcStr.replace(/^\{\{/, "").replace(/\}\}$/, "");
78
+ return getFunc(stateStr, opt);
79
+ }
80
+
81
+ /**
82
+ * 根据函数字符串,获取可执行函数
83
+ * @param {string} funcStr
84
+ * @param {Object} opt
85
+ * @returns
86
+ */
87
+ export function getFunc(
88
+ funcStr = "",
89
+ // opt = {"scope", "$self", "$values"},
90
+ opt = {},
91
+ ) {
92
+ const _opt = {
93
+ // opt = ["scope", "$self", "$deps", "$values", "$observable", "$memo", "$effect"],
94
+ $observable: observable,
95
+ $memo: autorun.memo,
96
+ $effect: autorun.effect,
97
+ scope: {},
98
+ ...opt,
99
+ };
100
+ const fn = new Function(
101
+ "opt",
102
+ `const { ${Object.keys(_opt).join(", ")} } = opt || {}; const {${Object.keys(_opt.scope || {}).join(
103
+ ", ",
104
+ )}} = scope || {}; return ${funcStr}; `,
105
+ );
106
+
107
+ return function (opt) {
108
+ return fn.call(this, {
109
+ // opt = ["scope", "$self", "$deps", "$values", "$observable", "$memo", "$effect"],
110
+ $observable: observable,
111
+ $memo: autorun.memo,
112
+ $effect: autorun.effect,
113
+ ...opt,
114
+ });
115
+ };
116
+ }
117
+
118
+ /**
119
+ * 根据 formily 函数字符串 {{xxx}},获取可执行的异步函数
120
+ * @param {string} funcStr
121
+ * @param {Object} opt
122
+ * @returns
123
+ */
124
+ export function getStateSyncFunc(funcStr = "", opt) {
125
+ if (typeof funcStr !== "string" || !funcStr.startsWith("{{")) {
126
+ return funcStr;
127
+ }
128
+ const stateStr = funcStr.replace(/^\{\{/, "").replace(/\}\}$/, "");
129
+ return getFuncSync(stateStr, opt);
130
+ }
131
+
132
+ /**
133
+ * 根据函数字符串,获取可执行的异步函数
134
+ * @param {string} funcStr
135
+ * @param {Object} opt
136
+ * @returns
137
+ */
138
+ export function getFuncSync(
139
+ funcStr = "",
140
+ // opt = {"scope", "$self", "$values"},
141
+ opt = {},
142
+ ) {
143
+ const _opt = {
144
+ // opt = ["scope", "$self", "$deps", "$values", "$observable", "$memo", "$effect"],
145
+ $observable: observable,
146
+ $memo: autorun.memo,
147
+ $effect: autorun.effect,
148
+ scope: {},
149
+ ...opt,
150
+ };
151
+
152
+ // 把 effectFn 字符串转为可执行函数,并传入所需的参数,支持异步
153
+ const getAsyncFunction = new Function(`return Object.getPrototypeOf(async function(){}).constructor;`);
154
+ const AsyncFunction = getAsyncFunction();
155
+ // 只能这样拿到 AsyncFunction
156
+ // 直接写 Object.getPrototypeOf(async function(){}).constructor
157
+ // 会被 babel 转成 Function
158
+ const asyncFunc = new AsyncFunction(
159
+ "opt",
160
+ `try {const {${Object.keys(_opt).join(", ")}} = opt || {}; const {${Object.keys(_opt.scope || {}).join(
161
+ ", ",
162
+ )}} = scope || {}; return ${funcStr}; } catch(err) {console.error("Error Formily Reactive Function: ", err);}`,
163
+ );
164
+
165
+ return function (opt) {
166
+ return asyncFunc.call(this, {
167
+ // opt = ["scope", "$self", "$deps", "$values", "$observable", "$memo", "$effect"],
168
+ $observable: observable,
169
+ $memo: autorun.memo,
170
+ $effect: autorun.effect,
171
+ ...opt,
172
+ });
173
+ };
174
+ }
@@ -1,3 +1,4 @@
1
+ import { autorun, observable } from "@formily/reactive";
1
2
  import { Schema } from "@formily/json-schema";
2
3
  import _ from "lodash";
3
4
  import dayjs from "dayjs";
@@ -5,6 +6,7 @@ import advancedFormat from "dayjs/plugin/advancedFormat";
5
6
  import weekOfYear from "dayjs/plugin/weekOfYear";
6
7
 
7
8
  import { cloneSchema } from "@hzab/utils/src/formily/cloneSchema";
9
+ import { getScopeVal } from "./formily-utils";
8
10
 
9
11
  dayjs.extend(advancedFormat);
10
12
  dayjs.extend(weekOfYear);
@@ -94,7 +96,7 @@ export function getDateVal(val, format) {
94
96
  }
95
97
 
96
98
  export function getFieldList(_schema, fieldList = [], opt = {}, isTableSortXIdex = false) {
97
- const schema = _schema?.schema || _schema;
99
+ const schema = cloneSchema(_schema?.schema || _schema);
98
100
 
99
101
  // 解决 schema 字符串可执行代码、变量等
100
102
  const properties = Schema.getOrderProperties(schema);
@@ -108,11 +110,33 @@ export function getFieldList(_schema, fieldList = [], opt = {}, isTableSortXIdex
108
110
 
109
111
  schema?.properties &&
110
112
  properties.forEach((item) => {
111
- const field = item.schema;
113
+ const field = { ...item.schema };
112
114
  if (!field.name) {
113
115
  field.name = item.key;
114
116
  }
115
117
  const componentName = field["x-component"];
118
+ // 处理 title 渲染逻辑,解析 scope 字符串变量
119
+ if (typeof field.title === "string" && field.title.startsWith("{{")) {
120
+ field.title = getScopeVal(field.title, { scope: opt.schemaScope });
121
+ if (typeof field.title === "function") {
122
+ field.title = field.title({
123
+ ...opt,
124
+ scope: opt.schemaScope,
125
+ scenario: "listRender",
126
+ // $deps,
127
+ $self: field,
128
+ $effect: autorun.effect,
129
+ // $form: $form,
130
+ // $values: $values,
131
+ $observable: observable,
132
+ $memo: autorun.memo,
133
+ $props: {
134
+ // formilyRef.current.fields formilyRef.current.fieldSchemas
135
+ formilyRef: opt.formilyRef,
136
+ },
137
+ });
138
+ }
139
+ }
116
140
  if (_boxList.includes(componentName)) {
117
141
  getFieldList(field, fieldList, opt);
118
142
  } else {
@@ -15,7 +15,7 @@ export interface Item {
15
15
 
16
16
  export interface EditableCellProps extends React.HTMLAttributes<HTMLElement> {
17
17
  SchemaField: any;
18
- field: {
18
+ getField: () => {
19
19
  type: string;
20
20
  name: string;
21
21
  };
@@ -38,7 +38,7 @@ export interface EditableCellProps extends React.HTMLAttributes<HTMLElement> {
38
38
 
39
39
  export const EditableCell: React.FC<EditableCellProps> = ({
40
40
  SchemaField,
41
- field,
41
+ getField,
42
42
  editing,
43
43
  dataIndex,
44
44
  title,
@@ -53,6 +53,7 @@ export const EditableCell: React.FC<EditableCellProps> = ({
53
53
  children,
54
54
  ...restProps
55
55
  }) => {
56
+ const field = getField && getField();
56
57
  const fPattern = field?.["x-pattern"];
57
58
  const fDisplay = field?.["x-display"];
58
59
  const { editMode = "modal" } = topProps || {};
@@ -80,7 +81,7 @@ export const EditableCell: React.FC<EditableCellProps> = ({
80
81
  schema={{
81
82
  type: "object",
82
83
  properties: {
83
- [field.name]: {
84
+ [field?.name]: {
84
85
  ...field,
85
86
  title: "",
86
87
  ["x-component-props"]: {
@@ -195,6 +196,9 @@ export const useEditTable = (props) => {
195
196
  const onEdit = async (record: Partial<Item> & { key: React.Key }, key, opt = { fieldRef: null }) => {
196
197
  const { fieldRef } = opt || {};
197
198
  editingFiledRef.current = fieldRef;
199
+ // 清除上一次的数据,解决切换表单数据未清除问题
200
+ // TODO: 拆分表单解决
201
+ formRender.values = {};
198
202
  formRender?.setValues(record);
199
203
  setEditingId(record[idKey]);
200
204
  setEditingKey(key);
@@ -222,7 +226,7 @@ export const useEditTable = (props) => {
222
226
  try {
223
227
  // 点击保存,数据抛出
224
228
  (await formRender?.validate()) as Item;
225
- onEditSubmit && (await onEditSubmit(formRender.values, { field } ));
229
+ onEditSubmit && (await onEditSubmit(formRender.values, { field }));
226
230
  // 清除编辑目标
227
231
  setEditingId("");
228
232
  setEditingKey("");
@@ -243,7 +247,8 @@ export const useEditTable = (props) => {
243
247
  ...(col.onCell ? col.onCell(record, ...args) : {}),
244
248
  record,
245
249
  SchemaField,
246
- field: col.field,
250
+ // 函数获取解决 cloneDepp 报错问题
251
+ getField: col.getField,
247
252
  dataIndex: col.dataIndex,
248
253
  title: col.title,
249
254
  editable: col.editable,
@@ -23,11 +23,12 @@ export const FormilyField = memo(function (props: formilyFieldPropsT) {
23
23
  const _schema = useMemo(() => {
24
24
  const schemaRmInTable = cloneSchema(schema);
25
25
  // 去除 inTable = false 的字段
26
- Object.keys(schemaRmInTable?.schema?.properties)?.forEach((key) => {
27
- if (schemaRmInTable?.schema?.properties?.[key]?.inTable === false) {
28
- delete schemaRmInTable?.schema?.properties?.[key];
29
- }
30
- });
26
+ schemaRmInTable?.properties &&
27
+ Object.keys(schemaRmInTable?.properties)?.forEach((key) => {
28
+ if (schemaRmInTable?.properties?.[key]?.inTable === false) {
29
+ delete schemaRmInTable?.properties?.[key];
30
+ }
31
+ });
31
32
  return schemaRmInTable;
32
33
  }, [schema]);
33
34
  const SchemaField = useCallback(
@@ -49,7 +50,7 @@ export const FormilyField = memo(function (props: formilyFieldPropsT) {
49
50
  return Com && <Com {...props} />;
50
51
  },
51
52
  },
52
- scope: { ...schemaScope },
53
+ scope: { ...schemaScope, scenario: "tableReactionsForm" },
53
54
  } as any),
54
55
  [],
55
56
  );
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import ListRender from "./list-render.jsx";
2
-
3
- export * from "@hzab/data-model";
4
-
5
- export default ListRender;
1
+ import ListRender from "./list-render.jsx";
2
+
3
+ export * from "@hzab/data-model";
4
+
5
+ export default ListRender;
@@ -130,7 +130,7 @@ const ListRender = forwardRef(function (props, parentRef) {
130
130
  }
131
131
  });
132
132
 
133
- !props.closeAutoRequest && getList({ ...(modelQueryRef.current || {}), ...getUrlQuery });
133
+ !props.closeAutoRequest && getList({ ...queryFormInitialValues, ...(modelQueryRef.current || {}), ...getUrlQuery });
134
134
  }, []);
135
135
 
136
136
  useEffect(() => {
@@ -512,7 +512,7 @@ const ListRender = forwardRef(function (props, parentRef) {
512
512
  setShowSearch={handleSetShowSearch}
513
513
  loading={listLoading}
514
514
  tableProps={props.tableProps}
515
- getFieldListOpt={props.getFieldListOpt}
515
+ getFieldListOpt={{ ...props.getFieldListOpt, schemaScope: props.schemaScope }}
516
516
  components={props.components}
517
517
  schemaScope={props.schemaScope}
518
518
  i18n={i18n}
@@ -102,7 +102,7 @@ const TableRender = forwardRef(function (props, tableRef) {
102
102
  if (!(props.schema && props.schema.properties)) {
103
103
  return;
104
104
  }
105
- const fieldList = getFieldList(props.schema, [], props.getFieldListOpt, isTableSortXIdex);
105
+ const fieldList = getFieldList(props.schema, [], { ...props.getFieldListOpt, formilyRef }, isTableSortXIdex);
106
106
  const columns = [];
107
107
 
108
108
  // 序号列
@@ -218,11 +218,13 @@ const TableRender = forwardRef(function (props, tableRef) {
218
218
  }
219
219
 
220
220
  columns.push({
221
- // field, // title 传入 ReactNode,内部深克隆导致页面报错白屏
221
+ // field, // HACK: 直接传入 field 在 title 传入 ReactNode,内部深克隆导致页面报错白屏。使用函数获取解决
222
+ getField: () => field,
222
223
  editable: true,
223
224
  ..._colConf,
224
225
  onCell: (record, rowIndex) =>
225
226
  _colConf?.onCell?.({ ...record, _field: { ...field, ...(fieldSchemas?.[name] || {}) } }, rowIndex) || {},
227
+ // 函数式传入,解决 title ReactNode 传入报错问题
226
228
  title: () => (isFunction(_colConf?.title) ? _colConf?.title() : _title),
227
229
  key: name,
228
230
  dataIndex: name,