Material-UI 当 FormControl only for Select 改变 FormHelperText 的边距

Material-UI change margin of FormHelperText when FormControl only for Select

我目前需要为FormHelperText 添加8px 的marginLeft。但是无法在 MuiInput formControl 规则或 MuiSelect 中完成。

这是 React 代码:

<FormControl error className={classes.margin} fullWidth>
        <InputLabel className={classes.label} id="demo-error-select-label">
          Error select
        </InputLabel>
        <Select
          labelId="demo-error-select-label"
          id="demo-error-select"
          value={option}
          onChange={handleChange}
          IconComponent={() => <Icon24UtilityChevron component="select" height="20px" />}>
          <MenuItem value="">Select</MenuItem>
          <MenuItem value={1}>Option1</MenuItem>
          <MenuItem value={2}>Option2</MenuItem>
          <MenuItem value={3}>Option3</MenuItem>
        </Select>
        <FormHelperText>Error</FormHelperText>
      </FormControl>

这是使用 createMuiTheme 覆盖默认样式的 MuiSelect 对象(为简要说明而省略):

const MuiSelect = {
  select: {
    backgroundColor: selectColors.background,
    padding: '12px 16px 12px 16px !important',
    fontSize: '18px',
    borderRadius: 12,
    borderWidth: '1px',
    borderStyle: 'solid',
    borderColor: selectColors.border,
    '&:focus': {
      borderRadius: 12,
      borderWidth: '2px',
      borderStyle: 'solid',
      borderColor: selectColors.borderFocus,
    },
  },
  selectMenu: {
    '&:hover:not(.Mui-disabled):not(.Mui-error)': {
      backgroundColor: selectColors.hoverBackground,
    },
  },
};

这是使用 createMuiTheme 覆盖默认样式的 MuiInput 对象(为简要说明而省略):


This is an example of the FormControl being created (need to move the Error label 8px to the left):
[![enter image description here][1]][1]
const MuiInput = {
  formControl: {
    'label + &': {
      marginTop: '2px',
    },
  },
  root: {
    borderRadius: '12px',
    borderColor: inputColors.inputBorder,
    borderWidth: '1px',
    borderStyle: 'solid',
    '&$error': {
      borderColor: inputColors.inputError,
    },
    '&:focus-within': {
      borderColor: inputColors.inputBorderFocus,
    },
    '& svg': {
      marginRight: '16px',
    },
  },
  input: {
    backgroundColor: inputColors.inputBackground,
    caretColor: inputColors.inputCaret,
    paddingLeft: '8px',
    paddingRight: '8px',
    color: inputColors.inputText,
    borderRadius: '12px',
  },
  multiline: {
    paddingTop: '0px',
    paddingBottom: '0px',
  },
  underline: {
    '&:hover:not(.Mui-disabled):before': { borderBottomWidth: '0px' },
    '&:before': { borderBottomWidth: '0px' },
    '&:after': { borderBottomWidth: '0px' },
  },
};

看看代码的可视化示例(下面的错误文本是需要移动的):

我可以使用 ~ 选择器来解决它。代码:

const MuiInput = {
  formControl: {
    'label + &': {
      marginTop: '2px',
    },
    '& ~ p': {
      marginTop: '4px',
      marginLeft: '8px',
    },
  },
...