2020-11-02 16:53:56 +08:00
|
|
|
import Cell from './Cell';
|
2021-06-19 21:51:29 +08:00
|
|
|
import { getSlot, getClass, getStyle } from '../_util/props-util';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { FunctionalComponent, VNode } from 'vue';
|
|
|
|
import { inject, ref } from 'vue';
|
|
|
|
import type { DescriptionsContextProp } from './index';
|
|
|
|
import { descriptionsContext } from './index';
|
2020-11-02 16:53:56 +08:00
|
|
|
|
|
|
|
interface CellConfig {
|
|
|
|
component: string | [string, string];
|
|
|
|
type: string;
|
|
|
|
showLabel?: boolean;
|
|
|
|
showContent?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RowProps {
|
|
|
|
prefixCls: string;
|
|
|
|
vertical: boolean;
|
|
|
|
row: any[];
|
|
|
|
bordered: boolean;
|
|
|
|
colon: boolean;
|
|
|
|
index: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Row: FunctionalComponent<RowProps> = props => {
|
|
|
|
const renderCells = (
|
2020-11-13 10:51:12 +08:00
|
|
|
items: VNode[],
|
2020-11-02 16:53:56 +08:00
|
|
|
{ colon, prefixCls, bordered },
|
2021-06-18 17:45:29 +08:00
|
|
|
{
|
|
|
|
component,
|
|
|
|
type,
|
|
|
|
showLabel,
|
|
|
|
showContent,
|
|
|
|
labelStyle: rootLabelStyle,
|
|
|
|
contentStyle: rootContentStyle,
|
|
|
|
}: CellConfig & DescriptionsContextProp,
|
2020-11-02 16:53:56 +08:00
|
|
|
) => {
|
|
|
|
return items.map((item, index) => {
|
2021-06-18 17:45:29 +08:00
|
|
|
const {
|
|
|
|
prefixCls: itemPrefixCls = prefixCls,
|
|
|
|
span = 1,
|
|
|
|
labelStyle,
|
|
|
|
contentStyle,
|
2021-06-19 21:51:29 +08:00
|
|
|
label = (item.children as any)?.label?.(),
|
|
|
|
} = item.props || {};
|
2020-11-02 16:53:56 +08:00
|
|
|
const children = getSlot(item);
|
|
|
|
const className = getClass(item);
|
|
|
|
const style = getStyle(item);
|
|
|
|
const { key } = item;
|
|
|
|
|
|
|
|
if (typeof component === 'string') {
|
|
|
|
return (
|
|
|
|
<Cell
|
2021-08-10 15:21:13 +08:00
|
|
|
key={`${type}-${String(key) || index}`}
|
2020-11-02 16:53:56 +08:00
|
|
|
class={className}
|
|
|
|
style={style}
|
2021-06-19 21:51:29 +08:00
|
|
|
labelStyle={{ ...rootLabelStyle.value, ...labelStyle }}
|
|
|
|
contentStyle={{ ...rootContentStyle.value, ...contentStyle }}
|
2020-11-02 16:53:56 +08:00
|
|
|
span={span}
|
|
|
|
colon={colon}
|
|
|
|
component={component}
|
|
|
|
itemPrefixCls={itemPrefixCls}
|
|
|
|
bordered={bordered}
|
|
|
|
label={showLabel ? label : null}
|
|
|
|
content={showContent ? children : null}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
<Cell
|
2021-08-10 15:21:13 +08:00
|
|
|
key={`label-${String(key) || index}`}
|
2020-11-02 16:53:56 +08:00
|
|
|
class={className}
|
2021-06-19 21:51:29 +08:00
|
|
|
style={{ ...rootLabelStyle.value, ...style, ...labelStyle }}
|
2020-11-02 16:53:56 +08:00
|
|
|
span={1}
|
|
|
|
colon={colon}
|
|
|
|
component={component[0]}
|
|
|
|
itemPrefixCls={itemPrefixCls}
|
|
|
|
bordered={bordered}
|
|
|
|
label={label}
|
|
|
|
/>,
|
|
|
|
<Cell
|
2021-08-10 15:21:13 +08:00
|
|
|
key={`content-${String(key) || index}`}
|
2020-11-02 16:53:56 +08:00
|
|
|
class={className}
|
2021-06-19 21:51:29 +08:00
|
|
|
style={{ ...rootContentStyle.value, ...style, ...contentStyle }}
|
2020-11-02 16:53:56 +08:00
|
|
|
span={span * 2 - 1}
|
|
|
|
component={component[1]}
|
|
|
|
itemPrefixCls={itemPrefixCls}
|
|
|
|
bordered={bordered}
|
|
|
|
content={children}
|
|
|
|
/>,
|
|
|
|
];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const { prefixCls, vertical, row, index, bordered } = props;
|
2021-06-18 17:45:29 +08:00
|
|
|
const { labelStyle, contentStyle } = inject(descriptionsContext, {
|
2021-06-19 21:51:29 +08:00
|
|
|
labelStyle: ref({}),
|
|
|
|
contentStyle: ref({}),
|
2021-06-18 17:45:29 +08:00
|
|
|
});
|
2020-11-02 16:53:56 +08:00
|
|
|
if (vertical) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<tr key={`label-${index}`} class={`${prefixCls}-row`}>
|
2021-06-18 17:45:29 +08:00
|
|
|
{renderCells(row, props, {
|
|
|
|
component: 'th',
|
|
|
|
type: 'label',
|
|
|
|
showLabel: true,
|
2021-06-19 21:51:29 +08:00
|
|
|
labelStyle,
|
|
|
|
contentStyle,
|
2021-06-18 17:45:29 +08:00
|
|
|
})}
|
2020-11-02 16:53:56 +08:00
|
|
|
</tr>
|
|
|
|
<tr key={`content-${index}`} class={`${prefixCls}-row`}>
|
|
|
|
{renderCells(row, props, {
|
|
|
|
component: 'td',
|
|
|
|
type: 'content',
|
|
|
|
showContent: true,
|
2021-06-19 21:51:29 +08:00
|
|
|
labelStyle,
|
|
|
|
contentStyle,
|
2020-11-02 16:53:56 +08:00
|
|
|
})}
|
|
|
|
</tr>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<tr key={index} class={`${prefixCls}-row`}>
|
|
|
|
{renderCells(row, props, {
|
|
|
|
component: bordered ? ['th', 'td'] : 'td',
|
|
|
|
type: 'item',
|
|
|
|
showLabel: true,
|
|
|
|
showContent: true,
|
2021-06-19 21:51:29 +08:00
|
|
|
labelStyle,
|
|
|
|
contentStyle,
|
2020-11-02 16:53:56 +08:00
|
|
|
})}
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Row;
|