Commit 819127e807123cccc7ae50f0fdffb43a662465d4

Authored by vben
1 parent b71e4e51

fix: fix descriotions title not work

src/components/Description/index.ts
1   -export { default as Description } from './src/index';
  1 +import DescriptionLib from './src/index';
  2 +
  3 +import { withInstall } from '../util';
  4 +
2 5 export * from './src/types';
3 6 export { useDescription } from './src/useDescription';
  7 +
  8 +export const Description = withInstall(DescriptionLib);
... ...
src/components/Description/src/index.tsx
... ... @@ -2,7 +2,9 @@ import type { DescOptions, DescInstance, DescItem } from './types';
2 2  
3 3 import { defineComponent, computed, ref, unref, CSSProperties } from 'vue';
4 4 import { Descriptions } from 'ant-design-vue';
  5 +import { DescriptionsProps } from 'ant-design-vue/es/descriptions/index';
5 6 import { CollapseContainer, CollapseContainerOptions } from '/@/components/Container/index';
  7 +
6 8 import descProps from './props';
7 9  
8 10 import { isFunction } from '/@/utils/is';
... ... @@ -17,29 +19,27 @@ export default defineComponent({
17 19 emits: ['register'],
18 20 setup(props, { attrs, slots, emit }) {
19 21 const propsRef = ref<Partial<DescOptions> | null>(null);
  22 +
20 23 // Custom title component: get title
21 24 const getMergeProps = computed(() => {
22 25 return {
23 26 ...props,
24   - ...unref(propsRef),
25   - };
  27 + ...(unref(propsRef) as any),
  28 + } as DescOptions;
26 29 });
27 30  
28 31 const getProps = computed(() => {
29 32 const opt = {
30   - ...props,
31   - ...(unref(propsRef) || {}),
  33 + ...unref(getMergeProps),
32 34 title: undefined,
33 35 };
34   - return opt;
  36 + return opt as DescOptions;
35 37 });
36 38  
37 39 /**
38 40 * @description: Whether to setting title
39 41 */
40   - const useWrapper = computed(() => {
41   - return !!unref(getMergeProps).title;
42   - });
  42 + const useWrapper = computed(() => !!unref(getMergeProps).title);
43 43  
44 44 /**
45 45 * @description: Get configuration Collapse
... ... @@ -54,6 +54,10 @@ export default defineComponent({
54 54 }
55 55 );
56 56  
  57 + const getDescriptionsProps = computed(() => {
  58 + return { ...attrs, ...unref(getProps) } as DescriptionsProps;
  59 + });
  60 +
57 61 /**
58 62 * @description:设置desc
59 63 */
... ... @@ -63,12 +67,6 @@ export default defineComponent({
63 67 propsRef.value = cloneDeep(mergeProps);
64 68 }
65 69  
66   - const methods: DescInstance = {
67   - setDescProps,
68   - };
69   -
70   - emit('register', methods);
71   -
72 70 // Prevent line breaks
73 71 function renderLabel({ label, labelMinWidth, labelStyle }: DescItem) {
74 72 if (!labelStyle && !labelMinWidth) {
... ... @@ -87,33 +85,27 @@ export default defineComponent({
87 85 const { schema } = unref(getProps);
88 86 return unref(schema).map((item) => {
89 87 const { render, field, span, show, contentMinWidth } = item;
90   - const { data } = unref(getProps) as any;
  88 + const { data } = unref(getProps) as DescOptions;
91 89  
92 90 if (show && isFunction(show) && !show(data)) {
93 91 return null;
94 92 }
95 93  
96 94 const getContent = () =>
97   - isFunction(render)
98   - ? render(data && data[field], data)
99   - : unref(data) && unref(data)[field];
  95 + isFunction(render) ? render(data?.[field], data) : unref(data) && unref(data)[field];
100 96  
101 97 const width = contentMinWidth;
102 98 return (
103 99 <Descriptions.Item label={renderLabel(item)} key={field} span={span}>
104   - {() =>
105   - contentMinWidth ? (
106   - <div
107   - style={{
108   - minWidth: `${width}px`,
109   - }}
110   - >
111   - {getContent()}
112   - </div>
113   - ) : (
114   - getContent()
115   - )
116   - }
  100 + {() => {
  101 + if (!contentMinWidth) {
  102 + return getContent();
  103 + }
  104 + const style: CSSProperties = {
  105 + minWidth: `${width}px`,
  106 + };
  107 + return <div style={style}>{getContent()}</div>;
  108 + }}
117 109 </Descriptions.Item>
118 110 );
119 111 });
... ... @@ -121,7 +113,7 @@ export default defineComponent({
121 113  
122 114 const renderDesc = () => {
123 115 return (
124   - <Descriptions class={`${prefixCls}`} {...{ ...attrs, ...(unref(getProps) as any) }}>
  116 + <Descriptions class={`${prefixCls}`} {...(unref(getDescriptionsProps) as any)}>
125 117 {() => renderItem()}
126 118 </Descriptions>
127 119 );
... ... @@ -130,19 +122,29 @@ export default defineComponent({
130 122 const renderContainer = () => {
131 123 const content = props.useCollapse ? renderDesc() : <div>{renderDesc()}</div>;
132 124 // Reduce the dom level
133   - const { title, canExpand, helpMessage } = unref(getCollapseOptions);
134   - return props.useCollapse ? (
  125 +
  126 + if (!props.useCollapse) {
  127 + return content;
  128 + }
  129 +
  130 + const { canExpand, helpMessage } = unref(getCollapseOptions);
  131 + const { title } = unref(getMergeProps);
  132 +
  133 + return (
135 134 <CollapseContainer title={title} canExpan={canExpand} helpMessage={helpMessage}>
136 135 {{
137 136 default: () => content,
138 137 action: () => getSlot(slots, 'action'),
139 138 }}
140 139 </CollapseContainer>
141   - ) : (
142   - content
143 140 );
144 141 };
145 142  
  143 + const methods: DescInstance = {
  144 + setDescProps,
  145 + };
  146 +
  147 + emit('register', methods);
146 148 return () => (unref(useWrapper) ? renderContainer() : renderDesc());
147 149 },
148 150 });
... ...
src/components/Description/src/props.ts
... ... @@ -5,15 +5,20 @@ import type { DescItem } from &#39;./types&#39;;
5 5 import { propTypes } from '/@/utils/propTypes';
6 6 export default {
7 7 useCollapse: propTypes.bool.def(true),
  8 +
8 9 title: propTypes.string.def(''),
  10 +
9 11 size: propTypes.oneOf(['small', 'default', 'middle', undefined]).def('small'),
  12 +
10 13 bordered: propTypes.bool.def(true),
  14 +
11 15 column: {
12 16 type: [Number, Object] as PropType<number | any>,
13 17 default: () => {
14 18 return { xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 };
15 19 },
16 20 },
  21 +
17 22 collapseOptions: {
18 23 type: Object as PropType<CollapseContainerOptions>,
19 24 default: null,
... ...
src/components/Description/src/types.ts
1   -import type { VNode } from 'vue';
  1 +import type { VNode, CSSProperties } from 'vue';
2 2 import type { CollapseContainerOptions } from '/@/components/Container/index';
  3 +import type { DescriptionsProps } from 'ant-design-vue/es/descriptions/index';
3 4  
4 5 export interface DescItem {
5 6 labelMinWidth?: number;
6 7  
7 8 contentMinWidth?: number;
8 9  
9   - labelStyle?: any;
  10 + labelStyle?: CSSProperties;
10 11  
11 12 field: string;
12   - label: any;
  13 + label: string | VNode | JSX.Element;
13 14 // Merge column
14 15 span?: number;
15 16 show?: (...arg: any) => boolean;
16 17 // render
17   - render?: (val: string, data: any) => VNode | undefined | Element | string | number;
  18 + render?: (val: string, data: any) => VNode | undefined | JSX.Element | Element | string | number;
18 19 }
19 20  
20   -export interface DescOptions {
  21 +export interface DescOptions extends DescriptionsProps {
21 22 // Whether to include the collapse component
22 23 useCollapse?: boolean;
23 24 /**
... ... @@ -35,52 +36,6 @@ export interface DescOptions {
35 36 * @type CollapseContainerOptions
36 37 */
37 38 collapseOptions?: CollapseContainerOptions;
38   - /**
39   - * descriptions size type
40   - * @default 'default'
41   - * @type string
42   - */
43   - size?: 'default' | 'middle' | 'small';
44   -
45   - /**
46   - * custom prefixCls
47   - * @type string
48   - */
49   - prefixCls?: string;
50   -
51   - /**
52   - * whether descriptions have border
53   - * @default false
54   - * @type boolean
55   - */
56   - bordered?: boolean;
57   -
58   - /**
59   - * custom title
60   - * @type any
61   - */
62   - title?: any;
63   -
64   - /**
65   - * the number of descriptionsitem in one line
66   - * @default 3
67   - * @type number | object
68   - */
69   - column?: number | object;
70   -
71   - /**
72   - * descriptions layout
73   - * @default 'horizontal'
74   - * @type string
75   - */
76   - layout?: 'horizontal' | 'vertical';
77   -
78   - /**
79   - * whether have colon in descriptionsitem
80   - * @default true
81   - * @type boolean
82   - */
83   - colon?: boolean;
84 39 }
85 40  
86 41 export interface DescInstance {
... ...