material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

Preventing a row from being selected when a child cell is clicked does not work

Open zealoushacker opened this issue 10 years ago • 14 comments

Problem description

In my use case I have a <SelectField> being rendered in a specific <TableRowColumn>, which allows the user to pick an option for a specific row, which is used elsewhere in the app.

When this <SelectField> is clicked, the <TableRow> in which that field is rendered is selected.

I attempted to prevent this by implementing an onCellClick handler on the containing <Table>:

<Table 
  ... 
  onCellClick={(row, column, evt) => { 
    if (column === 3) evt.stopPropagation(); evt.preventDefault(); 
  } 
  ...
>
  ...
</Table>

The column index in question is definitely 3.

Unfortunately this doesn't seem to do what it ought to, intuitively speaking: prevent that row from being selected or deselected.

i.e.: When the user picks something from a <SelectField> contained in a row, that row should not be selected or deselected.

I've verified that onCellClick is triggered before row selection.

Steps to reproduce

See above.

Versions

  • Material-UI: "material-ui": "^0.15.0",
  • React: "react": "^15.0.1",
  • Browser: Version 50.0.2661.102 (64-bit)

zealoushacker avatar Jun 21 '16 16:06 zealoushacker

@zealoushacker

Use selectable={false} in the desired TableRow

nehalbhanushali avatar Jul 01 '16 21:07 nehalbhanushali

@nehalbhanushali that isn't a workable solution, because I need that row to be selectable, while still giving me the flexibility to manipulate a field within that row.

zealoushacker avatar Jul 01 '16 23:07 zealoushacker

Hey @zealoushacker, what you have to do is write a React component that returns a TableRowColumn, and pass along whatever events you want to be fired.

Example of me:

import React from 'react';
import {TableRowColumn} from 'material-ui';

const TableRowColumnWrapper = React.createClass({

    render() {
        return (
            <TableRowColumn style={this.props.style} onCellClick={this.props.onCellClick}>
                {this.props.children}
            </TableRowColumn>
        );
    }
});

export default TableRowColumnWrapper;

This prevents onRowSelection being called on the given props. You basically have to do nothing instead of using this component as your 'column' that should not call a row selection. So somewhere in your table code:

<TableBody>
    _.map(this.props.data, (item, index) => {
        return (
            <TableRow>
                <TableRowColumn>{item.propertyA}</TableRowColumn>
                <TableRowColumn>{item.propertyB}</TableRowColumn>
                <TableRowColumn>{item.propertyC}</TableRowColumn>
                <TableRowColumnWrapper>
                    {item.propertyD} // You could also render a TextField here to modify some data such as a numeric value
                </TableRowColumnWrapper>
            </TableRow>
        );
    })
</TableBody>

Serfenia avatar Jul 08 '16 14:07 Serfenia

Hi,

I have the same issue. I have a Table with IconButtons inside. Every Row in the Table needs to have different actions. Selection needs to be available too. However if I do the callback like this:

                <TableRow key={index}>
                    <TableRowColumn>{file.timestamp}</TableRowColumn>
                    <TableRowColumn colSpan="2">{file.name}</TableRowColumn>
                    <TableRowColumn colSpan="1">{file.fileSize}</TableRowColumn>
                    <TableRowColumn>{file.path}</TableRowColumn>
                    <TableRowColumn>{file.senderHostname}</TableRowColumn>
                    <TableRowColumn>{file.senderUsername}</TableRowColumn>
                    <TableRowColumn>{file.uuid}</TableRowColumn>
                    <TableRowColumn>{file.env}</TableRowColumn>
                    <TableRowColumn>
                        <IconButton onTouchTap={event => this._download(event, file.id) }><DownloadIcon /></IconButton>
                        <IconButton onTouchTap={event => this._delete(event, file.id) }><DeleteIcon /></IconButton>
                        <IconButton onTouchTap={event => this._preview(event, file.id) }><SearchIcon /></IconButton>
                    </TableRowColumn>
                </TableRow>
_delete (event, fileId) {
    const { archiveDataStore, dispatch } = this.props;

    dispatch(deleteFile(fileId));

    event.stopPropagation();
    event.preventDefault();
}`

The Row gets selected.

jonashartwig avatar Jul 11 '16 06:07 jonashartwig

Hello @jonashartwig, just use a wrapper for the column like I did, then the event won't even pass through to the TableBody and Table

Serfenia avatar Jul 11 '16 07:07 Serfenia

@Serfenia I did not think that the same solution would work, but it does. I bit strange to do it like this. I would assume the event.preventDefault() solution to work.

jonashartwig avatar Jul 11 '16 08:07 jonashartwig

@jonashartwig that's what I was hoping for as well. I had tried preventDefault() and stopPropagation() with no luck too. I think that interface should be honored.

@Serfenia I think your solution is a good workaround in the meantime.

zealoushacker avatar Jul 11 '16 19:07 zealoushacker

@jonashartwig @zealoushacker Indeed you would suspect it, though the onCellClick is not a 1 on 1 binding with the callback you provide. It stilll calls onRowClick, as the Table property onCellClick is passed down to the TableRow. That is visible here

Serfenia avatar Jul 12 '16 15:07 Serfenia

@zealoushacker The table component has to be re-designed and hopefully, all the issues that the table component has right now, won't be there in the new component. Meanwhile, let's close this issue. However, if you still have some questions regarding it, you can comment below and we'll look into it! 😄

aahan96 avatar Jul 22 '16 16:07 aahan96

@zealoushacker: you can try this code

          <TableRowColumn>
            <div onClick={(e) => {
              e.preventDefault();
              e.stopPropagation();
            }}>
              <AutoComplete
                id={ `${i + 1}`}
                hintText=""
                dataSource={this.state.titles}
                onUpdateInput={(v) => this.onUpdateInput(v)}

              />
            </div>
          </TableRowColumn>

ghost avatar Sep 26 '16 15:09 ghost

@ghost Thank you for the code, works like a charm, but I just wanted to add a note that in my case, this does not work when onClick is substituted with onTouchTap.

jnishiyama avatar Feb 12 '17 11:02 jnishiyama

For me, @Serfenia 's solution worked to the point of not raising the row select event and highlighting the row, however when you then want to select the row, the background for each of the cells will change apart from the TableRowColumnWrapper cell?

Is this what you are seeing also?

timbjames avatar Jul 28 '17 08:07 timbjames

I had the same problem and I was able to work around it by adding a class to the cell and checking the event.target.className on the onClick function.

lolero avatar Apr 05 '19 13:04 lolero

Setting: disableClickEventBubbling: true in the column def worked for me

harveyconnor avatar Apr 28 '21 00:04 harveyconnor