chore: Col support key if user providered (#18578)

This commit is contained in:
二货机器人 2019-08-30 15:56:44 +08:00 committed by GitHub
parent 93c0fc1361
commit 4dfed8d0b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 39 deletions

View File

@ -39,7 +39,7 @@ exports[`Descriptions Descriptions support colon 1`] = `
</DescriptionsItem>
}
colon={false}
key="label-0"
key="0"
layout="horizontal"
type="label"
>
@ -116,7 +116,7 @@ exports[`Descriptions Descriptions support style 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="horizontal"
type="label"
>
@ -183,7 +183,7 @@ exports[`Descriptions Descriptions.Item support className 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="horizontal"
type="label"
>
@ -241,7 +241,7 @@ exports[`Descriptions column is number 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="horizontal"
type="label"
>
@ -274,7 +274,7 @@ exports[`Descriptions column is number 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-1"
key="1"
layout="horizontal"
type="label"
>
@ -307,7 +307,7 @@ exports[`Descriptions column is number 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-2"
key="2"
layout="horizontal"
type="label"
>
@ -346,7 +346,7 @@ exports[`Descriptions column is number 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="horizontal"
type="label"
>
@ -414,7 +414,7 @@ exports[`Descriptions vertical layout 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="vertical"
type="label"
>
@ -446,7 +446,7 @@ exports[`Descriptions vertical layout 1`] = `
</DescriptionsItem>
}
colon={true}
key="content-0"
key="0"
layout="vertical"
type="content"
>
@ -478,7 +478,7 @@ exports[`Descriptions vertical layout 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="vertical"
type="label"
>
@ -510,7 +510,7 @@ exports[`Descriptions vertical layout 1`] = `
</DescriptionsItem>
}
colon={true}
key="content-0"
key="0"
layout="vertical"
type="content"
>
@ -542,7 +542,7 @@ exports[`Descriptions vertical layout 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="vertical"
type="label"
>
@ -574,7 +574,7 @@ exports[`Descriptions vertical layout 1`] = `
</DescriptionsItem>
}
colon={true}
key="content-0"
key="0"
layout="vertical"
type="content"
>
@ -607,7 +607,7 @@ exports[`Descriptions vertical layout 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="vertical"
type="label"
>
@ -640,7 +640,7 @@ exports[`Descriptions vertical layout 1`] = `
</DescriptionsItem>
}
colon={true}
key="content-0"
key="0"
layout="vertical"
type="content"
>
@ -701,7 +701,7 @@ exports[`Descriptions when item is rendered conditionally 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="horizontal"
type="label"
>
@ -739,7 +739,7 @@ exports[`Descriptions when item is rendered conditionally 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="horizontal"
type="label"
>
@ -777,7 +777,7 @@ exports[`Descriptions when item is rendered conditionally 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="horizontal"
type="label"
>
@ -816,7 +816,7 @@ exports[`Descriptions when item is rendered conditionally 1`] = `
</DescriptionsItem>
}
colon={true}
key="label-0"
key="0"
layout="horizontal"
type="label"
>

View File

@ -175,4 +175,14 @@ describe('Descriptions', () => {
);
expect(wrapper).toMatchSnapshot();
});
it('keep key', () => {
const wrapper = mount(
<Descriptions>
<Descriptions.Item key="bamboo" />
</Descriptions>,
);
expect(wrapper.find('Col').key()).toBe('bamboo');
});
});

View File

@ -61,7 +61,7 @@ const generateChildrenRows = (
const lastItem = index === itemNodes.length - 1;
let lastSpanSame = true;
if (lastItem) {
lastSpanSame = (itemNode.props.span || 1) === leftSpans;
lastSpanSame = !itemNode.props.span || itemNode.props.span === leftSpans;
itemNode = React.cloneElement(itemNode, {
span: leftSpans,
});
@ -95,24 +95,25 @@ const renderRow = (
colon: boolean,
) => {
const renderCol = (
childrenItem: React.ReactElement<DescriptionsItemProps>,
colItem: React.ReactElement<DescriptionsItemProps>,
type: 'label' | 'content',
idx: number,
) => (
<Col
child={childrenItem}
bordered={bordered}
colon={colon}
type={type}
key={`${type}-${idx}`}
layout={layout}
/>
);
) => {
return (
<Col
child={colItem}
bordered={bordered}
colon={colon}
type={type}
key={colItem.key || idx}
layout={layout}
/>
);
};
const cloneChildren: JSX.Element[] = [];
const cloneContentChildren: JSX.Element[] = [];
React.Children.forEach(
children,
toArray(children).forEach(
(childrenItem: React.ReactElement<DescriptionsItemProps>, idx: number) => {
cloneChildren.push(renderCol(childrenItem, 'label', idx));
if (layout === 'vertical') {
@ -224,17 +225,16 @@ class Descriptions extends React.Component<
const prefixCls = getPrefixCls('descriptions', customizePrefixCls);
const column = this.getColumn();
const cloneChildren = React.Children.map(
children,
(child: React.ReactElement<DescriptionsItemProps>) => {
const cloneChildren = toArray(children)
.map((child: React.ReactElement<DescriptionsItemProps>) => {
if (React.isValidElement(child)) {
return React.cloneElement(child, {
prefixCls,
});
}
return child;
},
);
return null;
})
.filter((node: React.ReactElement) => node);
const childrenArray: Array<
React.ReactElement<DescriptionsItemProps>[]