【问题标题】:Material UI: How can I change the border color of the Select component?Material UI:如何更改 Select 组件的边框颜色?
【发布时间】:2021-08-15 00:01:08
【问题描述】:

我正在尝试从 Material UI 自定义 Select 组件。

这就是它的样子:

However, when the select component is focused, I want to change the border-color from material UI's blue to a custom red color.

我尝试设置样式,但它根本没有做任何事情

import FormControl from '@material-ui/core/FormControl';
import InputLabel from '@material-ui/core/InputLabel';
import MuiSelect from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles((theme) => ({
  formControl: {
    margin: theme.spacing(1),
    minWidth: 120,
  },
  select: {
    borderColor: '#FF0000',      //<------------ this does nothing
  },
}));

const Select = () => {
  const classes = useStyles();
  return (
    <FormControl variant="outlined" className={classes.formControl}>
      <InputLabel>Months</InputLabel>
      <MuiSelect label="Months" className={classes.select}>
        <MenuItem value="1">January</MenuItem>
        <MenuItem value="2">February</MenuItem>
        <MenuItem value="3">March</MenuItem>
        <MenuItem value="4">April</MenuItem>
      </MuiSelect>
    </FormControl>
  );
};

Select.propTypes = {};

export default Select;

【问题讨论】:

    标签: css reactjs material-ui


    【解决方案1】:

    试试这个:

    const useStyles = makeStyles((theme) => ({
      formControl: {
        margin: theme.spacing(1),
        minWidth: 120,
      },
      select: {
        '&.Mui-focused .MuiOutlinedInput-notchedOutline': {
            borderColor: 'red',
        },
      },
    }));
    
    

    【讨论】:

    【解决方案2】:

    如果有人对最新版本的 MUI 5 感兴趣。

    import {
      Select,
    } from '@mui/material';
    import { SxProps } from '@mui/material/styles';
    import classes from './component.module.css';
    
    const styles: SxProps = {
      select: {
        '.MuiOutlinedInput-notchedOutline': {
          borderColor: '#color',
        },
        '&:hover .MuiOutlinedInput-notchedOutline': {
          borderColor: '#color',
          borderWidth: '0.15rem',
        },
      },
    };
    
    <Select
         variant="outlined"
         sx={styles.select}
         inputProps={{
           classes: {
             icon: classes.icon,
               },
           }}
      >
    

    对于图标,可以单独创建一个文件component.module.css

    .icon {
        fill: #color;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 1970-01-01
      • 2018-12-25
      • 2021-12-07
      • 2022-08-23
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多