简体   繁体   中英

MaterialUI, how to overwrite styles of nested MUI component?

I am using react-admin and I am using their Create component that use TextField from @material-ui/core .

Of course I have read the documentation here .

I would like to completely customize the render for a custom view, this is how look the generated html:

<div class="create-page Component-root-55" notification="">
   <div class="MuiPaper-root-58 MuiPaper-elevation1-61 MuiPaper-rounded-59 MuiCard-root-57 Component-card-56 Create-card-53">
      <form class="simple-form" locale="en">
         <div class="MuiCardContent-root-86 CardContentInner-root-85">
            <div class="ra-input ra-input-email">
               <div class="MuiFormControl-root-11 MuiFormControl-marginNormal-12 MuiFormControl-fullWidth-14" locale="en">
                  <label class="MuiFormLabel-root-20 MuiInputLabel-root-15 MuiInputLabel-formControl-16 MuiInputLabel-animated-19" data-shrink="false" for="email"><span>Email *</span></label>
                  <div class="MuiInput-root-27 MuiInput-fullWidth-34 MuiInput-formControl-28 MuiInput-underline-31"><input aria-invalid="false" class="MuiInput-input-35 MuiInput-inputType-38 withRouter-Connect-SubscribeNewsletterForm---inputType-52" id="email" name="email" type="email" value=""></div>
               </div>
            </div>
         </div>
         <div class="MuiToolbar-root-97 MuiToolbar-regular-99 MuiToolbar-gutters-98 Toolbar-toolbar-92 Toolbar-desktopToolbar-93" role="toolbar">
            <div class="Toolbar-defaultToolbar-95">
               <button tabindex="0" class="MuiButtonBase-root-129 MuiButton-root-103 MuiButton-contained-114 MuiButton-containedPrimary-115 MuiButton-raised-117 MuiButton-raisedPrimary-118 SaveButton-button-101" type="submit">
                  <span class="MuiButton-label-104">
                     <svg class="MuiSvgIcon-root-132 SaveButton-iconPaddingStyle-102" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
                        <g>
                           <path d="M17 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V7l-4-4zm-5 16c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm3-10H5V5h10v4z"></path>
                        </g>
                     </svg>
                     Save
                  </span>
                  <span class="MuiTouchRipple-root-139"></span>
               </button>
            </div>
         </div>
         <div class="Toolbar-spacer-96"></div>
      </form>
   </div>
</div>

I have just a few experience in overriding styles in material-ui.

After succeeding in finding how to remove the boxShadow to the card thanks to the explanation in the Create.js , I am struggling finding how to edit what is within the create.

For instance, I would like to remove the background of MuiToolbar , identified with MuiToolbar-regular-99 , this is what I have:


const createStyles = {
  card: {
    boxShadow: 'none',
    borderRadius: 0,
  },
  toolbar: {
    toolbar: {
      backgroundColor: 'transparent',
    },
  },
};

const Create = withStyles(createStyles)(function Create({ classes, ...rest }) {
  return (
    <CreateDefault
      classes={classes}
      {...rest}
    />
  );
});

I also tried:

const createStyles = {
  card: {
    boxShadow: 'none',
    borderRadius: 0,
  },
  toolbar: {
    backgroundColor: 'transparent',
  },
};

const Create = withStyles(createStyles)(function Create({ classes, ...rest }) {
  console.log(classes);
  return (
    <CreateDefault
      classes={{
        card: classes.card,
        toolbar: classes.toolbar
      }}
      {...rest}
    />
  );
});

This doesn't apply any CSS to the toolbar, I don't get the logic and found jss so complex that I must ask for help.

How can I override nested styles with Material UI component, even if using a wrapper that wrap many?

Edit codesandbox

The closest I could get too: https://codesandbox.io/s/2uboj

It doesn't have by default the grey toolbar because of probably react-admin version used, but you can see the goal.

You don't have a sandbox and I can't see where you're using the <TextField /> component so I can't test if this works, but I can show you two options that should help you with classes.

  1. To apply multiple classes, you should do:

    className={[classes.card, classes.toolbar].join(' ')}

  2. To access the elements inside a material component, you need to use the CSS APIs listed under the component APIs on the material docs. TextField is a bit more complicated as it combines several other components. You need to pass your classes to each component as a prop. Eg

    <TextField InputProps={classes={classes}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM