I am using react-admin and I am using their Create
ponent that use TextField
from @material-ui/core
.
Of course I have read the documentation here.
I would like to pletely 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 plex that I must ask for help.
How can I override nested styles with Material UI ponent, even if using a wrapper that wrap many?
Edit codesandbox
The closest I could get too:
It doesn't have by default the grey toolbar because of probably react-admin version used, but you can see the goal.
I am using react-admin and I am using their Create
ponent that use TextField
from @material-ui/core
.
Of course I have read the documentation here.
I would like to pletely 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 plex that I must ask for help.
How can I override nested styles with Material UI ponent, 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.
Share Improve this question edited Jun 17, 2020 at 11:14 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked May 15, 2019 at 14:37 Dimitri KopriwaDimitri Kopriwa 14.4k33 gold badges116 silver badges232 bronze badges 1- please create a simple sandbox example or one here stackblitz. – Junius L Commented May 15, 2019 at 15:10
1 Answer
Reset to default 5You don't have a sandbox and I can't see where you're using the <TextField />
ponent so I can't test if this works, but I can show you two options that should help you with classes.
To apply multiple classes, you should do:
className={[classes.card, classes.toolbar].join(' ')}
To access the elements inside a material ponent, you need to use the CSS APIs listed under the ponent APIs on the material docs.
TextField
is a bit more plicated as it bines several other ponents. You need to pass your classes to each ponent as a prop. E.g.<TextField InputProps={classes={classes}}