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

Something wrong with actions as functions with enabled selection (action as a function ignoring position props)

Open scipe opened this issue 6 years ago • 5 comments

Describe the bug My mission is add new action to table (where selection is true) and show this action only with condition. As a result I can not see my button. Table settings:

padding: 'dense',
actionsColumnIndex: -1,
...
selection: true,

Actions:

(rowData) => ({
   icon: LockIcon,
   tooltip: 'Do smth',
   position: 'row',
   hidden: false,
   onClick: (e, rowData) => {console.log('do something')}
}),

And there is no action until I turned off selection.

If I will remove rowData => ({...my_action}) and make action as object, everything start working again.

To Reproduce Steps to reproduce the behavior:

  1. Set table selection to true
  2. Add action as a function (as in example in Conditionals Actions Example here https://material-table.com/#/docs/features/actions)
  3. Profit, you not able to see your action

Expected behavior We should able to see action button as a row action

Versions: "material-table": "^1.57.2", "@material-ui/core": "4.9.2", "@material-ui/icons": "4.9.1", "@material-ui/lab": "^4.0.0-alpha.42", "@material-ui/styles": "^4.9.0", "react": "^16.9.0",

Any ideas?

P.S. I forked main branch and see the same behavior in demo.

scipe avatar Feb 18 '20 16:02 scipe

Ok, I think that problem in /src/material-table.js on rows 125-138. For some reason if we using actions as a function and selection in parent table, we set position for current action as 'toolbarOnSelect' instead of 'row'. I not sure that I understand why we doing this and what logic author wanna use here. @mbrn can you give me idea what are you wanna see here?

On local I am using for fix:

if (typeof action === "function") return { action: action, position: action().position ? action().position : "toolbarOnSelect" };

In /src/material-table.js on row 132

scipe avatar Feb 18 '20 17:02 scipe

+1

goncalolucas avatar Mar 17 '20 15:03 goncalolucas

I was able to get this to work - see here for more details

matthewoestreich avatar May 15 '20 16:05 matthewoestreich

I just wanna confirm that @oze4 PR resolve this problem. But its a kind of hammer and not a scalpel. The strange behavior still here.

scipe avatar Jun 04 '20 20:06 scipe

I have been looking at this and i can confirm what @scipe is mentioning. When you have something like this for actions:

actions={[
            (rowData) => {
              console.log('rowdata', rowData);
              return {
                icon: tableIcons.Steps,
                tooltip: 'Stuuuuffffff',
                onClick: () => setDialog(true),
                position: 'row'
              }
            },
            {
              icon: tableIcons.Add,
              tooltip: 'Stuuuuffffff',
              onClick: () => setDialog(true),
              position: 'row'
            }
          ]}

only the action element in the actions array that is not a function will be displayed. Which is a problem because it implies that you cannot have conditional action rendering with selection: true option.

LuisPinaSoares avatar Oct 04 '20 17:10 LuisPinaSoares