1- import { FC , ReactNode } from 'react'
1+ import { ComponentProps , FC , ReactNode } from 'react'
22import { styled } from '@mui/material/styles'
33import Box from '@mui/material/Box'
44import { Skeleton } from '@oasisprotocol/ui-library/src/components/ui/skeleton'
5- import TableContainer from '@mui/material/TableContainer'
6- import MuiTable from '@mui/material/ Table'
7- import TableHead from '@mui/material/TableHead'
8- import TableBody from '@mui/material/TableBody'
9- import TableRow , { TableRowProps as MuiTableRowProps } from '@mui/material/TableRow'
10- import TableCell from '@mui/material/TableCell'
11- import { useScreenSize } from '../../hooks/useScreensize'
12- import { COLORS } from '../../../styles/theme/colors '
5+ import {
6+ Table as BaseTable ,
7+ TableHeader ,
8+ TableHead ,
9+ TableBody ,
10+ TableRow ,
11+ TableCell ,
12+ } from '@oasisprotocol/ui-library/src/components/table '
1313import { TablePagination , TablePaginationProps } from './TablePagination'
1414import { backgroundColorAnimation } from '../../../styles/theme/animations'
1515import { CardEmptyState } from '../CardEmptyState'
16+ import { cn } from '@oasisprotocol/ui-library/src/lib/utils'
1617
1718type SkeletonTableRowsProps = {
1819 rowsNumber : number
@@ -32,7 +33,7 @@ const SkeletonTableRows: FC<SkeletonTableRowsProps> = ({ rowsNumber, columnsNumb
3233 </ >
3334)
3435
35- type StyledTableRowProps = MuiTableRowProps & {
36+ type StyledTableRowProps = ComponentProps < typeof TableRow > & {
3637 highlight ?: boolean
3738 backgroundColor ?: string
3839}
@@ -50,6 +51,18 @@ export enum TableCellAlign {
5051 Right = 'right' ,
5152}
5253
54+ const getAlignClass = ( align ?: TableCellAlign ) : string => {
55+ switch ( align ) {
56+ case TableCellAlign . Center :
57+ return 'text-center'
58+ case TableCellAlign . Right :
59+ return 'text-right'
60+ case TableCellAlign . Left :
61+ default :
62+ return 'text-left'
63+ }
64+ }
65+
5366type TableCellProps = {
5467 align ?: TableCellAlign
5568 content : ReactNode
@@ -78,7 +91,6 @@ type TableProps = {
7891 pagination : false | TablePaginationProps
7992 rows ?: TableRowProps [ ]
8093 rowsNumber ?: number
81- stickyColumn ?: boolean
8294 extraHorizontalSpaceOnMobile ?: boolean | undefined
8395 alwaysWaitWhileLoading ?: boolean | undefined
8496 /**
@@ -90,88 +102,67 @@ type TableProps = {
90102 emptyMessage ?: string | undefined
91103}
92104
93- const stickyColumnStyles = {
94- position : 'sticky' ,
95- left : 0 ,
96- backgroundColor : COLORS . antiFlashWhite ,
97- zIndex : 1 ,
98- }
99-
100- const extraHorizontalPaddingStyles = {
101- px : 4 ,
102- }
103-
104105export const Table : FC < TableProps > = ( {
105106 columns,
106107 isLoading,
107108 name,
108109 pagination,
109110 rows,
110111 rowsNumber = 5 ,
111- stickyColumn = false ,
112112 extraHorizontalSpaceOnMobile = false ,
113113 alwaysWaitWhileLoading = false ,
114114 emptyMessage = undefined ,
115115} ) => {
116- const { isMobile } = useScreenSize ( )
117-
118116 return ! isLoading && ! rows ?. length ? ( // This is known to be empty
119117 emptyMessage ? ( // If we have a message, show it
120118 < CardEmptyState label = { emptyMessage } />
121119 ) : null // otherwise just show nothing
122120 ) : (
123121 < >
124- < TableContainer >
125- < MuiTable aria-label = { name } >
126- < TableHead >
127- < TableRow >
128- { columns . map ( ( column , index ) => {
129- if ( column . hide ) {
122+ < BaseTable aria-label = { name } >
123+ < TableHeader >
124+ < TableRow >
125+ { columns . map ( ( column , index ) => {
126+ if ( column . hide ) {
127+ return null
128+ }
129+ return (
130+ < TableHead
131+ key = { column . key }
132+ className = { getAlignClass ( column . align ) }
133+ style = { {
134+ width : column . width || 'auto' ,
135+ } }
136+ >
137+ { column . content }
138+ </ TableHead >
139+ )
140+ } ) }
141+ </ TableRow >
142+ </ TableHeader >
143+ < TableBody >
144+ { ( alwaysWaitWhileLoading || ! rows ) && isLoading && (
145+ < SkeletonTableRows rowsNumber = { rowsNumber } columnsNumber = { columns . length } />
146+ ) }
147+ { rows ?. map ( row => (
148+ < StyledTableRow key = { row . key } highlight = { row . highlight } backgroundColor = { row . backgroundColor } >
149+ { row . data . map ( ( cell , index ) => {
150+ if ( cell . hide ) {
130151 return null
131152 }
132153 return (
133154 < TableCell
134- key = { column . key }
135- align = { column . align }
136- sx = { {
137- width : column . width || 'auto' ,
138- ...( stickyColumn && ! index && isMobile ? stickyColumnStyles : { } ) ,
139- } }
155+ key = { cell . key }
156+ className = { cn ( getAlignClass ( cell . align ) , extraHorizontalSpaceOnMobile && 'sm:px-8' ) }
140157 >
141- { column . content }
158+ { cell . content }
142159 </ TableCell >
143160 )
144161 } ) }
145- </ TableRow >
146- </ TableHead >
147- < TableBody >
148- { ( alwaysWaitWhileLoading || ! rows ) && isLoading && (
149- < SkeletonTableRows rowsNumber = { rowsNumber } columnsNumber = { columns . length } />
150- ) }
151- { rows ?. map ( row => (
152- < StyledTableRow key = { row . key } highlight = { row . highlight } backgroundColor = { row . backgroundColor } >
153- { row . data . map ( ( cell , index ) => {
154- if ( cell . hide ) {
155- return null
156- }
157- return (
158- < TableCell
159- key = { cell . key }
160- align = { cell . align }
161- sx = { {
162- ...( stickyColumn && ! index && isMobile ? stickyColumnStyles : { } ) ,
163- ...( extraHorizontalSpaceOnMobile && isMobile ? extraHorizontalPaddingStyles : { } ) ,
164- } }
165- >
166- { cell . content }
167- </ TableCell >
168- )
169- } ) }
170- </ StyledTableRow >
171- ) ) }
172- </ TableBody >
173- </ MuiTable >
174- </ TableContainer >
162+ </ StyledTableRow >
163+ ) ) }
164+ </ TableBody >
165+ </ BaseTable >
175166 { pagination && (
176167 < Box sx = { { display : 'flex' , justifyContent : 'center' } } >
177168 < TablePagination { ...pagination } />
0 commit comments