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

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,9 @@
1
1
  # @hzab/list-render@1.10.12
2
2
 
3
+ feat: schema title 支持传入 ReactNode,支持通过 schemaScope 传入
3
4
  fix: table 响应数据 去除 inTable == false 的字段
4
- feat: schema title 支持传入 ReactNode
5
- feat: schema clone 函数
5
+ fix: table 响应表单 FormilyField 环境变量 scenario: "tableReactionsForm"
6
+ fix: 行内编辑 清除上一次的数据,解决切换表单数据未清除问题
6
7
 
7
8
  # @hzab/list-render@1.10.11
8
9
 
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-beta2",
4
4
  "description": "",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  import { observer } from "@formily/react";
2
- import { untracked, autorun, observable } from "@formily/reactive";
2
+ import { autorun, observable } from "@formily/reactive";
3
3
 
4
4
  export function handleTableProps(field, { scope, value, record }) {
5
5
  if (!field) {
@@ -45,6 +45,25 @@ export function getColRender(render) {
45
45
  };
46
46
  }
47
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
+
48
67
  /**
49
68
  * 根据 formily 函数字符串 {{xxx}},获取可执行函数
50
69
  * @param {string} funcStr
@@ -52,8 +71,11 @@ export function getColRender(render) {
52
71
  * @returns
53
72
  */
54
73
  export function getStateFunc(funcStr = "", opt) {
74
+ if (typeof funcStr !== "string" || !funcStr.startsWith("{{")) {
75
+ return funcStr;
76
+ }
55
77
  const stateStr = funcStr.replace(/^\{\{/, "").replace(/\}\}$/, "");
56
- return getFunc(`return ${stateStr};`, opt);
78
+ return getFunc(stateStr, opt);
57
79
  }
58
80
 
59
81
  /**
@@ -79,7 +101,7 @@ export function getFunc(
79
101
  "opt",
80
102
  `const { ${Object.keys(_opt).join(", ")} } = opt || {}; const {${Object.keys(_opt.scope || {}).join(
81
103
  ", ",
82
- )}} = scope || {}; ${funcStr}`,
104
+ )}} = scope || {}; return ${funcStr}; `,
83
105
  );
84
106
 
85
107
  return function (opt) {
@@ -100,8 +122,11 @@ export function getFunc(
100
122
  * @returns
101
123
  */
102
124
  export function getStateSyncFunc(funcStr = "", opt) {
125
+ if (typeof funcStr !== "string" || !funcStr.startsWith("{{")) {
126
+ return funcStr;
127
+ }
103
128
  const stateStr = funcStr.replace(/^\{\{/, "").replace(/\}\}$/, "");
104
- return getFuncSync(`return ${stateStr}`, opt);
129
+ return getFuncSync(stateStr, opt);
105
130
  }
106
131
 
107
132
  /**
@@ -134,7 +159,7 @@ export function getFuncSync(
134
159
  "opt",
135
160
  `try {const {${Object.keys(_opt).join(", ")}} = opt || {}; const {${Object.keys(_opt.scope || {}).join(
136
161
  ", ",
137
- )}} = scope || {}; ${funcStr}} catch(err) {console.error("Error Formily Reactive Function: ", err);}`,
162
+ )}} = scope || {}; return ${funcStr}; } catch(err) {console.error("Error Formily Reactive Function: ", err);}`,
138
163
  );
139
164
 
140
165
  return function (opt) {
@@ -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
  );
@@ -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,