Skip to content

Commit 0792b82

Browse files
committed
chore: refactor
1 parent cdafc5a commit 0792b82

File tree

4 files changed

+49
-40
lines changed

4 files changed

+49
-40
lines changed

src/components/Table.tsx

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useStore, useTask$, component$ } from "@builder.io/qwik"
22
import SortArrows from "./SortArrows"
33
import FilterInput from "./FilterInput"
4-
import { param2string, sortTable, filterTable } from "../utils"
4+
import { param2string, sortTable } from "../utils"
55
import type {
66
TableProps,
77
TableRecord,
@@ -31,8 +31,8 @@ export default component$(
3131
tData: tData,
3232
tHeadings: {
3333
headingList: Object.keys(tData[0]),
34-
customHeadings: { ...tHeadings.customHeadings },
35-
...tHeadings,
34+
customHeadings: { ...tHeadings?.customHeadings },
35+
...tHeadings!,
3636
},
3737
tColumns: {
3838
...tColumns,
@@ -56,11 +56,19 @@ export default component$(
5656
useTask$(async ({ track }) => {
5757
track(() => [sortConfigs.param, sortConfigs.order, filterConfigs.params])
5858

59-
const filteredData = await filterTable(
60-
tData,
61-
tableData.tHeadings.headingList,
62-
filterConfigs
63-
)
59+
const filteredData = tData.filter((record: TableRecord) => {
60+
let i = 0
61+
tableData.tHeadings.headingList.map((param: string) => {
62+
if (
63+
record[param]
64+
?.toString()
65+
.toLowerCase()
66+
.includes(filterConfigs.params[param]?.toString().toLowerCase())
67+
)
68+
++i
69+
})
70+
if (i === Object.keys(filterConfigs?.params).length) return record
71+
})
6472

6573
if (sortConfigs.order === 0) {
6674
tableData.tData = filteredData
@@ -82,10 +90,10 @@ export default component$(
8290
<th
8391
key={index}
8492
class={`
85-
${tableData.tHeadings.classList}
86-
${tableData.tColumns?.classList}
87-
${tableData.tColumns?.columnClassList?.[heading]}
88-
`}
93+
${tableData.tHeadings.classList}
94+
${tableData.tColumns?.classList}
95+
${tableData.tColumns?.columnClassList?.[heading]}
96+
`}
8997
>
9098
<div class="flex flex-col">
9199
<div class="flex flex-row items-center justify-between gap-[10px]">
@@ -95,16 +103,22 @@ export default component$(
95103
? tableData.tHeadings.element$[heading](
96104
tableData.tHeadings.customHeadings?.[heading]!
97105
) || tableData.tHeadings.customHeadings?.[heading]
98-
: tableData.tHeadings.element$[heading](
106+
: tableData.tHeadings.element$?.[heading]?.(
99107
param2string(heading)
100108
) || param2string(heading)}
101109
{tableData.sortOptions?.params?.includes(heading) && (
102110
<SortArrows
103111
heading={heading}
104112
classList={{
105-
container: "",
106-
arrowUp: "",
107-
arrowDown: "",
113+
container:
114+
tableData.tableOptions?.sortArrowsClassList
115+
?.container,
116+
arrowUp:
117+
tableData.tableOptions?.sortArrowsClassList
118+
?.arrowUp,
119+
arrowDown:
120+
tableData.tableOptions?.sortArrowsClassList
121+
?.arrowDown,
108122
}}
109123
sortConfigs={sortConfigs}
110124
highlightColor={tableData.sortOptions.highlightColor}
@@ -118,7 +132,9 @@ export default component$(
118132
{tableData.filterOptions?.params?.[heading] ===
119133
"search" ? (
120134
<FilterInput
121-
classList={""}
135+
classList={
136+
tableData.tableOptions?.filterInputClassList
137+
}
122138
heading={heading}
123139
filterConfigs={filterConfigs}
124140
/>

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
export * from "./components/Table"
1+
import Table from "./components/Table"
2+
3+
export default Table
24
export * from "./types"

src/types/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ export type TableData = {
3333
}
3434
tableOptions?: {
3535
classList?: string
36+
sortArrowsClassList?: {
37+
container?: string
38+
arrowUp?: string
39+
arrowDown?: string
40+
}
41+
filterInputClassList?: string
3642
}
3743
sortOptions?: {
3844
params?: Array<string>
@@ -86,6 +92,12 @@ export type TableProps = {
8692
}
8793
tableOptions?: {
8894
classList?: string
95+
sortArrowsClassList?: {
96+
container?: string
97+
arrowUp?: string
98+
arrowDown?: string
99+
}
100+
filterInputClassList?: string
89101
}
90102
sortOptions?: {
91103
params?: Array<string>

src/utils/table.utils.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { $ } from "@builder.io/qwik"
2-
import type { TableRecord, FilterConfigs } from "../types"
2+
import type { TableRecord } from "../types"
33

44
export const sortTable = $(
55
(data: Array<TableRecord>, param: string, isAscending: boolean) => {
@@ -18,27 +18,6 @@ export const sortTable = $(
1818
}
1919
)
2020

21-
export const filterTable = $(
22-
(
23-
records: Array<TableRecord>,
24-
headings: Array<string>,
25-
filterConfigs: FilterConfigs
26-
) =>
27-
records.filter((record: TableRecord) => {
28-
let i = 0
29-
headings.map((param: string) => {
30-
if (
31-
record[param]
32-
?.toString()
33-
.toLowerCase()
34-
.includes(filterConfigs.params[param].toString().toLowerCase())
35-
)
36-
++i
37-
})
38-
if (i === Object.keys(filterConfigs.params).length) return record
39-
})
40-
)
41-
4221
export const getTotalPages = (
4322
total_record: number,
4423
record_per_page: number

0 commit comments

Comments
 (0)