简体   繁体   English

如何使用React覆盖默认的MaterialUI样式?

[英]How to override default MaterialUI styles with React?

I can´t style the Outline Select component with the properties I want when using Material UI with react, how can override default styles? 在将Material UI与React一起使用时,我无法使用想要的属性来对Outline Select组件进行样式设置,如何覆盖默认样式?

I have used withStyles but I can't achieve the expected look and feel. 我已经使用过withStyles,但无法达到预期的外观。 For example if I change the border using a custom Input in the Select, then the Label doesn't work as expected, the border and the label touch instead of the label like floating. 例如,如果我在“选择”中使用自定义输入来更改边框,则“标签”将无法正常工作,边框和标签会像浮动的那样触摸而不是标签。

import { createStyles, makeStyles, withStyles } from '@material-ui/core/styles';
import OutlinedInput from '@material-ui/core/OutlinedInput';
import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import FormControl from '@material-ui/core/FormControl';
import Select from '@material-ui/core/Select';

export const StyledSelect = ({ name, value, onChange, items }) => {
  const classes = useStyles();
  const inputLabel = React.useRef<HTMLLabelElement>(null);
  const [labelWidth, setLabelWidth] = React.useState(0);

  React.useEffect(() => {
    setLabelWidth(inputLabel.current!.offsetWidth);
  }, []);
  return (
    <FormControl variant="outlined" />
      <InputLabel
        ref={inputLabel}
        htmlFor={name}
      >
        {name}
      </InputLabel>
      <Select
        value={value || ''}
        onChange={onChange}
        input={<CustomInput labelWidth={labelWidth} />}
      >
        {items.map(item => {
          return (
            <MenuItem key={item.key} value={item}>
              {item.label}
            </MenuItem>
          );
        })}
      </Select>
    </FormControl>
  );
};


const CustomInput = withStyles(theme => ({
  root: {
    'label + &': {
      /* marginTop: theme.spacing(3), */
    },
  },
  /* label: {
    width: '',
  }, */
  input: {
    'borderRadius': 4,
    'position': 'relative',
    /* 'backgroundColor': theme.palette.background.paper, */
    'border': '2px solid #ced4da',
    /* 'fontSize': 16, */
    /* 'transition': theme.transitions.create(['border-color', 'box-shadow']), */
    // Use the system font instead of the default Roboto font.
    'fontFamily': [
      '-apple-system',
      'BlinkMacSystemFont',
      '"Segoe UI"',
      'Roboto',
      '"Helvetica Neue"',
      'Arial',
      'sans-serif',
      '"Apple Color Emoji"',
      '"Segoe UI Emoji"',
      '"Segoe UI Symbol"',
    ].join(','),
    '&:hover': {
      border: '2px solid red',
      borderRadius: 4,
    },
    '&:focus': {
      border: '2px solid #ced4da',
      borderRadius: 4,
      borderRadius: 4,
      borderColor: "#80bdff",
      boxShadow: "0 0 0 0.2rem rgba(0,123,255,.25)"
    },
  },
}))(OutlinedInput);

I expect to style only what I need and don't break the functionality of the OutlineSelect component. 我希望仅设置所需样式,并且不会破坏OutlineSelect组件的功能。

When you click an input field in material UI the class names is changed and a different style is applied to the label field . 当您单击材质UI中的输入字段时,类名称将更改,并且将不同的样式应用于label field

More specifically the class .MuiInputLabel-shrink is added to the label element. 更具体地说,将.MuiInputLabel-shrink类添加到了label元素。 If you want to target this styling, you must reference this class in your withStyles() 如果要定位此样式,则必须在withStyles()引用此类。

See the imput-label API here: https://material-ui.com/api/input-label/#main-content 请在此处查看imput-label API: https//material-ui.com/api/input-label/#main-content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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