Skip to content

Commit 85fccb3

Browse files
Merge pull request #367 from nginformatica/fix/data-table-slice
Fix/data table slice
2 parents 4a916ed + 9e1c335 commit 85fccb3

File tree

3 files changed

+486
-480
lines changed

3 files changed

+486
-480
lines changed

package.json

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flipper-ui",
3-
"version": "0.35.4",
3+
"version": "0.35.5",
44
"description": "React UI based on the @mui/material toolkit for the web",
55
"main": "dist/index.js",
66
"author": "NG",
@@ -32,64 +32,64 @@
3232
"dependencies": {
3333
"@emotion/react": "11.14.0",
3434
"@emotion/styled": "11.14.0",
35-
"@mui/icons-material": "6.3.1",
36-
"@mui/material": "6.3.1",
37-
"@mui/system": "6.3.1",
38-
"@mui/x-date-pickers": "7.23.3",
35+
"@mui/icons-material": "6.4.2",
36+
"@mui/material": "6.4.2",
37+
"@mui/system": "6.4.2",
38+
"@mui/x-date-pickers": "7.24.1",
3939
"ramda": "0.30.1",
4040
"react-loading-skeleton": "3.5.0",
4141
"react-number-format": "5.4.3"
4242
},
4343
"devDependencies": {
4444
"@babel/cli": "7.26.4",
45-
"@babel/core": "7.26.0",
45+
"@babel/core": "7.26.7",
4646
"@babel/plugin-transform-runtime": "7.25.9",
47-
"@babel/preset-env": "7.26.0",
47+
"@babel/preset-env": "7.26.7",
4848
"@babel/preset-react": "7.26.3",
4949
"@babel/preset-typescript": "7.26.0",
50-
"@faker-js/faker": "9.3.0",
51-
"@storybook/addon-essentials": "8.4.7",
50+
"@faker-js/faker": "9.4.0",
51+
"@storybook/addon-essentials": "8.5.2",
5252
"@storybook/addon-webpack5-compiler-babel": "3.0.5",
53-
"@storybook/blocks": "8.4.7",
54-
"@storybook/react": "8.4.7",
55-
"@storybook/react-webpack5": "8.4.7",
56-
"@stylistic/eslint-plugin": "2.12.1",
53+
"@storybook/blocks": "8.5.2",
54+
"@storybook/react": "8.5.2",
55+
"@storybook/react-webpack5": "8.5.2",
56+
"@stylistic/eslint-plugin": "3.0.1",
5757
"@testing-library/dom": "10.4.0",
5858
"@testing-library/jest-dom": "6.6.3",
59-
"@testing-library/react": "16.1.0",
60-
"@testing-library/user-event": "14.5.2",
59+
"@testing-library/react": "16.2.0",
60+
"@testing-library/user-event": "14.6.1",
6161
"@types/jest": "29.5.14",
62-
"@types/node": "22.10.5",
62+
"@types/node": "22.12.0",
6363
"@types/ramda": "0.30.2",
6464
"@types/react": "18.3.12",
6565
"@types/uuid": "10.0.0",
66-
"@typescript-eslint/eslint-plugin": "8.19.1",
67-
"@typescript-eslint/parser": "8.19.1",
66+
"@typescript-eslint/eslint-plugin": "8.22.0",
67+
"@typescript-eslint/parser": "8.22.0",
6868
"babel-loader": "9.2.1",
6969
"babel-plugin-import": "1.13.8",
7070
"babel-plugin-module-resolver": "5.0.2",
7171
"date-fns": "4.1.0",
72-
"eslint": "9.17.0",
73-
"eslint-config-prettier": "9.1.0",
72+
"eslint": "9.19.0",
73+
"eslint-config-prettier": "10.0.1",
7474
"eslint-import-resolver-typescript": "3.7.0",
7575
"eslint-plugin-import": "2.31.0",
76-
"eslint-plugin-prettier": "5.2.1",
77-
"eslint-plugin-react": "7.37.3",
76+
"eslint-plugin-prettier": "5.2.3",
77+
"eslint-plugin-react": "7.37.4",
7878
"eslint-plugin-react-hooks": "5.1.0",
79-
"fs-extra": "11.2.0",
79+
"fs-extra": "11.3.0",
8080
"identity-obj-proxy": "3.0.0",
8181
"jest": "29.7.0",
8282
"jest-environment-jsdom": "29.7.0",
8383
"prettier": "3.4.2",
8484
"react": "18.3.1",
8585
"react-dom": "18.3.1",
86-
"storybook": "8.4.7",
86+
"storybook": "8.5.2",
8787
"styled-components": "6.1.14",
8888
"ts-jest": "29.2.5",
89-
"ts-loader": "9.5.1",
89+
"ts-loader": "9.5.2",
9090
"typescript": "5.7.3",
91-
"typescript-eslint": "8.19.1",
92-
"uuid": "11.0.4",
91+
"typescript-eslint": "8.22.0",
92+
"uuid": "11.0.5",
9393
"webpack": "5.97.1"
9494
},
9595
"peerDependencies": {

src/core/data-display/data-table/data-table.tsx

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import MuiTableFooter from '@mui/material/TableFooter'
1717
import MuiTableHead from '@mui/material/TableHead'
1818
import MuiTablePagination from '@mui/material/TablePagination'
1919
import MuiTableRow from '@mui/material/TableRow'
20-
import { last } from 'ramda'
20+
import { equals, last } from 'ramda'
2121
import type {
2222
ColumnSpec,
2323
Data,
@@ -87,11 +87,8 @@ const sliceData = <D,>(data: D[], page: number, rowsPerPage: number) => {
8787
export const DataTable = <D extends Data, V extends StackView>(
8888
props: DataTableProps<D, V>
8989
) => {
90-
const [page, setPage] = useState(0)
91-
const pagination = { ...defaultPagination, ...props.pagination }
92-
const [rowsPerPage, setRowsPerPage] = useState(pagination.rowsPerPage)
9390
const {
94-
data,
91+
data = [],
9592
columns,
9693
rowViews,
9794
onRowClick,
@@ -112,20 +109,28 @@ export const DataTable = <D extends Data, V extends StackView>(
112109
checkboxProps
113110
} = props
114111

112+
const pagination = { ...defaultPagination, ...props.pagination }
113+
114+
const controller = useRef<DataTableController<D, V>>()
115+
116+
const [page, setPage] = useState(0)
117+
const [rows, setRows] = useState<D[]>([])
115118
const [newRow, setNewRow] = useState<PartialData<D> | undefined>()
119+
const [rowsPerPage, setRowsPerPage] = useState(pagination.rowsPerPage)
116120

117-
const rows = useMemo(() => {
118-
if (pagination.disabled) {
119-
return data
120-
}
121+
useEffect(() => {
122+
const newRows = pagination.disabled
123+
? data
124+
: sliceData(
125+
data,
126+
page,
127+
newRow && page === 0 ? rowsPerPage - 1 : rowsPerPage
128+
)
121129

122-
// this case is not good for the useRowsMode hook
123-
if (newRow && page === 0) {
124-
return sliceData(data, page, rowsPerPage - 1)
130+
if (!equals(rows, newRows)) {
131+
setRows(newRows)
125132
}
126-
127-
return sliceData(data, page, rowsPerPage)
128-
}, [pagination.disabled, data, rowsPerPage, page, newRow])
133+
}, [data, page, newRow, rowsPerPage, pagination.disabled])
129134

130135
const {
131136
getRowState,
@@ -135,8 +140,6 @@ export const DataTable = <D extends Data, V extends StackView>(
135140
popRowView
136141
} = useRowsState<D, V>(rows, hidden, newRow)
137142

138-
const controller = useRef<DataTableController<D, V>>()
139-
140143
useEffect(() => {
141144
const nextController: DataTableController<D, V> = {
142145
editRow: (id: string) => {
@@ -166,7 +169,7 @@ export const DataTable = <D extends Data, V extends StackView>(
166169
},
167170
pushRowView,
168171
popRowView
169-
} as const
172+
}
170173

171174
controller.current = nextController
172175

0 commit comments

Comments
 (0)