material-table-core / core

Datatable for React based on material-ui's table with additional features. Support us at https://opencollective.com/material-table-core
https://material-table-core.github.io
MIT License
296 stars 146 forks source link

How to style show/hide columns menu? #737

Closed Ian-Harrington closed 1 year ago

Ian-Harrington commented 1 year ago

How to style show/hide columns menu?

I am looking to style the items within the popover menu for the default action "columnsButton". From the source code here, I'd expect that passing styling for "formControlLabel" to an overridden Toolbar component would be propagated to elements within the popover menu. Doing so in the codesandbox, the searchField and showColumns action icon can be styled, but no change is seen in the menu styling. What am I missing to style the checkboxes and text in the menu?

codesandbox: https://codesandbox.io/s/material-table-starter-template-forked-iwtjhm?file=/src/index.js

Screen Shot 2023-01-30 at 15 07 21
import React, { useState } from "react";
import ReactDOM from "react-dom";
import MaterialTable, { MTableToolbar } from "@material-table/core";
import { makeStyles, createStyles } from "@mui/styles";

const rando = (max) => Math.floor(Math.random() * max);

const words = ["Paper", "Rock", "Scissors"];

const rawData = [];
for (let i = 0; i < 100; i++) {
  rawData.push({ id: rando(300), word: words[i % words.length] });
}

const columns = [
  { title: "Id", field: "id" },
  { title: "Word", field: "word" }
];

const useStyles = makeStyles((theme) =>
  createStyles({
    formControlLabel: {
      "& .MuiButtonBase-root": {
        color: "red"
      },
      "& .MuiCheckbox-root": {
        color: "red"
      },
      "& .Mui-Checked": {
        color: "pink"
      }
    },
    searchField: {
      "& .MuiInputBase-input": {
        color: "blue"
      }
    }
  })
);

const App = () => {
  const [data, setData] = useState(rawData);
  const classes = useStyles();
  return (
    <MaterialTable
      data={data}
      columns={columns}
      title="Starter Template"
      options={{ columnsButton: true }}
      components={{
        Toolbar: (props) => (
          <MTableToolbar
            {...props}
            className={classes.searchField + " " + classes.formControlLabel}
          />
        )
      }}
    />
  );
};
const rootNode = document.querySelector("#root");
ReactDOM.render(<App />, rootNode);
Domino987 commented 1 year ago

Hi, so https://github.com/material-table-core/core/blob/master/src/components/MTableToolbar/index.js#L184 this style just takes our styling, which is not the passed but generated by us. The classes are just used for the outer most wrapper. How would you propose to pass items to each child Menuitem?

Ian-Harrington commented 1 year ago

Understood. I'm new to React and don't have sufficient experience to suggest an implementation that is robust/extensible enough to include in this package.

For my own purposes I ended up creating a custom show/hide menu component with the desired styling which I included in the toolbar. Some thoughts that might be helpful to others with similar issues:

Thanks for the reply.