@hzab/list-render 1.10.21-alpha.5 → 1.10.21-alpha.6

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.
@@ -1,82 +1,56 @@
1
- import { MinusCircleOutlined, MinusSquareOutlined, PlusCircleOutlined } from "@ant-design/icons";
2
- import { useEffect, useState, memo } from "react";
1
+ import { MinusCircleOutlined, PlusCircleOutlined } from "@ant-design/icons";
2
+ import { useEffect, useState } from "react";
3
3
  import "./index.less";
4
4
  export const ExpandListTemplate = (props) => {
5
+ const { cell } = props;
6
+ const { Slots, setExpandKeys, expandKeys, rowKey, setNewDataSource, record, columnId } = cell;
5
7
  const [expand, setExpand] = useState<boolean>(false);
6
- const { record, item } = props;
7
- const { Slots, setExpandKeys, expandKeys }: any = item;
8
8
  const onExpand = (isExpand) => {
9
- item?.setNewDataSource((pre) => {
9
+ setNewDataSource((pre) => {
10
10
  const tempPre = [...pre];
11
- const childrenIds = props?.record?.children?.map((el) => el?.groupId);
11
+ const childrenIds = record?.children?.map((el) => el?.[rowKey]);
12
12
  if (isExpand) {
13
- const findIndex = pre?.findIndex((el) => el?.groupId == props?.record?.groupId);
14
- const filterList = pre?.filter((el) => childrenIds?.includes(el?.groupId)) || [];
13
+ const findIndex = pre?.findIndex((el) => el?.[rowKey] == record?.[rowKey]);
14
+ const filterList = pre?.filter((el) => childrenIds?.includes(el?.[rowKey])) || [];
15
15
  if (filterList?.length == 0) {
16
- tempPre.splice(findIndex + 1, 0, ...(props?.record?.children || []));
16
+ tempPre.splice(findIndex + 1, 0, ...(record?.children || []));
17
17
  }
18
18
  return [...tempPre];
19
19
  }
20
20
 
21
- return tempPre?.filter((el) => !childrenIds?.includes(el?.groupId));
21
+ return pre?.filter((el) => !childrenIds?.includes(el?.[rowKey]));
22
22
  });
23
23
  };
24
24
  useEffect(() => {
25
- if (expandKeys?.includes(props?.record?.groupId)) {
25
+ if (expandKeys?.includes(record?.[rowKey])) {
26
26
  setExpand(true);
27
27
  onExpand(true);
28
- } else {
29
- setExpand(false);
30
- onExpand(false);
31
28
  }
29
+ }, [cell?.record]);
32
30
 
33
- // setExpand(props?.record?.expand);
34
- }, [props?.record]);
35
-
36
- const colRender = function (text, record) {
37
- const Slot = Slots[item?.columnId];
38
- const slotProps = {
39
- text,
40
- record,
41
- // field: { ...field, ...fieldSchemas?.[name] },
42
- // fieldSchema: fieldSchemas?.[name],
43
- };
44
- return <Slot {...slotProps} />;
31
+ const colRender = function (record) {
32
+ const Slot = Slots[columnId];
33
+ return <Slot record={record} />;
34
+ };
35
+ /** 展开关闭子项 */
36
+ const onClickExpand = () => {
37
+ setExpandKeys((preExpandKeys) => {
38
+ if (preExpandKeys?.includes(record?.[rowKey])) {
39
+ return preExpandKeys?.filter((el) => el != record?.[rowKey]);
40
+ }
41
+ return [...preExpandKeys, record?.[rowKey]];
42
+ });
43
+ setExpand(!expand);
44
+ onExpand(!expand);
45
45
  };
46
46
  return (
47
47
  <div className="expandList-template">
48
- {props?.record?.children && (
48
+ {record?.children && (
49
49
  <>
50
50
  {!expand ? (
51
- <PlusCircleOutlined
52
- className="expandList-template-icon"
53
- onClick={() => {
54
- props.record["expand"] = !expand;
55
- setExpandKeys((preExpandKeys) => {
56
- if (preExpandKeys?.includes(props?.record?.groupId)) {
57
- return preExpandKeys?.filter((el) => el != props?.record?.groupId);
58
- }
59
- return [...preExpandKeys, props?.record?.groupId];
60
- });
61
- setExpand(!expand);
62
- onExpand(!expand);
63
- }}
64
- />
51
+ <PlusCircleOutlined className="expandList-template-icon" onClick={onClickExpand} />
65
52
  ) : (
66
- <MinusCircleOutlined
67
- className="expandList-template-icon"
68
- onClick={() => {
69
- props.record["expand"] = !expand;
70
- setExpandKeys((preExpandKeys) => {
71
- if (preExpandKeys?.includes(props?.record?.groupId)) {
72
- return preExpandKeys?.filter((el) => el != props?.record?.groupId);
73
- }
74
- return [...preExpandKeys, props?.record?.groupId];
75
- });
76
- setExpand(!expand);
77
- onExpand(!expand);
78
- }}
79
- />
53
+ <MinusCircleOutlined className="expandList-template-icon" onClick={onClickExpand} />
80
54
  )}
81
55
  </>
82
56
  )}
@@ -85,7 +59,7 @@ export const ExpandListTemplate = (props) => {
85
59
  className={`expandList-template-name ${record?.spiltTag == 0 ? "expandList-template-indent" : ""}`}
86
60
  style={record?.spiltTag == 1 && !record?.children ? { marginLeft: "22px" } : {}}
87
61
  >
88
- {Slots?.[item?.columnId] ? colRender("", record) : record[item?.columnId]}
62
+ {Slots?.[columnId] ? colRender(record) : record[columnId]}
89
63
  </div>
90
64
  </div>
91
65
  );
@@ -0,0 +1,33 @@
1
+ import { useMemo } from "react";
2
+ import { getVal } from "../../../common/utils";
3
+
4
+ export const GetBaseTemplate = (props) => {
5
+ const { pattern, cell, componentProps, Com } = props;
6
+
7
+ const echoValue = useMemo(() => {
8
+ if (!cell?.getField) {
9
+ return;
10
+ }
11
+ return getVal(
12
+ { ...(cell?.getField && cell?.getField()), enum: cell?.getField()?.enum || cell?.values },
13
+ { [cell?.getField && cell?.getField().name]: cell.text },
14
+ );
15
+ }, [cell]);
16
+ return (
17
+ <div className="base-hoc">
18
+ {pattern == "readPretty" ? (
19
+ <div>{echoValue}</div>
20
+ ) : (
21
+ <Com
22
+ className="rg-input"
23
+ ref={(input) => {
24
+ if (input) {
25
+ input.focus();
26
+ }
27
+ }}
28
+ {...componentProps}
29
+ />
30
+ )}
31
+ </div>
32
+ );
33
+ };
@@ -26,6 +26,7 @@ export interface BaseCell extends Cell {
26
26
  isDisabled?: boolean;
27
27
  isOpen?: boolean;
28
28
  inputValue?: string;
29
+ commit?: boolean;
29
30
  }
30
31
  interface DIProps {
31
32
  onCellChanged: (...args: any[]) => void;
@@ -51,7 +52,13 @@ export const WidthWrap = (opt: any, edit = false) => {
51
52
  selectedValue = undefined;
52
53
  }
53
54
  }
55
+ let commit: boolean;
54
56
 
57
+ try {
58
+ commit = getCellProperty(uncertainCell, "commit", "boolean");
59
+ } catch {
60
+ commit = false;
61
+ }
55
62
  let values: any;
56
63
  try {
57
64
  values = getCellProperty(uncertainCell, "values", "object");
@@ -77,7 +84,7 @@ export const WidthWrap = (opt: any, edit = false) => {
77
84
 
78
85
  const text = selectedValue || "";
79
86
 
80
- return { ...uncertainCell, selectedValue, text, value, values, isDisabled, inputValue };
87
+ return { ...uncertainCell, selectedValue, text, value, values, isDisabled, inputValue, commit };
81
88
  }
82
89
 
83
90
  update(cell: Compatible<BaseCell>, cellToMerge: UncertainCompatible<BaseCell>): Compatible<BaseCell> {
@@ -89,6 +96,7 @@ export const WidthWrap = (opt: any, edit = false) => {
89
96
  selectedValue: selectedValueFromText,
90
97
  isOpen: cellToMerge?.isOpen,
91
98
  inputValue: cellToMerge.inputValue,
99
+ commit: cellToMerge?.commit,
92
100
  });
93
101
  }
94
102
 
@@ -223,18 +231,19 @@ export const WidthWrap = (opt: any, edit = false) => {
223
231
  input?.focus();
224
232
  }
225
233
  }}
226
- open={isOpen}
234
+ // open={isOpen}
227
235
  style={{ width: "100%", display: "flex", alignItems: "center", justifyContent: "center" }}
228
236
  value={cell[_value]}
229
237
  options={cell.values}
230
238
  onChange={(e) => {
231
- setIsOpen(false);
232
- if (cell?.type.includes("DatePicker")) {
233
- onCellChanged({ ...cell, inputValue: e?.target?.value || e });
234
- return;
235
- }
239
+ cell["commit"] = false;
236
240
  onCellChanged({ ...cell, selectedValue: e?.target?.value || e });
237
241
  }}
242
+ onBlur={(e) => {
243
+ cell["commit"] = true;
244
+ setIsOpen(false);
245
+ onCellChanged({ ...cell, selectedValue: cell?.text });
246
+ }}
238
247
  {...((cell?.getField && cell?.getField()["x-component-props"]) || {})}
239
248
  ></WrapComponent>
240
249
  </div>
@@ -0,0 +1,215 @@
1
+ import { useEffect, useMemo, useState } from "react";
2
+
3
+ import {
4
+ Cell,
5
+ CellTemplate,
6
+ Compatible,
7
+ getCellProperty,
8
+ getCharFromKey,
9
+ isAlphaNumericKey,
10
+ isNavigationKey,
11
+ keyCodes,
12
+ Uncertain,
13
+ UncertainCompatible,
14
+ } from "@silevis/reactgrid";
15
+ import { getVal } from "@hzab/list-render/src/common/utils";
16
+ import { InfoCircleOutlined } from "@ant-design/icons";
17
+ import { Popover } from "antd";
18
+
19
+ export type OptionType = {
20
+ label: string;
21
+ value: string | number;
22
+ isDisabled?: boolean;
23
+ };
24
+
25
+ export interface BaseCell extends Cell {
26
+ selectedValue?: string;
27
+ value: string | number;
28
+ values: OptionType[];
29
+ isDisabled?: boolean;
30
+ commit?: string;
31
+ }
32
+ interface DIProps {
33
+ onCellChanged: (...args: any[]) => void;
34
+ cell: Record<string, any>;
35
+ isInEditMode?: boolean;
36
+ }
37
+
38
+ export const WidthWrapHoc = (WrapComponent: any) => {
39
+ class createBaseCellTemplate implements CellTemplate<BaseCell> {
40
+ getCompatibleCell(uncertainCell: Uncertain<BaseCell>): Compatible<BaseCell> {
41
+ let selectedValue: string | undefined;
42
+ try {
43
+ selectedValue = getCellProperty(uncertainCell, "selectedValue", "number");
44
+ } catch {
45
+ selectedValue = undefined;
46
+ }
47
+ if (!selectedValue) {
48
+ try {
49
+ selectedValue = getCellProperty(uncertainCell, "selectedValue", "string");
50
+ } catch {
51
+ selectedValue = undefined;
52
+ }
53
+ }
54
+ let commit: string;
55
+
56
+ try {
57
+ commit = getCellProperty(uncertainCell, "commit", "string");
58
+ } catch {
59
+ commit = "submit";
60
+ }
61
+
62
+ let values: any;
63
+ try {
64
+ values = getCellProperty(uncertainCell, "values", "object");
65
+ } catch {
66
+ values = [];
67
+ }
68
+ const value = selectedValue ? parseFloat(selectedValue) : NaN;
69
+ let isDisabled = true;
70
+ try {
71
+ isDisabled = getCellProperty(uncertainCell, "isDisabled", "boolean");
72
+ } catch {
73
+ isDisabled = false;
74
+ }
75
+ const text = selectedValue || "";
76
+ return { ...uncertainCell, selectedValue, text, value, values, isDisabled, commit };
77
+ }
78
+
79
+ update(cell: Compatible<BaseCell>, cellToMerge: UncertainCompatible<BaseCell>): Compatible<BaseCell> {
80
+ const selectedValueFromText = cell.values.some((val: any) => val.value == cellToMerge.text)
81
+ ? cellToMerge.text
82
+ : cellToMerge?.text || undefined;
83
+
84
+ return this.getCompatibleCell({
85
+ ...cell,
86
+ selectedValue: selectedValueFromText,
87
+ commit: cellToMerge?.commit,
88
+ });
89
+ }
90
+
91
+ getClassName(cell: Compatible<BaseCell>, isInEditMode: boolean): string {
92
+ return "";
93
+ }
94
+ /** enableEditMode为true 时输入中文会报错 */
95
+ handleCompositionEnd(
96
+ cell: Compatible<BaseCell>,
97
+ eventData: any,
98
+ ): { cell: Compatible<BaseCell>; enableEditMode: boolean } {
99
+ return {
100
+ cell: {
101
+ ...cell,
102
+ },
103
+ enableEditMode: false,
104
+ };
105
+ }
106
+ /** render 不能抽出来组件,否则点击会失效 */
107
+ render(cell: any, isInEditMode: boolean, onCellChanged: (cell: any, commit: boolean) => void): React.ReactNode {
108
+ const [isOpen, setIsOpen] = useState<boolean>(false);
109
+ const datatypeId = cell?.columnId + cell?.rowId;
110
+ const [comValue, setComValue] = useState(cell?.text);
111
+ useEffect(() => {
112
+ // 处理下拉选项弹层失焦逻辑
113
+ function onClick() {
114
+ if ((window as any).gridEditId != datatypeId) {
115
+ setIsOpen(false);
116
+ }
117
+ }
118
+ document.addEventListener("click", onClick);
119
+ return () => {
120
+ document.removeEventListener("click", onClick);
121
+ };
122
+ }, []);
123
+ const echoValue = useMemo(() => {
124
+ if (!cell?.getField) {
125
+ return;
126
+ }
127
+ return getVal(
128
+ { ...cell?.getField(), enum: cell?.getField()?.enum || cell?.values },
129
+ { [cell?.getField().name]: cell.text },
130
+ );
131
+ }, [cell]);
132
+
133
+ const isValidator = useMemo(() => {
134
+ let flag = false;
135
+ let isVerify = false;
136
+ for (let it of cell?.validatorList || []) {
137
+ if (it?.pattern?.test(cell.text)) {
138
+ flag = true;
139
+ }
140
+ break;
141
+ }
142
+ isVerify = !flag && cell?.validatorList?.length > 0;
143
+
144
+ return isVerify;
145
+ }, [cell?.validatorList, cell.text]);
146
+
147
+ return (
148
+ <div
149
+ className="base-hoc"
150
+ onDoubleClick={(e) => {
151
+ if (cell?.nonEditable) {
152
+ return;
153
+ }
154
+ (window as any).preCell = cell;
155
+ setIsOpen(true);
156
+ }}
157
+ >
158
+ <WrapComponent
159
+ componentProps={{
160
+ style: { width: "100%", display: "flex", alignItems: "center" },
161
+ value: comValue,
162
+ options: cell.values,
163
+ onChange: (e) => {
164
+ cell["commit"] = "change";
165
+ const targetValue = e?.target?.value ?? e;
166
+ setComValue(targetValue);
167
+ onCellChanged(
168
+ this.getCompatibleCell({
169
+ ...cell,
170
+ text: targetValue,
171
+ selectedValue: targetValue,
172
+ }),
173
+ true,
174
+ );
175
+ },
176
+ onBlur: (e) => {
177
+ cell["commit"] = "submit";
178
+ if (isValidator) {
179
+ cell["commit"] = "verify";
180
+ }
181
+ const targetValue = cell?.text;
182
+ onCellChanged(
183
+ this.getCompatibleCell({
184
+ ...cell,
185
+ text: targetValue,
186
+ selectedValue: targetValue,
187
+ }),
188
+ true,
189
+ );
190
+ setIsOpen(false);
191
+ },
192
+
193
+ onPointerDown: (e) => {
194
+ e.stopPropagation();
195
+ },
196
+ onKeyDown: (e) => {
197
+ if (isAlphaNumericKey(e.keyCode) || isNavigationKey(e.keyCode)) e.stopPropagation();
198
+ },
199
+ ...((cell?.getField && cell?.getField()["x-component-props"]) || {}),
200
+ }}
201
+ pattern={isOpen ? "" : "readPretty"}
202
+ cell={cell}
203
+ ></WrapComponent>
204
+ {isValidator && (
205
+ <Popover content="该字段不合法">
206
+ <InfoCircleOutlined style={{ cursor: "pointer" }} />
207
+ </Popover>
208
+ )}
209
+ </div>
210
+ );
211
+ }
212
+ }
213
+
214
+ return new createBaseCellTemplate();
215
+ };
@@ -0,0 +1,14 @@
1
+ import { DatePicker, Input, TimePicker } from "antd";
2
+
3
+ export const childAntdComponent = {
4
+ "DatePicker.MonthPicker": DatePicker.MonthPicker,
5
+ "DatePicker.QuarterPicker": DatePicker.QuarterPicker,
6
+ "DatePicker.RangePicker": DatePicker.RangePicker,
7
+ "DatePicker.TimePicker": DatePicker.TimePicker,
8
+ "DatePicker.WeekPicker": DatePicker.WeekPicker,
9
+ "DatePicker.YearPicker": DatePicker.YearPicker,
10
+ "Input.Password": Input.Password,
11
+ "Input.Search": Input.Search,
12
+ "Input.TextArea": Input.TextArea,
13
+ "TimePicker.RangePicker": TimePicker.RangePicker,
14
+ }