feat: update descriptions (#2385)

* feat: update descriptions

* fix: merge Col props
pull/2415/head
言肆 2020-06-11 22:35:09 +08:00 committed by GitHub
parent e2ce58fa7a
commit e5924d5ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 72 deletions

View File

@ -1,5 +1,5 @@
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import { getOptionProps, getSlots, getComponentFromProp } from '../_util/props-util'; import { getOptionProps } from '../_util/props-util';
const ColProps = { const ColProps = {
child: PropTypes.any, child: PropTypes.any,
@ -9,17 +9,21 @@ const ColProps = {
layout: PropTypes.oneOf(['horizontal', 'vertical']), layout: PropTypes.oneOf(['horizontal', 'vertical']),
}; };
const Col = { const Col = (_, { attrs }) => {
functional: true, // props: {
props: ColProps, // child: PropTypes.any,
render(h, ctx) { // bordered: PropTypes.bool,
const { child, bordered, colon, type, layout } = ctx.props; // colon: PropTypes.bool,
// type: PropTypes.oneOf(['label', 'content']),
// layout: PropTypes.oneOf(['horizontal', 'vertical']),
// }
const { child, bordered, colon, type, layout } = attrs;
const { prefixCls, span = 1 } = getOptionProps(child); const { prefixCls, span = 1 } = getOptionProps(child);
const { key } = ctx.data; const { key, children = {} } = child || {};
const label = getComponentFromProp(child, 'label'); const label = children.label && children.label();
const slots = getSlots(child); const defaultSlot = children.default && children.default();
const labelProps = {
attrs: {}, const someLabelProps = {
class: [ class: [
`${prefixCls}-item-label`, `${prefixCls}-item-label`,
{ {
@ -29,17 +33,18 @@ const Col = {
], ],
key: `${key}-label`, key: `${key}-label`,
}; };
if (layout === 'vertical') { if (layout === 'vertical') {
labelProps.attrs.colSpan = span * 2 - 1; someLabelProps.colSpan = span * 2 - 1;
} }
if (bordered) { if (bordered) {
if (type === 'label') { if (type === 'label') {
return <th {...labelProps}>{label}</th>; return <th {...someLabelProps}>{label}</th>;
} }
return ( return (
<td class={`${prefixCls}-item-content`} key={`${key}-content`} colSpan={span * 2 - 1}> <td class={`${prefixCls}-item-content`} key={`${key}-content`} colSpan={span * 2 - 1}>
{slots.default} {defaultSlot}
</td> </td>
); );
} }
@ -48,7 +53,7 @@ const Col = {
return ( return (
<td colSpan={span} class={`${prefixCls}-item`}> <td colSpan={span} class={`${prefixCls}-item`}>
<span class={`${prefixCls}-item-content`} key={`${key}-content`}> <span class={`${prefixCls}-item-content`} key={`${key}-content`}>
{slots.default} {defaultSlot}
</span> </span>
</td> </td>
); );
@ -66,13 +71,12 @@ const Col = {
} }
return ( return (
<td colSpan={span} class={`${prefixCls}-item`}> <td colSpan={span} class={`${prefixCls}-item`}>
<span {...labelProps}>{label}</span> <span {...someLabelProps}>{label}</span>
<span class={`${prefixCls}-item-content`} key={`${key}-content`}> <span class={`${prefixCls}-item-content`} key={`${key}-content`}>
{slots.default} {defaultSlot}
</span> </span>
</td> </td>
); );
},
}; };
export default Col; export default Col;

View File

@ -1,17 +1,11 @@
import { inject, isVNode, cloneVNode } from 'vue';
import warning from '../_util/warning'; import warning from '../_util/warning';
import ResponsiveObserve, { responsiveArray } from '../_util/responsiveObserve'; import ResponsiveObserve, { responsiveArray } from '../_util/responsiveObserve';
import { ConfigConsumerProps } from '../config-provider'; import { ConfigConsumerProps } from '../config-provider';
import Col from './Col'; import Col from './Col';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import { import { initDefaultProps, getOptionProps, getComponent } from '../_util/props-util';
initDefaultProps,
isValidElement,
getOptionProps,
getComponentFromProp,
} from '../_util/props-util';
import BaseMixin from '../_util/BaseMixin'; import BaseMixin from '../_util/BaseMixin';
import Base from '../base';
import { cloneElement } from '../_util/vnode';
export const DescriptionsItemProps = { export const DescriptionsItemProps = {
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
@ -70,7 +64,7 @@ const generateChildrenRows = (children, column) => {
let lastSpanSame = true; let lastSpanSame = true;
if (lastItem) { if (lastItem) {
lastSpanSame = !itemProps.span || itemProps.span === leftSpans; lastSpanSame = !itemProps.span || itemProps.span === leftSpans;
itemNode = cloneElement(itemNode, { itemNode = cloneVNode(itemNode, {
props: { props: {
span: leftSpans, span: leftSpans,
}, },
@ -109,12 +103,14 @@ const Descriptions = {
name: 'ADescriptions', name: 'ADescriptions',
Item: DescriptionsItem, Item: DescriptionsItem,
mixins: [BaseMixin], mixins: [BaseMixin],
inject: {
configProvider: { default: () => ConfigConsumerProps },
},
props: initDefaultProps(DescriptionsProps, { props: initDefaultProps(DescriptionsProps, {
column: defaultColumnMap, column: defaultColumnMap,
}), }),
setup() {
return {
configProvider: inject('configProvider', ConfigConsumerProps),
};
},
data() { data() {
return { return {
screens: {}, screens: {},
@ -205,16 +201,16 @@ const Descriptions = {
layout = 'horizontal', layout = 'horizontal',
colon = true, colon = true,
} = this.$props; } = this.$props;
const title = getComponentFromProp(this, 'title') || null; const title = getComponent(this, 'title') || null;
const getPrefixCls = this.configProvider.getPrefixCls; const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('descriptions', customizePrefixCls); const prefixCls = getPrefixCls('descriptions', customizePrefixCls);
const column = this.getColumn(); const column = this.getColumn();
const children = this.$slots.default; const children = this.$slots.default && this.$slots.default();
const cloneChildren = toArray(children) const cloneChildren = toArray(children)
.map(child => { .map(child => {
if (isValidElement(child)) { if (isVNode(child)) {
return cloneElement(child, { return cloneVNode(child, {
props: { props: {
prefixCls, prefixCls,
}, },
@ -259,10 +255,9 @@ const Descriptions = {
}, },
}; };
Descriptions.install = function(Vue) { Descriptions.install = function(app) {
Vue.use(Base); app.component(Descriptions.name, Descriptions);
Vue.component(Descriptions.name, Descriptions); app.component(Descriptions.Item.name, Descriptions.Item);
Vue.component(Descriptions.Item.name, Descriptions.Item);
}; };
export default Descriptions; export default Descriptions;