1
1
import {
2
2
inject ,
3
+ ref ,
3
4
App ,
4
5
defineComponent ,
5
6
PropType ,
6
7
VNode ,
7
- VNodeTypes ,
8
8
HTMLAttributes ,
9
9
ExtractPropTypes ,
10
+ onMounted ,
11
+ onBeforeUnmount ,
10
12
} from 'vue' ;
11
13
import warning from '../_util/warning' ;
12
14
import ResponsiveObserve , {
@@ -17,27 +19,16 @@ import ResponsiveObserve, {
17
19
import { defaultConfigProvider } from '../config-provider' ;
18
20
import Row from './Row' ;
19
21
import PropTypes from '../_util/vue-types' ;
20
- import { getComponent , getSlot } from '../_util/props-util' ;
21
- import BaseMixin from '../_util/BaseMixin' ;
22
22
import { tuple } from '../_util/type' ;
23
23
import { cloneElement } from '../_util/vnode' ;
24
+ import { filterEmpty } from '../_util/props-util' ;
24
25
25
26
export const DescriptionsItemProps = {
26
27
prefixCls : PropTypes . string ,
27
28
label : PropTypes . any ,
28
29
span : PropTypes . number ,
29
30
} ;
30
31
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
-
41
32
export const DescriptionsItem = {
42
33
name : 'ADescriptionsItem' ,
43
34
props : {
@@ -94,8 +85,8 @@ function getFilledItem(node: VNode, span: number | undefined, rowRestCol: number
94
85
return clone ;
95
86
}
96
87
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 ) ;
99
90
const rows : VNode [ ] [ ] = [ ] ;
100
91
101
92
let tmpRow : VNode [ ] = [ ] ;
@@ -146,88 +137,82 @@ export type DescriptionsProps = HTMLAttributes &
146
137
const Descriptions = defineComponent < DescriptionsProps > ( {
147
138
name : 'ADescriptions' ,
148
139
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 ;
169
152
} ) ;
170
153
} ) ;
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 >
207
209
</ 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 >
225
210
</ div >
226
- </ div >
227
- ) ;
211
+ ) ;
212
+ } ;
228
213
} ,
229
214
} ) ;
230
-
215
+
231
216
Descriptions . props = descriptionsProps ;
232
217
233
218
Descriptions . install = function ( app : App ) {
0 commit comments