You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Material UI Datepicker聚焦时样式不生效,如何添加focus状态box-shadow效果

解决方案

样式不生效的核心原因是:.MuiInputBase-root是输入框的外层容器,本身不会触发:focus伪类,实际获得焦点的是其内部的原生<input>元素。
你只需要把样式中的&:focus选择器替换为&:focus-within即可,:focus-within伪类会在容器内部任意元素获得焦点时触发。另外注意你写的边框色值#86b7f位数不足,需要补全为6位十六进制色值,示例中补为#86b7fe。
修改后的样式代码如下:

export const styles = makeStyles(() => ({
  root: {
    marginTop: 0,
    "& .MuiInputBase-root": {
      border: "1px solid grey",
      borderRadius: "0.25rem",
      padding: "0.375rem 0.75rem",
      // 替换为:focus-within实现内部元素聚焦时触发样式
      "&:focus-within": {
        borderColor: "#86b7fe",
        boxShadow: "0 0 0 0.25rem red"
      },
      "&:before": {
        borderBottom: "none"
      },
      "&:after": {
        borderBottom: "none"
      },
      "&:hover:hover:not(.Mui-disabled):before": {
        borderBottom: "none"
      },
      "& .MuiButtonBase-root": {
        padding: 0
      }
    }
  }
}));

修改后输入框聚焦时就会正常显示你需要的边框和阴影效果。

内容的提问来源于stack exchange,提问作者CCCC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 22:30:02