React JS MUI Datepicker奇怪的红色边框
在我的reactJS应用程序中,我设置了一些日期选择器,一切正常。
在自定义页面中,我有另一个日期选择器,我看到它周围有一个奇怪的红色边框:
代码是:
<DatePicker
disableFuture
style={{ width: "90%", border: "1px solid black" }}
inputFormat="yyyy-MM-dd"
renderInput={(params) => <TextField fullWidth {...params} />}
value={props.value}
fullWidth
onMouseDown={(e) => e.stopPropagation()}
onChange={(e) =>
props.handleChangeComponentValue(props.id, e.target.value)
}
onBlur={(e) => props.handleBlurComponent(props.id, e.target.value)}
/>
用 chrome 检查它我发现样式
<fieldset aria-hidden="true" class="sc-gKseQn jzeLFY MuiOutlinedInput-notchedOutline-SmBCs dwFpjw MuiOutlinedInput-notchedOutline">
<legend class="sc-iBPTik gOBiIn">
<span class="notranslate">​</span>
</legend>
</fieldset>
特别来自:
.hiztcv.Mui-error .MuiOutlinedInput-notchedOutline {
border-color: #f44336;
}
我没有设置任何它的类别,颜色是没有在我的项目中设置。
我错过了什么吗?
In my reactJS app, I've setted up some datepicker and everything works good.
In a custom page, i have another datepicker and i see a strange red border around it:
The code is:
<DatePicker
disableFuture
style={{ width: "90%", border: "1px solid black" }}
inputFormat="yyyy-MM-dd"
renderInput={(params) => <TextField fullWidth {...params} />}
value={props.value}
fullWidth
onMouseDown={(e) => e.stopPropagation()}
onChange={(e) =>
props.handleChangeComponentValue(props.id, e.target.value)
}
onBlur={(e) => props.handleBlurComponent(props.id, e.target.value)}
/>
inspecting it with chrome i see that the style comes from
<fieldset aria-hidden="true" class="sc-gKseQn jzeLFY MuiOutlinedInput-notchedOutline-SmBCs dwFpjw MuiOutlinedInput-notchedOutline">
<legend class="sc-iBPTik gOBiIn">
<span class="notranslate">​</span>
</legend>
</fieldset>
in particular:
.hiztcv.Mui-error .MuiOutlinedInput-notchedOutline {
border-color: #f44336;
}
I'm not setting any class of it, and that color is not set in my project.
Did i miss something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了解决这个问题,我们可以在日期选择器组件中应用 slotProps。
访问 https://mui.com/x/react-date-pickers/custom -组件/
To fix this issue we can apply slotProps in the Date picker component.
Visit https://mui.com/x/react-date-pickers/custom-components/
我也面临同样的问题。当我将 console.log 参数传入 renderInput 属性时,我看到它的默认属性为 error:true。
所以你可以这样修复它:
I also faced the same issue. When I console.log params coming to the renderInput property, I saw that it has the default property of error:true.
So you can fix it like this:
我有同样的情况,问题出在日期的初始值上。它是一个空字符串,DatePicker 将其解释为无效日期,因此我将初始值更改为
undefined
,现在它默认为今天的日期,并且没有错误状态。I had the same case and the problem was in the initial value of the date. It was an empty string and DatePicker interpreted it as an invalid date, so I changed the initial value to
undefined
, and now it has today's date as default and doesn't has an error state.在此处输入图像描述
当您同时使用两个材质 UI 数据选择器组件时,会出现此行为在一个页面中。有两种方法可以解决这个问题
一种是覆盖CSS
另一种是通过设置 error={false} 的默认值
1.重写CSS
上面的代码有Mui-error类,它负责显示红线。您可以尝试覆盖它。
enter image description here
This behaviour is seen when you are using two material UI data-picker components simultaneous in a single page.There are two ways of solving this
one is overriding the css
other is by setting the default value of error={false}
1.Overriding the css
The above code has Mui-error class which is responsible for show that red line. You can try overriding that.
问题是初始值不应该是空字符串,而应该是
null
,请参阅作者的信息 https://github.com/mui/mui-x/issues/10274当前投票最多的解决方案会导致以下问题:即使用户没有,日期也始终被视为有效t 填写任何日期...
The issue is that the initial value should NOT be an empty string but instead
null
, see info from author https://github.com/mui/mui-x/issues/10274The currently most upvoted solution causes the issue that the date is always considered valid even if a user hasn't filled in any date...