1
1
import { computed , defineComponent , h , PropType } from 'vue'
2
2
3
- import { Color } from '../props'
4
3
import { CTableBody } from './CTableBody'
5
4
import { CTableCaption } from './CTableCaption'
6
5
import { CTableDataCell } from './CTableDataCell'
@@ -9,43 +8,9 @@ import { CTableHead } from './CTableHead'
9
8
import { CTableHeaderCell } from './CTableHeaderCell'
10
9
import { CTableRow } from './CTableRow'
11
10
12
- export interface Column {
13
- label ?: string
14
- key : string
15
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
- _style ?: any
17
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
- _props ?: any
19
- }
20
-
21
- export interface FooterItem {
22
- label ?: string
23
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
- _props ?: any
25
- }
26
-
27
- export type Item = {
28
- [ key : string ] : number | string | any
29
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
- _props ?: any
31
- }
32
-
33
- const pretifyName = ( name : string ) => {
34
- return name
35
- . replace ( / [ - _ . ] / g, ' ' )
36
- . replace ( / + / g, ' ' )
37
- . replace ( / ( [ a - z 0 - 9 ] ) ( [ A - Z ] ) / g, '$1 $2' )
38
- . split ( ' ' )
39
- . map ( ( word ) => word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) )
40
- . join ( ' ' )
41
- }
42
-
43
- const label = ( column : Column | string ) =>
44
- typeof column === 'object'
45
- ? column . label !== undefined
46
- ? column . label
47
- : pretifyName ( column . key )
48
- : pretifyName ( column )
11
+ import { Color } from '../props'
12
+ import { getColumnLabel , getColumnNames } from './utils'
13
+ import type { Column , FooterItem , Item } from './types'
49
14
50
15
const CTable = defineComponent ( {
51
16
name : 'CTable' ,
@@ -183,14 +148,14 @@ const CTable = defineComponent({
183
148
} ,
184
149
} ,
185
150
setup ( props , { slots, attrs } ) {
186
- const rawColumnNames = computed ( ( ) =>
187
- props . columns
188
- ? props . columns . map ( ( column : Column | string ) => {
189
- if ( typeof column === 'object' ) return column . key
190
- else return column
191
- } )
192
- : Object . keys ( ( props . items && props . items [ 0 ] ) || { } ) . filter ( ( el ) => el . charAt ( 0 ) !== '_' ) ,
193
- )
151
+ const columnNames = computed ( ( ) => getColumnNames ( props . columns , props . items ) )
152
+ // props.columns
153
+ // ? props.columns.map((column: Column | string) => {
154
+ // if (typeof column === 'object') return column.key
155
+ // else return column
156
+ // })
157
+ // : Object.keys((props.items && props.items[0]) || {}).filter((el) => el.charAt(0) !== '_'),
158
+ // )
194
159
195
160
const table = ( ) =>
196
161
h (
@@ -247,7 +212,7 @@ const CTable = defineComponent({
247
212
column . _style && { style : { ...column . _style } } ) ,
248
213
} ,
249
214
{
250
- default : ( ) => label ( column ) ,
215
+ default : ( ) => getColumnLabel ( column ) ,
251
216
} ,
252
217
) ,
253
218
) ,
@@ -271,7 +236,7 @@ const CTable = defineComponent({
271
236
} ,
272
237
{
273
238
default : ( ) => [
274
- rawColumnNames . value . map (
239
+ columnNames . value && columnNames . value . map (
275
240
( colName : string ) =>
276
241
item [ colName ] &&
277
242
h (
0 commit comments