Skip to content

Commit 0ab8555

Browse files
authored
implements #756 | fixes row initial render (#760)
1 parent 72b0566 commit 0ab8555

File tree

7 files changed

+23544
-11627
lines changed

7 files changed

+23544
-11627
lines changed

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-data-table-component",
3-
"version": "7.0.0-alpha-4",
3+
"version": "7.0.0-alpha-5",
44
"description": "A declarative react based data table",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.es.js",
@@ -52,17 +52,17 @@
5252
"@types/jest": "^26.0.20",
5353
"@types/lodash-es": "^4.17.4",
5454
"@types/lodash.orderby": "^4.6.6",
55-
"@types/node": "^14.14.21",
55+
"@types/node": "^14.14.22",
5656
"@types/react": "^17.0.0",
5757
"@types/react-dom": "^17.0.0",
5858
"@types/styled-components": "^5.1.7",
59-
"@typescript-eslint/eslint-plugin": "^4.13.0",
60-
"@typescript-eslint/parser": "^4.13.0",
59+
"@typescript-eslint/eslint-plugin": "^4.14.0",
60+
"@typescript-eslint/parser": "^4.14.0",
6161
"axios": "^0.21.1",
6262
"babel-eslint": "^10.1.0",
6363
"codecov": "^3.8.1",
6464
"eslint": "^7.18.0",
65-
"eslint-config-prettier": "^7.1.0",
65+
"eslint-config-prettier": "^7.2.0",
6666
"eslint-config-react-app": "^6.0.0",
6767
"eslint-plugin-import": "^2.22.1",
6868
"eslint-plugin-jest": "^24.1.3",
@@ -82,14 +82,14 @@
8282
"react-app-polyfill": "^2.0.0",
8383
"react-dom": "^17.0.1",
8484
"rimraf": "^3.0.2",
85-
"rollup": "^2.36.2",
85+
"rollup": "^2.37.0",
8686
"rollup-plugin-commonjs": "^10.1.0",
8787
"rollup-plugin-node-resolve": "^5.2.0",
8888
"rollup-plugin-terser": "^7.0.2",
8989
"rollup-plugin-typescript2": "^0.29.0",
9090
"rollup-plugin-visualizer": "^4.2.0",
9191
"styled-components": "^5.0.1",
92-
"stylelint": "^13.8.0",
92+
"stylelint": "^13.9.0",
9393
"stylelint-config-recommended": "^3.0.0",
9494
"stylelint-config-styled-components": "^0.1.1",
9595
"stylelint-processor-styled-components": "^1.10.0",

src/DataTable/DataTable.tsx

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,25 @@ function DataTable<T extends RowRecord>(props: TableProps<T>): JSX.Element {
121121
defaultSortFieldId,
122122
]);
123123

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+
);
136143

137144
const [
138145
{ rowsPerPage, rows, currentPage, selectedRows, allSelected, selectedCount, selectedColumn, sortDirection },
@@ -148,6 +155,18 @@ function DataTable<T extends RowRecord>(props: TableProps<T>): JSX.Element {
148155
const expandableRowsComponentMemo = React.useMemo(() => expandableRowsComponent, [expandableRowsComponent]);
149156
const wrapperProps = React.useMemo(() => ({ ...(direction !== 'auto' && { dir: direction }) }), [direction]);
150157

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+
151170
const handleSort = (action: SortAction<T>) => {
152171
dispatch(action);
153172
};
@@ -199,17 +218,21 @@ function DataTable<T extends RowRecord>(props: TableProps<T>): JSX.Element {
199218
return rows.length > 0 && !progressPending;
200219
};
201220

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+
}
207225

208-
return rows.slice(firstIndex, lastIndex);
226+
if (title) {
227+
return true;
209228
}
210229

211-
return rows;
212-
}, [currentPage, pagination, paginationServer, rows, rowsPerPage]);
230+
if (actions) {
231+
return true;
232+
}
233+
234+
return false;
235+
};
213236

214237
// recalculate the pagination and currentPage if the rows length changes
215238
if (pagination && !paginationServer && rows.length > 0 && calculatedRows.length === 0) {
@@ -278,7 +301,7 @@ function DataTable<T extends RowRecord>(props: TableProps<T>): JSX.Element {
278301

279302
return (
280303
<ThemeProvider theme={currentTheme}>
281-
{!noHeader && (
304+
{showHeader() && (
282305
<TableHeader
283306
title={title}
284307
actions={actions}

src/DataTable/TableHeader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type TableHeaderProps = {
4848

4949
const TableHeader = ({
5050
title,
51-
actions = [],
51+
actions = null,
5252
contextMessage,
5353
contextActions,
5454
contextComponent,
@@ -58,7 +58,7 @@ const TableHeader = ({
5858
}: TableHeaderProps): JSX.Element => (
5959
<TableHeaderStyle className="rdt_TableHeader" role="heading" aria-level={1}>
6060
<Title>{title}</Title>
61-
<Actions>{actions}</Actions>
61+
{actions && <Actions>{actions}</Actions>}
6262

6363
{showMenu && (
6464
<ContextMenu

src/DataTable/__tests__/DataTable.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,13 @@ describe('DataTable::Header', () => {
18531853
expect(container.firstChild).toMatchSnapshot();
18541854
});
18551855

1856+
test('header should not display with no title', () => {
1857+
const mock = dataMock();
1858+
const { container } = render(<DataTable data={mock.data} columns={mock.columns} />);
1859+
1860+
expect(container.firstChild).toMatchSnapshot();
1861+
});
1862+
18561863
test('should render header actions when they are provided', () => {
18571864
const mock = dataMock();
18581865
const { container } = render(

0 commit comments

Comments
 (0)