diff --git a/src/components/main/ItemsGrid.js b/src/components/main/ItemsGrid.js index 9fd4502c5..821a4f383 100644 --- a/src/components/main/ItemsGrid.js +++ b/src/components/main/ItemsGrid.js @@ -1,14 +1,28 @@ +import { Box } from '@material-ui/core'; +import FormControl from '@material-ui/core/FormControl'; import Grid from '@material-ui/core/Grid'; +import InputLabel from '@material-ui/core/InputLabel'; +import MenuItem from '@material-ui/core/MenuItem'; +import Select from '@material-ui/core/Select'; import { withStyles } from '@material-ui/core/styles'; +import Pagination from '@material-ui/lab/Pagination'; import { List } from 'immutable'; import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { withRouter } from 'react-router'; +import { + ITEMS_GRID_ITEMS_PER_PAGE_SELECT_ID, + ITEMS_GRID_ITEMS_PER_PAGE_SELECT_LABEL_ID, +} from '../../config/selectors'; import { ItemSearchInput, NoItemSearchResult } from '../item/ItemSearch'; import EmptyItem from './EmptyItem'; import Item from './Item'; import TableToolbar from './TableToolbar'; +/* possible choices for number of items per page in grid, + (must be common multiple for possible row counts of 1,2,3,4,6) */ +const GRID_ITEMS_PER_PAGE_CHOICES = [12, 24, 36, 48]; + const styles = (theme) => ({ empty: { padding: theme.spacing(1, 2.5) }, title: { @@ -16,6 +30,17 @@ const styles = (theme) => ({ alignItems: 'center', marginBottom: theme.spacing(1), }, + paginationControls: { + padding: theme.spacing(2), + }, + itemsPerPageSelect: { + position: 'absolute', + left: theme.spacing(1), + }, + formControl: { + margin: theme.spacing(1), + minWidth: 120, + }, }); class ItemsGrid extends Component { @@ -27,6 +52,9 @@ class ItemsGrid extends Component { classes: PropTypes.shape({ title: PropTypes.string.isRequired, empty: PropTypes.string.isRequired, + paginationControls: PropTypes.string.isRequired, + formControl: PropTypes.string.isRequired, + itemsPerPageSelect: PropTypes.string.isRequired, }).isRequired, title: PropTypes.string.isRequired, itemSearch: PropTypes.shape({ @@ -39,8 +67,25 @@ class ItemsGrid extends Component { itemSearch: null, }; - renderItems = () => { - const { classes, items, itemSearch } = this.props; + state = { + page: 1, + itemsPerPage: GRID_ITEMS_PER_PAGE_CHOICES[0], + }; + + handleItemsPerPage = (event) => { + this.setState({ + itemsPerPage: event.target.value, + }); + }; + + handlePagination = (event, value) => { + this.setState({ + page: value, + }); + }; + + renderItems = (items) => { + const { classes, itemSearch } = this.props; if (!items || !items.size) { return itemSearch && itemSearch.text ? ( @@ -62,13 +107,51 @@ class ItemsGrid extends Component { }; render() { - const { title, itemSearch } = this.props; + const { classes, items, title, itemSearch } = this.props; + const { page, itemsPerPage } = this.state; + + const pagesCount = Math.ceil(items.size / itemsPerPage); + + const start = (page - 1) * itemsPerPage; + const end = start + itemsPerPage; + const itemsInPage = items.slice(start, end); + return (