@hsu-react/ui 2.5.0 → 2.5.2

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.
@@ -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), item.name);
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), item.name);
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), item.name);
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: item.props.name,
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
- remarkPlugins: [remarkGfm, remarkMath],
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,
@@ -143,22 +143,29 @@ export var useTabPath = function useTabPath(_ref) {
143
143
  }, [location.pathname, location.search]);
144
144
 
145
145
  /**
146
- * Closing the last tab navigates to `basePath` — but when that tab *was* `basePath`, the
147
- * location never changes, so path matching would not re-run and the bar would be left empty
148
- * while its page is still on screen. Going empty has to re-trigger the match on its own.
146
+ * 路径匹配只跟着**地址**跑,不跟着 openKeys 的空/非空跑。
149
147
  *
150
- * This settles rather than looping: re-matching adds the tab back, which flips `isEmpty` and
151
- * runs the effect once more, and that pass finds the tab already present and returns the same
152
- * state object — so React stops there. (Only true because the setters below bail out when
153
- * nothing changed; without that this would spin.)
148
+ * 这里原本还挂了一个 `isEmpty` 依赖:openKeys 一变空就重跑一次匹配,用意是补上
149
+ * 「关掉的正是 basePath 那一页 navigate(basePath) 不改变地址 匹配不会重跑」
150
+ * 这一档。但它会把**刚关掉的那个页签原样加回来**:
151
+ *
152
+ * 关闭按钮里 `setOpenkeys([])` 是默认优先级的更新,而 `navigate()` 引起的
153
+ * location 变更走的是 router 的低优先级更新(startTransition / deferred 路由表)。
154
+ * React 先提交前者,此时 `useLocation()` 还停在**被关掉的那一页**,本 effect 因
155
+ * `isEmpty` 翻转而跑了一遍,匹配到的自然还是它,于是页签复活;等 location 真正
156
+ * 落地,匹配又把目标页加成第二个页签。用户看到的就是「点一次关不掉,还多出一个」,
157
+ * 非得再点一次。
158
+ *
159
+ * 所以不补那一档:**页签栏空着是合法状态**。关掉最后一个页签时,若目标地址与当前
160
+ * 地址不同,location 变化自会把目标页签匹配出来;若相同(关掉的就是 basePath),
161
+ * 页签栏就空着,内容区仍是 basePath 那一页 —— 一次点击、不复活、不空白。
154
162
  */
155
- var isEmpty = openKeys.length === 0;
156
163
  useEffect(function () {
157
164
  // Handle affixed routes first to populate openKeys
158
165
  _checkAffix(items);
159
166
  // Then run path matching
160
167
  _checkPath(items);
161
- }, [_checkAffix, _checkPath, items, isEmpty]);
168
+ }, [_checkAffix, _checkPath, items]);
162
169
  return {
163
170
  tabKey: tabKey,
164
171
  setTabKey: setTabKey,
@@ -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
- }, item.name)), extraFormItems?.map(item => {
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
- }, item.name))
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 => /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
111
- className: `${_indexModule.default.formItemGroup} ${formItemGroupClassName ?? ""}`,
112
- id: key,
113
- children: [key && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
114
- className: `${_indexModule.default.formItemTitle} ${formItemGroupTitleClassName ?? ""}`,
115
- children: key
116
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
117
- className: _indexModule.default.formItemGroupContent,
118
- style: {
119
- paddingLeft: !key ? 0 : undefined
120
- },
121
- children: _formItems?.[key]?.map(item => /*#__PURE__*/(0, _jsxRuntime.jsx)(_FormItem.default, {
122
- requiredMsg: item.requiredMsg ?? (item.name?.endsWith("En") ? `${_FormItem.PlaceholderDictEn[item.type]} ${item.name}` : `${_FormItem.PlaceholderDict[item.type]}${item.label}`),
123
- className: `${formItemClassName} ${item.className} ${_indexModule.default.formItem}`,
124
- disabled: disabled,
125
- required: disabled ? false : item.required,
126
- layout: formItemLayout,
127
- ...item
128
- }, item.name))
129
- })]
130
- }, key)), extraFormItems?.map(item => {
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: item.props.name,
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
- remarkPlugins: [_remarkGfm.default, _remarkMath.default],
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,
@@ -116,22 +116,29 @@ const useTabPath = ({
116
116
  }, [location.pathname, location.search]);
117
117
 
118
118
  /**
119
- * Closing the last tab navigates to `basePath` — but when that tab *was* `basePath`, the
120
- * location never changes, so path matching would not re-run and the bar would be left empty
121
- * while its page is still on screen. Going empty has to re-trigger the match on its own.
119
+ * 路径匹配只跟着**地址**跑,不跟着 openKeys 的空/非空跑。
122
120
  *
123
- * This settles rather than looping: re-matching adds the tab back, which flips `isEmpty` and
124
- * runs the effect once more, and that pass finds the tab already present and returns the same
125
- * state object — so React stops there. (Only true because the setters below bail out when
126
- * nothing changed; without that this would spin.)
121
+ * 这里原本还挂了一个 `isEmpty` 依赖:openKeys 一变空就重跑一次匹配,用意是补上
122
+ * 「关掉的正是 basePath 那一页 navigate(basePath) 不改变地址 匹配不会重跑」
123
+ * 这一档。但它会把**刚关掉的那个页签原样加回来**:
124
+ *
125
+ * 关闭按钮里 `setOpenkeys([])` 是默认优先级的更新,而 `navigate()` 引起的
126
+ * location 变更走的是 router 的低优先级更新(startTransition / deferred 路由表)。
127
+ * React 先提交前者,此时 `useLocation()` 还停在**被关掉的那一页**,本 effect 因
128
+ * `isEmpty` 翻转而跑了一遍,匹配到的自然还是它,于是页签复活;等 location 真正
129
+ * 落地,匹配又把目标页加成第二个页签。用户看到的就是「点一次关不掉,还多出一个」,
130
+ * 非得再点一次。
131
+ *
132
+ * 所以不补那一档:**页签栏空着是合法状态**。关掉最后一个页签时,若目标地址与当前
133
+ * 地址不同,location 变化自会把目标页签匹配出来;若相同(关掉的就是 basePath),
134
+ * 页签栏就空着,内容区仍是 basePath 那一页 —— 一次点击、不复活、不空白。
127
135
  */
128
- const isEmpty = openKeys.length === 0;
129
136
  (0, _react.useEffect)(() => {
130
137
  // Handle affixed routes first to populate openKeys
131
138
  _checkAffix(items);
132
139
  // Then run path matching
133
140
  _checkPath(items);
134
- }, [_checkAffix, _checkPath, items, isEmpty]);
141
+ }, [_checkAffix, _checkPath, items]);
135
142
  return {
136
143
  tabKey,
137
144
  setTabKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hsu-react/ui",
3
- "version": "2.5.0",
3
+ "version": "2.5.2",
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={item.name}
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={item.name}
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={item.name}
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
- {extraFormItems?.map((item) => {
198
+ );
199
+ })}
200
+ {extraFormItems?.map((item, idx) => {
191
201
  item = {
192
202
  ...item,
193
- key: item.props.name,
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
- remarkPlugins={[remarkGfm, remarkMath]}
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,
@@ -129,23 +129,29 @@ export const useTabPath = ({ items, affixRouter }: UseTabPathOptions) => {
129
129
  );
130
130
 
131
131
  /**
132
- * Closing the last tab navigates to `basePath` — but when that tab *was* `basePath`, the
133
- * location never changes, so path matching would not re-run and the bar would be left empty
134
- * while its page is still on screen. Going empty has to re-trigger the match on its own.
132
+ * 路径匹配只跟着**地址**跑,不跟着 openKeys 的空/非空跑。
135
133
  *
136
- * This settles rather than looping: re-matching adds the tab back, which flips `isEmpty` and
137
- * runs the effect once more, and that pass finds the tab already present and returns the same
138
- * state object — so React stops there. (Only true because the setters below bail out when
139
- * nothing changed; without that this would spin.)
134
+ * 这里原本还挂了一个 `isEmpty` 依赖:openKeys 一变空就重跑一次匹配,用意是补上
135
+ * 「关掉的正是 basePath 那一页 navigate(basePath) 不改变地址 匹配不会重跑」
136
+ * 这一档。但它会把**刚关掉的那个页签原样加回来**:
137
+ *
138
+ * 关闭按钮里 `setOpenkeys([])` 是默认优先级的更新,而 `navigate()` 引起的
139
+ * location 变更走的是 router 的低优先级更新(startTransition / deferred 路由表)。
140
+ * React 先提交前者,此时 `useLocation()` 还停在**被关掉的那一页**,本 effect 因
141
+ * `isEmpty` 翻转而跑了一遍,匹配到的自然还是它,于是页签复活;等 location 真正
142
+ * 落地,匹配又把目标页加成第二个页签。用户看到的就是「点一次关不掉,还多出一个」,
143
+ * 非得再点一次。
144
+ *
145
+ * 所以不补那一档:**页签栏空着是合法状态**。关掉最后一个页签时,若目标地址与当前
146
+ * 地址不同,location 变化自会把目标页签匹配出来;若相同(关掉的就是 basePath),
147
+ * 页签栏就空着,内容区仍是 basePath 那一页 —— 一次点击、不复活、不空白。
140
148
  */
141
- const isEmpty = openKeys.length === 0;
142
-
143
149
  useEffect(() => {
144
150
  // Handle affixed routes first to populate openKeys
145
151
  _checkAffix(items);
146
152
  // Then run path matching
147
153
  _checkPath(items);
148
- }, [_checkAffix, _checkPath, items, isEmpty]);
154
+ }, [_checkAffix, _checkPath, items]);
149
155
 
150
156
  return { tabKey, setTabKey, openKeys, setOpenkeys };
151
157
  };