1- import React , { useMemo } from "react" ;
1+ import React from "react" ;
22import CardMenu from "components/card/CardMenu" ;
33import Checkbox from "components/checkbox" ;
44import Card from "components/card" ;
55
66import {
7- useGlobalFilter ,
8- usePagination ,
9- useSortBy ,
10- useTable ,
11- } from "react-table" ;
7+ createColumnHelper ,
8+ flexRender ,
9+ getCoreRowModel ,
10+ getSortedRowModel ,
11+ useReactTable ,
12+ } from "@tanstack/react-table" ;
1213
13- const CheckTable = ( props ) => {
14- const { columnsData, tableData } = props ;
15-
16- const columns = useMemo ( ( ) => columnsData , [ columnsData ] ) ;
17- const data = useMemo ( ( ) => tableData , [ tableData ] ) ;
18-
19- const tableInstance = useTable (
20- {
21- columns,
22- data,
14+ function CheckTable ( props ) {
15+ const { tableData } = props ;
16+ const [ sorting , setSorting ] = React . useState ( [ ] ) ;
17+ let defaultData = tableData ;
18+ const columns = [
19+ columnHelper . accessor ( "name" , {
20+ id : "name" ,
21+ header : ( ) => (
22+ < p className = "text-sm font-bold text-gray-600 dark:text-white" > NAME</ p >
23+ ) ,
24+ cell : ( info ) => (
25+ < div className = "flex items-center" >
26+ < Checkbox
27+ defaultChecked = { info . getValue ( ) [ 1 ] }
28+ colorScheme = "brandScheme"
29+ me = "10px"
30+ />
31+ < p className = "ml-3 text-sm font-bold text-navy-700 dark:text-white" >
32+ { info . getValue ( ) [ 0 ] }
33+ </ p >
34+ </ div >
35+ ) ,
36+ } ) ,
37+ columnHelper . accessor ( "progress" , {
38+ id : "progress" ,
39+ header : ( ) => (
40+ < p className = "text-sm font-bold text-gray-600 dark:text-white" >
41+ PROGRESS
42+ </ p >
43+ ) ,
44+ cell : ( info ) => (
45+ < p className = "text-sm font-bold text-navy-700 dark:text-white" >
46+ { info . getValue ( ) }
47+ </ p >
48+ ) ,
49+ } ) ,
50+ columnHelper . accessor ( "quantity" , {
51+ id : "quantity" ,
52+ header : ( ) => (
53+ < p className = "text-sm font-bold text-gray-600 dark:text-white" >
54+ QUANTITY
55+ </ p >
56+ ) ,
57+ cell : ( info ) => (
58+ < p className = "text-sm font-bold text-navy-700 dark:text-white" >
59+ { info . getValue ( ) }
60+ </ p >
61+ ) ,
62+ } ) ,
63+ columnHelper . accessor ( "date" , {
64+ id : "date" ,
65+ header : ( ) => (
66+ < p className = "text-sm font-bold text-gray-600 dark:text-white" > DATE</ p >
67+ ) ,
68+ cell : ( info ) => (
69+ < p className = "text-sm font-bold text-navy-700 dark:text-white" >
70+ { info . getValue ( ) }
71+ </ p >
72+ ) ,
73+ } ) ,
74+ ] ; // eslint-disable-next-line
75+ const [ data , setData ] = React . useState ( ( ) => [ ...defaultData ] ) ;
76+ const table = useReactTable ( {
77+ data,
78+ columns,
79+ state : {
80+ sorting,
2381 } ,
24- useGlobalFilter ,
25- useSortBy ,
26- usePagination
27- ) ;
28-
29- const {
30- getTableProps,
31- getTableBodyProps,
32- headerGroups,
33- page,
34- prepareRow,
35- initialState,
36- } = tableInstance ;
37- initialState . pageSize = 11 ;
38-
82+ onSortingChange : setSorting ,
83+ getCoreRowModel : getCoreRowModel ( ) ,
84+ getSortedRowModel : getSortedRowModel ( ) ,
85+ debugTable : true ,
86+ } ) ;
3987 return (
4088 < Card extra = { "w-full h-full sm:overflow-auto px-6" } >
4189 < header className = "relative flex items-center justify-between pt-4" >
@@ -47,86 +95,63 @@ const CheckTable = (props) => {
4795 </ header >
4896
4997 < div className = "mt-8 overflow-x-scroll xl:overflow-x-hidden" >
50- < table
51- { ...getTableProps ( ) }
52- className = "w-full"
53- variant = "simple"
54- color = "gray-500"
55- mb = "24px"
56- >
98+ < table className = "w-full" >
5799 < thead >
58- { headerGroups . map ( ( headerGroup , index ) => (
59- < tr { ...headerGroup . getHeaderGroupProps ( ) } key = { index } >
60- { headerGroup . headers . map ( ( column , index ) => (
61- < th
62- { ...column . getHeaderProps ( column . getSortByToggleProps ( ) ) }
63- className = "border-b border-gray-200 pr-16 pb-[10px] text-start dark:!border-navy-700"
64- key = { index }
65- >
66- < div className = "text-xs font-bold tracking-wide text-gray-600 lg:text-xs" >
67- { column . render ( "Header" ) }
68- </ div >
69- </ th >
70- ) ) }
100+ { table . getHeaderGroups ( ) . map ( ( headerGroup ) => (
101+ < tr key = { headerGroup . id } className = "!border-px !border-gray-400" >
102+ { headerGroup . headers . map ( ( header ) => {
103+ return (
104+ < th
105+ key = { header . id }
106+ colSpan = { header . colSpan }
107+ onClick = { header . column . getToggleSortingHandler ( ) }
108+ className = "cursor-pointer border-b-[1px] border-gray-200 pt-4 pb-2 pr-4 text-start"
109+ >
110+ < div className = "items-center justify-between text-xs text-gray-200" >
111+ { flexRender (
112+ header . column . columnDef . header ,
113+ header . getContext ( )
114+ ) }
115+ { {
116+ asc : "" ,
117+ desc : "" ,
118+ } [ header . column . getIsSorted ( ) ] ?? null }
119+ </ div >
120+ </ th >
121+ ) ;
122+ } ) }
71123 </ tr >
72124 ) ) }
73125 </ thead >
74- < tbody { ...getTableBodyProps ( ) } >
75- { page . map ( ( row , index ) => {
76- prepareRow ( row ) ;
77- return (
78- < tr { ...row . getRowProps ( ) } key = { index } >
79- { row . cells . map ( ( cell , index ) => {
80- let data = "" ;
81- if ( cell . column . Header === "NAME" ) {
82- data = (
83- < div className = "flex items-center gap-2" >
84- < Checkbox />
85- < p className = "text-sm font-bold text-navy-700 dark:text-white" >
86- { cell . value [ 0 ] }
87- </ p >
88- </ div >
89- ) ;
90- } else if ( cell . column . Header === "PROGRESS" ) {
91- data = (
92- < div className = "flex items-center" >
93- < p className = "text-sm font-bold text-navy-700 dark:text-white" >
94- { cell . value } %
95- </ p >
96- </ div >
97- ) ;
98- } else if ( cell . column . Header === "QUANTITY" ) {
99- data = (
100- < p className = "text-sm font-bold text-navy-700 dark:text-white" >
101- { " " }
102- { cell . value } { " " }
103- </ p >
104- ) ;
105- } else if ( cell . column . Header === "DATE" ) {
106- data = (
107- < p className = "text-sm font-bold text-navy-700 dark:text-white" >
108- { cell . value }
109- </ p >
126+ < tbody >
127+ { table
128+ . getRowModel ( )
129+ . rows . slice ( 0 , 5 )
130+ . map ( ( row ) => {
131+ return (
132+ < tr key = { row . id } >
133+ { row . getVisibleCells ( ) . map ( ( cell ) => {
134+ return (
135+ < td
136+ key = { cell . id }
137+ className = "min-w-[150px] border-white/0 py-3 pr-4"
138+ >
139+ { flexRender (
140+ cell . column . columnDef . cell ,
141+ cell . getContext ( )
142+ ) }
143+ </ td >
110144 ) ;
111- }
112- return (
113- < td
114- { ...cell . getCellProps ( ) }
115- key = { index }
116- className = "pt-[14px] pb-[16px] sm:text-[14px]"
117- >
118- { data }
119- </ td >
120- ) ;
121- } ) }
122- </ tr >
123- ) ;
124- } ) }
145+ } ) }
146+ </ tr >
147+ ) ;
148+ } ) }
125149 </ tbody >
126150 </ table >
127151 </ div >
128152 </ Card >
129153 ) ;
130- } ;
154+ }
131155
132156export default CheckTable ;
157+ const columnHelper = createColumnHelper ( ) ;
0 commit comments