Skip to content

Commit 0bf666f

Browse files
committed
refactor: descriptions use composition-api
1 parent f5ad748 commit 0bf666f

File tree

1 file changed

+76
-91
lines changed

1 file changed

+76
-91
lines changed

components/descriptions/index.tsx

+76-91
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {
22
inject,
3+
ref,
34
App,
45
defineComponent,
56
PropType,
67
VNode,
7-
VNodeTypes,
88
HTMLAttributes,
99
ExtractPropTypes,
10+
onMounted,
11+
onBeforeUnmount,
1012
} from 'vue';
1113
import warning from '../_util/warning';
1214
import ResponsiveObserve, {
@@ -17,27 +19,16 @@ import ResponsiveObserve, {
1719
import { defaultConfigProvider } from '../config-provider';
1820
import Row from './Row';
1921
import PropTypes from '../_util/vue-types';
20-
import { getComponent, getSlot } from '../_util/props-util';
21-
import BaseMixin from '../_util/BaseMixin';
2222
import { tuple } from '../_util/type';
2323
import { cloneElement } from '../_util/vnode';
24+
import { filterEmpty } from '../_util/props-util';
2425

2526
export const DescriptionsItemProps = {
2627
prefixCls: PropTypes.string,
2728
label: PropTypes.any,
2829
span: PropTypes.number,
2930
};
3031

31-
function toArray(value: any) {
32-
let ret = value;
33-
if (value === undefined) {
34-
ret = [];
35-
} else if (!Array.isArray(value)) {
36-
ret = [value];
37-
}
38-
return ret;
39-
}
40-
4132
export const DescriptionsItem = {
4233
name: 'ADescriptionsItem',
4334
props: {
@@ -94,8 +85,8 @@ function getFilledItem(node: VNode, span: number | undefined, rowRestCol: number
9485
return clone;
9586
}
9687

97-
function getRows(children: VNodeTypes, column: number) {
98-
const childNodes = toArray(children).filter(n => n);
88+
function getRows(children: VNode[], column: number) {
89+
const childNodes = filterEmpty(children);
9990
const rows: VNode[][] = [];
10091

10192
let tmpRow: VNode[] = [];
@@ -146,88 +137,82 @@ export type DescriptionsProps = HTMLAttributes &
146137
const Descriptions = defineComponent<DescriptionsProps>({
147138
name: 'ADescriptions',
148139
Item: DescriptionsItem,
149-
mixins: [BaseMixin],
150-
setup() {
151-
return {
152-
configProvider: inject('configProvider', defaultConfigProvider),
153-
};
154-
},
155-
data() {
156-
return {
157-
screens: {},
158-
token: undefined,
159-
};
160-
},
161-
mounted() {
162-
const { column } = this.$props;
163-
this.token = ResponsiveObserve.subscribe(screens => {
164-
if (typeof column !== 'object') {
165-
return;
166-
}
167-
this.setState({
168-
screens,
140+
setup(props, { slots }) {
141+
let token: number;
142+
143+
const screens = ref<ScreenMap>({});
144+
145+
onMounted(() => {
146+
token = ResponsiveObserve.subscribe(screen => {
147+
if (typeof props.column !== 'object') {
148+
return;
149+
}
150+
151+
screens.value = screen;
169152
});
170153
});
171-
},
172-
beforeUnmount() {
173-
ResponsiveObserve.unsubscribe(this.token);
174-
},
175-
render() {
176-
const {
177-
prefixCls: customizePrefixCls,
178-
column,
179-
size,
180-
bordered = false,
181-
layout = 'horizontal',
182-
colon = true,
183-
} = this.$props;
184-
const title = getComponent(this, 'title');
185-
const extra = getComponent(this, 'extra');
186-
187-
const getPrefixCls = this.configProvider.getPrefixCls;
188-
const prefixCls = getPrefixCls('descriptions', customizePrefixCls);
189-
const mergeColumn = getColumn(column, this.screens);
190-
const children = getSlot(this);
191-
const rows = getRows(children, mergeColumn);
192-
193-
return (
194-
<div
195-
class={[
196-
prefixCls,
197-
{
198-
[`${prefixCls}-${size}`]: size !== 'default',
199-
[`${prefixCls}-bordered`]: !!bordered,
200-
},
201-
]}
202-
>
203-
{(title || extra) && (
204-
<div class={`${prefixCls}-header`}>
205-
<div class={`${prefixCls}-title`}>{title}</div>
206-
<div class={`${prefixCls}-extra`}>{extra}</div>
154+
155+
onBeforeUnmount(() => {
156+
ResponsiveObserve.unsubscribe(token);
157+
});
158+
159+
return () => {
160+
const {
161+
prefixCls: customizePrefixCls,
162+
column,
163+
size,
164+
bordered = false,
165+
layout = 'horizontal',
166+
colon = true,
167+
title = slots.title?.() || undefined,
168+
extra = slots.extra?.() || undefined,
169+
} = props;
170+
171+
const { getPrefixCls } = inject('configProvider', defaultConfigProvider);
172+
const prefixCls = getPrefixCls('descriptions', customizePrefixCls);
173+
const mergeColumn = getColumn(column, screens.value);
174+
const children = slots.default?.();
175+
const rows = getRows(children, mergeColumn);
176+
177+
return (
178+
<div
179+
class={[
180+
prefixCls,
181+
{
182+
[`${prefixCls}-${size}`]: size !== 'default',
183+
[`${prefixCls}-bordered`]: !!bordered,
184+
},
185+
]}
186+
>
187+
{(title || extra) && (
188+
<div class={`${prefixCls}-header`}>
189+
<div class={`${prefixCls}-title`}>{title}</div>
190+
<div class={`${prefixCls}-extra`}>{extra}</div>
191+
</div>
192+
)}
193+
<div class={`${prefixCls}-view`}>
194+
<table>
195+
<tbody>
196+
{rows.map((row, index) => (
197+
<Row
198+
key={index}
199+
index={index}
200+
colon={colon}
201+
prefixCls={prefixCls}
202+
vertical={layout === 'vertical'}
203+
bordered={bordered}
204+
row={row}
205+
/>
206+
))}
207+
</tbody>
208+
</table>
207209
</div>
208-
)}
209-
<div class={`${prefixCls}-view`}>
210-
<table>
211-
<tbody>
212-
{rows.map((row, index) => (
213-
<Row
214-
key={index}
215-
index={index}
216-
colon={colon}
217-
prefixCls={prefixCls}
218-
vertical={layout === 'vertical'}
219-
bordered={bordered}
220-
row={row}
221-
/>
222-
))}
223-
</tbody>
224-
</table>
225210
</div>
226-
</div>
227-
);
211+
);
212+
};
228213
},
229214
});
230-
215+
231216
Descriptions.props = descriptionsProps;
232217

233218
Descriptions.install = function(app: App) {

0 commit comments

Comments
 (0)