1- import { ReactNode } from "react" ;
2-
3- import { Table as ChakraUiTable , TableContainer , Tbody , Td , Th , Thead , Tr , TableHeadProps , TableColumnHeaderProps } from "@chakra-ui/table" ;
4- import { Box , Skeleton } from "@chakra-ui/react" ;
1+ import { Box , Center , Skeleton } from "@chakra-ui/react" ;
2+ import { Table as ChakraUiTable , TableContainer , Tbody , Td , Th , Thead , Tr } from "@chakra-ui/table" ;
53import { Random } from "../../utils/random" ;
4+ import { Pagination , PaginationProps } from "../pagination" ;
5+ import { Body } from "./body" ;
6+ import { Head } from "./head" ;
7+ import { Options } from "./types" ;
68
7- export interface Column < T > {
8- title ?: string ;
9- disableSkeleton ?: boolean ;
10- options ?: TableColumnHeaderProps
11- property ?: keyof T ;
12- defaultValue ?: string ;
13- render ?: ( row : T ) => ReactNode ;
14- }
15-
16- export interface Options < T > {
17- loading ?: boolean ;
18- variant ?: 'simple' | 'striped' | 'unstyled' ;
19- size ?: 'sm' | 'md' | 'lg' ;
20- content : T [ ] ;
21- columns : Column < T > [ ] ;
9+ interface TableProps < T > extends Options < T > {
10+ pagination ?: PaginationProps
2211}
2312
24- export function Table < T > ( { loading, content, columns, size, variant } : Options < T > ) {
13+ export function Table < T > ( { loading, content, columns, size, variant, pagination } : TableProps < T > ) {
2514 return (
26- < TableContainer >
27- < ChakraUiTable size = { size } variant = { variant } >
28- < Thead >
29- < Tr >
30- { columns . map ( ( { title : name , property, options } , index ) => (
31- < Th { ...options } key = { index } >
32- { name ?? property ?. toString ( ) }
33- </ Th >
34- ) ) }
35- </ Tr >
36- </ Thead >
37- < Tbody >
38- { content && content . length === 0 && (
39- Array ( 10 )
40- . fill ( 0 )
41- . map ( ( _ , rowIndex ) => (
42- < Tr key = { rowIndex } >
43- { columns . map ( ( _ , columnIndex ) => (
44- < Td key = { columnIndex } >
45- < Box py = "2" >
46- < Skeleton height = "12px" borderRadius = "3" width = { `${ Random . between ( 25 , 100 ) } %` } />
47- </ Box >
48- </ Td >
49- ) ) }
50- </ Tr >
51- ) )
52- ) }
53- { content . map ( ( row , index ) => (
54- < Tr key = { index } >
55- { columns . map (
56- ( { render, property, defaultValue, disableSkeleton } , tdIndex ) => {
57- if ( loading && ! disableSkeleton ) {
58- return (
59- < Td key = { tdIndex } >
60- < Box py = "2.5" >
61- < Skeleton height = "13px" borderRadius = "3" width = { `${ Random . between ( 25 , 100 ) } %` } />
62- </ Box >
63- </ Td >
64- )
65- }
66-
67- if ( property ) {
68- const types = [ "number" , "boolean" , "string" ] ;
69- const value = row [ property ] ;
70-
71- if ( value && types . includes ( typeof value ) ) {
72- return < Td key = { tdIndex } > { `${ value } ` } </ Td > ;
73- }
74- }
75-
76- if ( render ) {
77- const element = render ( row ) ;
78- if ( element ) {
79- return < Td key = { tdIndex } > { element } </ Td > ;
80- }
81- }
82-
83- return < Td key = { tdIndex } > { defaultValue ?? "" } </ Td > ;
84- }
85- ) }
86- </ Tr >
87- ) ) }
88- </ Tbody >
89- </ ChakraUiTable >
90- </ TableContainer >
15+ < Box >
16+ < TableContainer >
17+ < ChakraUiTable size = { size } variant = { variant } >
18+ < Head columns = { columns } />
19+ < Body
20+ loading = { loading }
21+ columns = { columns }
22+ content = { content }
23+ />
24+ </ ChakraUiTable >
25+ </ TableContainer >
26+ { pagination && (
27+ < Center my = "12" >
28+ < Pagination
29+ loading = { pagination . loading }
30+ onChange = { pagination . onChange }
31+ page = { pagination . page }
32+ size = { pagination . size }
33+ sizes = { pagination . sizes }
34+ totalElements = { pagination . totalElements }
35+ totalPages = { pagination . totalPages }
36+ />
37+ </ Center >
38+ ) }
39+ </ Box >
9140 ) ;
9241}
0 commit comments