【问题标题】:Change InputLabel color of a Select component when clicked/focused单击/聚焦时更改 Select 组件的 InputLabel 颜色
【发布时间】:2019-06-17 22:39:50
【问题描述】:

如果您查看此处的组件:https://material-ui.com/components/selects/,您会看到单击时,标签会向上移动并最小化,但也会改变颜色(以及定义文本的底部边框/线)。

我想出了如何更改所有颜色,除了单击或聚焦时最小化的文本。如果有人可以请帮助我。快把我逼疯了

如果你能解释你是如何得出这个结论的,那么奖励积分,这样我也可以自己学习如何做到这一点。

const useStyles = makeStyles(theme => ({
  root: {
    display: 'flex',
    flexWrap: 'wrap',
  },
  formControl: {
    margin: theme.spacing(1),
    minWidth: 120,
  },
  inputLabel: {
    color: 'lightgray',
    focused: {
      color: 'orange'  // does not work
    }
  },
  select: {
    color: 'white',
    '&:before': {  // changes the bottom textbox border when not focused
      borderColor: 'red',
    },
    '&:after': {    // changes the bottom textbox border when clicked/focused.  thought it would be the same with input label
      borderColor: 'green',
    }
  }
}));

<FormControl className={classes.formControl}>
  <InputLabel
    className={classes.inputLabel}
  >Number of Lists</InputLabel>
  <Select
      className={classes.select}
      value={values.age}
      onChange={handleChange}
      inputProps={{
        name: 'age',
        id: 'age-simple',
      }}
  >
    <MenuItem value={1}>One</MenuItem>
    <MenuItem value={2}>Two</MenuItem>
  </Select>
</FormControl>

【问题讨论】:

    标签: javascript reactjs material-ui


    【解决方案1】:

    您可以通过以下方式实现此目的(假设 Material-UI v4 或更高版本):

      inputLabel: {
        color: "lightgray",
        "&.Mui-focused": {
          color: "orange"
        }
      },
    

    以下是相关文档:https://material-ui.com/customization/components/#pseudo-classes

    在版本 4 之前,您需要以下内容:

    // This is similar to Brad Ball's answer, but this nested syntax ensures proper specificity for the focused CSS.
      inputLabel: {
        color: "lightgray",
        "&$inputFocused": {
          color: "orange"
        }
      },
      inputFocused: {}
    
    // then in the JSX:
      <InputLabel
        className={classes.inputLabel}
        classes={{ focused: classes.inputFocused }}
      >
    

    【讨论】:

    • 是的,它成功了,非常感谢。并感谢您的链接。我只是通过 SO 链接找到了另一个,但我真的不明白如何自己想出解决方案。非常感谢!
    【解决方案2】:

    这里是使用focused的语法:

    const useStyles = makeStyles(theme => ({
      root: {
        display: "flex",
        flexWrap: "wrap"
      },
      formControl: {
        margin: theme.spacing(1),
        minWidth: 120
      },
      inputLabel: {
        color: "lightgray"
      },
      inputFocused: {
        color: "orange" // does not work
      },
      select: {
        color: "white",
        "&:before": {
          // changes the bottom textbox border when not focused
          borderColor: "red"
        },
        "&:after": {
          // changes the bottom textbox border when clicked/focused.  thought it would be the same with input label
          borderColor: "green"
        }
      }
    }));
    
    <FormControl className={classes.formControl}>
      <InputLabel
        className={classes.inputLabel}
        classes={{ focused: classes.inputFocused }}
      >
        Number of Lists
      </InputLabel>
      <Select
        className={classes.select}
        value={values.age}
        onChange={handleChange}
        inputProps={{
          name: "age",
          id: "age-simple"
        }}
      >
        <MenuItem value={1}>One</MenuItem>
        <MenuItem value={2}>Two</MenuItem>
      </Select>
    </FormControl>;
    

    【讨论】:

    • 我真的希望它能工作,但不幸的是我无法让它工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2011-09-10
    • 2016-08-09
    相关资源
    最近更新 更多