import React from 'react'; import { Table, TableHeader, TableBody, TableComposable, } from '@patternfly/react-table'; import { STATUS } from 'foremanReact/constants'; import { isEqual } from 'lodash'; import PropTypes from 'prop-types'; import './MainTable.scss'; import EmptyStateMessage from './EmptyStateMessage'; import Loading from '../../components/Loading'; const MainTable = ({ status, cells, rows, error, emptyContentTitle, emptyContentBody, emptySearchTitle, emptySearchBody, searchIsActive, activeFilters, defaultFilters, actionButtons, rowsCount, children, ...extraTableProps }) => { const tableHasNoRows = () => { if (children) return rowsCount === 0; return rows.length === 0; }; const filtersAreActive = activeFilters?.length && !isEqual(new Set(activeFilters), new Set(defaultFilters)); const isFiltering = searchIsActive || filtersAreActive; if (status === STATUS.PENDING) return (); // Can we display the error message? if (status === STATUS.ERROR) return (); if (status === STATUS.RESOLVED && isFiltering && tableHasNoRows()) { return (); } if (status === STATUS.RESOLVED && tableHasNoRows()) { return (); } const tableProps = { cells, rows, ...extraTableProps }; if (children) { return ( {children} ); } return (
); }; MainTable.propTypes = { status: PropTypes.string.isRequired, cells: PropTypes.arrayOf(PropTypes.oneOfType([ PropTypes.shape({ }), PropTypes.string])), rows: PropTypes.arrayOf(PropTypes.shape({ })), error: PropTypes.oneOfType([ PropTypes.shape({ }), PropTypes.string, ]), emptyContentTitle: PropTypes.string.isRequired, emptyContentBody: PropTypes.string.isRequired, emptySearchTitle: PropTypes.string.isRequired, emptySearchBody: PropTypes.string.isRequired, searchIsActive: PropTypes.bool, activeFilters: PropTypes.arrayOf(PropTypes.oneOfType([ PropTypes.string, PropTypes.arrayOf(PropTypes.string), ])), defaultFilters: PropTypes.arrayOf(PropTypes.oneOfType([ PropTypes.string, PropTypes.arrayOf(PropTypes.string), ])), actionButtons: PropTypes.bool, rowsCount: PropTypes.number, children: PropTypes.oneOfType([ PropTypes.arrayOf(PropTypes.node), PropTypes.node, ]), }; MainTable.defaultProps = { error: null, searchIsActive: false, activeFilters: [], defaultFilters: [], actionButtons: false, children: null, cells: undefined, rows: undefined, rowsCount: undefined, }; export default MainTable;