@@ -94,6 +100,7 @@ const GlobalFilter = ({ useTableProps }) => {
}
GlobalFilter.propTypes = {
+ className: PropTypes.string,
useTableProps: PropTypes.object.isRequired
}
diff --git a/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalSelectedRows.js b/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalSelectedRows.js
new file mode 100644
index 0000000000..694cd69d30
--- /dev/null
+++ b/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalSelectedRows.js
@@ -0,0 +1,65 @@
+/* ------------------------------------------------------------------------- *
+ * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems *
+ * *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may *
+ * not use this file except in compliance with the License. You may obtain *
+ * a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, software *
+ * distributed under the License is distributed on an "AS IS" BASIS, *
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
+ * See the License for the specific language governing permissions and *
+ * limitations under the License. *
+ * ------------------------------------------------------------------------- */
+import * as React from 'react'
+import PropTypes from 'prop-types'
+
+import { TableProps } from 'react-table'
+import { makeStyles, Chip } from '@material-ui/core'
+
+const useStyles = makeStyles({
+ root: {
+ display: 'flex',
+ flexWrap: 'wrap',
+ gap: 6,
+ alignItems: 'center'
+ }
+})
+
+/**
+ * Render all selected rows.
+ *
+ * @param {object} props - Props
+ * @param {TableProps} props.useTableProps - Table props
+ * @returns {React.JSXElementConstructor} Component JSX
+ */
+const GlobalSelectedRows = ({ useTableProps }) => {
+ const classes = useStyles()
+
+ const { preFilteredRows, state: { selectedRowIds } } = useTableProps
+ const selectedRows = preFilteredRows.filter(row => !!selectedRowIds[row.id])
+
+ return (
+
+ {React.useMemo(() =>
+ selectedRows?.map(({ original, id, toggleRowSelected }) => (
+ toggleRowSelected(false)}
+ />
+ )),
+ [selectedRows[0]?.id]
+ )}
+
+ )
+}
+
+GlobalSelectedRows.propTypes = {
+ useTableProps: PropTypes.object.isRequired
+}
+
+GlobalSelectedRows.displayName = ' GlobalSelectedRows'
+
+export default GlobalSelectedRows
diff --git a/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalSort.js b/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalSort.js
index 99568612cd..bc077ddaaa 100644
--- a/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalSort.js
+++ b/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalSort.js
@@ -21,6 +21,7 @@ import PropTypes from 'prop-types'
import { makeStyles, MenuItem, MenuList, Chip } from '@material-ui/core'
import { SortDown, ArrowDown, ArrowUp } from 'iconoir-react'
+import { TableInstance, UseSortByInstanceProps, UseSortByState } from 'react-table'
import HeaderPopover from 'client/components/Header/Popover'
import { T } from 'client/constants'
@@ -34,19 +35,25 @@ const useStyles = makeStyles({
}
})
+/**
+ * Render all selected sorters.
+ *
+ * @param {object} props - Props
+ * @param {TableInstance} props.useTableProps - Table props
+ * @returns {React.JSXElementConstructor} Component JSX
+ */
const GlobalSort = ({ useTableProps }) => {
const classes = useStyles()
- React.useEffect(() => () => setSortBy([]), [])
+ const { headers, state } = useTableProps
- /**
- * @type {
- * import('react-table').UseSortByInstanceProps &
- * import('react-table').TableInstance &
- * { state: import('react-table').UseSortByState }
- * }
- */
- const { headers, setSortBy, state: { sortBy } } = useTableProps
+ /** @type {UseSortByInstanceProps} */
+ const { setSortBy } = useTableProps
+
+ /** @type {UseSortByState} */
+ const { sortBy } = state
+
+ React.useEffect(() => () => setSortBy([]), [])
const headersNotSorted = React.useMemo(() =>
headers.filter(({ isSorted, canSort, isVisible }) =>
@@ -74,8 +81,7 @@ const GlobalSort = ({ useTableProps }) => {
buttonLabel={T.SortBy}
buttonProps={{
'data-cy': 'sort-by-button',
- disabled: headersNotSorted.length === 0,
- variant: 'outlined'
+ disabled: headersNotSorted.length === 0
}}
popoverProps= {{
anchorOrigin: {
diff --git a/src/fireedge/src/client/components/Tables/Enhanced/Utils/index.js b/src/fireedge/src/client/components/Tables/Enhanced/Utils/index.js
index ed7d5fb790..2a03bd71e5 100644
--- a/src/fireedge/src/client/components/Tables/Enhanced/Utils/index.js
+++ b/src/fireedge/src/client/components/Tables/Enhanced/Utils/index.js
@@ -15,6 +15,7 @@
* ------------------------------------------------------------------------- */
import CategoryFilter from 'client/components/Tables/Enhanced/Utils/CategoryFilter'
import GlobalFilter from 'client/components/Tables/Enhanced/Utils/GlobalFilter'
+import GlobalSelectedRows from 'client/components/Tables/Enhanced/Utils/GlobalSelectedRows'
import GlobalSort from 'client/components/Tables/Enhanced/Utils/GlobalSort'
import LabelFilter from 'client/components/Tables/Enhanced/Utils/LabelFilter'
@@ -23,6 +24,7 @@ export * from 'client/components/Tables/Enhanced/Utils/utils'
export {
CategoryFilter,
GlobalFilter,
+ GlobalSelectedRows,
GlobalSort,
LabelFilter
}
diff --git a/src/fireedge/src/client/components/Tables/Enhanced/Utils/utils.js b/src/fireedge/src/client/components/Tables/Enhanced/Utils/utils.js
index 66985f5796..d9a39c6b38 100644
--- a/src/fireedge/src/client/components/Tables/Enhanced/Utils/utils.js
+++ b/src/fireedge/src/client/components/Tables/Enhanced/Utils/utils.js
@@ -13,9 +13,18 @@
* See the License for the specific language governing permissions and *
* limitations under the License. *
* ------------------------------------------------------------------------- */
-/* eslint-disable jsdoc/require-jsdoc */
+import { Column } from 'react-table'
import { CategoryFilter } from 'client/components/Tables/Enhanced/Utils'
+/**
+ * Add filters defined in view yaml to columns.
+ *
+ * @param {object} config -
+ * @param {object[]} config.filters
+ * - List of criteria to filter the columns.
+ * @param {Column[]} config.columns - Columns
+ * @returns {object} Column with filters
+ */
export const createColumns = ({ filters = {}, columns = [] }) => {
if (Object.keys(filters).length === 0) return columns
@@ -25,18 +34,23 @@ export const createColumns = ({ filters = {}, columns = [] }) => {
const filterById = !!filters[String(id.toLowerCase())]
const filterByAccessor =
- typeof accessor === 'string' &&
- !!filters[String(accessor.toLowerCase())]
+ typeof accessor === 'string' &&
+ !!filters[String(accessor.toLowerCase())]
return {
...column,
- ...((filterById || filterByAccessor) &&
- createCategoryFilter(Header)
+ ...((filterById || filterByAccessor) && createCategoryFilter(Header)
)
}
})
}
+/**
+ * Create category filter as column.
+ *
+ * @param {string} title - Title
+ * @returns {Column} - Category filter
+ */
export const createCategoryFilter = title => ({
disableFilters: false,
Filter: ({ column }) => CategoryFilter({
diff --git a/src/fireedge/src/client/components/Tables/Enhanced/filters.js b/src/fireedge/src/client/components/Tables/Enhanced/filters.js
index 5bd569fd22..c1edc52e39 100644
--- a/src/fireedge/src/client/components/Tables/Enhanced/filters.js
+++ b/src/fireedge/src/client/components/Tables/Enhanced/filters.js
@@ -21,16 +21,16 @@ import clsx from 'clsx'
import { makeStyles, useMediaQuery, Card, CardContent } from '@material-ui/core'
import { UseTableInstanceProps } from 'react-table'
-import GlobalFilter from 'client/components/Tables/Enhanced/Utils/GlobalFilter'
+import { GlobalFilter } from 'client/components/Tables/Enhanced/Utils'
const useToolbarStyles = makeStyles({
root: {
display: 'flex'
},
rootNoFilters: {
- gridColumn: '1/3',
+ gridColumn: '1 / -1',
'& ~ div': {
- gridColumn: '1/3'
+ gridColumn: '1 / -1'
}
},
content: {
@@ -47,7 +47,7 @@ const useToolbarStyles = makeStyles({
}
})
-const Filters = ({ useTableProps }) => {
+const Filters = ({ onlyGlobalSearch, useTableProps }) => {
const classes = useToolbarStyles()
const isMobile = useMediaQuery(theme => theme.breakpoints.down('sm'))
@@ -64,8 +64,13 @@ const Filters = ({ useTableProps }) => {
) : null)
), [rows])
- if (isMobile) {
- return
+ if (isMobile || onlyGlobalSearch) {
+ return (
+
+ )
}
const noFilters = filters.length === 0
@@ -86,10 +91,12 @@ const Filters = ({ useTableProps }) => {
}
Filters.propTypes = {
+ onlyGlobalSearch: PropTypes.bool,
useTableProps: PropTypes.object
}
Filters.defaultProps = {
+ onlyGlobalSearch: false,
useTableProps: {}
}
diff --git a/src/fireedge/src/client/components/Tables/Enhanced/index.js b/src/fireedge/src/client/components/Tables/Enhanced/index.js
index 126734bf53..dcb239fb65 100644
--- a/src/fireedge/src/client/components/Tables/Enhanced/index.js
+++ b/src/fireedge/src/client/components/Tables/Enhanced/index.js
@@ -26,17 +26,17 @@ import {
useRowSelect,
useSortBy,
useTable,
+ useMountedLayoutEffect,
// types
UseRowSelectRowProps
} from 'react-table'
-import SplitPane from 'client/components/SplitPane'
import Toolbar from 'client/components/Tables/Enhanced/toolbar'
import Pagination from 'client/components/Tables/Enhanced/pagination'
import Filters from 'client/components/Tables/Enhanced/filters'
import EnhancedTableStyles from 'client/components/Tables/Enhanced/styles'
-import { Translate, ConditionalWrap } from 'client/components/HOC'
+import { Translate } from 'client/components/HOC'
import { T } from 'client/constants'
const EnhancedTable = ({
@@ -45,12 +45,15 @@ const EnhancedTable = ({
data,
fetchMore,
getRowId,
+ initialState,
isLoading,
+ onlyGlobalSearch,
+ onlyGlobalSelectedRows,
+ onSelectedRowsChange,
pageSize = 10,
- renderAllSelected = true,
- renderDetail,
RowComponent,
- showPageCount
+ showPageCount,
+ singleSelect = false
}) => {
const classes = EnhancedTableStyles()
@@ -85,7 +88,8 @@ const EnhancedTable = ({
autoResetSortBy: false,
// -------------------------------------
initialState: {
- pageSize
+ pageSize,
+ ...initialState
}
},
useGlobalFilter,
@@ -98,6 +102,7 @@ const EnhancedTable = ({
const {
getTableProps,
prepareRow,
+ toggleAllRowsSelected,
preFilteredRows,
rows,
page,
@@ -106,10 +111,11 @@ const EnhancedTable = ({
state: { pageIndex, selectedRowIds }
} = useTableProps
- const selectedRows = React.useMemo(
- () => preFilteredRows.filter(row => !!selectedRowIds[row.id]),
- [preFilteredRows, selectedRowIds]
- )
+ useMountedLayoutEffect(() => {
+ const selectedRows = preFilteredRows.filter(row => !!selectedRowIds[row.id])
+
+ onSelectedRowsChange?.(selectedRows)
+ }, [selectedRowIds])
const handleChangePage = newPage => {
gotoPage(newPage)
@@ -120,98 +126,100 @@ const EnhancedTable = ({
}
return (
-
0}
- wrap={children => {children}}
- >
-
-
- {!isFetching &&
}
-
- {page?.length > 0 && (
-
- )}
-
-
-
- {isLoading && (
-
+
+
+ {/* TOOLBAR */}
+ {!isFetching && (
+
)}
-
- {!isFetching &&
}
-
-
- {/* NO DATA MESSAGE */}
- {!isFetching && page?.length === 0 && (
-
-
-
-
- )}
-
- {/* DATALIST PER PAGE */}
- {page.map(row => {
- prepareRow(row)
-
- /** @type {UseRowSelectRowProps} */
- const { getRowProps, original, values, toggleRowSelected, isSelected } = row
- const { key, ...rowProps } = getRowProps()
-
- return (
- toggleRowSelected(!isSelected)}
- />
- )
- })}
-
+ {/* PAGINATION */}
+
+ {page?.length > 0 && (
+
+ )}
-
+
- {selectedRows?.length > 0 && (
-
- {selectedRows?.length === 1 && renderDetail
- ? renderDetail?.(selectedRows[0]?.values)
- : renderAllSelected && (
-
-
- {JSON.stringify(Object.keys(selectedRowIds)?.join(', '), null, 2)}
-
-
- )
- }
-
+ {isLoading && (
+
)}
-
+
+
+ {/* FILTERS */}
+ {!isFetching && (
+
+ )}
+
+
+ {/* NO DATA MESSAGE */}
+ {!isFetching && page?.length === 0 && (
+
+
+
+
+ )}
+
+ {/* DATALIST PER PAGE */}
+ {page.map(row => {
+ prepareRow(row)
+
+ /** @type {UseRowSelectRowProps} */
+ const { getRowProps, original, values, toggleRowSelected, isSelected } = row
+ const { key, ...rowProps } = getRowProps()
+
+ return (
+ {
+ singleSelect && toggleAllRowsSelected(false)
+ toggleRowSelected(!isSelected)
+ }}
+ />
+ )
+ })}
+
+
+
)
}
-EnhancedTable.propTypes = {
+export const EnhancedTableProps = {
canFetchMore: PropTypes.bool,
columns: PropTypes.array,
data: PropTypes.array,
fetchMore: PropTypes.func,
getRowId: PropTypes.func,
+ initialState: PropTypes.object,
isLoading: PropTypes.bool,
+ onlyGlobalSearch: PropTypes.bool,
+ onlyGlobalSelectedRows: PropTypes.bool,
+ onSelectedRowsChange: PropTypes.func,
pageSize: PropTypes.number,
- renderAllSelected: PropTypes.bool,
- renderDetail: PropTypes.func,
RowComponent: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
PropTypes.func
]),
- showPageCount: PropTypes.bool
+ showPageCount: PropTypes.bool,
+ singleSelect: PropTypes.bool
}
+EnhancedTable.propTypes = EnhancedTableProps
+
export default EnhancedTable
diff --git a/src/fireedge/src/client/components/Tables/Enhanced/pagination.js b/src/fireedge/src/client/components/Tables/Enhanced/pagination.js
index 5af651566e..a04a2e95ee 100644
--- a/src/fireedge/src/client/components/Tables/Enhanced/pagination.js
+++ b/src/fireedge/src/client/components/Tables/Enhanced/pagination.js
@@ -45,9 +45,10 @@ const Pagination = ({
return (
<>