11import classNames from 'classnames'
22import { Dispatch , MouseEvent , SetStateAction , useEffect , useState } from 'react'
33
4- import { Button , ButtonSize , ButtonStyle } from '../button'
4+ import { Button } from '../button'
55import { Sort } from '../pagination'
66import '../styles/_includes.scss'
77import { IconOutline } from '../svgs'
@@ -16,13 +16,10 @@ import styles from './Table.module.scss'
1616interface TableProps < T > {
1717 readonly columns : ReadonlyArray < TableColumn < T > >
1818 readonly data : ReadonlyArray < T >
19- readonly loadMoreBtnLabel ?: string
20- readonly loadMoreBtnSize ?: ButtonSize
21- readonly loadMoreBtnStyle ?: ButtonStyle
2219 readonly moreToLoad ?: boolean
23- readonly onLoadMoreClick ?: ( data : T ) => void
20+ readonly onLoadMoreClick ?: ( ) => void
2421 readonly onRowClick ?: ( data : T ) => void
25- readonly onToggleSort ?: ( sort : Sort | undefined ) => void
22+ readonly onToggleSort ?: ( sort : Sort ) => void
2623}
2724
2825interface DefaultSortDirectionMap {
@@ -41,7 +38,7 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
4138 Dispatch < SetStateAction < DefaultSortDirectionMap | undefined > >
4239 ]
4340 = useState < DefaultSortDirectionMap | undefined > ( )
44- const [ sortedData , setSortData ] : [ ReadonlyArray < T > , Dispatch < SetStateAction < ReadonlyArray < T > > > ]
41+ const [ sortedData , setSortedData ] : [ ReadonlyArray < T > , Dispatch < SetStateAction < ReadonlyArray < T > > > ]
4542 = useState < ReadonlyArray < T > > ( props . data )
4643
4744 useEffect ( ( ) => {
@@ -54,7 +51,11 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
5451 setDefaultSortDirectionMap ( map )
5552 }
5653
57- 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 )
5859 } ,
5960 [
6061 columns ,
@@ -84,9 +85,7 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
8485 setSort ( newSort )
8586
8687 // call the callback to notify parent for sort update
87- if ( props . onToggleSort ) {
88- props . onToggleSort ( newSort )
89- }
88+ props . onToggleSort ?.( newSort )
9089 }
9190
9291 const headerRow : Array < JSX . Element > = props . columns
@@ -95,13 +94,12 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
9594 const isCurrentlySorted : boolean = isSortable && col . propertyName === sort ?. fieldName
9695 const colorClass : string = isCurrentlySorted ? 'black-100' : 'black-60'
9796 const sortableClass : string | undefined = isSortable ? styles . sortable : undefined
98- const centerClass : string | undefined = col . centerHeader ? styles . centerHeader : undefined
9997 return (
10098 < th
10199 className = { styles . th }
102100 key = { index }
103101 >
104- < div className = { classNames ( styles [ 'header-container' ] , styles [ col . type ] , colorClass , sortableClass , centerClass ) } >
102+ < div className = { classNames ( styles [ 'header-container' ] , styles [ col . type ] , colorClass , sortableClass ) } >
105103 { col . label }
106104 { ! ! col . tooltip && (
107105 < div className = { styles . tooltip } >
@@ -172,9 +170,16 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
172170 </ tbody >
173171 </ table >
174172 {
175- props . moreToLoad && < div className = { styles [ 'loadBtnWrap' ] } >
176- < Button buttonStyle = { props . loadMoreBtnStyle } label = { props . loadMoreBtnLabel } size = { props . loadMoreBtnSize } onClick = { props . onLoadMoreClick } />
177- </ div >
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+ )
178183 }
179184 </ div >
180185 )
0 commit comments