1
- import React , { forwardRef , TableHTMLAttributes } from 'react'
1
+ import React , { forwardRef , TableHTMLAttributes , useMemo } from 'react'
2
2
import PropTypes from 'prop-types'
3
3
import classNames from 'classnames'
4
4
5
5
import { CTableHead , CTableHeadProps } from './CTableHead'
6
- import { CTableHeaderCell , CTableHeaderCellProps } from './CTableHeaderCell'
6
+ import { CTableHeaderCell } from './CTableHeaderCell'
7
7
import { CTableBody } from './CTableBody'
8
- import { CTableDataCell , CTableDataCellProps } from './CTableDataCell'
9
- import { CTableRow , CTableRowProps } from './CTableRow'
8
+ import { CTableDataCell } from './CTableDataCell'
9
+ import { CTableRow } from './CTableRow'
10
10
import { CTableFoot , CTableFootProps } from './CTableFoot'
11
11
import { CTableCaption } from './CTableCaption'
12
12
import { CTableResponsiveWrapper } from './CTableResponsiveWrapper'
13
13
14
14
import { colorPropType } from '../../props'
15
15
import type { Colors } from '../../types'
16
+ import { getColumnLabel , getColumnNames } from './utils'
17
+ import type { Column , FooterItem , Item } from './types'
16
18
17
19
export interface CTableProps extends Omit < TableHTMLAttributes < HTMLTableElement > , 'align' > {
18
20
/**
@@ -125,23 +127,6 @@ export interface CTableProps extends Omit<TableHTMLAttributes<HTMLTableElement>,
125
127
tableHeadProps ?: CTableHeadProps
126
128
}
127
129
128
- export interface Column {
129
- label ?: string
130
- key : string
131
- _style ?: any
132
- _props ?: CTableHeaderCellProps
133
- }
134
-
135
- export interface Item {
136
- [ key : string ] : number | string | any
137
- _props ?: CTableRowProps
138
- }
139
-
140
- export interface FooterItem {
141
- label ?: string
142
- _props ?: CTableDataCellProps
143
- }
144
-
145
130
export const CTable = forwardRef < HTMLTableElement , CTableProps > (
146
131
(
147
132
{
@@ -185,29 +170,7 @@ export const CTable = forwardRef<HTMLTableElement, CTableProps>(
185
170
className ,
186
171
)
187
172
188
- const rawColumnNames = columns
189
- ? columns . map ( ( column : Column ) => {
190
- if ( typeof column === 'object' ) return column . key
191
- else return column
192
- } )
193
- : Object . keys ( ( items && items [ 0 ] ) || { } ) . filter ( ( el ) => el . charAt ( 0 ) !== '_' )
194
-
195
- const pretifyName = ( name : string ) => {
196
- return name
197
- . replace ( / [ - _ . ] / g, ' ' )
198
- . replace ( / + / g, ' ' )
199
- . replace ( / ( [ a - z 0 - 9 ] ) ( [ A - Z ] ) / g, '$1 $2' )
200
- . split ( ' ' )
201
- . map ( ( word ) => word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) )
202
- . join ( ' ' )
203
- }
204
-
205
- const label = ( column : Column | string ) =>
206
- typeof column === 'object'
207
- ? column . label !== undefined
208
- ? column . label
209
- : pretifyName ( column . key )
210
- : pretifyName ( column )
173
+ const columnNames = useMemo ( ( ) => getColumnNames ( columns , items ) , [ columns , items ] )
211
174
212
175
return (
213
176
< CTableResponsiveWrapper responsive = { responsive } >
@@ -224,7 +187,7 @@ export const CTable = forwardRef<HTMLTableElement, CTableProps>(
224
187
{ ...( column . _style && { style : { ...column . _style } } ) }
225
188
key = { index }
226
189
>
227
- { label ( column ) }
190
+ { getColumnLabel ( column ) }
228
191
</ CTableHeaderCell >
229
192
) ) }
230
193
</ CTableRow >
@@ -234,19 +197,20 @@ export const CTable = forwardRef<HTMLTableElement, CTableProps>(
234
197
< CTableBody >
235
198
{ items . map ( ( item : Item , index : number ) => (
236
199
< CTableRow { ...( item . _props && { ...item . _props } ) } key = { index } >
237
- { rawColumnNames . map ( ( colName : string , index : number ) => {
238
- return item [ colName ] ? (
239
- < CTableDataCell
240
- { ...( item . _cellProps && {
241
- ...( item . _cellProps [ 'all' ] && { ...item . _cellProps [ 'all' ] } ) ,
242
- ...( item . _cellProps [ colName ] && { ...item . _cellProps [ colName ] } ) ,
243
- } ) }
244
- key = { index }
245
- >
246
- { item [ colName ] }
247
- </ CTableDataCell >
248
- ) : null
249
- } ) }
200
+ { columnNames &&
201
+ columnNames . map ( ( colName : string , index : number ) => {
202
+ return item [ colName ] ? (
203
+ < CTableDataCell
204
+ { ...( item . _cellProps && {
205
+ ...( item . _cellProps [ 'all' ] && { ...item . _cellProps [ 'all' ] } ) ,
206
+ ...( item . _cellProps [ colName ] && { ...item . _cellProps [ colName ] } ) ,
207
+ } ) }
208
+ key = { index }
209
+ >
210
+ { item [ colName ] }
211
+ </ CTableDataCell >
212
+ ) : null
213
+ } ) }
250
214
</ CTableRow >
251
215
) ) }
252
216
</ CTableBody >
0 commit comments