Skip to content

Commit cc7121a

Browse files
committed
fix(Form): Form adapts to React.Fragment, close#1702
1 parent b1e4610 commit cc7121a

File tree

3 files changed

+61
-29
lines changed

3 files changed

+61
-29
lines changed

src/form/form.jsx

+45-28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,50 @@ function pickerDefined(obj) {
1818
function preventDefault(e) {
1919
e.preventDefault();
2020
}
21+
const getNewChildren = (children, props) => {
22+
const {
23+
size,
24+
device,
25+
labelAlign,
26+
labelTextAlign,
27+
labelCol,
28+
wrapperCol,
29+
responsive,
30+
} = props;
31+
32+
return React.Children.map(children, child => {
33+
if (obj.isReactFragment(child)) {
34+
return getNewChildren(child.props.children, props);
35+
}
36+
37+
if (
38+
child &&
39+
typeof child.type === 'function' &&
40+
child.type._typeMark === 'form_item'
41+
) {
42+
const childrenProps = {
43+
labelCol: child.props.labelCol
44+
? child.props.labelCol
45+
: labelCol,
46+
wrapperCol: child.props.wrapperCol
47+
? child.props.wrapperCol
48+
: wrapperCol,
49+
labelAlign: child.props.labelAlign
50+
? child.props.labelAlign
51+
: device === 'phone'
52+
? 'top'
53+
: labelAlign,
54+
labelTextAlign: child.props.labelTextAlign
55+
? child.props.labelTextAlign
56+
: labelTextAlign,
57+
size: child.props.size ? child.props.size : size,
58+
responsive: responsive,
59+
};
60+
return React.cloneElement(child, pickerDefined(childrenProps));
61+
}
62+
return child;
63+
});
64+
};
2165

2266
/** Form */
2367
export default class Form extends React.Component {
@@ -214,34 +258,7 @@ export default class Form extends React.Component {
214258
[className]: !!className,
215259
});
216260

217-
const newChildren = React.Children.map(children, child => {
218-
if (
219-
child &&
220-
typeof child.type === 'function' &&
221-
child.type._typeMark === 'form_item'
222-
) {
223-
const childrenProps = {
224-
labelCol: child.props.labelCol
225-
? child.props.labelCol
226-
: labelCol,
227-
wrapperCol: child.props.wrapperCol
228-
? child.props.wrapperCol
229-
: wrapperCol,
230-
labelAlign: child.props.labelAlign
231-
? child.props.labelAlign
232-
: device === 'phone'
233-
? 'top'
234-
: labelAlign,
235-
labelTextAlign: child.props.labelTextAlign
236-
? child.props.labelTextAlign
237-
: labelTextAlign,
238-
size: child.props.size ? child.props.size : size,
239-
responsive: responsive,
240-
};
241-
return React.cloneElement(child, pickerDefined(childrenProps));
242-
}
243-
return child;
244-
});
261+
const newChildren = getNewChildren(children, this.props);
245262

246263
return (
247264
<Tag

src/responsive-grid/index.jsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { obj } from '../util';
66
import createStyle, { getGridChildProps } from './create-style';
77
import Cell from './cell';
88

9-
const { pickOthers } = obj;
9+
const { pickOthers, isReactFragment } = obj;
1010

1111
const createChildren = (children, device) => {
1212
const array = React.Children.toArray(children);
@@ -15,6 +15,10 @@ const createChildren = (children, device) => {
1515
}
1616

1717
return array.map(child => {
18+
if (isReactFragment(child)) {
19+
return createChildren(child.props.children, device);
20+
}
21+
1822
if (
1923
React.isValidElement(child) &&
2024
typeof child.type === 'function' &&

src/util/object.js

+11
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,14 @@ export function isClassComponent(component) {
293293
component.prototype.isReactComponent !== undefined
294294
);
295295
}
296+
297+
/**
298+
* 判断是否为 ReactFragment
299+
* @param {*} component 传入的组件
300+
*/
301+
export function isReactFragment(component) {
302+
if (component.type) {
303+
return component.type === React.Fragment;
304+
}
305+
return component === React.Fragment;
306+
}

0 commit comments

Comments
 (0)