add checkbox selection to table
This commit is contained in:
parent
e738ccef48
commit
922ea244b2
|
@ -1,4 +1,4 @@
|
||||||
import React, { Fragment } from 'react'
|
import React, { Fragment, useState } from 'react'
|
||||||
import classnames from 'classnames'
|
import classnames from 'classnames'
|
||||||
import Typography from '@material-ui/core/Typography'
|
import Typography from '@material-ui/core/Typography'
|
||||||
import Checkbox from '@material-ui/core/Checkbox'
|
import Checkbox from '@material-ui/core/Checkbox'
|
||||||
|
@ -70,7 +70,7 @@ const styles = () => ({
|
||||||
|
|
||||||
const useStyles = makeStyles(styles)
|
const useStyles = makeStyles(styles)
|
||||||
|
|
||||||
function TableHeader() {
|
function TableHeader({ allSelected, selectAll }) {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
const { tableHeader } = classes
|
const { tableHeader } = classes
|
||||||
return (
|
return (
|
||||||
|
@ -80,27 +80,33 @@ function TableHeader() {
|
||||||
<Typography className={classnames(tableHeader, classes.headerId)}>Pledge ID</Typography>
|
<Typography className={classnames(tableHeader, classes.headerId)}>Pledge ID</Typography>
|
||||||
<Typography className={classnames(tableHeader, classes.headerFunded)}>Funded on</Typography>
|
<Typography className={classnames(tableHeader, classes.headerFunded)}>Funded on</Typography>
|
||||||
<Typography className={classnames(tableHeader, classes.headerSelect)}>Select all</Typography>
|
<Typography className={classnames(tableHeader, classes.headerSelect)}>Select all</Typography>
|
||||||
<Checkbox classes={{ root: classnames(classes.select, classes.checkBox), checked: classes.checked }} color="primary" disableRipple labelPlacement="start" label="start" />
|
<Checkbox classes={{ root: classnames(classes.select, classes.checkBox), checked: classes.checked }} checked={allSelected} onChange={selectAll} color="primary" disableRipple labelPlacement="start" label="start" />
|
||||||
</Fragment>
|
</Fragment>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function TableRow({ pledge, amtFormatter, tokenLabel }) {
|
function TableRow({ pledge, amtFormatter, tokenLabel, selectedPledges, setSelected }) {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
const { id, amount, creationTime, pledgeState } = pledge
|
const { id, amount, creationTime, pledgeState } = pledge
|
||||||
|
const isSelected = selectedPledges ? selectedPledges.includes(id) : false
|
||||||
|
const handleChange = () => {
|
||||||
|
if (isSelected) return setSelected(selectedPledges.filter(p => p !== id))
|
||||||
|
setSelected([...selectedPledges, id])
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
<Typography className={classes.rowStatus}>{pledgeState}</Typography>
|
<Typography className={classes.rowStatus}>{pledgeState}</Typography>
|
||||||
<Typography className={classes.rowAmount}>{amtFormatter(amount)} {tokenLabel}</Typography>
|
<Typography className={classes.rowAmount}>{amtFormatter(amount)} {tokenLabel}</Typography>
|
||||||
<Typography className={classes.rowId}>{toDecimal(id)}</Typography>
|
<Typography className={classes.rowId}>{toDecimal(id)}</Typography>
|
||||||
<Typography className={classes.rowFunded}>{getDateFromTimestamp(creationTime, true)}</Typography>
|
<Typography className={classes.rowFunded}>{getDateFromTimestamp(creationTime, true)}</Typography>
|
||||||
<Checkbox classes={{ root: classnames(classes.select, classes.checkBox), checked: classes.checked }} color="primary" disableRipple />
|
<Checkbox classes={{ root: classnames(classes.select, classes.checkBox), checked: classes.checked }} color="primary" disableRipple checked={isSelected} onChange={handleChange} />
|
||||||
</Fragment>
|
</Fragment>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function Pledges({ match }) {
|
function Pledges({ match }) {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
const [selectedPledges, setSelected] = useState([])
|
||||||
const projectId = match.params.id
|
const projectId = match.params.id
|
||||||
const { loading, error, data } = useQuery(getProfileWithPledges, {
|
const { loading, error, data } = useQuery(getProfileWithPledges, {
|
||||||
variables: { id: formatProjectId(projectId) }
|
variables: { id: formatProjectId(projectId) }
|
||||||
|
@ -112,18 +118,25 @@ function Pledges({ match }) {
|
||||||
const { pledges, projectInfo: { goalToken } } = data.profile
|
const { pledges, projectInfo: { goalToken } } = data.profile
|
||||||
const amtFormatter = getHumanAmountFormatter(goalToken)
|
const amtFormatter = getHumanAmountFormatter(goalToken)
|
||||||
const tokenLabel = getTokenLabel(goalToken)
|
const tokenLabel = getTokenLabel(goalToken)
|
||||||
|
const allSelected = selectedPledges.length === pledges.length
|
||||||
|
const selectAll = () => {
|
||||||
|
if (allSelected) return setSelected([])
|
||||||
|
setSelected(pledges.map(p => p.id))
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classes.main}>
|
<div className={classes.main}>
|
||||||
<TableHeader />
|
<TableHeader allSelected={allSelected} selectAll={selectAll} />
|
||||||
{pledges.map(p =>
|
{pledges.map(p => (
|
||||||
<TableRow
|
<TableRow
|
||||||
key={p.id}
|
key={p.id}
|
||||||
pledge={p}
|
pledge={p}
|
||||||
amtFormatter={amtFormatter}
|
amtFormatter={amtFormatter}
|
||||||
tokenLabel={tokenLabel}
|
tokenLabel={tokenLabel}
|
||||||
|
selectedPledges={selectedPledges}
|
||||||
|
setSelected={setSelected}
|
||||||
/>
|
/>
|
||||||
)}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue