From d72c7ad83dd10fac6af01eb3c998d9bb11ff71b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hubert=20Soko=C5=82owski?= Date: Thu, 22 Apr 2021 02:45:54 +0200 Subject: [PATCH] new data viewer with selection --- package-lock.json | 5 + package.json | 1 + .../Node/DataViewer/DataViewerTable.jsx | 11 +- src/components/Node/DataViewer/ReactTable.jsx | 139 ++++++++++++++++++ 4 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 src/components/Node/DataViewer/ReactTable.jsx diff --git a/package-lock.json b/package-lock.json index c446623..00a36f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13904,6 +13904,11 @@ "tslib": "^1.0.0" } }, + "react-table": { + "version": "7.6.3", + "resolved": "https://registry.npmjs.org/react-table/-/react-table-7.6.3.tgz", + "integrity": "sha512-hfPF13zDLxPMpLKzIKCE8RZud9T/XrRTsaCIf8zXpWZIZ2juCl7qrGpo3AQw9eAetXV5DP7s2GDm+hht7qq5Dw==" + }, "read-pkg": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-2.0.0.tgz", diff --git a/package.json b/package.json index 1701bca..f5555ac 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "react-icons": "^4.1.0", "react-scripts": "4.0.1", "react-select-search": "^2.2.4", + "react-table": "^7.6.3", "web-vitals": "^0.2.4" }, "scripts": { diff --git a/src/components/Node/DataViewer/DataViewerTable.jsx b/src/components/Node/DataViewer/DataViewerTable.jsx index c4ad354..064b884 100644 --- a/src/components/Node/DataViewer/DataViewerTable.jsx +++ b/src/components/Node/DataViewer/DataViewerTable.jsx @@ -3,11 +3,13 @@ import React, { useState } from 'react'; import { useAttributesContext } from '../../../contexts/AttributesContext'; import { Search as SearchBar } from '../../SearchBar'; import { CloseIcon } from '@chakra-ui/icons'; +import ReactTable from './ReactTable'; function DataViewerTable({ data }) { - console.log(data); + //console.log(data); const [columns, setColumns] = useState([]); const [selected, setSelected] = useState([]); + const [state, setState] = useState({ selected: null }); const { attributes } = useAttributesContext(); function handleSetColumns(value) { setColumns(value); @@ -26,7 +28,7 @@ function DataViewerTable({ data }) { tmp.splice(index, 1); } } - console.log(tmp); + //console.log(tmp); setSelected(tmp); } @@ -59,7 +61,8 @@ function DataViewerTable({ data }) { - + + {/*
{columns.length !== 0 ? ( @@ -111,7 +114,7 @@ function DataViewerTable({ data }) { )} -
+ */}
); diff --git a/src/components/Node/DataViewer/ReactTable.jsx b/src/components/Node/DataViewer/ReactTable.jsx new file mode 100644 index 0000000..56aa96f --- /dev/null +++ b/src/components/Node/DataViewer/ReactTable.jsx @@ -0,0 +1,139 @@ +// @ts-nocheck +import React, { useState } from 'react'; +import { Table, Thead, Tbody, Tr, Th, Td } from '@chakra-ui/react'; +import { TriangleDownIcon, TriangleUpIcon } from '@chakra-ui/icons'; +import { useTable, useSortBy, useRowSelect } from 'react-table'; + +const IndeterminateCheckbox = React.forwardRef(({ indeterminate, ...rest }, ref) => { + const defaultRef = React.useRef(); + const resolvedRef = ref || defaultRef; + + React.useEffect(() => { + resolvedRef.current.indeterminate = indeterminate; + }, [resolvedRef, indeterminate]); + + return ( + <> + + + ); +}); + +function ReactTable({ set, cols }) { + console.log(set, cols); + const data = React.useMemo(() => set, []); + + function prepareHeaders(cols) { + let tmp = []; + for (let item of cols.sort()) { + tmp.push({ + Header: item, + accessor: item, + }); + } + console.log(tmp); + return tmp; + } + const columns = React.useMemo(() => prepareHeaders(cols), [cols]); + + const { + getTableProps, + getTableBodyProps, + headerGroups, + rows, + prepareRow, + selectedFlatRows, + state: { selectedRowIds }, + } = useTable( + { + columns, + data, + }, + useSortBy, + useRowSelect, + hooks => { + hooks.visibleColumns.push(columns => [ + // Let's make a column for selection + { + id: 'selection', + // The header can use the table's getToggleAllRowsSelectedProps method + // to render a checkbox + Header: ({ getToggleAllRowsSelectedProps }) => ( +
+ {cols.length !== 0 ? ( + + ) : ( +
Select columns to see rows
+ )} +
+ ), + // The cell can use the individual row's getToggleRowSelectedProps method + // to the render a checkbox + Cell: ({ row }) => ( +
+ +
+ ), + }, + ...columns, + ]); + } + ); + + return ( + + + {headerGroups.map(headerGroup => ( + + + {headerGroup.headers.map(column => ( + + ))} + + ))} + + + {rows.map((row, i) => { + prepareRow(row); + return ( + + + {row.cells.map(cell => ( + + ))} + + ); + })} + +
+ {cols.length !== 0 ? 'ID' : ''} + + {column.render('Header')} + {column.isSorted ? ( + column.isSortedDesc ? ( + + ) : ( + + ) + ) : null} +
+ {i + 1} + + {cell.render('Cell')} +
+ ); +} + +export default ReactTable;