11import { computed , defineComponent , h , PropType } from 'vue'
22
3- import { Color } from '../props'
43import { CTableBody } from './CTableBody'
54import { CTableCaption } from './CTableCaption'
65import { CTableDataCell } from './CTableDataCell'
@@ -9,43 +8,9 @@ import { CTableHead } from './CTableHead'
98import { CTableHeaderCell } from './CTableHeaderCell'
109import { CTableRow } from './CTableRow'
1110
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'
4914
5015const CTable = defineComponent ( {
5116 name : 'CTable' ,
@@ -183,14 +148,14 @@ const CTable = defineComponent({
183148 } ,
184149 } ,
185150 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+ // )
194159
195160 const table = ( ) =>
196161 h (
@@ -247,7 +212,7 @@ const CTable = defineComponent({
247212 column . _style && { style : { ...column . _style } } ) ,
248213 } ,
249214 {
250- default : ( ) => label ( column ) ,
215+ default : ( ) => getColumnLabel ( column ) ,
251216 } ,
252217 ) ,
253218 ) ,
@@ -271,7 +236,7 @@ const CTable = defineComponent({
271236 } ,
272237 {
273238 default : ( ) => [
274- rawColumnNames . value . map (
239+ columnNames . value && columnNames . value . map (
275240 ( colName : string ) =>
276241 item [ colName ] &&
277242 h (
0 commit comments