balance table fixes: add 5 to rowsPerPageOptions, remove props from initial state and use them as fallback values

This commit is contained in:
Mikhail Mikheev 2019-03-21 17:55:49 +04:00
parent 5ed00fc475
commit c2f46699a5
5 changed files with 337 additions and 139 deletions

408
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -108,7 +108,7 @@
"@babel/plugin-transform-member-expression-literals": "^7.2.0", "@babel/plugin-transform-member-expression-literals": "^7.2.0",
"@babel/plugin-transform-property-literals": "^7.2.0", "@babel/plugin-transform-property-literals": "^7.2.0",
"@babel/polyfill": "^7.4.0", "@babel/polyfill": "^7.4.0",
"@babel/preset-env": "^7.4.1", "@babel/preset-env": "^7.4.2",
"@babel/preset-flow": "^7.0.0-beta.40", "@babel/preset-flow": "^7.0.0-beta.40",
"@babel/preset-react": "^7.0.0-beta.40", "@babel/preset-react": "^7.0.0-beta.40",
"@sambego/storybook-state": "^1.0.7", "@sambego/storybook-state": "^1.0.7",

View File

@ -28,10 +28,10 @@ type Props<K> = {
type State = { type State = {
page: number, page: number,
order: Order, order: Order,
orderBy: string, orderBy?: string,
orderProp: boolean, orderProp: boolean,
rowsPerPage: number, rowsPerPage: number,
fixed: boolean, fixed?: boolean,
} }
const styles = { const styles = {
@ -64,22 +64,25 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
state = { state = {
page: 0, page: 0,
order: 'asc', order: 'asc',
orderBy: this.props.defaultOrderBy, orderBy: undefined,
fixed: !!this.props.defaultFixed, fixed: undefined,
orderProp: false, orderProp: false,
rowsPerPage: 5, rowsPerPage: 5,
} }
onSort = (property: string, orderProp: boolean) => { onSort = (newOrderBy: string, orderProp: boolean) => {
const orderBy = property const { order, orderBy } = this.state
let order = 'desc' let newOrder = 'desc'
if (this.state.orderBy === property && this.state.order === 'desc') { if (orderBy === newOrderBy && order === 'desc') {
order = 'asc' newOrder = 'asc'
} }
this.setState(() => ({ this.setState(() => ({
order, orderBy, orderProp, fixed: false, order: newOrder,
orderBy: newOrderBy,
orderProp,
fixed: false,
})) }))
} }
@ -98,12 +101,13 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
render() { render() {
const { const {
data, label, columns, classes, children, size, data, label, columns, classes, children, size, defaultOrderBy, defaultFixed,
} = this.props } = this.props
const { const {
order, orderBy, page, orderProp, rowsPerPage, fixed, order, orderBy, page, orderProp, rowsPerPage, fixed,
} = this.state } = this.state
const orderByParam = orderBy || defaultOrderBy
const fixedParam = typeof fixed !== 'undefined' ? fixed : !!defaultFixed
const backProps = { const backProps = {
'aria-label': 'Previous Page', 'aria-label': 'Previous Page',
@ -119,41 +123,39 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
input: classes.white, input: classes.white,
} }
const sortedData = stableSort(data, getSorting(order, orderBy, orderProp), fixed) const sortedData = stableSort(data, getSorting(order, orderByParam, orderProp), fixedParam).slice(
.slice(page * rowsPerPage, ((page * rowsPerPage) + rowsPerPage)) page * rowsPerPage,
page * rowsPerPage + rowsPerPage,
)
const emptyRows = rowsPerPage - Math.min(rowsPerPage, data.length - (page * rowsPerPage)) const emptyRows = rowsPerPage - Math.min(rowsPerPage, data.length - page * rowsPerPage)
const isEmpty = size === 0 const isEmpty = size === 0
return ( return (
<React.Fragment> <React.Fragment>
{ !isEmpty && {!isEmpty && (
<Table aria-labelledby={label} className={classes.root}> <Table aria-labelledby={label} className={classes.root}>
<TableHead <TableHead columns={columns} order={order} orderBy={orderByParam} onSort={this.onSort} />
columns={columns}
order={order}
orderBy={orderBy}
onSort={this.onSort}
/>
<TableBody> <TableBody>
{ children(sortedData) } {children(sortedData)}
{ emptyRows > 0 && {emptyRows > 0 && (
<TableRow style={this.getEmptyStyle(emptyRows)}> <TableRow style={this.getEmptyStyle(emptyRows)}>
<TableCell colSpan={4} /> <TableCell colSpan={4} />
</TableRow> </TableRow>
} )}
</TableBody> </TableBody>
</Table> </Table>
} )}
{ isEmpty && {isEmpty && (
<Row className={classNames(classes.loader, classes.root)} style={this.getEmptyStyle(emptyRows + 1)}> <Row className={classNames(classes.loader, classes.root)} style={this.getEmptyStyle(emptyRows + 1)}>
<CircularProgress size={60} /> <CircularProgress size={60} />
</Row> </Row>
} )}
<TablePagination <TablePagination
component="div" component="div"
count={size} count={size}
rowsPerPage={rowsPerPage} rowsPerPage={rowsPerPage}
rowsPerPageOptions={[5, 10, 25, 50, 100]}
page={page} page={page}
backIconButtonProps={backProps} backIconButtonProps={backProps}
nextIconButtonProps={nextProps} nextIconButtonProps={nextProps}

View File

@ -22,8 +22,8 @@ type Props = SelectorProps & {
const iconStyle = { const iconStyle = {
color: secondary, color: secondary,
width: '32px', padding: '8px',
height: '32px', marginRight: '5px',
} }
const back = () => { const back = () => {

View File

@ -33,8 +33,8 @@ type Props = {
const iconStyle = { const iconStyle = {
color: secondary, color: secondary,
width: '32px', padding: '8px',
height: '32px', marginRight: '5px',
} }
const back = () => { const back = () => {