@hsu-react/ui 2.5.0 → 2.5.1
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/es/components/Form/DrawerForm/index.js +3 -2
- package/es/components/Form/ImportForm/index.js +3 -2
- package/es/components/Form/ModalForm/index.js +13 -4
- package/es/components/Form/_utils/formItemKey.d.ts +29 -0
- package/es/components/Form/_utils/formItemKey.js +38 -0
- package/es/components/Markdown/Views/index.js +9 -2
- package/lib/components/Form/DrawerForm/index.js +3 -2
- package/lib/components/Form/ImportForm/index.js +3 -2
- package/lib/components/Form/ModalForm/index.js +31 -22
- package/lib/components/Form/_utils/formItemKey.d.ts +29 -0
- package/lib/components/Form/_utils/formItemKey.js +44 -0
- package/lib/components/Markdown/Views/index.js +9 -2
- package/package.json +2 -1
- package/src/components/Form/DrawerForm/index.tsx +3 -2
- package/src/components/Form/ImportForm/index.tsx +3 -2
- package/src/components/Form/ModalForm/index.tsx +18 -6
- package/src/components/Form/_utils/formItemKey.ts +42 -0
- package/src/components/Markdown/Views/index.tsx +8 -1
|
@@ -20,6 +20,7 @@ import React, { useEffect } from "react";
|
|
|
20
20
|
import Icon from "../../Icon";
|
|
21
21
|
import classNames from "classnames";
|
|
22
22
|
import styles from "./index.module.scss";
|
|
23
|
+
import { formItemKeys } from "../_utils/formItemKey";
|
|
23
24
|
import useLabelWidth from "../../../hooks/useLabelWidth";
|
|
24
25
|
import usePermissions from "../../../hooks/usePermissions";
|
|
25
26
|
import { mergeSemantic } from "../../../utils/semantic";
|
|
@@ -87,13 +88,13 @@ var DrawerForm = function DrawerForm(props) {
|
|
|
87
88
|
children: [/*#__PURE__*/_jsxs(Form, {
|
|
88
89
|
form: form,
|
|
89
90
|
className: styles.form,
|
|
90
|
-
children: [formItems === null || formItems === void 0 ? void 0 : formItems.map(function (item) {
|
|
91
|
+
children: [formItems === null || formItems === void 0 ? void 0 : formItems.map(function (item, idx) {
|
|
91
92
|
var _item$name;
|
|
92
93
|
return /*#__PURE__*/_jsx(FormItem, _objectSpread({
|
|
93
94
|
requiredMsg: (_item$name = item.name) !== null && _item$name !== void 0 && _item$name.endsWith("En") ? "".concat(PlaceholderDictEn[item.type], " ").concat(item.name) : "".concat(PlaceholderDict[item.type]).concat(item.label),
|
|
94
95
|
labelWidth: item.layout === "horizontal" ? labelWidth : undefined,
|
|
95
96
|
className: classNames(styles.formItem, item.className)
|
|
96
|
-
}, item),
|
|
97
|
+
}, item), formItemKeys(formItems)[idx]);
|
|
97
98
|
}), extraFormItems === null || extraFormItems === void 0 ? void 0 : extraFormItems.map(function (item) {
|
|
98
99
|
item = _objectSpread(_objectSpread({}, item), {}, {
|
|
99
100
|
key: item.props.name,
|
|
@@ -22,6 +22,7 @@ import { downloadFile } from "hsu-utils";
|
|
|
22
22
|
import { get } from "../../../request";
|
|
23
23
|
import { observer } from "mobx-react-lite";
|
|
24
24
|
import styles from "./index.module.scss";
|
|
25
|
+
import { formItemKeys } from "../_utils/formItemKey";
|
|
25
26
|
import useLabelWidth from "../../../hooks/useLabelWidth";
|
|
26
27
|
import usePermissions from "../../../hooks/usePermissions";
|
|
27
28
|
import Modal from "../../Modal";
|
|
@@ -101,13 +102,13 @@ var ImportForm = observer(function (props) {
|
|
|
101
102
|
}, modalConfig), {}, {
|
|
102
103
|
children: /*#__PURE__*/_jsx(Form, {
|
|
103
104
|
className: "".concat(styles.form, " ").concat(formClassName !== null && formClassName !== void 0 ? formClassName : ""),
|
|
104
|
-
children: formItems === null || formItems === void 0 ? void 0 : formItems.map(function (item) {
|
|
105
|
+
children: formItems === null || formItems === void 0 ? void 0 : formItems.map(function (item, idx) {
|
|
105
106
|
var _item$requiredMsg, _item$name;
|
|
106
107
|
return /*#__PURE__*/_jsx(FormItem, _objectSpread({
|
|
107
108
|
requiredMsg: (_item$requiredMsg = item.requiredMsg) !== null && _item$requiredMsg !== void 0 ? _item$requiredMsg : (_item$name = item.name) !== null && _item$name !== void 0 && _item$name.endsWith("En") ? "".concat(PlaceholderDictEn[item.type], " ").concat(item.name) : "".concat(PlaceholderDict[item.type]).concat(item.label),
|
|
108
109
|
labelWidth: labelWidth,
|
|
109
110
|
className: "".concat(formItemClassName, " ").concat(item.className)
|
|
110
|
-
}, item),
|
|
111
|
+
}, item), formItemKeys(formItems)[idx]);
|
|
111
112
|
})
|
|
112
113
|
})
|
|
113
114
|
}));
|
|
@@ -19,6 +19,7 @@ import Modal from "../../Modal";
|
|
|
19
19
|
import FormItem, { PlaceholderDict, PlaceholderDictEn } from "../../FormItem";
|
|
20
20
|
import React, { useEffect, useRef } from "react";
|
|
21
21
|
import styles from "./index.module.scss";
|
|
22
|
+
import { formItemKeys } from "../_utils/formItemKey";
|
|
22
23
|
import usePermissions from "../../../hooks/usePermissions";
|
|
23
24
|
import { useFormItems } from "./_hooks/useFormItems";
|
|
24
25
|
import { mergeSemantic } from "../../../utils/semantic";
|
|
@@ -86,6 +87,10 @@ var ModalForm = function ModalForm(props) {
|
|
|
86
87
|
};
|
|
87
88
|
var adaptiveColumnNum = useAdaptiveColumnNum(formContainer, columnNum, true, 1, undefined, 1200, !!open);
|
|
88
89
|
var _formItems = useFormItems(formItems, layout, adaptiveColumnNum);
|
|
90
|
+
/* `extraFormItems` 是 JSX 形态的表单项,同样可能重名(互斥字段) */
|
|
91
|
+
var extraKeys = formItemKeys(extraFormItems === null || extraFormItems === void 0 ? void 0 : extraFormItems.map(function (i) {
|
|
92
|
+
return i.props;
|
|
93
|
+
}));
|
|
89
94
|
if (!permitted) {
|
|
90
95
|
return null;
|
|
91
96
|
}
|
|
@@ -132,6 +137,8 @@ var ModalForm = function ModalForm(props) {
|
|
|
132
137
|
onValuesChange: onValuesChange,
|
|
133
138
|
children: [(_Object$keys = Object.keys(_formItems !== null && _formItems !== void 0 ? _formItems : {})) === null || _Object$keys === void 0 ? void 0 : _Object$keys.map(function (key) {
|
|
134
139
|
var _formItems$key;
|
|
140
|
+
/* key 一次算完(重名的第二条起加 `#n`),别在 map 体里每项重算 */
|
|
141
|
+
var itemKeys = formItemKeys(_formItems === null || _formItems === void 0 ? void 0 : _formItems[key]);
|
|
135
142
|
return /*#__PURE__*/_jsxs("div", {
|
|
136
143
|
className: "".concat(styles.formItemGroup, " ").concat(formItemGroupClassName !== null && formItemGroupClassName !== void 0 ? formItemGroupClassName : ""),
|
|
137
144
|
id: key,
|
|
@@ -143,7 +150,7 @@ var ModalForm = function ModalForm(props) {
|
|
|
143
150
|
style: {
|
|
144
151
|
paddingLeft: !key ? 0 : undefined
|
|
145
152
|
},
|
|
146
|
-
children: _formItems === null || _formItems === void 0 || (_formItems$key = _formItems[key]) === null || _formItems$key === void 0 ? void 0 : _formItems$key.map(function (item) {
|
|
153
|
+
children: _formItems === null || _formItems === void 0 || (_formItems$key = _formItems[key]) === null || _formItems$key === void 0 ? void 0 : _formItems$key.map(function (item, idx) {
|
|
147
154
|
var _item$requiredMsg, _item$name;
|
|
148
155
|
return /*#__PURE__*/_jsx(FormItem, _objectSpread({
|
|
149
156
|
requiredMsg: (_item$requiredMsg = item.requiredMsg) !== null && _item$requiredMsg !== void 0 ? _item$requiredMsg : (_item$name = item.name) !== null && _item$name !== void 0 && _item$name.endsWith("En") ? "".concat(PlaceholderDictEn[item.type], " ").concat(item.name) : "".concat(PlaceholderDict[item.type]).concat(item.label),
|
|
@@ -151,14 +158,16 @@ var ModalForm = function ModalForm(props) {
|
|
|
151
158
|
disabled: disabled,
|
|
152
159
|
required: disabled ? false : item.required,
|
|
153
160
|
layout: formItemLayout
|
|
154
|
-
}, item),
|
|
161
|
+
}, item), itemKeys[idx]);
|
|
155
162
|
})
|
|
156
163
|
})]
|
|
157
164
|
}, key);
|
|
158
|
-
}), extraFormItems === null || extraFormItems === void 0 ? void 0 : extraFormItems.map(function (item) {
|
|
165
|
+
}), extraFormItems === null || extraFormItems === void 0 ? void 0 : extraFormItems.map(function (item, idx) {
|
|
159
166
|
var _item$props$requiredM, _item$props$name;
|
|
160
167
|
item = _objectSpread(_objectSpread({}, item), {}, {
|
|
161
|
-
key
|
|
168
|
+
/* 与上面那组同一套 key 规则:重名的第二条起加 `#n`,
|
|
169
|
+
按声明顺序编号(见 formItemKeys 的说明) */
|
|
170
|
+
key: extraKeys[idx],
|
|
162
171
|
props: _objectSpread({
|
|
163
172
|
requiredMsg: (_item$props$requiredM = item.props.requiredMsg) !== null && _item$props$requiredM !== void 0 ? _item$props$requiredM : item.props.type && ((_item$props$name = item.props.name) !== null && _item$props$name !== void 0 && _item$props$name.endsWith("En") ? "".concat(PlaceholderDictEn[item.props.type], " ").concat(item.props.name) : "".concat(PlaceholderDict[item.props.type]).concat(item.props.label)),
|
|
164
173
|
className: "".concat(formItemClassName, " ").concat(item.props.className, " ").concat(styles.formItem),
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { FormItemProps } from "../../FormItem";
|
|
2
|
+
/**
|
|
3
|
+
* 表单项的 React key。
|
|
4
|
+
*
|
|
5
|
+
* 原来直接用 `item.name`,而**一个表单有两个互斥、同名的字段是合法的** ——
|
|
6
|
+
* 消费方真实写法:Bearer Token 用 `PASSWORD`、自定义请求头用 `TEXTAREA`,
|
|
7
|
+
* 两条都叫 `authValue`,靠 `visible` 互斥。`FormItemProps` 是按 `type` 判别的
|
|
8
|
+
* 联合类型,把 `type` 写成三元表达式会让 `componentProps` 退化成所有形态的
|
|
9
|
+
* 交集、编译期就通不过,所以拆成两条是**故意的**,不是写错。
|
|
10
|
+
*
|
|
11
|
+
* 于是控制台常年三条
|
|
12
|
+
* `Encountered two children with the same key, 'authValue'`。
|
|
13
|
+
* 目前行为无害(两条永远只渲染一个,另一个 return null),但 React 明说
|
|
14
|
+
* 重复 key 的行为不受支持、将来可能改。
|
|
15
|
+
*
|
|
16
|
+
* 修法是重名的第二条起加后缀,而不是改成按下标 —— 按下标会让
|
|
17
|
+
* 「条件性地从数组里删掉某一项」的表单在删除时整片重挂。
|
|
18
|
+
*
|
|
19
|
+
* ⚠️ **按声明数组编号,不能按「可见的那部分」编号。**互斥字段两条都在数组里,
|
|
20
|
+
* 只是其中一条渲染成 null。若先 `filter(visible)` 再编号,切换的瞬间剩下那条的
|
|
21
|
+
* key 会从 `authValue#1` 变成 `authValue` —— **key 一变就是重挂**,用户正在输入的
|
|
22
|
+
* 内容和焦点当场丢,比原来那条警告严重得多。数组是静态写死的,
|
|
23
|
+
* 按声明顺序编号则天然稳定,切 `visible` 不动 key。
|
|
24
|
+
*
|
|
25
|
+
* @param items 声明顺序的表单项数组(**不要预先过滤**)
|
|
26
|
+
* @returns 与入参等长、下标一一对应的 key 数组
|
|
27
|
+
*/
|
|
28
|
+
export declare const formItemKeys: (items?: FormItemProps[]) => string[];
|
|
29
|
+
export default formItemKeys;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 表单项的 React key。
|
|
3
|
+
*
|
|
4
|
+
* 原来直接用 `item.name`,而**一个表单有两个互斥、同名的字段是合法的** ——
|
|
5
|
+
* 消费方真实写法:Bearer Token 用 `PASSWORD`、自定义请求头用 `TEXTAREA`,
|
|
6
|
+
* 两条都叫 `authValue`,靠 `visible` 互斥。`FormItemProps` 是按 `type` 判别的
|
|
7
|
+
* 联合类型,把 `type` 写成三元表达式会让 `componentProps` 退化成所有形态的
|
|
8
|
+
* 交集、编译期就通不过,所以拆成两条是**故意的**,不是写错。
|
|
9
|
+
*
|
|
10
|
+
* 于是控制台常年三条
|
|
11
|
+
* `Encountered two children with the same key, 'authValue'`。
|
|
12
|
+
* 目前行为无害(两条永远只渲染一个,另一个 return null),但 React 明说
|
|
13
|
+
* 重复 key 的行为不受支持、将来可能改。
|
|
14
|
+
*
|
|
15
|
+
* 修法是重名的第二条起加后缀,而不是改成按下标 —— 按下标会让
|
|
16
|
+
* 「条件性地从数组里删掉某一项」的表单在删除时整片重挂。
|
|
17
|
+
*
|
|
18
|
+
* ⚠️ **按声明数组编号,不能按「可见的那部分」编号。**互斥字段两条都在数组里,
|
|
19
|
+
* 只是其中一条渲染成 null。若先 `filter(visible)` 再编号,切换的瞬间剩下那条的
|
|
20
|
+
* key 会从 `authValue#1` 变成 `authValue` —— **key 一变就是重挂**,用户正在输入的
|
|
21
|
+
* 内容和焦点当场丢,比原来那条警告严重得多。数组是静态写死的,
|
|
22
|
+
* 按声明顺序编号则天然稳定,切 `visible` 不动 key。
|
|
23
|
+
*
|
|
24
|
+
* @param items 声明顺序的表单项数组(**不要预先过滤**)
|
|
25
|
+
* @returns 与入参等长、下标一一对应的 key 数组
|
|
26
|
+
*/
|
|
27
|
+
export var formItemKeys = function formItemKeys(items) {
|
|
28
|
+
var used = {};
|
|
29
|
+
return (items !== null && items !== void 0 ? items : []).map(function (item, idx) {
|
|
30
|
+
var _used$name;
|
|
31
|
+
// name 缺省时退回下标:没有名字的项本来就互不相同,用下标不会撞
|
|
32
|
+
var name = (item === null || item === void 0 ? void 0 : item.name) == null ? "#".concat(idx) : String(item.name);
|
|
33
|
+
var seen = (_used$name = used[name]) !== null && _used$name !== void 0 ? _used$name : 0;
|
|
34
|
+
used[name] = seen + 1;
|
|
35
|
+
return seen === 0 ? name : "".concat(name, "#").concat(seen);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
export default formItemKeys;
|
|
@@ -12,6 +12,7 @@ import React, { createContext, useContext, useId } from "react";
|
|
|
12
12
|
import ReactMarkdown from "react-markdown";
|
|
13
13
|
import rehypeHighlight from "rehype-highlight";
|
|
14
14
|
import rehypeKatex from "rehype-katex";
|
|
15
|
+
import remarkCjkFriendly from "remark-cjk-friendly";
|
|
15
16
|
import remarkGfm from "remark-gfm";
|
|
16
17
|
import remarkMath from "remark-math";
|
|
17
18
|
import classNames from "classnames";
|
|
@@ -134,8 +135,14 @@ var MarkdownViews = function MarkdownViews(props) {
|
|
|
134
135
|
_jsx(CopyPropsContext.Provider, {
|
|
135
136
|
value: copyProps,
|
|
136
137
|
children: /*#__PURE__*/_jsx(ReactMarkdown, _objectSpread(_objectSpread({}, props), {}, {
|
|
137
|
-
rehypePlugins: [rehypeHighlight, rehypeKatex]
|
|
138
|
-
|
|
138
|
+
rehypePlugins: [rehypeHighlight, rehypeKatex]
|
|
139
|
+
/* `remarkCjkFriendly` 修的是中文里最常见的一处 markdown 失效:
|
|
140
|
+
`我得先知道**「这件事」是什么**` —— 开头那对 `**` 后面跟着 `「`
|
|
141
|
+
(标点)、前面是 `道`(非空白非标点),按 CommonMark 的 flanking 规则
|
|
142
|
+
它不是左侧定界符,于是整段**原样显示两个星号**。
|
|
143
|
+
而中文里「**「…」**」「**《…》**」这种写法极常见,模型输出里到处都是。
|
|
144
|
+
这个插件按 CJK 友好规则重判 flanking,是这条规则的通行修法。 */,
|
|
145
|
+
remarkPlugins: [remarkCjkFriendly, remarkGfm, remarkMath],
|
|
139
146
|
className: classNames(styles.MarkdownViews, className, "markdown-body"),
|
|
140
147
|
components: _objectSpread({
|
|
141
148
|
code: CodeBlock,
|
|
@@ -11,6 +11,7 @@ var _react = _interopRequireWildcard(require("react"));
|
|
|
11
11
|
var _Icon = _interopRequireDefault(require("../../Icon"));
|
|
12
12
|
var _classnames = _interopRequireDefault(require("classnames"));
|
|
13
13
|
var _indexModule = _interopRequireDefault(require("./index.module.scss"));
|
|
14
|
+
var _formItemKey = require("../_utils/formItemKey");
|
|
14
15
|
var _useLabelWidth = _interopRequireDefault(require("../../../hooks/useLabelWidth"));
|
|
15
16
|
var _usePermissions = _interopRequireDefault(require("../../../hooks/usePermissions"));
|
|
16
17
|
var _semantic = require("../../../utils/semantic");
|
|
@@ -77,12 +78,12 @@ const DrawerForm = props => {
|
|
|
77
78
|
children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_antd.Form, {
|
|
78
79
|
form: form,
|
|
79
80
|
className: _indexModule.default.form,
|
|
80
|
-
children: [formItems?.map(item => /*#__PURE__*/(0, _jsxRuntime.jsx)(_FormItem.default, {
|
|
81
|
+
children: [formItems?.map((item, idx) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_FormItem.default, {
|
|
81
82
|
requiredMsg: item.name?.endsWith("En") ? `${_FormItem.PlaceholderDictEn[item.type]} ${item.name}` : `${_FormItem.PlaceholderDict[item.type]}${item.label}`,
|
|
82
83
|
labelWidth: item.layout === "horizontal" ? labelWidth : undefined,
|
|
83
84
|
className: (0, _classnames.default)(_indexModule.default.formItem, item.className),
|
|
84
85
|
...item
|
|
85
|
-
},
|
|
86
|
+
}, (0, _formItemKey.formItemKeys)(formItems)[idx])), extraFormItems?.map(item => {
|
|
86
87
|
item = {
|
|
87
88
|
...item,
|
|
88
89
|
key: item.props.name,
|
|
@@ -13,6 +13,7 @@ var _hsuUtils = require("hsu-utils");
|
|
|
13
13
|
var _request = require("../../../request");
|
|
14
14
|
var _mobxReactLite = require("mobx-react-lite");
|
|
15
15
|
var _indexModule = _interopRequireDefault(require("./index.module.scss"));
|
|
16
|
+
var _formItemKey = require("../_utils/formItemKey");
|
|
16
17
|
var _useLabelWidth = _interopRequireDefault(require("../../../hooks/useLabelWidth"));
|
|
17
18
|
var _usePermissions = _interopRequireDefault(require("../../../hooks/usePermissions"));
|
|
18
19
|
var _Modal = _interopRequireDefault(require("../../Modal"));
|
|
@@ -93,12 +94,12 @@ const ImportForm = (0, _mobxReactLite.observer)(props => {
|
|
|
93
94
|
...modalConfig,
|
|
94
95
|
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_antd.Form, {
|
|
95
96
|
className: `${_indexModule.default.form} ${formClassName ?? ""}`,
|
|
96
|
-
children: formItems?.map(item => /*#__PURE__*/(0, _jsxRuntime.jsx)(_FormItem.default, {
|
|
97
|
+
children: formItems?.map((item, idx) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_FormItem.default, {
|
|
97
98
|
requiredMsg: item.requiredMsg ?? (item.name?.endsWith("En") ? `${_FormItem.PlaceholderDictEn[item.type]} ${item.name}` : `${_FormItem.PlaceholderDict[item.type]}${item.label}`),
|
|
98
99
|
labelWidth: labelWidth,
|
|
99
100
|
className: `${formItemClassName} ${item.className}`,
|
|
100
101
|
...item
|
|
101
|
-
},
|
|
102
|
+
}, (0, _formItemKey.formItemKeys)(formItems)[idx]))
|
|
102
103
|
})
|
|
103
104
|
});
|
|
104
105
|
});
|
|
@@ -10,6 +10,7 @@ var _Modal = _interopRequireDefault(require("../../Modal"));
|
|
|
10
10
|
var _FormItem = _interopRequireWildcard(require("../../FormItem"));
|
|
11
11
|
var _react = _interopRequireWildcard(require("react"));
|
|
12
12
|
var _indexModule = _interopRequireDefault(require("./index.module.scss"));
|
|
13
|
+
var _formItemKey = require("../_utils/formItemKey");
|
|
13
14
|
var _usePermissions = _interopRequireDefault(require("../../../hooks/usePermissions"));
|
|
14
15
|
var _useFormItems = require("./_hooks/useFormItems");
|
|
15
16
|
var _semantic = require("../../../utils/semantic");
|
|
@@ -73,6 +74,8 @@ const ModalForm = props => {
|
|
|
73
74
|
};
|
|
74
75
|
const adaptiveColumnNum = (0, _useAdaptiveColumnNum.useAdaptiveColumnNum)(formContainer, columnNum, true, 1, undefined, 1200, !!open);
|
|
75
76
|
const _formItems = (0, _useFormItems.useFormItems)(formItems, layout, adaptiveColumnNum);
|
|
77
|
+
/* `extraFormItems` 是 JSX 形态的表单项,同样可能重名(互斥字段) */
|
|
78
|
+
const extraKeys = (0, _formItemKey.formItemKeys)(extraFormItems?.map(i => i.props));
|
|
76
79
|
if (!permitted) {
|
|
77
80
|
return null;
|
|
78
81
|
}
|
|
@@ -107,30 +110,36 @@ const ModalForm = props => {
|
|
|
107
110
|
"--column-num": adaptiveColumnNum
|
|
108
111
|
},
|
|
109
112
|
onValuesChange: onValuesChange,
|
|
110
|
-
children: [Object.keys(_formItems ?? {})?.map(key =>
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
className: `${_indexModule.default.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
113
|
+
children: [Object.keys(_formItems ?? {})?.map(key => {
|
|
114
|
+
/* key 一次算完(重名的第二条起加 `#n`),别在 map 体里每项重算 */
|
|
115
|
+
const itemKeys = (0, _formItemKey.formItemKeys)(_formItems?.[key]);
|
|
116
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
|
|
117
|
+
className: `${_indexModule.default.formItemGroup} ${formItemGroupClassName ?? ""}`,
|
|
118
|
+
id: key,
|
|
119
|
+
children: [key && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
|
|
120
|
+
className: `${_indexModule.default.formItemTitle} ${formItemGroupTitleClassName ?? ""}`,
|
|
121
|
+
children: key
|
|
122
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
|
|
123
|
+
className: _indexModule.default.formItemGroupContent,
|
|
124
|
+
style: {
|
|
125
|
+
paddingLeft: !key ? 0 : undefined
|
|
126
|
+
},
|
|
127
|
+
children: _formItems?.[key]?.map((item, idx) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_FormItem.default, {
|
|
128
|
+
requiredMsg: item.requiredMsg ?? (item.name?.endsWith("En") ? `${_FormItem.PlaceholderDictEn[item.type]} ${item.name}` : `${_FormItem.PlaceholderDict[item.type]}${item.label}`),
|
|
129
|
+
className: `${formItemClassName} ${item.className} ${_indexModule.default.formItem}`,
|
|
130
|
+
disabled: disabled,
|
|
131
|
+
required: disabled ? false : item.required,
|
|
132
|
+
layout: formItemLayout,
|
|
133
|
+
...item
|
|
134
|
+
}, itemKeys[idx]))
|
|
135
|
+
})]
|
|
136
|
+
}, key);
|
|
137
|
+
}), extraFormItems?.map((item, idx) => {
|
|
131
138
|
item = {
|
|
132
139
|
...item,
|
|
133
|
-
key
|
|
140
|
+
/* 与上面那组同一套 key 规则:重名的第二条起加 `#n`,
|
|
141
|
+
按声明顺序编号(见 formItemKeys 的说明) */
|
|
142
|
+
key: extraKeys[idx],
|
|
134
143
|
props: {
|
|
135
144
|
requiredMsg: item.props.requiredMsg ?? (item.props.type && (item.props.name?.endsWith("En") ? `${_FormItem.PlaceholderDictEn[item.props.type]} ${item.props.name}` : `${_FormItem.PlaceholderDict[item.props.type]}${item.props.label}`)),
|
|
136
145
|
className: `${formItemClassName} ${item.props.className} ${_indexModule.default.formItem}`,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { FormItemProps } from "../../FormItem";
|
|
2
|
+
/**
|
|
3
|
+
* 表单项的 React key。
|
|
4
|
+
*
|
|
5
|
+
* 原来直接用 `item.name`,而**一个表单有两个互斥、同名的字段是合法的** ——
|
|
6
|
+
* 消费方真实写法:Bearer Token 用 `PASSWORD`、自定义请求头用 `TEXTAREA`,
|
|
7
|
+
* 两条都叫 `authValue`,靠 `visible` 互斥。`FormItemProps` 是按 `type` 判别的
|
|
8
|
+
* 联合类型,把 `type` 写成三元表达式会让 `componentProps` 退化成所有形态的
|
|
9
|
+
* 交集、编译期就通不过,所以拆成两条是**故意的**,不是写错。
|
|
10
|
+
*
|
|
11
|
+
* 于是控制台常年三条
|
|
12
|
+
* `Encountered two children with the same key, 'authValue'`。
|
|
13
|
+
* 目前行为无害(两条永远只渲染一个,另一个 return null),但 React 明说
|
|
14
|
+
* 重复 key 的行为不受支持、将来可能改。
|
|
15
|
+
*
|
|
16
|
+
* 修法是重名的第二条起加后缀,而不是改成按下标 —— 按下标会让
|
|
17
|
+
* 「条件性地从数组里删掉某一项」的表单在删除时整片重挂。
|
|
18
|
+
*
|
|
19
|
+
* ⚠️ **按声明数组编号,不能按「可见的那部分」编号。**互斥字段两条都在数组里,
|
|
20
|
+
* 只是其中一条渲染成 null。若先 `filter(visible)` 再编号,切换的瞬间剩下那条的
|
|
21
|
+
* key 会从 `authValue#1` 变成 `authValue` —— **key 一变就是重挂**,用户正在输入的
|
|
22
|
+
* 内容和焦点当场丢,比原来那条警告严重得多。数组是静态写死的,
|
|
23
|
+
* 按声明顺序编号则天然稳定,切 `visible` 不动 key。
|
|
24
|
+
*
|
|
25
|
+
* @param items 声明顺序的表单项数组(**不要预先过滤**)
|
|
26
|
+
* @returns 与入参等长、下标一一对应的 key 数组
|
|
27
|
+
*/
|
|
28
|
+
export declare const formItemKeys: (items?: FormItemProps[]) => string[];
|
|
29
|
+
export default formItemKeys;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.formItemKeys = exports.default = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* 表单项的 React key。
|
|
9
|
+
*
|
|
10
|
+
* 原来直接用 `item.name`,而**一个表单有两个互斥、同名的字段是合法的** ——
|
|
11
|
+
* 消费方真实写法:Bearer Token 用 `PASSWORD`、自定义请求头用 `TEXTAREA`,
|
|
12
|
+
* 两条都叫 `authValue`,靠 `visible` 互斥。`FormItemProps` 是按 `type` 判别的
|
|
13
|
+
* 联合类型,把 `type` 写成三元表达式会让 `componentProps` 退化成所有形态的
|
|
14
|
+
* 交集、编译期就通不过,所以拆成两条是**故意的**,不是写错。
|
|
15
|
+
*
|
|
16
|
+
* 于是控制台常年三条
|
|
17
|
+
* `Encountered two children with the same key, 'authValue'`。
|
|
18
|
+
* 目前行为无害(两条永远只渲染一个,另一个 return null),但 React 明说
|
|
19
|
+
* 重复 key 的行为不受支持、将来可能改。
|
|
20
|
+
*
|
|
21
|
+
* 修法是重名的第二条起加后缀,而不是改成按下标 —— 按下标会让
|
|
22
|
+
* 「条件性地从数组里删掉某一项」的表单在删除时整片重挂。
|
|
23
|
+
*
|
|
24
|
+
* ⚠️ **按声明数组编号,不能按「可见的那部分」编号。**互斥字段两条都在数组里,
|
|
25
|
+
* 只是其中一条渲染成 null。若先 `filter(visible)` 再编号,切换的瞬间剩下那条的
|
|
26
|
+
* key 会从 `authValue#1` 变成 `authValue` —— **key 一变就是重挂**,用户正在输入的
|
|
27
|
+
* 内容和焦点当场丢,比原来那条警告严重得多。数组是静态写死的,
|
|
28
|
+
* 按声明顺序编号则天然稳定,切 `visible` 不动 key。
|
|
29
|
+
*
|
|
30
|
+
* @param items 声明顺序的表单项数组(**不要预先过滤**)
|
|
31
|
+
* @returns 与入参等长、下标一一对应的 key 数组
|
|
32
|
+
*/
|
|
33
|
+
const formItemKeys = items => {
|
|
34
|
+
const used = {};
|
|
35
|
+
return (items ?? []).map((item, idx) => {
|
|
36
|
+
// name 缺省时退回下标:没有名字的项本来就互不相同,用下标不会撞
|
|
37
|
+
const name = item?.name == null ? `#${idx}` : String(item.name);
|
|
38
|
+
const seen = used[name] ?? 0;
|
|
39
|
+
used[name] = seen + 1;
|
|
40
|
+
return seen === 0 ? name : `${name}#${seen}`;
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
exports.formItemKeys = formItemKeys;
|
|
44
|
+
var _default = exports.default = formItemKeys;
|
|
@@ -8,6 +8,7 @@ var _react = _interopRequireWildcard(require("react"));
|
|
|
8
8
|
var _reactMarkdown = _interopRequireDefault(require("react-markdown"));
|
|
9
9
|
var _rehypeHighlight = _interopRequireDefault(require("rehype-highlight"));
|
|
10
10
|
var _rehypeKatex = _interopRequireDefault(require("rehype-katex"));
|
|
11
|
+
var _remarkCjkFriendly = _interopRequireDefault(require("remark-cjk-friendly"));
|
|
11
12
|
var _remarkGfm = _interopRequireDefault(require("remark-gfm"));
|
|
12
13
|
var _remarkMath = _interopRequireDefault(require("remark-math"));
|
|
13
14
|
var _classnames = _interopRequireDefault(require("classnames"));
|
|
@@ -137,8 +138,14 @@ const MarkdownViews = props => {
|
|
|
137
138
|
value: copyProps,
|
|
138
139
|
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactMarkdown.default, {
|
|
139
140
|
...props,
|
|
140
|
-
rehypePlugins: [_rehypeHighlight.default, _rehypeKatex.default]
|
|
141
|
-
|
|
141
|
+
rehypePlugins: [_rehypeHighlight.default, _rehypeKatex.default]
|
|
142
|
+
/* `remarkCjkFriendly` 修的是中文里最常见的一处 markdown 失效:
|
|
143
|
+
`我得先知道**「这件事」是什么**` —— 开头那对 `**` 后面跟着 `「`
|
|
144
|
+
(标点)、前面是 `道`(非空白非标点),按 CommonMark 的 flanking 规则
|
|
145
|
+
它不是左侧定界符,于是整段**原样显示两个星号**。
|
|
146
|
+
而中文里「**「…」**」「**《…》**」这种写法极常见,模型输出里到处都是。
|
|
147
|
+
这个插件按 CJK 友好规则重判 flanking,是这条规则的通行修法。 */,
|
|
148
|
+
remarkPlugins: [_remarkCjkFriendly.default, _remarkGfm.default, _remarkMath.default],
|
|
142
149
|
className: (0, _classnames.default)(_indexModule.default.MarkdownViews, className, "markdown-body"),
|
|
143
150
|
components: {
|
|
144
151
|
code: CodeBlock,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hsu-react/ui",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "一套基于 React + Ant Design 的中后台业务组件库",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -93,6 +93,7 @@
|
|
|
93
93
|
"react-markdown-editor-lite": "^1.3.4",
|
|
94
94
|
"rehype-highlight": "^7.0.2",
|
|
95
95
|
"rehype-katex": "^7.0.1",
|
|
96
|
+
"remark-cjk-friendly": "2.3.1",
|
|
96
97
|
"remark-gfm": "^4.0.1",
|
|
97
98
|
"remark-math": "^6.0.0",
|
|
98
99
|
"turndown": "^7.2.0",
|
|
@@ -11,6 +11,7 @@ import Icon from "../../Icon";
|
|
|
11
11
|
import { ItemContainerProps } from "../../FormItem/ItemContainer";
|
|
12
12
|
import classNames from "classnames";
|
|
13
13
|
import styles from "./index.module.scss";
|
|
14
|
+
import { formItemKeys } from "../_utils/formItemKey";
|
|
14
15
|
import useLabelWidth from "../../../hooks/useLabelWidth";
|
|
15
16
|
import usePermissions from "../../../hooks/usePermissions";
|
|
16
17
|
import { mergeSemantic } from "../../../utils/semantic";
|
|
@@ -89,9 +90,9 @@ const DrawerForm: React.FC<DrawerFormProps> = (props) => {
|
|
|
89
90
|
{...drawerConfig}
|
|
90
91
|
>
|
|
91
92
|
<Form form={form} className={styles.form}>
|
|
92
|
-
{formItems?.map((item) => (
|
|
93
|
+
{formItems?.map((item, idx) => (
|
|
93
94
|
<FormItem
|
|
94
|
-
key={
|
|
95
|
+
key={formItemKeys(formItems)[idx]}
|
|
95
96
|
requiredMsg={
|
|
96
97
|
(item.name as string)?.endsWith("En")
|
|
97
98
|
? `${PlaceholderDictEn[item.type]} ${item.name}`
|
|
@@ -13,6 +13,7 @@ import { downloadFile } from "hsu-utils";
|
|
|
13
13
|
import { get } from "../../../request";
|
|
14
14
|
import { observer } from "mobx-react-lite";
|
|
15
15
|
import styles from "./index.module.scss";
|
|
16
|
+
import { formItemKeys } from "../_utils/formItemKey";
|
|
16
17
|
import useLabelWidth from "../../../hooks/useLabelWidth";
|
|
17
18
|
import usePermissions from "../../../hooks/usePermissions";
|
|
18
19
|
import Modal, { ModalProps } from "../../Modal";
|
|
@@ -106,9 +107,9 @@ const ImportForm: React.FC<ImportFormProps> = observer((props) => {
|
|
|
106
107
|
{...modalConfig}
|
|
107
108
|
>
|
|
108
109
|
<Form className={`${styles.form} ${formClassName ?? ""}`}>
|
|
109
|
-
{formItems?.map((item) => (
|
|
110
|
+
{formItems?.map((item, idx) => (
|
|
110
111
|
<FormItem
|
|
111
|
-
key={
|
|
112
|
+
key={formItemKeys(formItems)[idx]}
|
|
112
113
|
requiredMsg={
|
|
113
114
|
item.requiredMsg ??
|
|
114
115
|
((item.name as string)?.endsWith("En")
|
|
@@ -10,6 +10,7 @@ import React, { useEffect, useRef } from "react";
|
|
|
10
10
|
|
|
11
11
|
import { ItemContainerProps } from "../../FormItem/ItemContainer";
|
|
12
12
|
import styles from "./index.module.scss";
|
|
13
|
+
import { formItemKeys } from "../_utils/formItemKey";
|
|
13
14
|
import usePermissions from "../../../hooks/usePermissions";
|
|
14
15
|
import { useFormItems } from "./_hooks/useFormItems";
|
|
15
16
|
import type { FormRef } from "../../../types/antd";
|
|
@@ -110,6 +111,10 @@ const ModalForm: React.FC<ModalFormProps> = (props) => {
|
|
|
110
111
|
);
|
|
111
112
|
|
|
112
113
|
const _formItems = useFormItems(formItems, layout, adaptiveColumnNum);
|
|
114
|
+
/* `extraFormItems` 是 JSX 形态的表单项,同样可能重名(互斥字段) */
|
|
115
|
+
const extraKeys = formItemKeys(
|
|
116
|
+
extraFormItems?.map((i) => i.props as FormItemProps),
|
|
117
|
+
);
|
|
113
118
|
|
|
114
119
|
if (!permitted) {
|
|
115
120
|
return null;
|
|
@@ -147,7 +152,11 @@ const ModalForm: React.FC<ModalFormProps> = (props) => {
|
|
|
147
152
|
style={{ "--column-num": adaptiveColumnNum } as React.CSSProperties}
|
|
148
153
|
onValuesChange={onValuesChange}
|
|
149
154
|
>
|
|
150
|
-
{Object.keys(_formItems ?? {})?.map((key) =>
|
|
155
|
+
{Object.keys(_formItems ?? {})?.map((key) => {
|
|
156
|
+
/* key 一次算完(重名的第二条起加 `#n`),别在 map 体里每项重算 */
|
|
157
|
+
const itemKeys = formItemKeys(_formItems?.[key]);
|
|
158
|
+
|
|
159
|
+
return (
|
|
151
160
|
<div
|
|
152
161
|
className={`${styles.formItemGroup} ${
|
|
153
162
|
formItemGroupClassName ?? ""
|
|
@@ -168,9 +177,9 @@ const ModalForm: React.FC<ModalFormProps> = (props) => {
|
|
|
168
177
|
className={styles.formItemGroupContent}
|
|
169
178
|
style={{ paddingLeft: !key ? 0 : undefined }}
|
|
170
179
|
>
|
|
171
|
-
{_formItems?.[key]?.map((item) => (
|
|
180
|
+
{_formItems?.[key]?.map((item, idx) => (
|
|
172
181
|
<FormItem
|
|
173
|
-
key={
|
|
182
|
+
key={itemKeys[idx]}
|
|
174
183
|
requiredMsg={
|
|
175
184
|
item.requiredMsg ??
|
|
176
185
|
((item.name as string)?.endsWith("En")
|
|
@@ -186,11 +195,14 @@ const ModalForm: React.FC<ModalFormProps> = (props) => {
|
|
|
186
195
|
))}
|
|
187
196
|
</div>
|
|
188
197
|
</div>
|
|
189
|
-
|
|
190
|
-
|
|
198
|
+
);
|
|
199
|
+
})}
|
|
200
|
+
{extraFormItems?.map((item, idx) => {
|
|
191
201
|
item = {
|
|
192
202
|
...item,
|
|
193
|
-
key
|
|
203
|
+
/* 与上面那组同一套 key 规则:重名的第二条起加 `#n`,
|
|
204
|
+
按声明顺序编号(见 formItemKeys 的说明) */
|
|
205
|
+
key: extraKeys[idx],
|
|
194
206
|
props: {
|
|
195
207
|
requiredMsg:
|
|
196
208
|
item.props.requiredMsg ??
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { FormItemProps } from "../../FormItem";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 表单项的 React key。
|
|
5
|
+
*
|
|
6
|
+
* 原来直接用 `item.name`,而**一个表单有两个互斥、同名的字段是合法的** ——
|
|
7
|
+
* 消费方真实写法:Bearer Token 用 `PASSWORD`、自定义请求头用 `TEXTAREA`,
|
|
8
|
+
* 两条都叫 `authValue`,靠 `visible` 互斥。`FormItemProps` 是按 `type` 判别的
|
|
9
|
+
* 联合类型,把 `type` 写成三元表达式会让 `componentProps` 退化成所有形态的
|
|
10
|
+
* 交集、编译期就通不过,所以拆成两条是**故意的**,不是写错。
|
|
11
|
+
*
|
|
12
|
+
* 于是控制台常年三条
|
|
13
|
+
* `Encountered two children with the same key, 'authValue'`。
|
|
14
|
+
* 目前行为无害(两条永远只渲染一个,另一个 return null),但 React 明说
|
|
15
|
+
* 重复 key 的行为不受支持、将来可能改。
|
|
16
|
+
*
|
|
17
|
+
* 修法是重名的第二条起加后缀,而不是改成按下标 —— 按下标会让
|
|
18
|
+
* 「条件性地从数组里删掉某一项」的表单在删除时整片重挂。
|
|
19
|
+
*
|
|
20
|
+
* ⚠️ **按声明数组编号,不能按「可见的那部分」编号。**互斥字段两条都在数组里,
|
|
21
|
+
* 只是其中一条渲染成 null。若先 `filter(visible)` 再编号,切换的瞬间剩下那条的
|
|
22
|
+
* key 会从 `authValue#1` 变成 `authValue` —— **key 一变就是重挂**,用户正在输入的
|
|
23
|
+
* 内容和焦点当场丢,比原来那条警告严重得多。数组是静态写死的,
|
|
24
|
+
* 按声明顺序编号则天然稳定,切 `visible` 不动 key。
|
|
25
|
+
*
|
|
26
|
+
* @param items 声明顺序的表单项数组(**不要预先过滤**)
|
|
27
|
+
* @returns 与入参等长、下标一一对应的 key 数组
|
|
28
|
+
*/
|
|
29
|
+
export const formItemKeys = (items?: FormItemProps[]): string[] => {
|
|
30
|
+
const used: Record<string, number> = {};
|
|
31
|
+
|
|
32
|
+
return (items ?? []).map((item, idx) => {
|
|
33
|
+
// name 缺省时退回下标:没有名字的项本来就互不相同,用下标不会撞
|
|
34
|
+
const name = item?.name == null ? `#${idx}` : String(item.name);
|
|
35
|
+
const seen = used[name] ?? 0;
|
|
36
|
+
used[name] = seen + 1;
|
|
37
|
+
|
|
38
|
+
return seen === 0 ? name : `${name}#${seen}`;
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default formItemKeys;
|
|
@@ -5,6 +5,7 @@ import ReactMarkdown, {
|
|
|
5
5
|
} from "react-markdown";
|
|
6
6
|
import rehypeHighlight from "rehype-highlight";
|
|
7
7
|
import rehypeKatex from "rehype-katex";
|
|
8
|
+
import remarkCjkFriendly from "remark-cjk-friendly";
|
|
8
9
|
import remarkGfm from "remark-gfm";
|
|
9
10
|
import remarkMath from "remark-math";
|
|
10
11
|
import classNames from "classnames";
|
|
@@ -125,7 +126,13 @@ const MarkdownViews: React.FC<MarkdownViewsProps> = (props) => {
|
|
|
125
126
|
<ReactMarkdown
|
|
126
127
|
{...props}
|
|
127
128
|
rehypePlugins={[rehypeHighlight, rehypeKatex]}
|
|
128
|
-
|
|
129
|
+
/* `remarkCjkFriendly` 修的是中文里最常见的一处 markdown 失效:
|
|
130
|
+
`我得先知道**「这件事」是什么**` —— 开头那对 `**` 后面跟着 `「`
|
|
131
|
+
(标点)、前面是 `道`(非空白非标点),按 CommonMark 的 flanking 规则
|
|
132
|
+
它不是左侧定界符,于是整段**原样显示两个星号**。
|
|
133
|
+
而中文里「**「…」**」「**《…》**」这种写法极常见,模型输出里到处都是。
|
|
134
|
+
这个插件按 CJK 友好规则重判 flanking,是这条规则的通行修法。 */
|
|
135
|
+
remarkPlugins={[remarkCjkFriendly, remarkGfm, remarkMath]}
|
|
129
136
|
className={classNames(styles.MarkdownViews, className, "markdown-body")}
|
|
130
137
|
components={{
|
|
131
138
|
code: CodeBlock,
|