@hsu-react/ui 2.4.9 → 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.
Files changed (30) hide show
  1. package/es/components/Form/DrawerForm/index.js +3 -2
  2. package/es/components/Form/ImportForm/index.js +3 -2
  3. package/es/components/Form/ModalForm/index.js +13 -4
  4. package/es/components/Form/_utils/formItemKey.d.ts +29 -0
  5. package/es/components/Form/_utils/formItemKey.js +38 -0
  6. package/es/components/FormItem/FormInput/FormInputNumber/index.d.ts +17 -1
  7. package/es/components/FormItem/FormInput/FormInputNumber/index.js +64 -5
  8. package/es/components/Input/Number/index.d.ts +5 -1
  9. package/es/components/Input/Number/index.js +19 -2
  10. package/es/components/Markdown/Views/index.js +9 -2
  11. package/lib/components/Form/DrawerForm/index.js +3 -2
  12. package/lib/components/Form/ImportForm/index.js +3 -2
  13. package/lib/components/Form/ModalForm/index.js +31 -22
  14. package/lib/components/Form/_utils/formItemKey.d.ts +29 -0
  15. package/lib/components/Form/_utils/formItemKey.js +44 -0
  16. package/lib/components/FormItem/FormInput/FormInputNumber/index.d.ts +17 -1
  17. package/lib/components/FormItem/FormInput/FormInputNumber/index.js +62 -1
  18. package/lib/components/Input/Number/index.d.ts +5 -1
  19. package/lib/components/Input/Number/index.js +19 -2
  20. package/lib/components/Markdown/Views/index.js +9 -2
  21. package/package.json +2 -1
  22. package/src/components/Form/DrawerForm/index.tsx +3 -2
  23. package/src/components/Form/ImportForm/index.tsx +3 -2
  24. package/src/components/Form/ModalForm/index.tsx +18 -6
  25. package/src/components/Form/_utils/formItemKey.ts +42 -0
  26. package/src/components/FormItem/FormInput/FormInputNumber/index.tsx +95 -3
  27. package/src/components/FormItem/index.md +16 -0
  28. package/src/components/Input/Number/index.tsx +28 -3
  29. package/src/components/Input/index.md +9 -1
  30. 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), 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;
@@ -2,7 +2,23 @@ import { ItemContainerProps } from "../../ItemContainer";
2
2
  import { InputNumberProps } from "../../../Input/Number";
3
3
  import React from "react";
4
4
  export interface FormInputNumberProps extends ItemContainerProps {
5
- componentProps?: InputNumberProps;
5
+ /**
6
+ * `onChange` 的签名比基础组件宽:作为**表单字段**,默认交出的是
7
+ * `number | null`(见 Bridge 的注释)。写这个回调的人拿到的与表单拿到的
8
+ * 是同一个值。
9
+ */
10
+ componentProps?: Omit<InputNumberProps, "onChange"> & {
11
+ onChange?: (value: string | number | null) => void;
12
+ };
13
+ /**
14
+ * 交给表单的是字符串而不是数字。默认 `false`。
15
+ *
16
+ * 只有真的要处理**超出 IEEE754 安全范围的大数**(订单号、雪花 ID)时才打开
17
+ * —— 那种值转成 number 会丢精度,必须一路以字符串传递。
18
+ *
19
+ * 2.5.0 之前没有这个开关,行为固定等同于 `stringMode`(见下方 Bridge 注释)。
20
+ */
21
+ stringMode?: boolean;
6
22
  }
7
23
  declare const FormInputNumber: React.FC<FormInputNumberProps>;
8
24
  export default FormInputNumber;
@@ -1,6 +1,7 @@
1
1
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
- var _excluded = ["componentProps", "className", "disabled"],
3
- _excluded2 = ["placeholder"];
2
+ var _excluded = ["value", "onChange", "componentOnChange", "stringMode"],
3
+ _excluded2 = ["componentProps", "className", "disabled", "stringMode"],
4
+ _excluded3 = ["placeholder", "onChange"];
4
5
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
5
6
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
6
7
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
@@ -11,19 +12,77 @@ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) r
11
12
  import ItemContainer from "../../ItemContainer";
12
13
  import Input from "../../../Input";
13
14
  import React from "react";
15
+
16
+ // value / onChange 要重新声明:`InputNumberProps` 上那两个是**基础组件**的
17
+ // 契约(字符串进、字符串出),而这里收的是**表单值**,可以是数字或 null
14
18
  import { jsx as _jsx } from "react/jsx-runtime";
19
+ /**
20
+ * 夹在 `Form.Item` 与 `Input.Number` 之间,**把字符串换成数字再交给表单**。
21
+ *
22
+ * 为什么需要它:`Input.Number` 内部以字符串保存(底层 `stringMode`,为的是不
23
+ * 丢大数精度),`onChange` 交出的也是字符串。这对一个**基础输入组件**是合理
24
+ * 的,但对一个叫 INPUTNUMBER 的**表单字段**不合理 —— 表单收上去的值会直接
25
+ * 进请求体,而后端字段多半是整数:
26
+ *
27
+ * ```
28
+ * 422 invalid type: string "2", expected i32
29
+ * ```
30
+ *
31
+ * 这个坑平时看不见:没动过的字段,值来自详情接口、本来就是数字,原样发回去
32
+ * 没事;**只有用户真的改了那一格**才会变成字符串。现象是「我明明只改了个
33
+ * 数字却保存失败」,而且报错在服务端,前端一行日志都没有。
34
+ *
35
+ * 转换只在**往上交**的方向做;`Input.Number` 内部仍然是字符串,所以正在敲的
36
+ * "1." / "1.50" 不会被改写(配套改动见 `Input/Number` 里的 sameNumber 判断)。
37
+ */
38
+ var Bridge = function Bridge(props) {
39
+ var value = props.value,
40
+ onChange = props.onChange,
41
+ componentOnChange = props.componentOnChange,
42
+ stringMode = props.stringMode,
43
+ rest = _objectWithoutProperties(props, _excluded);
44
+ var emit = function emit(next) {
45
+ onChange === null || onChange === void 0 || onChange(next);
46
+ componentOnChange === null || componentOnChange === void 0 || componentOnChange(next);
47
+ };
48
+ return /*#__PURE__*/_jsx(Input.Number, _objectSpread(_objectSpread({}, rest), {}, {
49
+ value: value,
50
+ onChange: function onChange(text) {
51
+ if (stringMode) {
52
+ emit(text);
53
+ return;
54
+ }
55
+
56
+ // 空 → null 而不是 "":语义是「没填」,不是「填了个空字符串」。
57
+ // 后端 `Option<i32>` 收得下 null,收不下 ""
58
+ if (text === "" || text === null || text === undefined) {
59
+ emit(null);
60
+ return;
61
+ }
62
+ var num = Number(text);
63
+ // 转不动就原样交出去(用户可能正敲到 "-" 或 "1e" 这种中间态)。
64
+ // 硬转成 NaN 会让表单里躺着一个既非数字也非空的值
65
+ emit(Number.isFinite(num) ? num : text);
66
+ }
67
+ }));
68
+ };
15
69
  var FormInputNumber = function FormInputNumber(props) {
16
70
  var _inputConfig$disabled;
17
71
  var _props$componentProps = props.componentProps,
18
72
  componentProps = _props$componentProps === void 0 ? {} : _props$componentProps,
19
73
  itemClassName = props.className,
20
74
  disabled = props.disabled,
21
- formItemProps = _objectWithoutProperties(props, _excluded);
75
+ stringMode = props.stringMode,
76
+ formItemProps = _objectWithoutProperties(props, _excluded2);
77
+ // onChange 必须摘出来,理由见 BridgeProps.componentOnChange
22
78
  var placeholder = componentProps.placeholder,
23
- inputConfig = _objectWithoutProperties(componentProps, _excluded2);
79
+ componentOnChange = componentProps.onChange,
80
+ inputConfig = _objectWithoutProperties(componentProps, _excluded3);
24
81
  return /*#__PURE__*/_jsx(ItemContainer, _objectSpread(_objectSpread({}, formItemProps), {}, {
25
82
  className: "".concat(itemClassName !== null && itemClassName !== void 0 ? itemClassName : ""),
26
- children: /*#__PURE__*/_jsx(Input.Number, _objectSpread(_objectSpread({}, inputConfig), {}, {
83
+ children: /*#__PURE__*/_jsx(Bridge, _objectSpread(_objectSpread({}, inputConfig), {}, {
84
+ componentOnChange: componentOnChange,
85
+ stringMode: stringMode,
27
86
  disabled: (_inputConfig$disabled = inputConfig.disabled) !== null && _inputConfig$disabled !== void 0 ? _inputConfig$disabled : disabled,
28
87
  placeholder: placeholder !== null && placeholder !== void 0 ? placeholder : "请输入"
29
88
  }))
@@ -3,7 +3,11 @@ import React from "react";
3
3
  export interface InputNumberProps extends Omit<AntdInputNumberProps, "ref" | "onChange" | "value"> {
4
4
  getRef?: (ref: HTMLInputElement | null) => void;
5
5
  onChange?: (value: string) => void;
6
- value?: string;
6
+ /**
7
+ * 受控值。本组件内部**始终以字符串保存**(底层开了 antd 的 `stringMode`,
8
+ * 为的是不丢大数精度),但外部传数字进来也接得住。
9
+ */
10
+ value?: string | number | null;
7
11
  allowClear?: boolean;
8
12
  wrapperClassName?: string;
9
13
  }
@@ -64,16 +64,33 @@ var InputNumber = function InputNumber(props) {
64
64
  // Update internal state only when the external value prop actually changes
65
65
  if (prevValueRef.current !== value) {
66
66
  prevValueRef.current = value;
67
- if (value !== undefined) {
67
+
68
+ // 数值相等就不要回写文本。
69
+ //
70
+ // 上游把值转成数字之后(`FormItem` 的 INPUTNUMBER 默认就会转),一个
71
+ // 回环会打回来:输入 "1." → onChange → 表单存 1 → value={1} 传回来。
72
+ // 若照单全收地写成 "1",用户刚敲的小数点当场消失、光标还会跳到末尾,
73
+ // "1.50" 也会被改写成 "1.5"。
74
+ //
75
+ // 判据用**数值相等**而不是字符串相等:这几种写法("1." / "1.50" / "+1")
76
+ // 表示的是同一个数,此时以用户正在敲的那份文本为准。
77
+ var sameNumber = value !== undefined && value !== null && _value !== "" && Number(value) === Number(_value);
78
+ if (sameNumber) {
79
+ // 只同步「已提交值」,不动正在编辑的文本
80
+ setLastValue(_value);
81
+ } else if (value !== undefined && value !== null) {
68
82
  var newValue = typeof value === "number" ? "".concat(value) : value === null || value === void 0 ? void 0 : value.toString();
69
83
  setValue(newValue);
70
84
  setLastValue(newValue);
71
85
  } else {
72
- // Clear only on initialization or when explicitly set to undefined externally
86
+ // Clear only on initialization or when explicitly set to undefined/null externally
73
87
  setValue("");
74
88
  setLastValue("");
75
89
  }
76
90
  }
91
+ // `_value` 是刻意不进依赖的:这个 effect 只该在**外部** value 变化时跑,
92
+ // 把它加进来会让每次键入都重跑一遍,等于绕过上面那个 prevValueRef 判断
93
+ // eslint-disable-next-line react-hooks/exhaustive-deps
77
94
  }, [value]);
78
95
  useEffect(function () {
79
96
  getRef === null || getRef === void 0 || getRef(ref.current);
@@ -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,
@@ -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;
@@ -2,7 +2,23 @@ import { ItemContainerProps } from "../../ItemContainer";
2
2
  import { InputNumberProps } from "../../../Input/Number";
3
3
  import React from "react";
4
4
  export interface FormInputNumberProps extends ItemContainerProps {
5
- componentProps?: InputNumberProps;
5
+ /**
6
+ * `onChange` 的签名比基础组件宽:作为**表单字段**,默认交出的是
7
+ * `number | null`(见 Bridge 的注释)。写这个回调的人拿到的与表单拿到的
8
+ * 是同一个值。
9
+ */
10
+ componentProps?: Omit<InputNumberProps, "onChange"> & {
11
+ onChange?: (value: string | number | null) => void;
12
+ };
13
+ /**
14
+ * 交给表单的是字符串而不是数字。默认 `false`。
15
+ *
16
+ * 只有真的要处理**超出 IEEE754 安全范围的大数**(订单号、雪花 ID)时才打开
17
+ * —— 那种值转成 number 会丢精度,必须一路以字符串传递。
18
+ *
19
+ * 2.5.0 之前没有这个开关,行为固定等同于 `stringMode`(见下方 Bridge 注释)。
20
+ */
21
+ stringMode?: boolean;
6
22
  }
7
23
  declare const FormInputNumber: React.FC<FormInputNumberProps>;
8
24
  export default FormInputNumber;
@@ -9,22 +9,83 @@ var _Input = _interopRequireDefault(require("../../../Input"));
9
9
  var _react = _interopRequireDefault(require("react"));
10
10
  var _jsxRuntime = require("react/jsx-runtime");
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
+ // value / onChange 要重新声明:`InputNumberProps` 上那两个是**基础组件**的
13
+ // 契约(字符串进、字符串出),而这里收的是**表单值**,可以是数字或 null
14
+
15
+ /**
16
+ * 夹在 `Form.Item` 与 `Input.Number` 之间,**把字符串换成数字再交给表单**。
17
+ *
18
+ * 为什么需要它:`Input.Number` 内部以字符串保存(底层 `stringMode`,为的是不
19
+ * 丢大数精度),`onChange` 交出的也是字符串。这对一个**基础输入组件**是合理
20
+ * 的,但对一个叫 INPUTNUMBER 的**表单字段**不合理 —— 表单收上去的值会直接
21
+ * 进请求体,而后端字段多半是整数:
22
+ *
23
+ * ```
24
+ * 422 invalid type: string "2", expected i32
25
+ * ```
26
+ *
27
+ * 这个坑平时看不见:没动过的字段,值来自详情接口、本来就是数字,原样发回去
28
+ * 没事;**只有用户真的改了那一格**才会变成字符串。现象是「我明明只改了个
29
+ * 数字却保存失败」,而且报错在服务端,前端一行日志都没有。
30
+ *
31
+ * 转换只在**往上交**的方向做;`Input.Number` 内部仍然是字符串,所以正在敲的
32
+ * "1." / "1.50" 不会被改写(配套改动见 `Input/Number` 里的 sameNumber 判断)。
33
+ */
34
+ const Bridge = props => {
35
+ const {
36
+ value,
37
+ onChange,
38
+ componentOnChange,
39
+ stringMode,
40
+ ...rest
41
+ } = props;
42
+ const emit = next => {
43
+ onChange?.(next);
44
+ componentOnChange?.(next);
45
+ };
46
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input.default.Number, {
47
+ ...rest,
48
+ value: value,
49
+ onChange: text => {
50
+ if (stringMode) {
51
+ emit(text);
52
+ return;
53
+ }
54
+
55
+ // 空 → null 而不是 "":语义是「没填」,不是「填了个空字符串」。
56
+ // 后端 `Option<i32>` 收得下 null,收不下 ""
57
+ if (text === "" || text === null || text === undefined) {
58
+ emit(null);
59
+ return;
60
+ }
61
+ const num = Number(text);
62
+ // 转不动就原样交出去(用户可能正敲到 "-" 或 "1e" 这种中间态)。
63
+ // 硬转成 NaN 会让表单里躺着一个既非数字也非空的值
64
+ emit(Number.isFinite(num) ? num : text);
65
+ }
66
+ });
67
+ };
12
68
  const FormInputNumber = props => {
13
69
  const {
14
70
  componentProps = {},
15
71
  className: itemClassName,
16
72
  disabled,
73
+ stringMode,
17
74
  ...formItemProps
18
75
  } = props;
76
+ // onChange 必须摘出来,理由见 BridgeProps.componentOnChange
19
77
  const {
20
78
  placeholder,
79
+ onChange: componentOnChange,
21
80
  ...inputConfig
22
81
  } = componentProps;
23
82
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ItemContainer.default, {
24
83
  ...formItemProps,
25
84
  className: `${itemClassName ?? ""}`,
26
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input.default.Number, {
85
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Bridge, {
27
86
  ...inputConfig,
87
+ componentOnChange: componentOnChange,
88
+ stringMode: stringMode,
28
89
  disabled: inputConfig.disabled ?? disabled,
29
90
  placeholder: placeholder ?? "请输入"
30
91
  })
@@ -3,7 +3,11 @@ import React from "react";
3
3
  export interface InputNumberProps extends Omit<AntdInputNumberProps, "ref" | "onChange" | "value"> {
4
4
  getRef?: (ref: HTMLInputElement | null) => void;
5
5
  onChange?: (value: string) => void;
6
- value?: string;
6
+ /**
7
+ * 受控值。本组件内部**始终以字符串保存**(底层开了 antd 的 `stringMode`,
8
+ * 为的是不丢大数精度),但外部传数字进来也接得住。
9
+ */
10
+ value?: string | number | null;
7
11
  allowClear?: boolean;
8
12
  wrapperClassName?: string;
9
13
  }
@@ -50,16 +50,33 @@ const InputNumber = props => {
50
50
  // Update internal state only when the external value prop actually changes
51
51
  if (prevValueRef.current !== value) {
52
52
  prevValueRef.current = value;
53
- if (value !== undefined) {
53
+
54
+ // 数值相等就不要回写文本。
55
+ //
56
+ // 上游把值转成数字之后(`FormItem` 的 INPUTNUMBER 默认就会转),一个
57
+ // 回环会打回来:输入 "1." → onChange → 表单存 1 → value={1} 传回来。
58
+ // 若照单全收地写成 "1",用户刚敲的小数点当场消失、光标还会跳到末尾,
59
+ // "1.50" 也会被改写成 "1.5"。
60
+ //
61
+ // 判据用**数值相等**而不是字符串相等:这几种写法("1." / "1.50" / "+1")
62
+ // 表示的是同一个数,此时以用户正在敲的那份文本为准。
63
+ const sameNumber = value !== undefined && value !== null && _value !== "" && Number(value) === Number(_value);
64
+ if (sameNumber) {
65
+ // 只同步「已提交值」,不动正在编辑的文本
66
+ setLastValue(_value);
67
+ } else if (value !== undefined && value !== null) {
54
68
  const newValue = typeof value === "number" ? `${value}` : value?.toString();
55
69
  setValue(newValue);
56
70
  setLastValue(newValue);
57
71
  } else {
58
- // Clear only on initialization or when explicitly set to undefined externally
72
+ // Clear only on initialization or when explicitly set to undefined/null externally
59
73
  setValue("");
60
74
  setLastValue("");
61
75
  }
62
76
  }
77
+ // `_value` 是刻意不进依赖的:这个 effect 只该在**外部** value 变化时跑,
78
+ // 把它加进来会让每次键入都重跑一遍,等于绕过上面那个 prevValueRef 判断
79
+ // eslint-disable-next-line react-hooks/exhaustive-deps
63
80
  }, [value]);
64
81
  (0, _react.useEffect)(() => {
65
82
  getRef?.(ref.current);
@@ -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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hsu-react/ui",
3
- "version": "2.4.9",
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={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,22 +5,114 @@ import { InputNumberProps } from "../../../Input/Number";
5
5
  import React from "react";
6
6
 
7
7
  export interface FormInputNumberProps extends ItemContainerProps {
8
- componentProps?: InputNumberProps;
8
+ /**
9
+ * `onChange` 的签名比基础组件宽:作为**表单字段**,默认交出的是
10
+ * `number | null`(见 Bridge 的注释)。写这个回调的人拿到的与表单拿到的
11
+ * 是同一个值。
12
+ */
13
+ componentProps?: Omit<InputNumberProps, "onChange"> & {
14
+ onChange?: (value: string | number | null) => void;
15
+ };
16
+ /**
17
+ * 交给表单的是字符串而不是数字。默认 `false`。
18
+ *
19
+ * 只有真的要处理**超出 IEEE754 安全范围的大数**(订单号、雪花 ID)时才打开
20
+ * —— 那种值转成 number 会丢精度,必须一路以字符串传递。
21
+ *
22
+ * 2.5.0 之前没有这个开关,行为固定等同于 `stringMode`(见下方 Bridge 注释)。
23
+ */
24
+ stringMode?: boolean;
9
25
  }
10
26
 
27
+ // value / onChange 要重新声明:`InputNumberProps` 上那两个是**基础组件**的
28
+ // 契约(字符串进、字符串出),而这里收的是**表单值**,可以是数字或 null
29
+ type BridgeProps = Omit<InputNumberProps, "value" | "onChange"> & {
30
+ /** antd Form.Item 注入 */
31
+ value?: string | number | null;
32
+ /** antd Form.Item 注入 */
33
+ onChange?: (value: string | number | null) => void;
34
+ /**
35
+ * 使用方写在 `componentProps.onChange` 里的那个回调。
36
+ *
37
+ * 单独接出来、不跟着 `componentProps` 一起展开:展开的话它会盖掉
38
+ * Form.Item 注入的那个 onChange,字段值就再也传不到表单里了。
39
+ * 它拿到的与表单拿到的**是同一个值**(默认已转成数字)。
40
+ */
41
+ componentOnChange?: (value: string | number | null) => void;
42
+ stringMode?: boolean;
43
+ };
44
+
45
+ /**
46
+ * 夹在 `Form.Item` 与 `Input.Number` 之间,**把字符串换成数字再交给表单**。
47
+ *
48
+ * 为什么需要它:`Input.Number` 内部以字符串保存(底层 `stringMode`,为的是不
49
+ * 丢大数精度),`onChange` 交出的也是字符串。这对一个**基础输入组件**是合理
50
+ * 的,但对一个叫 INPUTNUMBER 的**表单字段**不合理 —— 表单收上去的值会直接
51
+ * 进请求体,而后端字段多半是整数:
52
+ *
53
+ * ```
54
+ * 422 invalid type: string "2", expected i32
55
+ * ```
56
+ *
57
+ * 这个坑平时看不见:没动过的字段,值来自详情接口、本来就是数字,原样发回去
58
+ * 没事;**只有用户真的改了那一格**才会变成字符串。现象是「我明明只改了个
59
+ * 数字却保存失败」,而且报错在服务端,前端一行日志都没有。
60
+ *
61
+ * 转换只在**往上交**的方向做;`Input.Number` 内部仍然是字符串,所以正在敲的
62
+ * "1." / "1.50" 不会被改写(配套改动见 `Input/Number` 里的 sameNumber 判断)。
63
+ */
64
+ const Bridge: React.FC<BridgeProps> = (props) => {
65
+ const { value, onChange, componentOnChange, stringMode, ...rest } = props;
66
+
67
+ const emit = (next: string | number | null) => {
68
+ onChange?.(next);
69
+ componentOnChange?.(next);
70
+ };
71
+
72
+ return (
73
+ <Input.Number
74
+ {...rest}
75
+ value={value}
76
+ onChange={(text) => {
77
+ if (stringMode) {
78
+ emit(text);
79
+ return;
80
+ }
81
+
82
+ // 空 → null 而不是 "":语义是「没填」,不是「填了个空字符串」。
83
+ // 后端 `Option<i32>` 收得下 null,收不下 ""
84
+ if (text === "" || text === null || text === undefined) {
85
+ emit(null);
86
+ return;
87
+ }
88
+
89
+ const num = Number(text);
90
+ // 转不动就原样交出去(用户可能正敲到 "-" 或 "1e" 这种中间态)。
91
+ // 硬转成 NaN 会让表单里躺着一个既非数字也非空的值
92
+ emit(Number.isFinite(num) ? num : text);
93
+ }}
94
+ />
95
+ );
96
+ };
97
+
11
98
  const FormInputNumber: React.FC<FormInputNumberProps> = (props) => {
12
99
  const {
13
100
  componentProps = {},
14
101
  className: itemClassName,
15
102
  disabled,
103
+ stringMode,
16
104
  ...formItemProps
17
105
  } = props;
18
- const { placeholder, ...inputConfig } = componentProps;
106
+ // onChange 必须摘出来,理由见 BridgeProps.componentOnChange
107
+ const { placeholder, onChange: componentOnChange, ...inputConfig } =
108
+ componentProps;
19
109
 
20
110
  return (
21
111
  <ItemContainer {...formItemProps} className={`${itemClassName ?? ""}`}>
22
- <Input.Number
112
+ <Bridge
23
113
  {...inputConfig}
114
+ componentOnChange={componentOnChange}
115
+ stringMode={stringMode}
24
116
  disabled={inputConfig.disabled ?? disabled}
25
117
  placeholder={placeholder ?? "请输入"}
26
118
  />
@@ -93,6 +93,22 @@ export default () => (
93
93
  );
94
94
  ```
95
95
 
96
+ > **`2.5.0` 起,`INPUTNUMBER` 交给表单的是 `number | null`**(此前是字符串)。
97
+ >
98
+ > 表单值往往原样进请求体,而后端字段多半是整数,交字符串上去会被强类型服务端
99
+ > 拒掉(Rust/serde 会回 `invalid type: string "2", expected i32`)。这个问题
100
+ > 平时看不见:**没动过的字段**值来自详情接口、本来就是数字,只有用户真的改了
101
+ > 那一格才会变成字符串 —— 现象是「我只改了个数字却保存失败」。
102
+ >
103
+ > - 空值交出的是 `null`(「没填」),不是 `""`。
104
+ > - 敲到一半的中间态(`-`、`1e`)原样交出,不会变成 `NaN`。
105
+ > - 正在输入的文本不会被回写打断,`1.` / `1.50` 都保得住。
106
+ > - 确实需要字符串(大数不丢精度)时,加 `stringMode`:
107
+ >
108
+ > ```tsx
109
+ > <FormItem type="INPUTNUMBER" name="orderNo" label="订单号" stringMode />
110
+ > ```
111
+
96
112
  ## 范围输入
97
113
 
98
114
  ```tsx
@@ -17,7 +17,11 @@ export interface InputNumberProps extends Omit<
17
17
  > {
18
18
  getRef?: (ref: HTMLInputElement | null) => void;
19
19
  onChange?: (value: string) => void;
20
- value?: string;
20
+ /**
21
+ * 受控值。本组件内部**始终以字符串保存**(底层开了 antd 的 `stringMode`,
22
+ * 为的是不丢大数精度),但外部传数字进来也接得住。
23
+ */
24
+ value?: string | number | null;
21
25
  allowClear?: boolean;
22
26
  wrapperClassName?: string;
23
27
  }
@@ -76,17 +80,38 @@ const InputNumber: React.FC<InputNumberProps> = (props) => {
76
80
  if (prevValueRef.current !== value) {
77
81
  prevValueRef.current = value;
78
82
 
79
- if (value !== undefined) {
83
+ // 数值相等就不要回写文本。
84
+ //
85
+ // 上游把值转成数字之后(`FormItem` 的 INPUTNUMBER 默认就会转),一个
86
+ // 回环会打回来:输入 "1." → onChange → 表单存 1 → value={1} 传回来。
87
+ // 若照单全收地写成 "1",用户刚敲的小数点当场消失、光标还会跳到末尾,
88
+ // "1.50" 也会被改写成 "1.5"。
89
+ //
90
+ // 判据用**数值相等**而不是字符串相等:这几种写法("1." / "1.50" / "+1")
91
+ // 表示的是同一个数,此时以用户正在敲的那份文本为准。
92
+ const sameNumber =
93
+ value !== undefined &&
94
+ value !== null &&
95
+ _value !== "" &&
96
+ Number(value) === Number(_value);
97
+
98
+ if (sameNumber) {
99
+ // 只同步「已提交值」,不动正在编辑的文本
100
+ setLastValue(_value);
101
+ } else if (value !== undefined && value !== null) {
80
102
  const newValue =
81
103
  typeof value === "number" ? `${value}` : value?.toString();
82
104
  setValue(newValue);
83
105
  setLastValue(newValue);
84
106
  } else {
85
- // Clear only on initialization or when explicitly set to undefined externally
107
+ // Clear only on initialization or when explicitly set to undefined/null externally
86
108
  setValue("");
87
109
  setLastValue("");
88
110
  }
89
111
  }
112
+ // `_value` 是刻意不进依赖的:这个 effect 只该在**外部** value 变化时跑,
113
+ // 把它加进来会让每次键入都重跑一遍,等于绕过上面那个 prevValueRef 判断
114
+ // eslint-disable-next-line react-hooks/exhaustive-deps
90
115
  }, [value]);
91
116
 
92
117
  useEffect(() => {
@@ -36,7 +36,15 @@ export default () => {
36
36
 
37
37
  ## 数字输入框
38
38
 
39
- 数字输入框,`onChange` 返回字符串,内置 `allowClear`、`controls={false}` 与 `stringMode`。
39
+ 数字输入框,`onChange` 返回**字符串**,内置 `allowClear`、`controls={false}` 与 `stringMode`。
40
+
41
+ 返回字符串是刻意的:`stringMode` 让超出 IEEE754 安全范围的大数(订单号、雪花 ID)
42
+ 不丢精度,而转成 `number` 就丢了。
43
+
44
+ > **注意**:作为**表单字段**用时(`FormItem type="INPUTNUMBER"`)行为不同 ——
45
+ > 自 `2.5.0` 起它交给表单的是 `number | null`。表单值通常会直接进请求体,
46
+ > 而后端字段多半是整数,交字符串上去会被强类型服务端拒掉。
47
+ > 详见 [FormItem 的 INPUTNUMBER](/components/form-item)。
40
48
 
41
49
  ```tsx
42
50
  import React from "react";
@@ -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,