'How to add "plus-button" on top right in material-table for react and call my specific function in react

This is my code:

import React, { useState, useEffect } from 'react';
import { Fade } from "@material-ui/core";
import MaterialTable from 'material-table';
import { makeStyles } from '@material-ui/core/styles';

import './styles.css';

const useStyles = makeStyles(theme => ({
    root: {
        flexGrow: 1,
        width: '70%',
        margin: 'auto',
        marginTop: 20,
        boxShadow: '0px 0px 8px 0px rgba(0,0,0,0.4)'
    }
}));

function User(props) {
    const classes = useStyles();
    const [checked, setChecked] = React.useState(false);

    useEffect(() => {
        setChecked(prev => !prev);
    }, [])

    const [state, setState] = React.useState({
        columns: [
            { title: 'name', field: 'name' },
            { title: 'Setor', field: 'sector' }
        ],
        data: [
            { name: 'Tom Brady', setor: 'Quarterback'},
            { name: 'Aaron Rodgers', setor: 'Quarterback'},
            { name: 'Ryan Tannehill', setor: 'Quarterback'},
            { name: 'Julian Edelman', setor: 'Wide Receiver'},
            { name: 'Julio Jones', setor: 'Wide Receiver'},
            { name: 'Marcus Mariota', setor: 'Quarterback'},
            { name: 'Patrick Mahomes', setor: 'Quarterback'},
            { name: 'Antonio Brown', setor: 'Wide Receiver'},
            { name: 'Eli Manning', setor: 'Quarterback'},
            { name: 'Antonio Brown', setor: 'Wide Receiver'},
            { name: 'Mike Evans', setor: 'Wide Receiver'},
            { name: 'Russel Wilson', setor: 'Quarterback'},
            { name: 'Drew Brees', setor: 'Quarterback'},
            { name: 'Cam Newton', setor: 'Quarterback'}
        ],
        actions: [
            { icon: 'create', tooltip: 'Edit', onClick: (event, rowData) => alert('Edit ' + rowData.name + '?')},
            { icon: 'lock', tooltip: 'Block', onClick: (event, rowData) => alert('Block ' + rowData.name + '?')},
            { icon: 'delete', tooltip: 'Delete', onClick: (event, rowData) => alert('Delete ' + rowData.name + '?')}
        ],
        options: {
            headerStyle: { color: 'rgba(0, 0, 0, 0.54)' },
            actionsColumnIndex: -1,
            exportButton: true,
            paging: true,
            pageSize: 10,
            pageSizeOptions: [],
            paginationType: 'normal'
        }
    });


    return (
        <>
            <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
            <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />

            <Fade in={checked} style={{ transitionDelay: checked ? '300ms' : '0ms' }}>
                <div className={classes.root}>
                    <MaterialTable options={state.options} title="Users" columns={state.columns} data={state.data} actions={state.actions}></MaterialTable>
                </div>
            </Fade>
        </>
    );
}

export default User;

I wanna add this button:

enter image description here

If I follow the default code from here https://material-ui.com/pt/components/tables/, I must add this code:

 editable={{
        onRowAdd: newData =>
          new Promise(resolve => {
            setTimeout(() => {
              resolve();
              setState(prevState => {
                const data = [...prevState.data];
                data.push(newData);
                return { ...prevState, data };
              });
            }, 600);
          }),

But this code calls a specific function for material-table, I wanna call my own function, how can I add the previous button and call my own function? My own function will open a modal with some inputs, more inputs than I show on table.



Solution 1:[1]

You can use the position or isFreeAction property for having the action displayed there.

<MaterialTable
  title="Editable Example"
  columns={state.columns}
  data={state.data}
  actions={[
    {
      icon: "add_box",
      tooltip: "my tooltip",
      isFreeAction: true,
      onClick: () => {
        console.log("clicked");
      }
    }
  ]}
/>

The position parameter is more flexible providing different placements for the Button.

isFreeAction in the other hand accepts just a boolean for the action type.

Sandbox cloned from Mosh Feu https://codesandbox.io/s/material-demo-m8eug

Solution 2:[2]

You can override the toolbar, adding your custom button (or some other content!), as explained at https://stackoverflow.com/a/69854673/1288109 :

        <MaterialTable
            components={{
                Toolbar: (props) => (
                    <div
                        style={{
                            display: "flex",
                            justifyContent: "flex-end",
                            alignItems: "center"
                        }}
                    >
                        <div style={{ width: "13rem" }}>
                            <MTableToolbar {...props} />
                        </div>
                        <Tooltip title="Add">
                            <IconButton>
                                <AddBox />
                            </IconButton>
                        </Tooltip>
                    </div>
                ),
            }}
        />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aabdyli
Solution 2 Starnuto di topo