Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI List as children of Card, fires all onClick on main expand

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!

like image 261
George Batty Avatar asked Mar 26 '16 07:03

George Batty


2 Answers

There's a simpler way around this if you're using ES6

You can change your onClick method to:

onClick={() => { this.handleAccept(userName, prtNo) }}
like image 178
Sam Davies Avatar answered Oct 12 '22 10:10

Sam Davies


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)}
/>,
like image 4
Larry Maccherone Avatar answered Oct 12 '22 11:10

Larry Maccherone