@hzab/list-render 1.10.21-alpha.6 → 1.10.21-alpha.8
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/package.json +4 -4
- package/src/table-render/index.jsx +30 -31
- package/src/components/CellEditTable/Template/ExpandListTemplate/index.less +0 -18
- package/src/components/CellEditTable/Template/ExpandListTemplate/index.tsx +0 -66
- package/src/components/CellEditTable/Template/GetBaseTemplate.tsx +0 -33
- package/src/components/CellEditTable/Template/NumberCellTemplate.tsx +0 -168
- package/src/components/CellEditTable/Template/TextCellTemplate.tsx +0 -123
- package/src/components/CellEditTable/Template/utils.ts +0 -257
- package/src/components/CellEditTable/Template/widthWrap.tsx +0 -272
- package/src/components/CellEditTable/common/WidthWrapHoc.tsx +0 -215
- package/src/components/CellEditTable/common/childAntdComponent.ts +0 -14
- package/src/components/CellEditTable/common/utils.ts +0 -257
- package/src/components/CellEditTable/index.less +0 -28
- package/src/components/CellEditTable/index.tsx +0 -309
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hzab/list-render",
|
|
3
|
-
"version": "1.10.21-alpha.
|
|
3
|
+
"version": "1.10.21-alpha.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src",
|
|
6
6
|
"scripts": {
|
|
@@ -44,7 +44,9 @@
|
|
|
44
44
|
"react": "^17.0.2",
|
|
45
45
|
"react-dom": "^17.0.2",
|
|
46
46
|
"react-router-dom": "^6.8.1",
|
|
47
|
-
"typescript": "^4.9.4"
|
|
47
|
+
"typescript": "^4.9.4",
|
|
48
|
+
"@hzab/edit-table": "^0.0.1",
|
|
49
|
+
"@silevis/reactgrid": "^4.1.17"
|
|
48
50
|
},
|
|
49
51
|
"peerDependencies": {
|
|
50
52
|
"@formily/core": "2.3.1",
|
|
@@ -66,8 +68,6 @@
|
|
|
66
68
|
"lib": "lib"
|
|
67
69
|
},
|
|
68
70
|
"dependencies": {
|
|
69
|
-
"@hzab/edit-table": "^0.0.1",
|
|
70
|
-
"@silevis/reactgrid": "^4.1.17",
|
|
71
71
|
"array-move": "^4.0.0",
|
|
72
72
|
"react-sortable-hoc": "^2.0.0"
|
|
73
73
|
}
|
|
@@ -103,37 +103,37 @@ const TableRender = forwardRef(function (props, tableRef) {
|
|
|
103
103
|
const handleChildColumns = (childColumns) => {
|
|
104
104
|
const fieldSchemas = formilyRef.current?.fields;
|
|
105
105
|
const { Slots = {} } = props;
|
|
106
|
-
return childColumns?.map((
|
|
107
|
-
if (!(
|
|
106
|
+
return childColumns?.map((field, colIndex) => {
|
|
107
|
+
if (!(field.inTable !== false)) return
|
|
108
108
|
|
|
109
|
-
const { name, title } =
|
|
110
|
-
const comName =
|
|
109
|
+
const { name, title } = field;
|
|
110
|
+
const comName = field["x-component"];
|
|
111
111
|
let _colConf = {};
|
|
112
|
-
if (props.config?.colConf && props.config?.colConf[
|
|
113
|
-
_colConf = props.config?.colConf[
|
|
112
|
+
if (props.config?.colConf && props.config?.colConf[field?.name]) {
|
|
113
|
+
_colConf = props.config?.colConf[field?.name];
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
let colRender = undefined;
|
|
117
|
-
if (Slots && Slots[
|
|
117
|
+
if (Slots && Slots[field?.name]) {
|
|
118
118
|
colRender = function (text, record, index) {
|
|
119
|
-
const Slot = Slots[
|
|
119
|
+
const Slot = Slots[field?.name];
|
|
120
120
|
const slotProps = {
|
|
121
121
|
text,
|
|
122
122
|
record,
|
|
123
123
|
index,
|
|
124
|
-
field: { ...
|
|
125
|
-
fieldSchema: fieldSchemas?.[
|
|
124
|
+
field: { ...field, ...fieldSchemas?.[field?.name] },
|
|
125
|
+
fieldSchema: fieldSchemas?.[field?.name],
|
|
126
126
|
};
|
|
127
127
|
return <Slot {...slotProps} />;
|
|
128
128
|
};
|
|
129
129
|
} else {
|
|
130
130
|
colRender = function (text, record, index, ...args) {
|
|
131
131
|
const { width, ellipsis, emptyValue, showTags, showPrefixNode, showMode, enumRenderProps } = _colConf || {};
|
|
132
|
-
const schemaDefaultValue = fieldSchemas[
|
|
132
|
+
const schemaDefaultValue = fieldSchemas[field?.name]?.componentProps?.emptyValue;
|
|
133
133
|
const defaultValue = emptyValue ?? schemaDefaultValue ?? tableEmptyValue ?? "";
|
|
134
134
|
|
|
135
|
-
let val = getVal({ ...
|
|
136
|
-
fieldSchema: fieldSchemas?.[
|
|
135
|
+
let val = getVal({ ...field, ...fieldSchemas[field?.name] }, record, {
|
|
136
|
+
fieldSchema: fieldSchemas?.[field?.name],
|
|
137
137
|
});
|
|
138
138
|
|
|
139
139
|
if (val === "" || val === undefined || val === null) {
|
|
@@ -165,8 +165,8 @@ const TableRender = forwardRef(function (props, tableRef) {
|
|
|
165
165
|
return (
|
|
166
166
|
<EnumRender
|
|
167
167
|
showMode={_showMode}
|
|
168
|
-
value={_.get(record,
|
|
169
|
-
field={{ ...field, ...fieldSchemas?.[
|
|
168
|
+
value={_.get(record, field?.name)}
|
|
169
|
+
field={{ ...field, ...fieldSchemas?.[field?.name] }}
|
|
170
170
|
{...enumRenderProps}
|
|
171
171
|
/>
|
|
172
172
|
);
|
|
@@ -181,7 +181,7 @@ const TableRender = forwardRef(function (props, tableRef) {
|
|
|
181
181
|
if (_colConf?.title) {
|
|
182
182
|
_title = isFunction(_colConf?.title) ? _colConf?.title() : _colConf?.title;
|
|
183
183
|
}
|
|
184
|
-
const decoratorProps =
|
|
184
|
+
const decoratorProps = field["x-decorator-props"] || {};
|
|
185
185
|
if (decoratorProps.tooltip) {
|
|
186
186
|
_title = (
|
|
187
187
|
<span className="col-title-tooltip-wrap inline-block-max-w">
|
|
@@ -192,33 +192,31 @@ const TableRender = forwardRef(function (props, tableRef) {
|
|
|
192
192
|
</span>
|
|
193
193
|
);
|
|
194
194
|
}
|
|
195
|
-
const childSelectList = fieldSchemas?.[
|
|
195
|
+
const childSelectList = fieldSchemas?.[field?.name]?.dataSource?.map((el) => ({
|
|
196
196
|
...el,
|
|
197
|
-
label: el?.[
|
|
198
|
-
value: el?.[
|
|
197
|
+
label: el?.[field?.["x-component-props"]?.["fieldNames"]?.["label"]] || el?.label,
|
|
198
|
+
value: el?.[field?.["x-component-props"]?.["fieldNames"]?.["value"]] || el?.value,
|
|
199
199
|
}))
|
|
200
200
|
return {
|
|
201
|
-
getField: () =>
|
|
201
|
+
getField: () => field,
|
|
202
202
|
editable: true,
|
|
203
203
|
..._colConf,
|
|
204
204
|
onCell: (record, rowIndex, ci) =>
|
|
205
|
-
_colConf?.onCell?.({ ...record, _field: { ...
|
|
205
|
+
_colConf?.onCell?.({ ...record, _field: { ...field, ...(fieldSchemas?.[field?.name] || {}) } }, rowIndex, ci) || {},
|
|
206
206
|
// 函数式传入,解决 title ReactNode 传入报错问题(table 组件内部对 columns 进行 lodash.deepClone 导致 ReactNode 变成对象无法正常渲染) Uncaught TypeError: this.queryFeedbacks is not a function
|
|
207
207
|
title: () => _title,
|
|
208
|
-
key:
|
|
209
|
-
dataIndex:
|
|
210
|
-
type:
|
|
211
|
-
validatorList:
|
|
208
|
+
key: field?.name,
|
|
209
|
+
dataIndex: field?.name,
|
|
210
|
+
type: field["x-validator"] == "number" ? "Number" : comName,
|
|
211
|
+
validatorList: field["x-validator"] || [],
|
|
212
212
|
nonEditable: editMode != "cell",
|
|
213
|
-
values:
|
|
214
|
-
cellProps:
|
|
215
|
-
children: handleChildColumns(
|
|
213
|
+
values: field?.enum || childSelectList || [],
|
|
214
|
+
cellProps: field.cellProps,
|
|
215
|
+
children: handleChildColumns(field?.children),
|
|
216
216
|
render: getColRender(colRender),
|
|
217
217
|
}
|
|
218
218
|
|
|
219
|
-
})
|
|
220
|
-
|
|
221
|
-
|
|
219
|
+
})?.filter((el) => (el))
|
|
222
220
|
}
|
|
223
221
|
useEffect(() => {
|
|
224
222
|
if (!(props.schema && props.schema.properties)) {
|
|
@@ -444,6 +442,7 @@ const TableRender = forwardRef(function (props, tableRef) {
|
|
|
444
442
|
isCellEditTable ? <CellEditTable
|
|
445
443
|
{...tableProps}
|
|
446
444
|
{...cellEditTableProps}
|
|
445
|
+
{...props}
|
|
447
446
|
onEditSubmit={props?.onEditSubmit}
|
|
448
447
|
Slots={props?.Slots}
|
|
449
448
|
rowSelection={config?.rowSelection}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
.expandList-template {
|
|
2
|
-
display: flex;
|
|
3
|
-
align-items: center;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
.expandList-template-icon {
|
|
8
|
-
cursor: pointer;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
.expandList-template-name {
|
|
12
|
-
margin-left: 8px;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
.expandList-template-indent {
|
|
16
|
-
margin-left: 26px;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { MinusCircleOutlined, PlusCircleOutlined } from "@ant-design/icons";
|
|
2
|
-
import { useEffect, useState } from "react";
|
|
3
|
-
import "./index.less";
|
|
4
|
-
export const ExpandListTemplate = (props) => {
|
|
5
|
-
const { cell } = props;
|
|
6
|
-
const { Slots, setExpandKeys, expandKeys, rowKey, setNewDataSource, record, columnId } = cell;
|
|
7
|
-
const [expand, setExpand] = useState<boolean>(false);
|
|
8
|
-
const onExpand = (isExpand) => {
|
|
9
|
-
setNewDataSource((pre) => {
|
|
10
|
-
const tempPre = [...pre];
|
|
11
|
-
const childrenIds = record?.children?.map((el) => el?.[rowKey]);
|
|
12
|
-
if (isExpand) {
|
|
13
|
-
const findIndex = pre?.findIndex((el) => el?.[rowKey] == record?.[rowKey]);
|
|
14
|
-
const filterList = pre?.filter((el) => childrenIds?.includes(el?.[rowKey])) || [];
|
|
15
|
-
if (filterList?.length == 0) {
|
|
16
|
-
tempPre.splice(findIndex + 1, 0, ...(record?.children || []));
|
|
17
|
-
}
|
|
18
|
-
return [...tempPre];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return pre?.filter((el) => !childrenIds?.includes(el?.[rowKey]));
|
|
22
|
-
});
|
|
23
|
-
};
|
|
24
|
-
useEffect(() => {
|
|
25
|
-
if (expandKeys?.includes(record?.[rowKey])) {
|
|
26
|
-
setExpand(true);
|
|
27
|
-
onExpand(true);
|
|
28
|
-
}
|
|
29
|
-
}, [cell?.record]);
|
|
30
|
-
|
|
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
|
-
};
|
|
46
|
-
return (
|
|
47
|
-
<div className="expandList-template">
|
|
48
|
-
{record?.children && (
|
|
49
|
-
<>
|
|
50
|
-
{!expand ? (
|
|
51
|
-
<PlusCircleOutlined className="expandList-template-icon" onClick={onClickExpand} />
|
|
52
|
-
) : (
|
|
53
|
-
<MinusCircleOutlined className="expandList-template-icon" onClick={onClickExpand} />
|
|
54
|
-
)}
|
|
55
|
-
</>
|
|
56
|
-
)}
|
|
57
|
-
|
|
58
|
-
<div
|
|
59
|
-
className={`expandList-template-name ${record?.spiltTag == 0 ? "expandList-template-indent" : ""}`}
|
|
60
|
-
style={record?.spiltTag == 1 && !record?.children ? { marginLeft: "22px" } : {}}
|
|
61
|
-
>
|
|
62
|
-
{Slots?.[columnId] ? colRender(record) : record[columnId]}
|
|
63
|
-
</div>
|
|
64
|
-
</div>
|
|
65
|
-
);
|
|
66
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Cell,
|
|
3
|
-
CellTemplate,
|
|
4
|
-
Compatible,
|
|
5
|
-
getCellProperty,
|
|
6
|
-
getCharFromKey,
|
|
7
|
-
inNumericKey,
|
|
8
|
-
isAllowedOnNumberTypingKey,
|
|
9
|
-
isCharAllowedOnNumberInput,
|
|
10
|
-
isFunctionKey,
|
|
11
|
-
isNavigationKey,
|
|
12
|
-
isNumpadNumericKey,
|
|
13
|
-
keyCodes,
|
|
14
|
-
Uncertain,
|
|
15
|
-
UncertainCompatible,
|
|
16
|
-
} from "@silevis/reactgrid";
|
|
17
|
-
import * as React from "react";
|
|
18
|
-
import { parseLocaleNumber } from "./utils";
|
|
19
|
-
|
|
20
|
-
// NOTE: all modules imported below may be imported from '@silevis/reactgrid'
|
|
21
|
-
|
|
22
|
-
export interface NumberCell extends Cell {
|
|
23
|
-
type: "number";
|
|
24
|
-
value: number;
|
|
25
|
-
format?: Intl.NumberFormat;
|
|
26
|
-
validator?: (value: number) => boolean;
|
|
27
|
-
nanToZero?: boolean;
|
|
28
|
-
hideZero?: boolean;
|
|
29
|
-
errorMessage?: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class NumberCellTemplate implements CellTemplate<NumberCell> {
|
|
33
|
-
private wasEscKeyPressed = false;
|
|
34
|
-
|
|
35
|
-
getCompatibleCell(uncertainCell: Uncertain<NumberCell>): Compatible<NumberCell> {
|
|
36
|
-
let value: number;
|
|
37
|
-
try {
|
|
38
|
-
value = getCellProperty(uncertainCell, "value", "number");
|
|
39
|
-
} catch (error) {
|
|
40
|
-
value = NaN;
|
|
41
|
-
}
|
|
42
|
-
const numberFormat = uncertainCell.format || new Intl.NumberFormat(window.navigator.language);
|
|
43
|
-
const displayValue = uncertainCell.nanToZero && Number.isNaN(value) ? 0 : value;
|
|
44
|
-
const text =
|
|
45
|
-
Number.isNaN(displayValue) || (uncertainCell.hideZero && displayValue === 0)
|
|
46
|
-
? ""
|
|
47
|
-
: numberFormat.format(displayValue);
|
|
48
|
-
return { ...uncertainCell, value: displayValue, text };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
handleKeyDown(
|
|
52
|
-
cell: Compatible<NumberCell>,
|
|
53
|
-
keyCode: number,
|
|
54
|
-
ctrl: boolean,
|
|
55
|
-
shift: boolean,
|
|
56
|
-
alt: boolean,
|
|
57
|
-
key: string,
|
|
58
|
-
capsLock: boolean,
|
|
59
|
-
): { cell: Compatible<NumberCell>; enableEditMode: boolean } {
|
|
60
|
-
if (isNumpadNumericKey(keyCode)) keyCode -= 48;
|
|
61
|
-
if (isFunctionKey(keyCode)) {
|
|
62
|
-
if (keyCode === keyCodes.F2) return { cell, enableEditMode: true };
|
|
63
|
-
return { cell, enableEditMode: false };
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const char = getCharFromKey(key);
|
|
67
|
-
|
|
68
|
-
if (!ctrl && isCharAllowedOnNumberInput(char)) {
|
|
69
|
-
const value = Number(char);
|
|
70
|
-
|
|
71
|
-
if (Number.isNaN(value) && isCharAllowedOnNumberInput(char))
|
|
72
|
-
return { cell: { ...this.getCompatibleCell({ ...cell, value }), text: char }, enableEditMode: true };
|
|
73
|
-
return { cell: this.getCompatibleCell({ ...cell, value }), enableEditMode: true };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
cell,
|
|
78
|
-
enableEditMode: keyCode === keyCodes.POINTER || keyCode === keyCodes.ENTER,
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
update(cell: Compatible<NumberCell>, cellToMerge: UncertainCompatible<NumberCell>): Compatible<NumberCell> {
|
|
83
|
-
return this.getCompatibleCell({ ...cell, value: cellToMerge.value });
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
private getTextFromCharCode = (cellText: string): string => {
|
|
87
|
-
switch (cellText.charCodeAt(0)) {
|
|
88
|
-
case keyCodes.DASH:
|
|
89
|
-
case keyCodes.FIREFOX_DASH:
|
|
90
|
-
case keyCodes.SUBTRACT:
|
|
91
|
-
return "-";
|
|
92
|
-
case keyCodes.COMMA:
|
|
93
|
-
return ",";
|
|
94
|
-
case keyCodes.PERIOD:
|
|
95
|
-
case keyCodes.DECIMAL:
|
|
96
|
-
return ".";
|
|
97
|
-
default:
|
|
98
|
-
return cellText;
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
getClassName(cell: Compatible<NumberCell>, isInEditMode: boolean): string {
|
|
103
|
-
const isValid = cell.validator?.(cell.value) ?? true;
|
|
104
|
-
const className = cell.className || "";
|
|
105
|
-
return `${!isValid ? "rg-invalid" : ""} ${className}`;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
render(
|
|
109
|
-
cell: Compatible<NumberCell>,
|
|
110
|
-
isInEditMode: boolean,
|
|
111
|
-
onCellChanged: (cell: Compatible<NumberCell>, commit: boolean) => void,
|
|
112
|
-
): React.ReactNode {
|
|
113
|
-
if (!isInEditMode) {
|
|
114
|
-
const isValid = cell.validator?.(cell.value) ?? true;
|
|
115
|
-
const textToDisplay = !isValid && cell.errorMessage ? cell.errorMessage : cell.text;
|
|
116
|
-
return textToDisplay;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const locale = cell.format ? cell.format.resolvedOptions().locale : window.navigator.languages[0];
|
|
120
|
-
const format = new Intl.NumberFormat(locale, { useGrouping: false, maximumFractionDigits: 20 });
|
|
121
|
-
|
|
122
|
-
return (
|
|
123
|
-
<input
|
|
124
|
-
className="rg-input"
|
|
125
|
-
inputMode="decimal"
|
|
126
|
-
ref={(input) => {
|
|
127
|
-
if (input) {
|
|
128
|
-
input.focus();
|
|
129
|
-
input.setSelectionRange(input.value.length, input.value.length);
|
|
130
|
-
}
|
|
131
|
-
}}
|
|
132
|
-
defaultValue={Number.isNaN(cell.value) ? this.getTextFromCharCode(cell.text) : format.format(cell.value)}
|
|
133
|
-
onChange={(e) =>
|
|
134
|
-
onCellChanged(this.getCompatibleCell({ ...cell, value: parseLocaleNumber(e.currentTarget.value) }), false)
|
|
135
|
-
}
|
|
136
|
-
onBlur={(e) => {
|
|
137
|
-
onCellChanged(
|
|
138
|
-
this.getCompatibleCell({ ...cell, value: parseLocaleNumber(e.currentTarget.value) }),
|
|
139
|
-
!this.wasEscKeyPressed,
|
|
140
|
-
);
|
|
141
|
-
this.wasEscKeyPressed = false;
|
|
142
|
-
}}
|
|
143
|
-
onKeyDown={(e) => {
|
|
144
|
-
if (
|
|
145
|
-
inNumericKey(e.keyCode) ||
|
|
146
|
-
isNavigationKey(e.keyCode) ||
|
|
147
|
-
isAllowedOnNumberTypingKey(e.keyCode) ||
|
|
148
|
-
((e.ctrlKey || e.metaKey) && e.keyCode === keyCodes.KEY_A)
|
|
149
|
-
)
|
|
150
|
-
e.stopPropagation();
|
|
151
|
-
if (
|
|
152
|
-
!inNumericKey(e.keyCode) &&
|
|
153
|
-
!isNavigationKey(e.keyCode) &&
|
|
154
|
-
!isCharAllowedOnNumberInput(getCharFromKey(e.key))
|
|
155
|
-
)
|
|
156
|
-
e.preventDefault();
|
|
157
|
-
if (e.keyCode === keyCodes.ESCAPE) this.wasEscKeyPressed = true;
|
|
158
|
-
}}
|
|
159
|
-
onCopy={(e) => e.stopPropagation()}
|
|
160
|
-
onCut={(e) => e.stopPropagation()}
|
|
161
|
-
onPaste={(e) => e.stopPropagation()}
|
|
162
|
-
onPointerDown={(e) => e.stopPropagation()}
|
|
163
|
-
/>
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export const NumberTemplate = new NumberCellTemplate();
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Cell,
|
|
3
|
-
CellTemplate,
|
|
4
|
-
Compatible,
|
|
5
|
-
getCellProperty,
|
|
6
|
-
getCharFromKey,
|
|
7
|
-
isAlphaNumericKey,
|
|
8
|
-
isFunctionKey,
|
|
9
|
-
keyCodes,
|
|
10
|
-
Uncertain,
|
|
11
|
-
UncertainCompatible,
|
|
12
|
-
} from "@silevis/reactgrid";
|
|
13
|
-
import * as React from "react";
|
|
14
|
-
|
|
15
|
-
export interface TextCell extends Cell {
|
|
16
|
-
type: "Input";
|
|
17
|
-
text: string;
|
|
18
|
-
placeholder?: string;
|
|
19
|
-
validator?: (text: string) => boolean;
|
|
20
|
-
renderer?: (text: string) => React.ReactNode;
|
|
21
|
-
errorMessage?: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class TextCellTemplate implements CellTemplate<TextCell> {
|
|
25
|
-
private wasEscKeyPressed = false;
|
|
26
|
-
|
|
27
|
-
getCompatibleCell(uncertainCell: Uncertain<TextCell>): Compatible<TextCell> {
|
|
28
|
-
const text = getCellProperty(uncertainCell, "text", "string");
|
|
29
|
-
let placeholder: string | undefined;
|
|
30
|
-
try {
|
|
31
|
-
placeholder = getCellProperty(uncertainCell, "placeholder", "string");
|
|
32
|
-
} catch {
|
|
33
|
-
placeholder = "";
|
|
34
|
-
}
|
|
35
|
-
const value = parseFloat(text); // TODO more advanced parsing for all text based cells
|
|
36
|
-
return { ...uncertainCell, text, value, placeholder };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
update(cell: Compatible<TextCell>, cellToMerge: UncertainCompatible<TextCell>): Compatible<TextCell> {
|
|
40
|
-
return this.getCompatibleCell({ ...cell, text: cellToMerge.text, placeholder: cellToMerge.placeholder });
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
handleKeyDown(
|
|
44
|
-
cell: Compatible<TextCell>,
|
|
45
|
-
keyCode: number,
|
|
46
|
-
ctrl: boolean,
|
|
47
|
-
shift: boolean,
|
|
48
|
-
alt: boolean,
|
|
49
|
-
key: string,
|
|
50
|
-
capsLock: boolean,
|
|
51
|
-
): { cell: Compatible<TextCell>; enableEditMode: boolean } {
|
|
52
|
-
if (isFunctionKey(keyCode)) {
|
|
53
|
-
if (keyCode === keyCodes.F2) return { cell, enableEditMode: true };
|
|
54
|
-
return { cell, enableEditMode: false };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const char = getCharFromKey(key, shift, capsLock);
|
|
58
|
-
|
|
59
|
-
if (!ctrl && !alt && isAlphaNumericKey(keyCode) && !(shift && keyCode === keyCodes.SPACE))
|
|
60
|
-
return { cell: this.getCompatibleCell({ ...cell, text: char }), enableEditMode: true };
|
|
61
|
-
return {
|
|
62
|
-
cell,
|
|
63
|
-
enableEditMode: keyCode === keyCodes.POINTER || keyCode === keyCodes.ENTER,
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
handleCompositionEnd(
|
|
68
|
-
cell: Compatible<TextCell>,
|
|
69
|
-
eventData: any,
|
|
70
|
-
): { cell: Compatible<TextCell>; enableEditMode: boolean } {
|
|
71
|
-
return { cell: { ...cell, text: eventData }, enableEditMode: true };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
getClassName(cell: Compatible<TextCell>, isInEditMode: boolean): string {
|
|
75
|
-
const isValid = cell.validator ? cell.validator(cell.text) : true;
|
|
76
|
-
const className = cell.className ? cell.className : "";
|
|
77
|
-
return `${isValid ? "valid" : "rg-invalid"} ${
|
|
78
|
-
cell.placeholder && cell.text === "" ? "placeholder" : ""
|
|
79
|
-
} ${className}`;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
render(
|
|
83
|
-
cell: Compatible<TextCell>,
|
|
84
|
-
isInEditMode: boolean,
|
|
85
|
-
onCellChanged: (cell: Compatible<TextCell>, commit: boolean) => void,
|
|
86
|
-
): React.ReactNode {
|
|
87
|
-
if (!isInEditMode) {
|
|
88
|
-
const isValid = cell.validator ? cell.validator(cell.text) : true;
|
|
89
|
-
const cellText = cell.text || cell.placeholder || "";
|
|
90
|
-
const textToDisplay = !isValid && cell.errorMessage ? cell.errorMessage : cellText;
|
|
91
|
-
return cell.renderer ? cell.renderer(textToDisplay) : textToDisplay;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return (
|
|
95
|
-
<input
|
|
96
|
-
className="rg-input"
|
|
97
|
-
ref={(input) => {
|
|
98
|
-
if (input) {
|
|
99
|
-
input.focus();
|
|
100
|
-
// input.setSelectionRange(input.value.length, input.value.length);
|
|
101
|
-
}
|
|
102
|
-
}}
|
|
103
|
-
defaultValue={cell.text}
|
|
104
|
-
onChange={(e) => onCellChanged(this.getCompatibleCell({ ...cell, text: e.currentTarget.value }), false)}
|
|
105
|
-
onBlur={(e) => {
|
|
106
|
-
onCellChanged(this.getCompatibleCell({ ...cell, text: e.currentTarget.value }), !this.wasEscKeyPressed);
|
|
107
|
-
this.wasEscKeyPressed = false;
|
|
108
|
-
}}
|
|
109
|
-
onCopy={(e) => e.stopPropagation()}
|
|
110
|
-
onCut={(e) => e.stopPropagation()}
|
|
111
|
-
onPaste={(e) => e.stopPropagation()}
|
|
112
|
-
onPointerDown={(e) => e.stopPropagation()}
|
|
113
|
-
placeholder={cell.placeholder}
|
|
114
|
-
onKeyDown={(e) => {
|
|
115
|
-
// if (isAlphaNumericKey(e.keyCode) || isNavigationKey(e.keyCode)) e.stopPropagation();
|
|
116
|
-
// if (e.keyCode === keyCodes.ESCAPE) this.wasEscKeyPressed = true;
|
|
117
|
-
}}
|
|
118
|
-
/>
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export const TextTemplate = new TextCellTemplate();
|