MaterialUI - Changing the color Textfield on focus - reactjs

I'm trying to change the color of the label text in Textfield but I can't seem to figure it out.
Here is what I'm trying:
<TextField
value={value}
key={name}
label={label}
id={id}
name={name}
InputLabelProps={{
shrink: true,
FormLabelClasses: {
'root': {
'&:focused': {
color: 'white'
}
},
focused: 'true'
}
}}
/>
Can someone give me a pointer on what I'm doing wrong here?
I've also tried using the MuiThemeProvider but can't seem to figure that one out either:
const theme = createMuiTheme({
overrides: {
MuiFormLabel: {
focused: true,
root: {
'&.focused': {
color: 'white'
}
}
}
}
});
How can I change the color of the Label? In this photo, I want the "Notes" to match the color of the underline
Thanks for your help!

Tim!
Here is the snippet that should help you.
const {
TextField,
createMuiTheme,
MuiThemeProvider,
CssBaseline,
} = window['material-ui'];
const theme = createMuiTheme({
overrides: {
MuiFormLabel: {
root: {
"&$focused": {
color: "tomato",
fontWeight: "bold"
}
},
focused: {}
}
}
});
class Index extends React.Component {
render() {
return (
<MuiThemeProvider theme={theme}>
<div>
<CssBaseline />
<TextField label="Text field" InputLabelProps={{shrink:true}} />
</div>
</MuiThemeProvider>
);
}
}
ReactDOM.render(<Index />, document.getElementById('root'));
<script src="https://unpkg.com/react#latest/umd/react.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/react-dom#latest/umd/react-dom.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/#material-ui/core/umd/material-ui.development.js" crossorigin="anonymous"></script>
<div id="root"></div>

Another way of doing it without the override is to have:
const textStyles = makeStyles({
root: {
"& .Mui-focused": {
color: "tomato",
fontWeight: "bold"
},
});
class Index extends React.Component {
const TextClass = textStyles()
render() {
return (
<MuiThemeProvider theme={theme}>
<div>
<CssBaseline />
<TextField className={textStyles.root} label="Text field" InputLabelProps={{shrink:true}} />
</div>
</MuiThemeProvider>
);
}
}

for v5 of #mui this code works
const theme = createTheme({
components: {
MuiInputLabel: {
styleOverrides: {
root: {
fontWeight: 'bold',
"&.Mui-focused": {
color: 'rgba(0, 0, 0, 0.87)',
},
},
}
},
MuiTextField: {
defaultProps: {
variant: 'standard',
},
},
},

Related

Overriding MUI Stepper styles in React 2022

I've been struggling with the proper way to override styles for a React MUI Stepper component in 2022. More specifically in the active, and completed states for the label as well as the circle icon.
My Code
import { ThemeProvider } from '#mui/styles';
import { createTheme } from '#mui/system';
const theme = createTheme({
overrides: {
MuiStepIcon: {
root: {
'&$completed': {
color: 'pink',
},
'&$active': {
color: 'red',
},
},
active: {},
completed: {},
},
MuiStepLabel: {
root: {
color: 'red'
}
}
}
})
<ThemeProvider theme={theme}>
<Stepper activeStep={activeStep} className='serviceReferralWizardStepper'>
{steps.map((label) => {
const stepProps = {};
const labelProps = {};
return (
<Step className='stepper-holder' style={{ margin: '0 1.8rem' }} key={label} {...stepProps}>
<StepLabel {...labelProps}><span className='serviceReferralWizardStepper__label'>{label}</span></StepLabel>
</Step>
);
})}
</Stepper>
Currently I see no styles at all applied. Any advice would be apprecieted.
I recommend looking at MUI's documentation example: https://v4.mui.com/components/steppers/#CustomizedSteppers.js
You will need to create custom components to represent the styles you want.
const CustomeConnector = withStyles({
active: {
'& $line': {
borderColor: '#784af4',
},
},
completed: {
'& $line': {
borderColor: '#784af4',
},
},
})(StepConnector);
function CustomeStepIcon(props) {
const classes = // create styles
const { active, completed } = props;
return (
<div
className={clsx(classes.root, {
[classes.active]: active,
})}
>
{completed ? <Check className={classes.completed} /> : <div className={classes.circle} />}
</div>
);
}
<Stepper alternativeLabel activeStep={activeStep} connector={<CustomConnector />}>
{steps.map((label) => (
<Step key={label}>
<StepLabel StepIconComponent={CustomStepIcon}>{label}</StepLabel>
</Step>
))}
</Stepper>
UPDATE
Here is an updated link to the v5 implementation: https://mui.com/components/steppers/#customized-horizontal-stepper

How do you give material-ui drawer component a switchable theme?

I am making a react project but I have a problem getting my material-ui drawer to match with my dark/light switchable theme. I currently have given the drawer styling using makeStyles, but I can't for the life of me get it sync with ThemeProvider.
I have tried doing it all in ThemeProvider but the drawer required other methods. Then I tried doing it all with makeStyles and not using ThemeProvider at all but that didn't work either. I also tried putting a conditional ternary statement in makeStyles for this, but that didn't work.
Any help or advice that leads me in the right direction would be greatly appreciated!
function App() {
const dark_theme = createMuiTheme({
palette: {
primary: {
main: '#e78f23',
secondary: {
main: '#463d3d',
}
},
background: {
default: "#222222",
},
text: {
primary: "#ffffff"
},
}
})
const light_theme = createMuiTheme({
palete: {
primary: {
main: '#4892BC',
},
secondary: {
main: '#EEF3FF',
},
background: {
default: "#e4f0e2"
},
text: {
primary: "#000000"
},
}
});
const useStyles = makeStyles((theme) => ({
drawer: {
background : '#e78f23',
},
switchTrack: {
backgroundColor: "#000"
},
switchBase: {
color: "#000",
"&.Mui-checked": {
color: "white"
},
"&.Mui-checked + .MuiSwitch-track": {
backgroundColor: "white"
},
},
}));
const classes = useStyles()
const [light, setLight] = useState(true);
return (
<ThemeProvider theme={light ? light_theme : dark_theme}>
<CssBaseline />
<Router>
<main>
<Drawer id="Drawer" PaperProps={{className:classes.drawer}} open={drawerOpen} onClose={CloseDrawer}>
<List id="ListID">
<Button>
<ListItem button onClick={() => setLight(prev => !prev)} color="primary" class="themeButton">
<FormControl>
<FormLabel>Dark/Light</FormLabel>
<FormGroup>
<FormControlLabel
id="formControlLabel"
control = {
<Switch2
color="default"
classes={{
track: classes.switchTrack,
switchBase: classes.switchBase,
}}
size="medium"
position="center"
checked={checked}
onChange={toggleChecked}
labelPlacement="end"
/>
}
/>
</FormGroup>
</FormControl>
</ListItem>
</Button>

How to style the header of react-admin Datagrid?

I tried to follow https://marmelab.com/react-admin/Theming.html#writing-a-custom-theme to style the header of Datagrid (to use bold font style) as below:
const myTheme = createMuiTheme({
overrides: {
Datagrid: {
header: {
fontWeight: 'bold',
}
}
}
})
const App = () => (
<Admin theme={myTheme}>
// ...
</Admin>
);
However, the code above does not work.
What's wrong with that code?
And any ideas change styles the header of all of Datagrid instances?
Thanks,
Finally I managed to style the header of react-admin Datagrid as below:
const myTheme = createMuiTheme({
overrides:{
MuiTableRow: {
head: {
backgroundColor: 'lightgray',
"& > th ": {
color: 'black',
fontWeight: 'bold',
}
},
}
}
})
const App = () => (
<Admin theme={myTheme}>
// ...
</Admin>
);
To restyle ALL instances of a Data Grid in your application, e.g. set all headers to bold (which I personally think would make a better default anyway!):
const theme = createMuiTheme({
overrides: {
RaDatagrid: {
headerCell: {
fontWeight: 'bold',
},
}
},
});
<Admin theme={theme} ...>
</Admin>
Try this:
const myTheme = createMuiTheme({
datagrid: {
header: {
fontWeight: 'bold',
},
},
})
const listStyles = theme => ({
headerCell: theme.datagrid.header,
})
const CardList = withStyles(listStyles)(({ classes, ...props }) => (
<List {...props} >
<Datagrid classes={classes} >
<TextField source="id" />
...
</Datagrid>
</List>
))
Documentation: https://marmelab.com/react-admin/List.html#the-datagrid-component
section: "CSS API"
I wanted to customise the header of the ra-datagrid.
Here is how it looked before:
Here is how it looked after as required:
To do the above I added following styles:
const useStyles = makeStyles({
headerCell: {
backgroundColor: '#def2ff',
color: 'white',
fontWeight: 'bold',
},
row: {
'&:nth-of-type(odd)': {
backgroundColor: '#def2ff',
},
'&:last-child td, &:last-child th': {
border: 0,
},
},
});
const MyDataGridComponent = (props) => {
const classes = useStyles();
return (
<List
{...props}
>
<Datagrid
rowClick="show"
classes={{ headerCell: classes.headerCell, row: classes.row }}
>
<TextField source="id" label={'Kit ID'} />
<TextField source="mpi" label={'MPI ID'} />
<TextField source="jobNo" />
<DateField source="receivedDate" />
<DateField source="shippingDate" />
<TextField source="kitName" />
</Datagrid>
</List>
);
};
export default MyDataGridComponent;

Change TextField font color in MUI?

I'm currently using MUI.
And I'm having issues trying to change the font color of the multiline TextField.
<TextField className = "textfield"
fullWidth
multiline
label = "Debugger"
rows = "10"
margin = "normal"/>
And the CSS:
.textfield {
background-color: #000;
color: green;
}
However, somehow I only get the black background and the font is still black. Does anyone know how to properly change the font color of a TextField using MUI?
In MUI v5, you can just do this using sx prop:
<TextField sx={{ input: { color: 'red' } }} />
A bit longer approach if you want something more reusable:
const options = {
shouldForwardProp: (prop) => prop !== 'fontColor',
};
const StyledTextField = styled(
TextField,
options,
)(({ fontColor }) => ({
input: {
color: fontColor,
},
}));
<StyledTextField label="Outlined" fontColor="green" />
<StyledTextField label="Outlined" fontColor="purple" />
Live Demo
I referred this page TextField API
And I override the TextField using Classes
const styles = theme => ({
multilineColor:{
color:'red'
}
});
Apply the class to TextField using InputProps.
<TextField
className = "textfield"
fullWidth
multiline
InputProps={{
className: classes.multilineColor
}}
label = "Debugger"
rows = "10"
margin = "normal" />
EDIT In older version you have to specify the key input
<TextField
className = "textfield"
fullWidth
multiline
InputProps={{
classes: {
input: classes.multilineColor
}
}}
label = "Debugger"
rows = "10"
margin = "normal"
/>
Hope this will work.
Using inputProps is correct, as others have posted. Here's a slightly simpler example:
<TextField
multiline
inputProps={{ style: { color: "red" } }}
/* ... */
/>
How to set color property and background property of Text Field
import PropTypes from "prop-types";
import { withStyles } from "#material-ui/core/styles";
import TextField from "#material-ui/core/TextField";
const styles = {
root: {
background: "black"
},
input: {
color: "white"
}
};
function CustomizedInputs(props) {
const { classes } = props;
return (
<TextField
defaultValue="color"
className={classes.root}
InputProps={{
className: classes.input
}}
/>
);
}
CustomizedInputs.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(CustomizedInputs);
//textfield customization
const CssTextField = withStyles({
root: {
'& .MuiInputBase-root': {
color: 'white',
},
},
})(TextField);
This should work !
import { TextField, makeStyles } from "#material-ui/core";
const useStyles = makeStyles((theme) => ({
input: {
color: "#FFF",
},
}));
const MyInput = () => {
const classes = useStyles();
return (
<TextField
inputProps={{ className: classes.input }}
id="outlined-basic"
label="Write something..."
variant="outlined"
/>
);
};
export default MyInput;
If you are looking for a more generic fix, you can change your theme to contain that color, in my case I needed to change the input background and the disabled as well, so I end up using the ThemeProvider and a custom Theme.
Custom theme
const theme = createTheme({
components: {
MuiInputBase: {
styleOverrides: {
root: {
backgroundColor: '#fff',
'&.Mui-disabled': {
backgroundColor: '#e4e4e4',
},
},
},
},
},
});
const withDefaultTheme =
<P extends object>(Component: React.ComponentType<P>) =>
(props: any) =>
(
<ThemeProvider theme={theme}>
<Component {...props} />
</ThemeProvider>
);
This is working in my case:
import React from 'react';
import { TextField, } from '#mui/material';
import { makeStyles } from "#mui/styles";
const useStyles = makeStyles((theme) => ({
textfield_input: {
color: `#c5cae9 !important`,
}
}));
function Videoedit() {
const classes = useStyles();
return (<div>
<TextField
value={title}
required
label="Title"
variant="filled"
inputProps={{className: classes.textfield_input}}
color="primary"
focused
/>)
</div>;
}
export default Videoedit;
if you are using styled component with TextField then just write this code inside your styled component:
input: {
color: '#ffffff',
},
if you want to change placeholder color:
label: {
color: '#ffffff',
},
Use your Component like below
const MyTextField = withStyles({
root: {
"& .MuiInputBase-root.Mui-disabled": {
color: "rgba(0, 0, 0,0.0)"
},
"& .MuiFormLabel-root.Mui-disabled": {
color: "rgba(0, 0, 0,0.0)"
},
}
})(TextField);
Try below css
.textfield{
color: #000;
}

How to change Error color and underline color on input focus using material-ui v1.0.0-beta.40

I am using Material-UI v1.0.0-beta.40 and I want to change the border color and error text color.
How can this be done?
One of the ways to do it is inside of MuiTheme
import { createMuiTheme } from 'material-ui/styles';
const myTheme = createMuiTheme({
overrides:{
MuiInput: {
underline: {
'&:after': {
backgroundColor: 'any_color_hex',
}
},
},
}
});
export default myTheme;
and then import it into your component and use:
import {MuiThemeProvider} from 'material-ui/styles';
import myTheme from './components/myTheme'
<MuiThemeProvider theme = {myTheme}>
<TextField />
</MuiThemeProvider>
Hope this helps!
You can accomplish that by using errorStyle attribute
errorStyle The style object to use to override error styles
Version 0.20.0
<TextField
hintText="Hint Text"
errorText="This field is required"
errorStyle={{color: 'green'}}
/>
Working Demo
Version 1.0.0 beta
const styles = theme => ({
greenLabel: {
color: '#4CAF50',
},
greenUnderline: {
'&:before': {
backgroundColor: '#4CAF50',
},
},
greenUnderline: {
'&:after': {
backgroundColor: '#4CAF50',
},
},
});
<FormControl >
<InputLabel style={{color: 'green'}} htmlFor="name-simple">Error</InputLabel>
<Input classes={{ inkbar: classes.greenInkbar, underline: classes.greenUnderline }} id="name-simple" value={this.state.name} onChange={this.handleChange} />
</FormControl>
Here is the solution based on most recent changes by material-ui v1. Hope It's help
import { createMuiTheme } from '#material-ui/core/styles';
const inputBoxTheme = createMuiTheme({
overrides:{
MuiInput: {
underline: {
'&:after': {
borderBottom: '2px solid #fff',
},
"&:after": {
borderBottom: '2px solid #fff',
}
},
},
}
});
export default inputBoxTheme;
import { MuiThemeProvider } from '#material-ui/core/styles';
import inputBoxTheme from './theme/inputBoxTheme'
<MuiThemeProvider theme = {inputBoxTheme}>
<TextField />
</MuiThemeProvider>
An override isn't necessary to change the error color globally. It can be defined wherever you define your global colors / theme elements. For example:
import createMuiTheme from '#material-ui/core/styles/createMuiTheme';
export default createMuiTheme({
palette: {
primary: {
main: '#FFFFFF',
},
secondary: {
main: '#6200EE',
},
error: {
main: '#D0180B',
},
},
});

Resources