Skip to content

Commit a10eebe

Browse files
committed
refactor(CTable): extract types and utils to separate files, syntax improvements
1 parent e5fe9a9 commit a10eebe

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

packages/coreui-vue/src/components/table/CTable.ts

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { computed, defineComponent, h, PropType } from 'vue'
22

3-
import { Color } from '../props'
43
import { CTableBody } from './CTableBody'
54
import { CTableCaption } from './CTableCaption'
65
import { CTableDataCell } from './CTableDataCell'
@@ -9,43 +8,9 @@ import { CTableHead } from './CTableHead'
98
import { CTableHeaderCell } from './CTableHeaderCell'
109
import { 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-z0-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

5015
const 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(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export type Column = {
2+
label?: string
3+
key: string
4+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5+
_style?: any
6+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7+
_props?: any
8+
}
9+
10+
export type FooterItem = {
11+
label?: string
12+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13+
_props?: any
14+
}
15+
16+
export type Item = {
17+
[key: string]: number | string | any
18+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
19+
_props?: any
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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' ? column.label ?? pretifyName(column.key) : pretifyName(column)
15+
16+
export const getColumnNames = (columns: (string | Column)[] | undefined, items?: Item[]) =>
17+
columns
18+
? columns.map((column: Column | string) => {
19+
return typeof column === 'object' ? column.key : column
20+
})
21+
: items && getColumnNamesFromItems(items)
22+
23+
export const getColumnNamesFromItems = (items: Item[]) =>
24+
Object.keys(items[0] || {}).filter((el) => el.charAt(0) !== '_')

0 commit comments

Comments
 (0)