11import classNames from 'classnames'
22import { Dispatch , MouseEvent , SetStateAction , useEffect , useState } from 'react'
33
4+ import { Button } from '../button'
45import { Sort } from '../pagination'
56import '../styles/_includes.scss'
67import { IconOutline } from '../svgs'
@@ -15,7 +16,10 @@ import styles from './Table.module.scss'
1516interface TableProps < T > {
1617 readonly columns : ReadonlyArray < TableColumn < T > >
1718 readonly data : ReadonlyArray < T >
19+ readonly moreToLoad ?: boolean
20+ readonly onLoadMoreClick ?: ( ) => void
1821 readonly onRowClick ?: ( data : T ) => void
22+ readonly onToggleSort ?: ( sort : Sort ) => void
1923}
2024
2125interface DefaultSortDirectionMap {
@@ -34,7 +38,7 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
3438 Dispatch < SetStateAction < DefaultSortDirectionMap | undefined > >
3539 ]
3640 = useState < DefaultSortDirectionMap | undefined > ( )
37- const [ sortedData , setSortData ] : [ ReadonlyArray < T > , Dispatch < SetStateAction < ReadonlyArray < T > > > ]
41+ const [ sortedData , setSortedData ] : [ ReadonlyArray < T > , Dispatch < SetStateAction < ReadonlyArray < T > > > ]
3842 = useState < ReadonlyArray < T > > ( props . data )
3943
4044 useEffect ( ( ) => {
@@ -47,7 +51,11 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
4751 setDefaultSortDirectionMap ( map )
4852 }
4953
50- setSortData ( tableGetSorted ( data , columns , sort ) )
54+ // if we have a sort handler, don't worry about getting the sorted data;
55+ // otherwise, get the sorted data for the table
56+ const sorted : ReadonlyArray < T > = ! ! props . onToggleSort ? data : tableGetSorted ( data , columns , sort )
57+
58+ setSortedData ( sorted )
5159 } ,
5260 [
5361 columns ,
@@ -75,6 +83,9 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
7583 fieldName,
7684 }
7785 setSort ( newSort )
86+
87+ // call the callback to notify parent for sort update
88+ props . onToggleSort ?.( newSort )
7889 }
7990
8091 const headerRow : Array < JSX . Element > = props . columns
@@ -136,7 +147,7 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
136147 // return the entire row
137148 return (
138149 < tr
139- className = { classNames ( styles . tr , ! ! onRowClick ? styles . clickable : undefined ) }
150+ className = { classNames ( styles . tr , props . onRowClick ? styles . clickable : undefined ) }
140151 onClick = { onRowClick }
141152 key = { index }
142153 >
@@ -158,6 +169,18 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
158169 { rowCells }
159170 </ tbody >
160171 </ table >
172+ {
173+ ! ! props . moreToLoad && ! ! props . onLoadMoreClick && (
174+ < div className = { styles [ 'loadBtnWrap' ] } >
175+ < Button
176+ buttonStyle = 'tertiary'
177+ label = 'Load More'
178+ size = 'lg'
179+ onClick = { props . onLoadMoreClick }
180+ />
181+ </ div >
182+ )
183+ }
161184 </ div >
162185 )
163186 }
0 commit comments