Applying specific theme for react material-table
Asked Answered
R

2

8

I'm trying to integrate react material-table (https://github.com/mbrn/material-table) with my project.

  1. I want to update the styling/theme.

I used something like.

<MaterialTable options={{
                        rowStyle: x => {
                            if ( x.id % 2 ) {
                            return { backgroundColor: "#f2f2f2" }
                            }
                        },
                        'headerStyle' : {
                            backgroundColor: 'red',
                            color: theme.palette.common.white
                        }
                        }}
    columns={columns}
    data={data}
    title="New Table"
/>

However I want to have a generic styling and theme like

const CustomTableCell = withStyles(theme => ({
  head: {
    backgroundColor: theme.palette.common.black,
    color: theme.palette.common.white,
  },
  body: {
    fontSize: 14,
  },
}))(TableCell);

Basically i want to have something like CustomMaterialTable which is nothing but my theme/style.

  1. Striping the table rows. In the above code snippet, I used a logic to have striped rows.
if ( x.id % 2 ) {
    return { backgroundColor: "#f2f2f2" }
}

Since my table will be having sorting, I want the it to be on a auto generated row id rather than x.id (where x is my data).

  1. I want to use rtl and text in multiple languages based on the language selection (dynamic).
Rundle answered 10/4, 2019 at 11:44 Comment(0)
G
4
  1. You can overrides components. Look at example: https://mbrn.github.io/material-table/#/docz-examples-10-example-component-overriding

  2. Can you try x.tableData.id instead of x.id, please?

  3. You should use material-ui theme with direction (ltr or rtl): https://material-ui.com/customization/themes/

Grantley answered 10/4, 2019 at 13:0 Comment(0)
I
0

you can use material-UI makeStyles for changing material-table theme. This example when I'm changing the look for material-table I implemented striped rows using

'&:nth-child(even)': {
      backgroundColor: '#FAF7FA',
 },

because when using rowStyle and after changing the table sorting, the stripped rows remain attached to their main fields.

Here is the full example:

export const useClasses = makeStyles(({ palette }) => ({
  root: {
    borderTopRightRadius: '12px',
    borderTopLeftRadius: '12px',
    boxShadow: '0px 5px 40px rgb(0 0 0 / 10%)',
    fontFamily: 'Montserrat',
    overflow: 'hidden',
    '& .MuiPaper-root ': {
      boxShadow: 'none',
    },
    '& .MuiTable-root': {
      color: palette.text.textPrimary,
      '& .MuiTableHead-root': {
        '& .MuiTableRow-head': {
          '& .MuiTableCell-head': {
            background: 'rgba(90, 0, 90, 0.09)',
            color: palette.text.textPrimary,
          },
        },
      },
      '& .MuiTableRow-root': {
        '&:nth-child(even)': {
          backgroundColor: '#FAF7FA',
        },
      },
    },
  },
}));
Imf answered 22/3, 2021 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.