gpt4 book ai didi

React JS MUI Datepicker strange red border(反应JS MUI日期选择器奇怪的红色边框)

转载 作者:bug小助手 更新时间:2023-10-24 17:55:19 38 4
gpt4 key购买 nike



In my reactJS app, I've setted up some datepicker and everything works good.

在我的reactJS应用程序中,我设置了一些Date Picker,一切都运行得很好。


In a custom page, i have another datepicker and i see a strange red border around it:

在自定义页面中,我有另一个DatePicker,并且我看到它周围有一个奇怪的红色边框:


enter image description here


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">&ZeroWidthSpace;</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?

我错过了什么吗?


更多回答

If you pick a date, does it disappear? Seems like an error class is being applied to it by something, maybe that could be the default empty value? Is props.value a valid date? Have you tried using defaultValue instead?

如果你选了一个日期,它会消失吗?似乎某个错误类正在应用于它,也许这可能是默认的空值?Pros.Value是有效的日期吗?您是否尝试过使用defaultValue?

@ConorReid that's a valid point. I cannot pick a date because I'm working on the login behind it. I'll try it. Anyway I need to use value because is a controlled component (If I understood it correctly)

@ConorReid这是一个有效的观点。我不能选择日期,因为我正在处理它背后的登录。我要试一试。无论如何,我需要使用Value,因为它是一个受控组件(如果我理解正确的话)

@ConorReid you were right. After setting value={props.value || null} the border is disappeared. thanks!

@ConorReid你是对的。设置Value={pros.Value||NULL}后,边框消失。谢谢!

All good! Glad I could provide some help.

都很好!很高兴我能提供一些帮助。

优秀答案推荐

To fix this issue we can apply slotProps in the Date picker component.

要解决此问题,我们可以在日期选取器组件中应用slotProps。


Visit https://mui.com/x/react-date-pickers/custom-components/

请访问https://mui.com/x/react-date-pickers/custom-components/。


<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
slotProps={{
textField: {
size: "small"
error: false,
},
}}
value={value || dayjs(field.value)}
onChange={field.onChange}
/>
</LocalizationProvider>;


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.

我也面临着同样的问题。当我将.log参数添加到renderInput属性时,我看到它的默认属性为Error:True。


So you can fix it like this:

因此,您可以这样修复它:


<DatePicker
...
renderInput={(params) => <TextField {...params} error={false} />}
...
/>


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.

我也有同样的情况,问题出在日期的初始值上。它是一个空字符串,而Datepicker将其解释为无效日期,因此我将初始值更改为UNDefined,现在它将今天的日期作为默认日期,并且没有错误状态。



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

当您在单个页面中同时使用两个重要的UI数据选取器组件时,会出现这种行为。有两种方法可以解决此问题,一种是覆盖CSS,另一种是通过设置缺省值Error={False}。覆盖CSS


<div class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-colorPrimary Mui-error MuiInputBase-formControl MuiInputBase-adornedEnd css-1bn53lx"><input aria-invalid="true" id=":r1m:" placeholder="mm/dd/yyyy" type="tel" class="MuiInputBase-input MuiOutlinedInput-input MuiInputBase-inputAdornedEnd css-1uvydh2" value=""><div class="MuiInputAdornment-root MuiInputAdornment-positionEnd MuiInputAdornment-outlined MuiInputAdornment-sizeMedium css-1nvf7g0"><button class="MuiButtonBase-root MuiIconButton-root MuiIconButton-edgeEnd MuiIconButton-sizeMedium css-slyssw" tabindex="0" type="button" aria-label="Choose date"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-vubbuv" focusable="false" aria-hidden="true" viewBox="0 0 24 24" data-testid="CalendarIcon"><path d="M17 12h-5v5h5v-5zM16 1v2H8V1H6v2H5c-1.11 0-1.99.9-1.99 2L3 19c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2h-1V1h-2zm3 18H5V8h14v11z"></path></svg><span class="MuiTouchRipple-root css-w0pj6f"></span></button></div><fieldset aria-hidden="true" class="MuiOutlinedInput-notchedOutline css-igs3ac"><legend class="css-ihdtdm"><span class="notranslate">​</span></legend></fieldset></div>

The above code has Mui-error class which is responsible for show that red line. You can try overriding that.

上面代码有Mui-Error类,负责显示红线。您可以尝试覆盖该选项。



  1. Do pass error={false} by default where you are using renderInput component after params



<DatePicker
...
renderInput={(params) => <TextField {...params} error={false} />}
... />




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/10274

问题是初始值不应该是空字符串,而应该是NULL,请参阅Author https://github.com/mui/mui-x/issues/10274的信息


The currently most upvoted solution causes the issue that the date is always considered valid even if a user hasn't filled in any date...

目前最受欢迎的解决方案导致的问题是,即使用户没有填写任何日期,日期也总是被认为是有效的。


更多回答

Actually this is completely wrong and this causes another problem instead github.com/mui/mui-x/issues/10274 , I found the proper soluton, will post the answer below

实际上这是完全错误的,这导致了另一个问题,而不是githorb.com/mui/mui-x/Issues/10274,我找到了合适的解决方案,将在下面发布答案

This corrected it for me and seems like the more appropriate way to address the issue than the other answers. The other answers rely on styling or overriding the the error state.

这对我来说纠正了它,似乎是比其他答案更合适的解决问题的方法。其他答案取决于设置样式或覆盖错误状态。

Pass null instead of undefined. It will show you an empty date picker field.

传递NULL而不是UNDefined。它将向您显示一个空的日期选择器字段。

There is no accepted answer. See comments below question and you'll find the same solution you've proposed

目前还没有公认的答案。查看问题下面的评论,您将找到与您提出的解决方案相同的解决方案

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com