Search code examples
reactjsmaterial-table

Add Dropdown Component to Material-Table Actions


I can't seem to figure out how to place a custom dropdown component within the Action section in the top right portion of the material-table. Overriding the action component doesn't render anything.

Below is what I had using a different table library, but what I'm going for with Material-Table.

enter image description here

Current Code

<CustomMaterialTable
  title='Data Comparison'
  columns={tableCols}
  data={tableData}
  options={{
    exportButton: true,
    exportCsv: (columns, data) => { csvDownload(data, buildFileName(stateName)) },
    pageSize: tableData.length
  }}
  components={{
    Container: props => props.children,
    Action: props => (
      <YearDropDown
        year={year}
        type='secondary'
        minWidth='full'
        handleChange={handleYearChange}
        variant='outlined'
        required
      />
    )
  }}
/>

CustomMaterialTable

import MaterialTable from 'material-table'
import React from 'react'

export default function CustomMaterialTable (props) {
  return (
    <MaterialTable {...props} />
  )
}

Solution

  • I think that what you are asking could be done by overriding the Action component as explained here, docs.

    Following that I ended up with this:

    enter image description here

    Of course you need to do some work on styling, but this could point you to the right direction.

    Table definition, look at components and action props:

    <MaterialTable
            tableRef={tableRef}
            columns={tableColumns}
            data={tableData}
            title="Custom Free Action example"
            options={{
              tableLayout: "fixed",
              actionsColumnIndex: -1,
              search: false,
              filtering: true
            }}
            components={{
              Action: props => (
                <div
                  style={{
                    marginBottom: "5rem",
                    marginTop: "1rem",
                    width: "150px"
                  }}
                >
                  <Select
                    options={selectData}
                  />
                </div>
              )
            }}
            actions={[
              {
                onClick: (event, rowData) => alert("something"),
                isFreeAction: true
              }
            ]}
          />
    

    Sandbox here.

    I don't know exactly what you are looking for but I added the filtering option to the table. Maybe that could be a different way to filter out data without having to overwrite the component and having to fight against the buil-in styles.

    Hope this helps! good luck