@huibo-ui/react-antd 1.0.16 → 1.0.17
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/lib/components/Form.js +7 -1
- package/package.json +1 -1
package/lib/components/Form.js
CHANGED
|
@@ -262,7 +262,13 @@ export function FormItem(props) {
|
|
|
262
262
|
const [localErrors, setLocalErrors] = React.useState([]);
|
|
263
263
|
const errors = (ctx?.errors[propName] ?? localErrors) || [];
|
|
264
264
|
const isRequired = required || rules?.some((r) => r.required);
|
|
265
|
-
|
|
265
|
+
// 对齐 antd FormItem 的容错:antd 不会因 children 为空/数组而崩溃。
|
|
266
|
+
// React.Children.only 在 children 为 undefined/null/数组时会抛错,
|
|
267
|
+
// 业务代码(尤其条件渲染场景)可能传入 0 个或多个子元素,这里做兼容:
|
|
268
|
+
// - undefined/null/0 元素 → child 为 undefined(渲染空 FormItem)
|
|
269
|
+
// - 多个元素 → 取第一个(与 antd 单控件包裹语义一致)
|
|
270
|
+
const childArr = React.Children.toArray(children);
|
|
271
|
+
const child = (childArr.length > 0 ? childArr[0] : undefined);
|
|
266
272
|
const labelColSpan = labelCol?.span ?? ctx?.labelColSpan;
|
|
267
273
|
// wrapperCol 支持 { span, offset }:offset 在 wrapper 左侧留出与 labelCol 等比的空间。
|
|
268
274
|
// 注意:|| 与 ?? 不可直接混用(babel parser 报错),用显式括号 + 层级判断。
|