@@ -121,18 +121,25 @@ function DataTable<T extends RowRecord>(props: TableProps<T>): JSX.Element {
121
121
defaultSortFieldId ,
122
122
] ) ;
123
123
124
- const initialState : TableState < T > = {
125
- allSelected : false ,
126
- rows : [ ] ,
127
- selectedCount : 0 ,
128
- selectedRows : [ ] ,
129
- selectedColumn : defaultSortColumn || { name : '' } ,
130
- sortDirection : defaultSortDirection ,
131
- currentPage : paginationDefaultPage ,
132
- rowsPerPage : paginationPerPage ,
133
- selectedRowsFlag : false ,
134
- contextMessage : defaultProps . contextMessage ,
135
- } ;
124
+ // Run once
125
+ const initialState : TableState < T > = React . useMemo (
126
+ ( ) => ( {
127
+ allSelected : false ,
128
+ rows : defaultSortColumn ?. selector
129
+ ? sort ( data , defaultSortColumn . selector , defaultSortDirection , sortFunction )
130
+ : data ,
131
+ selectedCount : 0 ,
132
+ selectedRows : [ ] ,
133
+ selectedColumn : defaultSortColumn || { name : '' } ,
134
+ sortDirection : defaultSortDirection ,
135
+ currentPage : paginationDefaultPage ,
136
+ rowsPerPage : paginationPerPage ,
137
+ selectedRowsFlag : false ,
138
+ contextMessage : defaultProps . contextMessage ,
139
+ } ) ,
140
+ // eslint-disable-next-line react-hooks/exhaustive-deps
141
+ [ ] ,
142
+ ) ;
136
143
137
144
const [
138
145
{ rowsPerPage, rows, currentPage, selectedRows, allSelected, selectedCount, selectedColumn, sortDirection } ,
@@ -148,6 +155,18 @@ function DataTable<T extends RowRecord>(props: TableProps<T>): JSX.Element {
148
155
const expandableRowsComponentMemo = React . useMemo ( ( ) => expandableRowsComponent , [ expandableRowsComponent ] ) ;
149
156
const wrapperProps = React . useMemo ( ( ) => ( { ...( direction !== 'auto' && { dir : direction } ) } ) , [ direction ] ) ;
150
157
158
+ const calculatedRows = React . useMemo ( ( ) => {
159
+ if ( pagination && ! paginationServer ) {
160
+ // when using client-side pagination we can just slice the rows set
161
+ const lastIndex = currentPage * rowsPerPage ;
162
+ const firstIndex = lastIndex - rowsPerPage ;
163
+
164
+ return rows . slice ( firstIndex , lastIndex ) ;
165
+ }
166
+
167
+ return rows ;
168
+ } , [ currentPage , pagination , paginationServer , rows , rowsPerPage ] ) ;
169
+
151
170
const handleSort = ( action : SortAction < T > ) => {
152
171
dispatch ( action ) ;
153
172
} ;
@@ -199,17 +218,21 @@ function DataTable<T extends RowRecord>(props: TableProps<T>): JSX.Element {
199
218
return rows . length > 0 && ! progressPending ;
200
219
} ;
201
220
202
- const calculatedRows = React . useMemo ( ( ) => {
203
- if ( pagination && ! paginationServer ) {
204
- // when using client-side pagination we can just slice the rows set
205
- const lastIndex = currentPage * rowsPerPage ;
206
- const firstIndex = lastIndex - rowsPerPage ;
221
+ const showHeader = ( ) => {
222
+ if ( noHeader ) {
223
+ return false ;
224
+ }
207
225
208
- return rows . slice ( firstIndex , lastIndex ) ;
226
+ if ( title ) {
227
+ return true ;
209
228
}
210
229
211
- return rows ;
212
- } , [ currentPage , pagination , paginationServer , rows , rowsPerPage ] ) ;
230
+ if ( actions ) {
231
+ return true ;
232
+ }
233
+
234
+ return false ;
235
+ } ;
213
236
214
237
// recalculate the pagination and currentPage if the rows length changes
215
238
if ( pagination && ! paginationServer && rows . length > 0 && calculatedRows . length === 0 ) {
@@ -278,7 +301,7 @@ function DataTable<T extends RowRecord>(props: TableProps<T>): JSX.Element {
278
301
279
302
return (
280
303
< ThemeProvider theme = { currentTheme } >
281
- { ! noHeader && (
304
+ { showHeader ( ) && (
282
305
< TableHeader
283
306
title = { title }
284
307
actions = { actions }
0 commit comments