Material-UI, Meteor, React
So I want to nest a drop down list with onTouchTap (or onClick) functions inside of a card.
It seems to work fine, renders everything no bother - but when you expand the parent card, all in the list the onTouchTap fire at the same time.
I have tired also nesting other Card elements inside each other to the same effect.
Is this just a restriction of the code, or is there some work around?
<Card>
<CardHeader
title={this.props.foodItem.foodName}
subtitle={this.genPrtnImg()}
avatar={this.props.foodItem.imgURL}
actAsExpander={true}
showExpandableButton={true}
/>
<CardText expandable={true}>
<List subheader="Item Requests">
<ListItem
primaryText={userName}
secondaryText={"Has requested " + prtNo + " portions"}
leftAvatar={<Avatar src="http://thesocialmediamonthly.com/wp-content/uploads/2015/08/photo.png" />}
primaryTogglesNestedList={true}
nestedItems={[
<ListItem
key={1}
primaryText="Accept"
leftIcon={<SvgIcons.ActionCheckCircle color='Green'/>}
onTouchTap={this.handleAccept(userName, prtNo)}
/>,
<ListItem
key={2}
primaryText="Reject"
leftIcon={<SvgIcons.ContentBlock color='Red'/>}
onTouchTap={this.handleReject(userName)}
/>,
]}
/>
</List>
</CardText>
</Card>
Any help would be greatly appreciated, thanks!
There's a simpler way around this if you're using ES6
You can change your onClick method to:
onClick={() => { this.handleAccept(userName, prtNo) }}
onClick={this.handleAccept(userName, prtNo)}
will not work. That will call this.handleAccept
when it instantiates, NOT when the event triggers. You'll have to use a partial or a closure to get those variables.
Here's how you'd do it with a closure:
getAcceptHandler: function(userName, prtNo) {
handleAccept = function(event) {
do_something_with_event_or_ignore_it = event.target...
do_something_with_userName_and_or_prtNo = userName + prtNo
this.save(do_something_with_userName_and_or_prtNo)
...
}
return handleAccept
}
Then change the JSX like this:
<ListItem
key={1}
primaryText="Accept"
leftIcon={<SvgIcons.ActionCheckCircle color='Green'/>}
onTouchTap={this.getAcceptHandler(userName, prtNo)}
/>,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With