Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Text Field focus style in Material-UI?

I am having a Search box like this when focusing: enter image description here

How to change the colour of its background, border and icon of it? And also the way to modify when not focusing.

My code:

<TextField
    className={classes.searchBarStyle}
    placeholder="Search"
    type="search"
    margin="normal"
    variant="outlined"
    InputProps={{
        startAdornment: (
            <InputAdornment position="start">
                <Search />
            </InputAdornment>
        ),
        classes: {input: classes.input}
    }}
/>

CSS:

searchBarStyle: {
    height: "40px",
    width: "200px",
    margin: "0 0 0 0",
    float: "right",
},
input: {
    color: "white",
    textDecoration: "none",
    '&::placeholder': {
        color: 'white',
        fontWeight: "400"
    }
}
like image 368
quanpham0805 Avatar asked Dec 12 '18 02:12

quanpham0805


People also ask

How do I change the color on my Mui focus?

Material-UI v5 The easiest way to change the focus color of the TextField is to set the color props which is quite limited because it only accepts these values, (the color can be extended but you need to write a bit of code).

What is Helpertext?

Helper text gives context about a select, such as how the selection will be used. It should be visible either persistently or only on invalid state.

How do you make a text field mandatory in material UI?

We can use TextField component in Material UI to show input textbox. import TextField from "@material-ui/core/TextField"; When using the TextField component, we can pass the required attribute to mark the component as required.


1 Answers

I've same problem too then I found your question and try to solve it by myself. I think this isn't late to answer.

You can use makeStyles from @material-ui/core/styles for example, I create border-color & border-raduis on default, border-color and border-width in focus and Change icon to

import { makeStyles } from '@material-ui/core/styles';
import { TextField } from '@material-ui/core/TextField';
import PersonAddIcon from '@material-ui/icons/PersonAdd';

const useStyles = makeStyles(theme => ({
    searchBarStyle: {
        height: "40px",
        width: "200px",
        margin: "0 0 0 0",
        float: "right",
        "& .MuiOutlinedInput-root": {
            "& fieldset": { 
                borderRadius: "20px",
                borderColor: "#000fff"
            },
        "&.Mui-focused fieldset": {
            borderColor: "#C52328"
            borderWidth: "2px"
        }
    }
}
}))


const CustomTextField = () => {
const classes = useStyles()
return <TextField
    className={classes.searchBarStyle}
    placeholder="Search"
    type="search"
    margin="normal"
    variant="outlined"
    InputProps={{
        startAdornment: (
            <InputAdornment position="start">
                <PersonAddIcon />
            </InputAdornment>
        )
    }}
/>
}

export default CustomTextField;

All of className that I modified can check in Inspect mode in Browser

if you want more information, check this link (Check at Customized Inputs)

TextField React component- Material-UI

I hope this answer can help you.

like image 92
CartoonNetwork Avatar answered Sep 30 '22 14:09

CartoonNetwork