微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

仅用于Select

如何解决仅用于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>

这是 MuiSelect 对象,它使用 createMuiTheme 覆盖了认样式(省略了其简要说明):

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',borderColor: selectColors.borderFocus,},selectMenu: {
    '&:hover:not(.Mui-disabled):not(.Mui-error)': {
      backgroundColor: selectColors.hoverBackground,};

这是 MuiInput 对象,它使用 createMuiTheme 覆盖了认样式(省略了其简要说明):


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,'&$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' },};

看一下代码的直观示例(较低的错误文本是需要移动的文本):

example

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。