Skip to content

Commit 7c503bf

Browse files
committed
refactor: syntax formatting and cleaning
1 parent 4134ad6 commit 7c503bf

File tree

3 files changed

+71
-58
lines changed

3 files changed

+71
-58
lines changed

packages/coreui-react/src/components/table/CTable.tsx

+22-58
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import React, { forwardRef, TableHTMLAttributes } from 'react'
1+
import React, { forwardRef, TableHTMLAttributes, useMemo } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44

55
import { CTableHead, CTableHeadProps } from './CTableHead'
6-
import { CTableHeaderCell, CTableHeaderCellProps } from './CTableHeaderCell'
6+
import { CTableHeaderCell } from './CTableHeaderCell'
77
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'
1010
import { CTableFoot, CTableFootProps } from './CTableFoot'
1111
import { CTableCaption } from './CTableCaption'
1212
import { CTableResponsiveWrapper } from './CTableResponsiveWrapper'
1313

1414
import { colorPropType } from '../../props'
1515
import type { Colors } from '../../types'
16+
import { getColumnLabel, getColumnNames } from './utils'
17+
import type { Column, FooterItem, Item } from './types'
1618

1719
export interface CTableProps extends Omit<TableHTMLAttributes<HTMLTableElement>, 'align'> {
1820
/**
@@ -125,23 +127,6 @@ export interface CTableProps extends Omit<TableHTMLAttributes<HTMLTableElement>,
125127
tableHeadProps?: CTableHeadProps
126128
}
127129

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-
145130
export const CTable = forwardRef<HTMLTableElement, CTableProps>(
146131
(
147132
{
@@ -185,29 +170,7 @@ export const CTable = forwardRef<HTMLTableElement, CTableProps>(
185170
className,
186171
)
187172

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-z0-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])
211174

212175
return (
213176
<CTableResponsiveWrapper responsive={responsive}>
@@ -224,7 +187,7 @@ export const CTable = forwardRef<HTMLTableElement, CTableProps>(
224187
{...(column._style && { style: { ...column._style } })}
225188
key={index}
226189
>
227-
{label(column)}
190+
{getColumnLabel(column)}
228191
</CTableHeaderCell>
229192
))}
230193
</CTableRow>
@@ -234,19 +197,20 @@ export const CTable = forwardRef<HTMLTableElement, CTableProps>(
234197
<CTableBody>
235198
{items.map((item: Item, index: number) => (
236199
<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+
})}
250214
</CTableRow>
251215
))}
252216
</CTableBody>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { CTableDataCellProps } from '../table/CTableDataCell'
2+
import { CTableHeaderCellProps } from '../table/CTableHeaderCell'
3+
import { CTableRowProps } from '../table/CTableRow'
4+
5+
export type Column = {
6+
label?: string
7+
key: string
8+
_style?: any
9+
_props?: CTableHeaderCellProps
10+
}
11+
12+
export type FooterItem = {
13+
label?: string
14+
_props?: CTableDataCellProps
15+
}
16+
17+
export type Item = {
18+
[key: string]: number | string | any
19+
_props?: CTableRowProps
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Column, Item } from './types'
2+
3+
export const pretifyName = (name: string) => {
4+
return name
5+
.replace(/[-_.]/g, ' ')
6+
.replace(/ +/g, ' ')
7+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
8+
.split(' ')
9+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
10+
.join(' ')
11+
}
12+
13+
export const getColumnLabel = (column: Column | string) =>
14+
typeof column === 'object'
15+
? column.label !== undefined
16+
? column.label
17+
: pretifyName(column.key)
18+
: pretifyName(column)
19+
20+
export const getColumnNames = (columns: (string | Column)[] | undefined, items?: Item[]) =>
21+
columns
22+
? columns.map((column: Column | string) => {
23+
if (typeof column === 'object') return column.key
24+
else return column
25+
})
26+
: items && getColumnNamesFromItems(items)
27+
28+
export const getColumnNamesFromItems = (items: Item[]) =>
29+
Object.keys(items[0] || {}).filter((el) => el.charAt(0) !== '_')

0 commit comments

Comments
 (0)