Skip to content

Commit

Permalink
Add sorting function for transaction history table
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed Jan 22, 2024
1 parent 7a8f30b commit 4692228
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 4 deletions.
26 changes: 24 additions & 2 deletions dapp/src/components/TransactionHistory/Table/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from "react"
import { Thead, Tr, Th } from "@chakra-ui/react"
import { Thead, Tr, Th, Icon, useMultiStyleConfig, Box } from "@chakra-ui/react"
import { flexRender } from "@tanstack/react-table"
import { UseTransactionHistoryTableResult } from "#/types"
import { SORT_ICONS } from "./utils/sorting"

function TableHeader({ table }: { table: UseTransactionHistoryTableResult }) {
const styles = useMultiStyleConfig("Table")

return (
<Thead>
{table.getHeaderGroups().map(({ id, headers }) => (
Expand All @@ -14,7 +17,26 @@ function TableHeader({ table }: { table: UseTransactionHistoryTableResult }) {
onClick={column.getToggleSortingHandler()}
textAlign={column.columnDef.meta?.style.textAlign}
>
{flexRender(column.columnDef.header, getContext())}
<Box
__css={styles.header}
style={{
...(column.getCanSort() && { cursor: "pointer" }),
}}
>
{flexRender(column.columnDef.header, getContext())}
{column.getCanSort() && (
<Box __css={styles.sortContainer}>
{SORT_ICONS.map(({ key, icon, getColor, boxSize }) => (
<Icon
key={key}
as={icon}
boxSize={boxSize}
color={getColor(column.getIsSorted())}
/>
))}
</Box>
)}
</Box>
</Th>
))}
</Tr>
Expand Down
34 changes: 34 additions & 0 deletions dapp/src/components/TransactionHistory/Table/utils/sorting.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { SortASC, SortDESC } from "#/static/icons"
import { SortDirection } from "@tanstack/react-table"
import { Icon } from "@chakra-ui/react"

const BOX_SIZE = 1.5

const getSortIconColor = (
isSorted: false | SortDirection,
direction: SortDirection,
) => {
if (isSorted === direction) return "brand.400"

return undefined
}

export const SORT_ICONS: {
key: SortDirection
icon: typeof Icon
boxSize: number
getColor: (isSorted: false | SortDirection) => string | undefined
}[] = [
{
key: "asc",
icon: SortASC,
boxSize: BOX_SIZE,
getColor: (isSorted) => getSortIconColor(isSorted, "asc"),
},
{
key: "desc",
icon: SortDESC,
boxSize: BOX_SIZE,
getColor: (isSorted) => getSortIconColor(isSorted, "desc"),
},
]
6 changes: 6 additions & 0 deletions dapp/src/hooks/useTransactionHistoryTable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
ColumnDef,
PaginationState,
SortingState,
getCoreRowModel,
getPaginationRowModel,
getSortedRowModel,
useReactTable,
} from "@tanstack/react-table"
import { StakeHistory, UseTransactionHistoryTableResult } from "#/types"
Expand All @@ -20,16 +22,20 @@ export function useTransactionHistoryTable({
data: StakeHistory[]
columns: ColumnDef<StakeHistory>[]
}): UseTransactionHistoryTableResult {
const [sorting, setSorting] = useState<SortingState>([])
const [pagination, setPagination] =
useState<PaginationState>(PAGINATION_STATE)

const table = useReactTable({
columns,
data,
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
onPaginationChange: setPagination,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
state: {
sorting,
pagination,
},
})
Expand Down
8 changes: 8 additions & 0 deletions dapp/src/static/icons/SortASC.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from "react"
import { createIcon } from "@chakra-ui/react"

export const SortASC = createIcon({
displayName: "SortASC",
viewBox: "0 0 7 7",
path: <path d="M3.51671 0.5L7 6.3H0L3.51671 0.5Z" fill="currentColor" />,
})
8 changes: 8 additions & 0 deletions dapp/src/static/icons/SortDESC.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from "react"
import { createIcon } from "@chakra-ui/react"

export const SortDESC = createIcon({
displayName: "SortDESC",
viewBox: "0 0 7 7",
path: <path d="M3.51671 6.5L7 0.7H0L3.51671 6.5Z" fill="currentColor" />,
})
2 changes: 2 additions & 0 deletions dapp/src/static/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export * from "./ShieldPlus"
export * from "./Pending"
export * from "./Syncing"
export * from "./Complete"
export * from "./SortASC"
export * from "./SortDESC"
30 changes: 28 additions & 2 deletions dapp/src/theme/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { createMultiStyleConfigHelpers, defineStyle } from "@chakra-ui/react"
const BORDER_RADIUS_SIZE = "md"
const BORDER_STYLE = "1px solid white"

const KEYS = [...parts.keys, "cellContainer", "cell", "divider"] as const
const KEYS = [
...parts.keys,
"cellContainer",
"cell",
"divider",
"header",
"sortContainer",
] as const

const multiStyleConfig = createMultiStyleConfigHelpers(KEYS)

Expand Down Expand Up @@ -55,6 +62,17 @@ const variantDoubleRowDivider = defineStyle({
borderBottomWidth: "2px",
})

const baseStyleSortContainer = defineStyle({
display: "flex",
flexDirection: "column",
})

const baseStyleHeader = defineStyle({
display: "flex",
alignItems: "center",
gap: 2.5,
})

const variantDoubleRow = multiStyleConfig.definePartsStyle({
table: variantDoubleRowTable,
td: variantDoubleRowTd,
Expand All @@ -68,4 +86,12 @@ const variants = {
"double-row": variantDoubleRow,
}

export const tableTheme = multiStyleConfig.defineMultiStyleConfig({ variants })
const baseStyle = multiStyleConfig.definePartsStyle({
header: baseStyleHeader,
sortContainer: baseStyleSortContainer,
})

export const tableTheme = multiStyleConfig.defineMultiStyleConfig({
baseStyle,
variants,
})

0 comments on commit 4692228

Please sign in to comment.